Skip to content

Instantly share code, notes, and snippets.

@biniama
Created February 24, 2021 11:59
Show Gist options
  • Save biniama/e208de1e445fa351f53afad35794274e to your computer and use it in GitHub Desktop.
Save biniama/e208de1e445fa351f53afad35794274e to your computer and use it in GitHub Desktop.
Hacker Rank Exercises
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'balancedSum' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#
def balancedSum(arr):
# Calculate left sum
leftSum = [0]
x=0
for i in range(1, len(arr)):
x += arr[i-1]
leftSum.append(x)
# Calculate right sum
rightSum = [0]
y=0
arr.reverse()
for i in range(1, (len(arr))):
y += arr[i-1]
rightSum.append(y)
rightSum.reverse()
# Compare if the array is balanced
arr.reverse()
for i in range(1, len(arr)):
if leftSum[i] == rightSum[i]:
print (i)
return i
# Generated Code
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
arr_count = int(input().strip())
arr = []
for _ in range(arr_count):
arr_item = int(input().strip())
arr.append(arr_item)
result = balancedSum(arr)
fptr.write(str(result) + '\n')
fptr.close()
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'countMax' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts STRING_ARRAY upRight as parameter.
#
import sys
def countMax(upRight):
minRow = sys.maxsize
minColumn = sys.maxsize
print(upRight)
# print(minRow)
# print(minColumn)
for i in upRight:
row = int(i.split()[0])
colunm = int(i.split()[1])
minRow = min(minRow, row)
print("minRow ", minRow)
minColumn = min(minColumn, colunm)
print("minColumn ", minColumn)
print(minRow * minColumn)
return minRow * minColumn
# Generated code
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
upRight_count = int(input().strip())
upRight = []
for _ in range(upRight_count):
upRight_item = input()
upRight.append(upRight_item)
result = countMax(upRight)
fptr.write(str(result) + '\n')
fptr.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment