Algorithms
Algorithms
Simple Array Sum a='1 2 3 4 10 11' arr = map(int,a.strip().split(' ')) print arr #!/bin/python import sys n = int(raw_input().strip()) arr = map(int,raw_input().strip().split(' ')) print arr
|
||
Compare the Triplets
#!/bin/python import sys a0,a1,a2 = raw_input().strip().split(' ') a0,a1,a2 = [int(a0),int(a1),int(a2)] b0,b1,b2 = raw_input().strip().split(' ') b0,b1,b2 = [int(b0),int(b1),int(b2)] x=0;y=0 def je(m,n): global x,y if m>n: x+=1 elif m<n: y+=1 je(a0,b0) je(a1,b1) je(a2,b2) print x,y
|
||
Diagonal Difference
Sample Input
Sample Output
Explanation The primary diagonal is: Sum across the primary diagonal: 11 + 5 – 12 = 4 The secondary diagonal is: #!/bin/python import sys n = int(raw_input().strip()) a = [] for a_i in xrange(n): a_temp = map(int,raw_input().strip().split(' ')) a.append(a_temp) l=0 r=0 for i in range(len(a)): l+=a[i][i] r+=a[::-1][i][i] print abs(l-r)
|
||
staircase
n = int(input()) for i in range(1,n+1): print(('#'*i).rjust(n,' ')) OR n = int(raw_input()) for i in range(1,n+1): print " "*(n-i) + "#"*i OR #!/bin/python import sys n = int(raw_input().strip()) for i in range(n,0,-1): print ' '*(i-1)+'#'*(n-(i-1))
|
||
Time Conversion
#!/bin/python import sys time = raw_input().strip() if time[-2:]=='PM': if int(time[:2])==12: h='12'+time[2:-2] else: h=str(int(time[:2])+12)+time[2:-2] if time[-2:]=='AM': if int(time[:2])==12: h='00'+time[2:-2] else: h=str(int(time[:2]))+time[2:-2] print h #!/bin/python from time import strptime, strftime print strftime("%H:%M:%S", strptime(raw_input(), "%I:%M:%S%p"))
|
||
Circular Array Rotation
#!/bin/python import sys n,k,m = raw_input().strip().split(' ') n,k,m = [int(n),int(k),int(m)] arr = map(int,raw_input().strip().split(' ')) k %= n arr = arr[-k:] + arr[:-k] for i in range(m): print(arr[int(input())])
|
||