Algorithms

Warmup

Solve Me First

In [89]:
def solveMeFirst(a,b):
    return a+b
num1 = int(input())
num2 = int(input())
res = solveMeFirst(num1,num2)
print(res)
1
2
3

Simple Array Sum

In [91]:
n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]

print(sum(arr))
3
1 2 3
6

Compare the Triplets

In [9]:
a0,a1,a2 = input().strip().split(' ')
a0,a1,a2 = [int(a0),int(a1),int(a2)]
b0,b1,b2 = input().strip().split(' ')
b0,b1,b2 = [int(b0),int(b1),int(b2)]

array1 = [a0, a1, a2]
array2 = [b0, b1, b2]

sum1 = 0;
sum2 = 0;

for i in range(0, len(array1)):
    if array1[i] > array2[i]:
        sum1 += 1
    if array1[i] < array2[i]:
        sum2 += 1
print(sum1, sum2)
5 6 7
3 6 10
True
1 1

A Very Big Sum

In [11]:
n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]

summation = 0;

for i in range(0, len(arr)):
    summation += arr[i]
    
print(summation)
5
1
1

Diagonal Difference

In [28]:
n = int(input().strip())
a = []
for a_i in range(n):
    a_t = [int(a_temp) for a_temp in input().strip().split(' ')]
    a.append(a_t)
    
sum1 = 0
sum2 = 0
for i in range(0, n):
    sum1 += a[i][i]
    sum2 +=a[i][(n-1)-i]
print(abs(sum1 - sum2))
3
11 2 4
4 5 6
10 8 -12
4 19

Plus Minus

In [32]:
n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]

zeroes = 0
positives = 0
negatives = 0

for i in range(n):
    if arr[i] > 0:
        positives +=1
    if arr[i] < 0:
        negatives +=1
    if arr[i] == 0:
        zeroes +=1
print(positives/n)
print(negatives/n)
print(zeroes)
6
-4 3 -9 0 4 1
0.5
0.3333333333333333
1

Staircase

In [38]:
#n = int(input().strip())

n = 5
for i in range(1, (n+1)):
    print(((n-i) * " ")+(i*"#"))
    
    #
   ##
  ###
 ####
#####

Mini-Max

In [49]:
a,b,c,d,e = input().strip().split(' ')
a,b,c,d,e = [int(a),int(b),int(c),int(d),int(e)]

sortedList = [a, b, c, d, e]
sortedList.sort()
sum1 = 0
sum2 = 0

for i in range(len(sortedList)-1):
    sum1 += sortedList[i]
    sum2 += sortedList[i+1]
print(sum1, sum2)
5 4 3 2 1
[1, 2, 3, 4, 5]
10 14

Time Conversion

In [83]:
testString = "12:40:03AM"

newHour = ""

def getHour(string):
    h = int(string[0]+string[1])
    return h

if (testString[8] == "A"):
    if(getHour(testString) == 12):
        newHour = "00"
    else:
        newHour = str(getHour(testString))
if (testString[8] == "P"):
    if (getHour(testString) == 12):
        newHour = "12"
    else:
        newHour = str(getHour(testString) + 12)

if(int(newHour) < 10 and newHour != "00"):
    newHour = "0"+newHour
print(newHour+testString[2:10])
00:40:03AM

Birthday Cake Candles

In [86]:
n = int(input().strip())
height = [int(height_temp) for height_temp in input().strip().split(' ')]

count = 0;
biggest = max(height)
for i in range(n):
    if height[i] == biggest:
        count += 1
print(count)
5
1 2 3 4 4
2
In [ ]: