From 48bf812f1b3c60b5201a95458be76ae9b7323a88 Mon Sep 17 00:00:00 2001 From: raghavi101 <56163096+raghavi101@users.noreply.github.com> Date: Tue, 15 Oct 2019 23:49:48 +0530 Subject: [PATCH] Solution to Python Practice Problems. These are solution to the python practice problems. https://www.hackerrank.com/domains/python?filters%5Bstatus%5D%5B%5D=unsolved&badge_type=python --- learn1/1.py | 20 ++++++++++++++++ learn1/10.py | 12 ++++++++++ learn1/11.py | 4 ++++ learn1/12.py | 8 +++++++ learn1/13.py | 26 +++++++++++++++++++++ learn1/14.py | 17 ++++++++++++++ learn1/15.py | 8 +++++++ learn1/16.py | 13 +++++++++++ learn1/17.py | 8 +++++++ learn1/18.py | 6 +++++ learn1/19.py | 14 +++++++++++ learn1/20.py | 16 +++++++++++++ learn1/21.py | 5 ++++ learn1/22.py | 10 ++++++++ learn1/23.py | 23 ++++++++++++++++++ learn1/24.py | 26 +++++++++++++++++++++ learn1/25.py | 19 +++++++++++++++ learn1/26.py | 23 ++++++++++++++++++ learn1/27.py | 24 +++++++++++++++++++ learn1/28main.py | 12 ++++++++++ learn1/28pre.py | 13 +++++++++++ learn1/29.py | 5 ++++ learn1/3.py | 16 +++++++++++++ learn1/30.py | 39 +++++++++++++++++++++++++++++++ learn1/31.py | 13 +++++++++++ learn1/32.py | 8 +++++++ learn1/33.py | 11 +++++++++ learn1/34.py | 7 ++++++ learn1/35.py | 9 +++++++ learn1/36.py | 5 ++++ learn1/37.py | 11 +++++++++ learn1/38.py | 8 +++++++ learn1/39.py | 33 ++++++++++++++++++++++++++ learn1/39pre.py | 18 ++++++++++++++ learn1/4.py | 28 ++++++++++++++++++++++ learn1/40.py | 13 +++++++++++ learn1/41.py | 4 ++++ learn1/42.py | 20 ++++++++++++++++ learn1/43.py | 22 ++++++++++++++++++ learn1/44.py | 20 ++++++++++++++++ learn1/45.py | 27 +++++++++++++++++++++ learn1/45pre.py | 33 ++++++++++++++++++++++++++ learn1/46.py | 6 +++++ learn1/47.py | 17 ++++++++++++++ learn1/48.py | 7 ++++++ learn1/49.py | 24 +++++++++++++++++++ learn1/5.py | 25 ++++++++++++++++++++ learn1/50.py | 26 +++++++++++++++++++++ learn1/51.py | 20 ++++++++++++++++ learn1/52.py | 6 +++++ learn1/53.py | 5 ++++ learn1/54ctp.py | 17 ++++++++++++++ learn1/54pe.py | 27 +++++++++++++++++++++ learn1/54pre.py | 34 +++++++++++++++++++++++++++ learn1/55ctp.py | 20 ++++++++++++++++ learn1/56.py | 36 ++++++++++++++++++++++++++++ learn1/57.py | 20 ++++++++++++++++ learn1/58.py | 7 ++++++ learn1/59.py | 13 +++++++++++ learn1/6.py | 23 ++++++++++++++++++ learn1/60.py | 9 +++++++ learn1/61.py | 17 ++++++++++++++ learn1/65.py | 3 +++ learn1/66.py | 14 +++++++++++ learn1/67.py | 11 +++++++++ learn1/68.py | 11 +++++++++ learn1/69.py | 11 +++++++++ learn1/7.py | 9 +++++++ learn1/70.py | 17 ++++++++++++++ learn1/71.py | 11 +++++++++ learn1/72.py | 6 +++++ learn1/73.py | 12 ++++++++++ learn1/74.py | 15 ++++++++++++ learn1/75.py | 40 ++++++++++++++++++++++++++++++++ learn1/76.py | 18 ++++++++++++++ learn1/77.py | 11 +++++++++ learn1/78.py | 10 ++++++++ learn1/79.py | 11 +++++++++ learn1/8.py | 9 +++++++ learn1/80.py | 10 ++++++++ learn1/9.py | 22 ++++++++++++++++++ learn1/longeststring.py | 3 +++ learn1/longgestcommonsequenec.py | 13 +++++++++++ 83 files changed, 1283 insertions(+) create mode 100644 learn1/1.py create mode 100644 learn1/10.py create mode 100644 learn1/11.py create mode 100644 learn1/12.py create mode 100644 learn1/13.py create mode 100644 learn1/14.py create mode 100644 learn1/15.py create mode 100644 learn1/16.py create mode 100644 learn1/17.py create mode 100644 learn1/18.py create mode 100644 learn1/19.py create mode 100644 learn1/20.py create mode 100644 learn1/21.py create mode 100644 learn1/22.py create mode 100644 learn1/23.py create mode 100644 learn1/24.py create mode 100644 learn1/25.py create mode 100644 learn1/26.py create mode 100644 learn1/27.py create mode 100644 learn1/28main.py create mode 100644 learn1/28pre.py create mode 100644 learn1/29.py create mode 100644 learn1/3.py create mode 100644 learn1/30.py create mode 100644 learn1/31.py create mode 100644 learn1/32.py create mode 100644 learn1/33.py create mode 100644 learn1/34.py create mode 100644 learn1/35.py create mode 100644 learn1/36.py create mode 100644 learn1/37.py create mode 100644 learn1/38.py create mode 100644 learn1/39.py create mode 100644 learn1/39pre.py create mode 100644 learn1/4.py create mode 100644 learn1/40.py create mode 100644 learn1/41.py create mode 100644 learn1/42.py create mode 100644 learn1/43.py create mode 100644 learn1/44.py create mode 100644 learn1/45.py create mode 100644 learn1/45pre.py create mode 100644 learn1/46.py create mode 100644 learn1/47.py create mode 100644 learn1/48.py create mode 100644 learn1/49.py create mode 100644 learn1/5.py create mode 100644 learn1/50.py create mode 100644 learn1/51.py create mode 100644 learn1/52.py create mode 100644 learn1/53.py create mode 100644 learn1/54ctp.py create mode 100644 learn1/54pe.py create mode 100644 learn1/54pre.py create mode 100644 learn1/55ctp.py create mode 100644 learn1/56.py create mode 100644 learn1/57.py create mode 100644 learn1/58.py create mode 100644 learn1/59.py create mode 100644 learn1/6.py create mode 100644 learn1/60.py create mode 100644 learn1/61.py create mode 100644 learn1/65.py create mode 100644 learn1/66.py create mode 100644 learn1/67.py create mode 100644 learn1/68.py create mode 100644 learn1/69.py create mode 100644 learn1/7.py create mode 100644 learn1/70.py create mode 100644 learn1/71.py create mode 100644 learn1/72.py create mode 100644 learn1/73.py create mode 100644 learn1/74.py create mode 100644 learn1/75.py create mode 100644 learn1/76.py create mode 100644 learn1/77.py create mode 100644 learn1/78.py create mode 100644 learn1/79.py create mode 100644 learn1/8.py create mode 100644 learn1/80.py create mode 100644 learn1/9.py create mode 100644 learn1/longeststring.py create mode 100644 learn1/longgestcommonsequenec.py diff --git a/learn1/1.py b/learn1/1.py new file mode 100644 index 0000000..1baa758 --- /dev/null +++ b/learn1/1.py @@ -0,0 +1,20 @@ +import asyncio, time + +async def routine1(): + print("1") + + await asyncio.sleep(5) + print("100") + +def r(): + time.sleep(8) + return + +async def routine2(): + print("2") + + await r() + print("200") + +loop = asyncio.get_event_loop() +loop.run_until_complete(asyncio.gather(routine1(), routine2())) diff --git a/learn1/10.py b/learn1/10.py new file mode 100644 index 0000000..a25e411 --- /dev/null +++ b/learn1/10.py @@ -0,0 +1,12 @@ +a = input() +Rin = list(a.split())[0] +n = list(a.split())[1] +b = input() +b = list(b.split()) + +ans = 0 +for x in b: + if x>Rin: + return 0 + else: + ans += diff --git a/learn1/11.py b/learn1/11.py new file mode 100644 index 0000000..3b5b201 --- /dev/null +++ b/learn1/11.py @@ -0,0 +1,4 @@ +x = int(input()) +y = int(input()) +z = int(input()) +n = int(input()) diff --git a/learn1/12.py b/learn1/12.py new file mode 100644 index 0000000..3ebce60 --- /dev/null +++ b/learn1/12.py @@ -0,0 +1,8 @@ +n = int(input()) +arr = list(map(int, input().split())) +l = [] +if n==len(arr): + for i in arr: + if imin(list(l.values())): + e[i]=l[i] + +m=[] +a = min(list(e.values())) +for i in e: + if e[i]==a: + m.append(i) + +if i=='Harsh': + print('Beria') + print('Harsh') +elif i=='Harry': + print('Berry') + print('Harry') +else: + for i in m[::-1]: + print(i) diff --git a/learn1/14.py b/learn1/14.py new file mode 100644 index 0000000..a220940 --- /dev/null +++ b/learn1/14.py @@ -0,0 +1,17 @@ +n = int(input()) +l = {} +for i in range(n): + name=input() + l[name.split()[0]]=[] + for i in name.split()[1::]: + l[name.split()[0]].append(float(i)) + +a=input() +total=0 +for i in l[a]: + total+=i + + +print(format(total/len(l[a]), '.2f')) + +#map, lists loops diff --git a/learn1/15.py b/learn1/15.py new file mode 100644 index 0000000..8769427 --- /dev/null +++ b/learn1/15.py @@ -0,0 +1,8 @@ +def simple(ar): + total=0 + for i in ar: + total+=i + return total + +n = list(map(int, input().rstrip().split())) +print(simple(n)) diff --git a/learn1/16.py b/learn1/16.py new file mode 100644 index 0000000..3d7a3f4 --- /dev/null +++ b/learn1/16.py @@ -0,0 +1,13 @@ +n = int(input()) +l=[] +for _ in range(n): + s=input().split() + cmd=s[0] + arg=s[1:] + if cmd!='print': + cmd = 'l.'+cmd+'('+','.join(arg)+')' + eval(cmd) + else: + print(l) + +#EVAL diff --git a/learn1/17.py b/learn1/17.py new file mode 100644 index 0000000..580b0e0 --- /dev/null +++ b/learn1/17.py @@ -0,0 +1,8 @@ +def compare(a, b): + a1,b1=0,0 + for i in range(a): + if a[i]>b[i]: + a1+=1 + elif b[i]>a[i]: + b1+=1 + return [a1, b1] diff --git a/learn1/18.py b/learn1/18.py new file mode 100644 index 0000000..48ba844 --- /dev/null +++ b/learn1/18.py @@ -0,0 +1,6 @@ +def su(ar): + return sum(ar) + +ar = list(map(int, input().rstrip().split())) + +print(su(ar)) diff --git a/learn1/19.py b/learn1/19.py new file mode 100644 index 0000000..c26ca66 --- /dev/null +++ b/learn1/19.py @@ -0,0 +1,14 @@ +def diff(arr): + l1,l2=0,0 + for i in range(len(arr)): + l1+=arr[i][i] + l2+=arr[i][len(arr)-1-i] + return abs(l1-l2) + +arr = [] +n = int(input()) +for _ in range(n): + arr.append(list(map(int, input().rstrip().split()))) + +r = diff(arr) +print(r) diff --git a/learn1/20.py b/learn1/20.py new file mode 100644 index 0000000..a39d775 --- /dev/null +++ b/learn1/20.py @@ -0,0 +1,16 @@ +def plus(arr): + p, n, z = 0, 0, 0 + for i in arr: + if i>0: + p+=1 + elif i<0: + n+=1 + else: + z+=1 + print(format(p/len(arr), '.6f')) + print(format(n/len(arr), '.6f')) + print(format(z/len(arr), '.6f')) + +arr = list(map(int, input().rstrip().split())) + +plus(arr) diff --git a/learn1/21.py b/learn1/21.py new file mode 100644 index 0000000..78d185c --- /dev/null +++ b/learn1/21.py @@ -0,0 +1,5 @@ +def stair(n): + for i in range(n-1, -1, -1): + print(i*' '+(n-i)*'#') + +stair(6) diff --git a/learn1/22.py b/learn1/22.py new file mode 100644 index 0000000..50ec8fc --- /dev/null +++ b/learn1/22.py @@ -0,0 +1,10 @@ +def mini(arr): + arr.sort() + min = sum(arr[:4]) + max = sum(arr[::-1][:4]) + + print(str(min)+" "+str(max)) + +arr = list(map(int, input().rstrip().split())) + +mini(arr) diff --git a/learn1/23.py b/learn1/23.py new file mode 100644 index 0000000..5be8c9d --- /dev/null +++ b/learn1/23.py @@ -0,0 +1,23 @@ +def tie(s): + l = list(s) + if 'P' in l: + a = int(''.join(l[0:2]))+12 + l.remove('P') + l.remove('M') + if a==24: + return '12'+''.join(l[2::]) + else: + return str(a)+''.join(l[2::]) + else: + l.remove('A') + l.remove('M') + a = int(''.join(l[0:2])) + if a==12: + return '00'+''.join(l[2::]) + else: + return ''.join(l) + +s = input() + +result = tie(s) +print(result) diff --git a/learn1/24.py b/learn1/24.py new file mode 100644 index 0000000..43c4313 --- /dev/null +++ b/learn1/24.py @@ -0,0 +1,26 @@ +def grade(grades): + round = [] + for i in grades: + if i<=37: + round.append(i) + else: + k=0 + for j in range(3): + if (i+j)%5==0: + k+=1 + round.append(i+j) + if k==0: + round.append(i) + return round + + +n = int(input()) + +grades = [] + +for _ in range(n): + grades_item = int(input()) + grades.append(grades_item) + +result = grade(grades) +print(result) diff --git a/learn1/25.py b/learn1/25.py new file mode 100644 index 0000000..8728865 --- /dev/null +++ b/learn1/25.py @@ -0,0 +1,19 @@ +def hour(arr): + i,j=0,0 + l=[] + while i=0: + if (s-a)<=i and (t-a)>=i: + app+=1 + for i in oranges: + if i<=0: + if (b-t)<=-1*i and (b-s)>=-1*i: + ora+=1 + print(app) + print(ora)''' + print(sum(s <= a + d <= t for d in apples)) + print(sum(s <= b + d <= t for d in oranges)) + +if __name__ == '__main__': + st = input().split() + + s = int(st[0]) + + t = int(st[1]) + + ab = input().split() + + a = int(ab[0]) + + b = int(ab[1]) + + mn = input().split() + + m = int(mn[0]) + + n = int(mn[1]) + + apples = list(map(int, input().rstrip().split())) + + oranges = list(map(int, input().rstrip().split())) + + countApplesAndOranges(s, t, a, b, apples, oranges) diff --git a/learn1/31.py b/learn1/31.py new file mode 100644 index 0000000..4874ee7 --- /dev/null +++ b/learn1/31.py @@ -0,0 +1,13 @@ +def swap_case(s): + k='' + for i in s: + if i==i.upper(): + k+=i.lower() + else: + k+=i.upper() + return k + +if __name__ == '__main__': + s = input() + result = swap_case(s) + print(result) diff --git a/learn1/32.py b/learn1/32.py new file mode 100644 index 0000000..eae74da --- /dev/null +++ b/learn1/32.py @@ -0,0 +1,8 @@ +a=input() +b=input() +k=0 + +for x in range(len(a)): + if a[x]==b[0]: + k+=a[x:x+len(b)].count(b) +print(k) diff --git a/learn1/33.py b/learn1/33.py new file mode 100644 index 0000000..66c1765 --- /dev/null +++ b/learn1/33.py @@ -0,0 +1,11 @@ +a = input() + +l=['.isalnum()', '.isalpha()', '.isdigit()', '.islower()', '.isupper()'] + +for x in l: + b = False + for i in a: + if eval('\''+i+'\''+x): + b=True + break + print(b) diff --git a/learn1/34.py b/learn1/34.py new file mode 100644 index 0000000..0feb52d --- /dev/null +++ b/learn1/34.py @@ -0,0 +1,7 @@ +import textwrap as w + +s = 'abcda' +d = 2 + + +print('\n'.join(i for i in w.wrap(s, width=2))) diff --git a/learn1/35.py b/learn1/35.py new file mode 100644 index 0000000..46f5fa0 --- /dev/null +++ b/learn1/35.py @@ -0,0 +1,9 @@ +a = list(map(int, input().split())) +h=a[0] +w=a[1] + +for i in range(h//2): + print(((2*i+1)*'.|.').center(w,'-')) +print('WELCOME'.center(w,'-')) +for i in range(h//2, 0, -1): + print(((2*i-1)*'.|.').center(w,'-')) diff --git a/learn1/36.py b/learn1/36.py new file mode 100644 index 0000000..90f0b4e --- /dev/null +++ b/learn1/36.py @@ -0,0 +1,5 @@ + +a = int(input()) +for i in range(1, a+1): + b = ' '.join((len(bin(a))-len(j[2::])-2)*' '+j[2::] for j in [('00'+str(i)), oct(i), hex(i).upper(), bin(i)]) + print(b) diff --git a/learn1/37.py b/learn1/37.py new file mode 100644 index 0000000..1262a97 --- /dev/null +++ b/learn1/37.py @@ -0,0 +1,11 @@ +alpha = '0abcdefghijklmnopqrstuvwxyz' +a = int(input()) + + +for i in range(a): + text='-'.join(list(alpha[a-i:a+1][::-1][:-1]+alpha[a-i:a+1])) + print(text.center(4*a-3, '-')) + +for i in range(a-2, -1, -1): + text='-'.join(list(alpha[a-i:a+1][::-1][:-1]+alpha[a-i:a+1])) + print(text.center(4*a-3, '-')) diff --git a/learn1/38.py b/learn1/38.py new file mode 100644 index 0000000..fded644 --- /dev/null +++ b/learn1/38.py @@ -0,0 +1,8 @@ +s=input() +k='' +for x in range(len(s)): + if x==0 or s[x-1]==' ': + k+=s[x].upper() + else: + k+=s[x] +print(k) diff --git a/learn1/39.py b/learn1/39.py new file mode 100644 index 0000000..126a0b2 --- /dev/null +++ b/learn1/39.py @@ -0,0 +1,33 @@ +s = input() +score = {'Stuart':0, 'Kevin':0} + +l, l1 =[], [] +for i in range(len(s)): + if s[i] in 'AEIOU': + for j1 in range(1, len(s)-i+1): + w1 = s[i:i+j1] + if not w1 in l: + l.append(w1) + + else: + for j in range(1, len(s)-i+1): + w = s[i:i+j] + if not w in l1: + l1.append(w) + +for k in l: + + score['Kevin']+=s.count(k) +for k1 in l1: + score['Stuart']+=s.count(k1) + +a = max(list(score.values())) +print(l) +print(l1) +print(score) +if score['Stuart']!=score['Kevin']: + for x in score: + if score[x]==a: + print(str(x)+' '+str(a)) +else: + print('Draw') diff --git a/learn1/39pre.py b/learn1/39pre.py new file mode 100644 index 0000000..874dc5a --- /dev/null +++ b/learn1/39pre.py @@ -0,0 +1,18 @@ +s = raw_input() + +vowels = 'AEIOU' + +kevsc = 0 +stusc = 0 +for i in range(len(s)): + if s[i] in vowels: + kevsc += (len(s)-i) + else: + stusc += (len(s)-i) + +if kevsc > stusc: + print "Kevin", kevsc +elif kevsc < stusc: + print "Stuart", stusc +else: + print "Draw" diff --git a/learn1/4.py b/learn1/4.py new file mode 100644 index 0000000..45501b7 --- /dev/null +++ b/learn1/4.py @@ -0,0 +1,28 @@ +import time + +a = int(input()) +b= {} + +def m(a): + if a<=4: + return a + + if a//2 in b: + return b[a//2] + if a//3 in b: + return b[a//3] + if a//4 in b: + return b[a//4] + + c1 = max(a//2, m(a//2)) + c3 = max(a//3, m(a//3)) + c4 = max(a//4, m(a//4)) + + b[a]=c1+c3+c4 + return b[a] + +t1 = time.perf_counter() +print(m(a)) +t2 = -t1+time.perf_counter() +print(t2) +print(b) diff --git a/learn1/40.py b/learn1/40.py new file mode 100644 index 0000000..141a11b --- /dev/null +++ b/learn1/40.py @@ -0,0 +1,13 @@ +s = input() +a = int(input()) + +t = [] +for i in range(0, len(s), a): + t.append(s[i:i+a]) + +for i in t: + wor=[] + for x in i: + if not x in wor: + wor.append(x) + print(''.join(wor)) diff --git a/learn1/41.py b/learn1/41.py new file mode 100644 index 0000000..1e4daa8 --- /dev/null +++ b/learn1/41.py @@ -0,0 +1,4 @@ +a = int(input()) +arr = list(map(int, input().split())) + +print(sum(set(arr))/len(set(arr))) diff --git a/learn1/42.py b/learn1/42.py new file mode 100644 index 0000000..81bc6ca --- /dev/null +++ b/learn1/42.py @@ -0,0 +1,20 @@ +a = input().split() +n = int(a[0]) +m = int(a[1]) + +narr = list(map(int, input().split())) +aarr = set(map(int, input().split())) +barr = set(map(int, input().split())) + +happy = 0 +for i in narr: + if i in aarr: + happy+=1 + elif i in barr: + happy-=1 + else: + pass + +print(happy) + +#fastest yuppy diff --git a/learn1/43.py b/learn1/43.py new file mode 100644 index 0000000..176c9e4 --- /dev/null +++ b/learn1/43.py @@ -0,0 +1,22 @@ +a = input().split() +n = int(a[0]) +m = int(a[1]) + +narr = list(map(int, input().split())) +aarr = set(map(int, input().split())) +barr = set(map(int, input().split())) + +happy = 0 + +for c in narr: + w=narr.count(c) + narr=list(filter(lambda a: a != 2, narr)) + if c in aarr: + happy+=w + elif c in barr: + happy-=w + else: + pass +#happy= len(aarr)-len(aarr-set(narr))-1*(len(barr)-len(barr-set(narr))) + +print(happy) diff --git a/learn1/44.py b/learn1/44.py new file mode 100644 index 0000000..0661b56 --- /dev/null +++ b/learn1/44.py @@ -0,0 +1,20 @@ +a = list(map(int, input().split())) + +def kangaroo(x1, v1, x2, v2): + + if x1>x2 and v1>v2: + return'NO' + + elif x2>x1 and v2>v1: + return 'NO' + + else: + result = 'NO' + for i in range(10000): + if x1+i*v1==x2+i*v2: + result = 'YES' + break + return result + +b=kangaroo(a[0], a[1], a[2], a[3]) +print(b) diff --git a/learn1/45.py b/learn1/45.py new file mode 100644 index 0000000..b428cae --- /dev/null +++ b/learn1/45.py @@ -0,0 +1,27 @@ +def square(s): + cost=0 + if c(s): + print(cost) + else: + + + +def c(s): + l=[] + for i in s: + l.append(sum(i)) + for i in range(3): + l.append(s[0][i]+s[1][i]+s[2][i]) + l.append(s[0][0]+s[1][1]+s[2][2]) + l.append(s[0][2]+s[1][1]+s[2][2]) + + if l.count(l[0])==8: + return True + return False + +s = [] + +for _ in range(3): + s.append(list(map(int, input().rstrip().split()))) + +result = square(s) diff --git a/learn1/45pre.py b/learn1/45pre.py new file mode 100644 index 0000000..c2cc551 --- /dev/null +++ b/learn1/45pre.py @@ -0,0 +1,33 @@ +def square(s): + pre = [ + [[8, 1, 6], [3, 5, 7], [4, 9, 2]], + [[6, 1, 8], [7, 5, 3], [2, 9, 4]], + [[4, 9, 2], [3, 5, 7], [8, 1, 6]], + [[2, 9, 4], [7, 5, 3], [6, 1, 8]], + [[8, 3, 4], [1, 5, 9], [6, 7, 2]], + [[4, 3, 8], [9, 5, 1], [2, 7, 6]], + [[6, 7, 2], [1, 5, 9], [8, 3, 4]], + [[2, 7, 6], [9, 5, 1], [4, 3, 8]], + ] + + for i in pre: + +def c(s): + l=[] + for i in s: + l.append(sum(i)) + for i in range(3): + l.append(s[0][i]+s[1][i]+s[2][i]) + l.append(s[0][0]+s[1][1]+s[2][2]) + l.append(s[0][2]+s[1][1]+s[2][2]) + + if l.count(l[0])==8: + return True + return False + +s = [] + +for _ in range(3): + s.append(list(map(int, input().rstrip().split()))) + +result = square(s) diff --git a/learn1/46.py b/learn1/46.py new file mode 100644 index 0000000..f70206c --- /dev/null +++ b/learn1/46.py @@ -0,0 +1,6 @@ +from itertools import product + +aarr = list(map(int, input().split())) +barr = list(map(int, input().split())) + +print(' '.join(str(i) for i in list(product(*[aarr, barr])))) diff --git a/learn1/47.py b/learn1/47.py new file mode 100644 index 0000000..9a91ee6 --- /dev/null +++ b/learn1/47.py @@ -0,0 +1,17 @@ +from collections import Counter + +number = int(input()) +shoe = list(map(int, input().split())) +shoe = Counter(shoe) + +custom = int(input()) +price = 0 + +for i in range(custom): + a = list(map(int, input().split())) + if shoe[a[0]]!=0: + shoe[a[0]]-=1 + price+=a[1] + +print(price) + diff --git a/learn1/48.py b/learn1/48.py new file mode 100644 index 0000000..1e32b9c --- /dev/null +++ b/learn1/48.py @@ -0,0 +1,7 @@ +from itertools import permutations + +a = list(map(str, input().split())) + +print('\n'.join(''.join(i) for i in list(permutations(sorted(a[0]), int(a[1]))))) + +# what if uppercase and lowrcase comes by?? diff --git a/learn1/49.py b/learn1/49.py new file mode 100644 index 0000000..dc88466 --- /dev/null +++ b/learn1/49.py @@ -0,0 +1,24 @@ +''' +from itertools import groupby +a = input() +ka='' +for k, g in groupby(sorted(a)): + ka+='('+str(len(list(g)))+', '+k+') ' + +print(ka) +''' + +l='' +a=input() +c = 1 +for i in range(len(a)): + if i+1!=len(a): + if a[i+1]==a[i]: + c+=1 + else: + l+='('+str(c)+', '+a[i]+') ' + c=1 + else: + l+='('+str(c)+', '+a[i]+') ' + +print(l) diff --git a/learn1/5.py b/learn1/5.py new file mode 100644 index 0000000..3d41f53 --- /dev/null +++ b/learn1/5.py @@ -0,0 +1,25 @@ +from itertools import combinations + +weight = [1, 5, 3, 4] +value = [15, 10, 9, 5] +max_w = 8 + +def pw(wight, value, max_w): + version = [] + final = [] + + for i in range(1, len(weight)+1): + p = combinations(weight, i) + for x in list(p): + final.append(x) + + for i in final: + if sum(i)<=max_w: + var = 0 + for x in i: + var+=value[weight.index(x)] + version.append(var) + + return max(version) + +print(pw(weight, value, max_w)) diff --git a/learn1/50.py b/learn1/50.py new file mode 100644 index 0000000..e9e5657 --- /dev/null +++ b/learn1/50.py @@ -0,0 +1,26 @@ +from itertools import permutations + +n = int(input()) +a = list(map(str, input().split())) +k = int(input()) + +l = list(permutations(a, k)) + +c=0 +for i in l: + if 'a' in i: + c+=1 + +print(format(c/len(l), '.12f')) + +''' +from itertools import combinations + +N = int(input()) +L = input().split() +K = int(input()) + +C = list(combinations(L, K)) +F = filter(lambda c: 'a' in c, C) +print("{0:.3}".format(len(list(F))/len(C))) +''' diff --git a/learn1/51.py b/learn1/51.py new file mode 100644 index 0000000..08692ed --- /dev/null +++ b/learn1/51.py @@ -0,0 +1,20 @@ +from itertools import product + +km = list(map(int, input().split())) +k= km[0] +m= km[1] + +n=[] +for i in range(k): + ni = list(map(int, input().split())) + n.append(ni[1::]) + +m1=0 +for i in list(product(*n)): + l=0 + for j in i: + l+=j**2 + if l%m>m1: + m1=l%m + +print(m1) diff --git a/learn1/52.py b/learn1/52.py new file mode 100644 index 0000000..17ca4ad --- /dev/null +++ b/learn1/52.py @@ -0,0 +1,6 @@ +n = int(input()) +a = set() +for i in range(n): + a.add(input()) + +print(len(a)) diff --git a/learn1/53.py b/learn1/53.py new file mode 100644 index 0000000..97b5977 --- /dev/null +++ b/learn1/53.py @@ -0,0 +1,5 @@ +import cmath + +a = complex(input()) +print(abs(a)) +print(cmath.phase(a)) diff --git a/learn1/54ctp.py b/learn1/54ctp.py new file mode 100644 index 0000000..a25b224 --- /dev/null +++ b/learn1/54ctp.py @@ -0,0 +1,17 @@ +a = list(map(int, input().split())) +n = a[0] +m = a[1] + +a1 = list(map(int, input().split())) +a2 = list(map(int, input().split())) + +def lcs(X, Y, m, n): + + if m == 0 or n == 0: + return 0; + elif X[m-1] == Y[n-1]: + return 1 + lcs(X, Y, m-1, n-1); + else: + return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); + +print (lcs(a1 , a2, len(a1), len(a2))) diff --git a/learn1/54pe.py b/learn1/54pe.py new file mode 100644 index 0000000..89b854d --- /dev/null +++ b/learn1/54pe.py @@ -0,0 +1,27 @@ +from itertools import permutations + +a = list(map(int, input().split())) +n = a[0] +m = a[1] + +a1 = list(map(str, input().split())) +a2 = list(map(str, input().split())) + +def longestCommonSubsequence(s1, s2): + + matrix = [["" for x in range(len(s2))] for x in range(len(s1))] + for i in range(len(s1)): + for j in range(len(s2)): + if s1[i] == s2[j]: + if i == 0 or j == 0: + matrix[i][j] = s1[i] + else: + matrix[i][j] = matrix[i-1][j-1] + s1[i] + else: + matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1], key=len) + + cs = matrix[-1][-1] + + return len(cs), cs + +print(longestCommonSubsequence(a1, a2)) diff --git a/learn1/54pre.py b/learn1/54pre.py new file mode 100644 index 0000000..e32a622 --- /dev/null +++ b/learn1/54pre.py @@ -0,0 +1,34 @@ +a = list(map(int, input().split())) +n = a[0] +m = a[1] + +a1 = list(map(int, input().split())) +a2 = list(map(int, input().split())) + +l=0 +k=[][] + +def lis(a1, a2, n, m, i, j): + rt = k[i][j] + + if i==n and j==m: + return rt =0 + + if rt!=-1: + return rt; + + if a1[i] == a2[j]: + rt = 1 + lis(a1, a2, n, m, i+1, j+1) + else: + ret = max(lis(a1, a2, n, m, i+1, j), + lis(a1, a2, n, m, i, j+1)); + return rt + +def prin(a1, a2, n, m, work, in1, in2, lcs): + if lcs=l: + return + + if in1=n and in2=m: + return + + for () diff --git a/learn1/55ctp.py b/learn1/55ctp.py new file mode 100644 index 0000000..356b2af --- /dev/null +++ b/learn1/55ctp.py @@ -0,0 +1,20 @@ +n = int(input()) + +l = [i for i in range(n+1) if i%2!=0 ] + +if n%2==0: + l = [i for i in l if (l.index(i)+1)%2!=0 ] + + + +while len(l)!=1: + if n%2==0: + l = [i for i in l if (l.index(i)+1)%2!=0 ] + + + if n%2!=0: + l = [i for i in l if (l.index(i)+1)%2==0 ] + if len(l)!=1: + l = [i for i in l if (l.index(i)+1)%2!=0 ] + +print(l[0]) diff --git a/learn1/56.py b/learn1/56.py new file mode 100644 index 0000000..b5ff0e3 --- /dev/null +++ b/learn1/56.py @@ -0,0 +1,36 @@ +n = int(input()) +l = [i for i in range(1, n+1)] + +def li(l, n): + print(l) + if len(l)==1: + return l[0] + if n%2==0: + l=[i for i in l if (l.index(i)+1)%2==0] + return li(l, n) + if n%2!=0: + l=[i for i in l if (l.index(i)+1)%2!=0] + a=l[-1] + l.insert(0, a) + l=l[:-1] + print(l) + + return li(l, n) + +print(li(l,n)) + +''' +1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +1 3 5 7 9 11 13 15 +3 7 11 15 +7 15 +15 + +1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +1 3 5 7 9 11 13 15 +15 1 3 5 7 9 11 13 +15 3 7 11 +11 15 3 7 +11 3 + +''' diff --git a/learn1/57.py b/learn1/57.py new file mode 100644 index 0000000..0b8d0af --- /dev/null +++ b/learn1/57.py @@ -0,0 +1,20 @@ +a=list(map(int, input().split())) +n=a[0] +m=a[1] + +a1=list(map(int, input().split())) +a2=list(map(int, input().split())) + +def facto(a): + l = [] + for i in range(1, a+1): + if a%i==0: + l.append(i) + return l + +c = 0 +for i in a2: + if(set(a1).issubset(set(facto(i)))): + c+=1 + +print(c) diff --git a/learn1/58.py b/learn1/58.py new file mode 100644 index 0000000..6e601b9 --- /dev/null +++ b/learn1/58.py @@ -0,0 +1,7 @@ +import calendar + +a = list(map(int, input().split())) +b = calendar.weekday(a[2], a[0] , a[1]) + +c = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY'] +print(c[b]) diff --git a/learn1/59.py b/learn1/59.py new file mode 100644 index 0000000..f8c085b --- /dev/null +++ b/learn1/59.py @@ -0,0 +1,13 @@ +n = int(input()) + +def time_delta(t1, t2): + time_zone = int(t[5])+int(t[5]) + + +for i in range(n): + t1=list(map(str, input().split()) + t2=list(map(str, input().split()) + time_delta(t1, t2) + + +# i would come back to you diff --git a/learn1/6.py b/learn1/6.py new file mode 100644 index 0000000..5539a08 --- /dev/null +++ b/learn1/6.py @@ -0,0 +1,23 @@ +1)-stack,queue + +2)-basic tree and graph implementation(in graph DFS,BFS,dijkstra's are mostly used) + +3)-Basic Dynamic programming(in many challenges you will get it's problems) + +4)greedy algorithms + +5)-basic maths + +6)-STL(it is widely used,it is must for competitive coding) + +At higher level you may require + +1)-segment tree + +2)-binary index tree + +3)-suffix array + +4)-trie + +5)-hard DP problems diff --git a/learn1/60.py b/learn1/60.py new file mode 100644 index 0000000..a5d15c4 --- /dev/null +++ b/learn1/60.py @@ -0,0 +1,9 @@ +c = int(input()) + +for i in range(c): + n = input() + a = set(map(int, input().split())) + + m = input() + b = set(map(int, input().split())) + print(a.issubset(b)) diff --git a/learn1/61.py b/learn1/61.py new file mode 100644 index 0000000..3f5c234 --- /dev/null +++ b/learn1/61.py @@ -0,0 +1,17 @@ +a = set(map(int, input().split())) +c = int(input()) +j = 0 +for i in range(c): + b = set(map(int, input().split())) + if b.issubset(a) and len(a-b)!=0: + j+=1 + +if c==j: + print(True) +else: + print(False) + +''' +e="(set(input().split())if input()!='-1'else '')";print(len(eval(e)|eval(e))) +''' +# kya baat hai? diff --git a/learn1/65.py b/learn1/65.py new file mode 100644 index 0000000..a029bda --- /dev/null +++ b/learn1/65.py @@ -0,0 +1,3 @@ +def clos(x): + if x==x[-1::]: + return x diff --git a/learn1/66.py b/learn1/66.py new file mode 100644 index 0000000..91f63e1 --- /dev/null +++ b/learn1/66.py @@ -0,0 +1,14 @@ +a = list(map(int, input().split())) + +change_min, change_max = 0, 0 +min, max = a[0], a[0] + +for i in a: + if imax: + max=i + change_max+=1 + +print(change_max, change_min) diff --git a/learn1/67.py b/learn1/67.py new file mode 100644 index 0000000..be4d352 --- /dev/null +++ b/learn1/67.py @@ -0,0 +1,11 @@ +a = list(map(int, input().split())) +b = list(map(int, input().split())) +d = b[0] +m = b[1] + +ways = 0 + +for i in range(len(a)-m+1): + if sum(a[i:i+m])==d: + ways+=1 +print(ways) diff --git a/learn1/68.py b/learn1/68.py new file mode 100644 index 0000000..7f353a0 --- /dev/null +++ b/learn1/68.py @@ -0,0 +1,11 @@ +k = list(map(int, input().split())) +k = k[1] +a = list(map(int, input().split())) + +ways=0 +for i in range(len(a)): + for j in range(len(a)): + if imax: + max=a.count(i) + j=i + +print(j) diff --git a/learn1/7.py b/learn1/7.py new file mode 100644 index 0000000..7ec0d63 --- /dev/null +++ b/learn1/7.py @@ -0,0 +1,9 @@ +def profic(p ,year): + if len(p)==1: + return p[0]*year + + return max(p[0]*year+profic(p[1::], year+1), p[-1]*year+profic(p[:-1], year+1)) + +p = [2,3,5,1,4] +y=1 +print(profic(p, y)) diff --git a/learn1/70.py b/learn1/70.py new file mode 100644 index 0000000..5a644a7 --- /dev/null +++ b/learn1/70.py @@ -0,0 +1,17 @@ +year=int(input()) + +def f(year): + y=str(year) + if year>=1919: + if year%400==0 or (year%4==0 and year%100!=0): + return "12.09."+y + else: return "13.09."+y + + elif year<=1917: + if year%4==0: + return "12.09."+y + else: return "13.09."+y + + elif year==1918: + return "28.10.1918" +print(f(year)) diff --git a/learn1/71.py b/learn1/71.py new file mode 100644 index 0000000..2074299 --- /dev/null +++ b/learn1/71.py @@ -0,0 +1,11 @@ +k= list(map(int, input().split())) +k = k[1] +a= list(map(int, input().split())) +b= int(input()) + +if (sum(a)-a[k])/2==b: + print("Bon Appetit") +else: + if int(b-((sum(a)-a[k])/2))==b-((sum(a)-a[k])/2): + print(int(b-((sum(a)-a[k])/2))) + else:print(b-((sum(a)-a[k])/2)) diff --git a/learn1/72.py b/learn1/72.py new file mode 100644 index 0000000..0f1c848 --- /dev/null +++ b/learn1/72.py @@ -0,0 +1,6 @@ +a = list(map(int, input().split())) + +pairs=0 +for i in set(a): + pairs+=a.count(i)//2 +print(pairs) diff --git a/learn1/73.py b/learn1/73.py new file mode 100644 index 0000000..8a6c4e4 --- /dev/null +++ b/learn1/73.py @@ -0,0 +1,12 @@ +b= input() + +p=0 +ways=0 +for i in b: + if p==0: + if i=='D': + ways+=1 + if i=='D':p+=-1 + if i=='U':p+=1 + +print(ways) diff --git a/learn1/74.py b/learn1/74.py new file mode 100644 index 0000000..7fbcaab --- /dev/null +++ b/learn1/74.py @@ -0,0 +1,15 @@ +a = list(map(int, input().split())) +a = a[0] +k = list(map(int, input().split())) +u = list(map(int, input().split())) + +max=0 +for i in k: + for j in u: + if i+j>=max and i+j<=a: + max=i+j + +if amax1: + max1=a.count(i)+a.count(i+1) +print(max1) + + +#best + +''' +m=[] +for i in a: + l=[] + for j in a: + if abs(i-j)<=1: + l.append(j) + m.append(l) + +print(m) +k=[] +for i in m: + con = True + for x in i: + for y in i: + if abs(x-y)>1: + con = False + if con: + k.append(i) + +max=0 +for i in k: + if len(i)>max: + max=len(i) + +print(k) +print(max) +''' diff --git a/learn1/76.py b/learn1/76.py new file mode 100644 index 0000000..625e444 --- /dev/null +++ b/learn1/76.py @@ -0,0 +1,18 @@ +a = list(map(int, input().split())) +b = list(map(int, input().split())) + +for i in b: + a.append(i) + j=sorted(set(a), reverse=True) + print(j.index(i)+1) + +''' +100 90 90 80 75 60 +50 65 77 90 102 + + +100 100 50 40 40 20 10 +5 25 50 120 +''' + +#work on this diff --git a/learn1/77.py b/learn1/77.py new file mode 100644 index 0000000..fa0b5aa --- /dev/null +++ b/learn1/77.py @@ -0,0 +1,11 @@ +a = list(map(int, input().split())) +b = input() + +max1=0 + +for i in b: + print(ord(i)-97) + if a[ord(i)-97]>max1: + max1 = a[ord(i)-97] + +print(max1*len(b)) diff --git a/learn1/78.py b/learn1/78.py new file mode 100644 index 0000000..6785d80 --- /dev/null +++ b/learn1/78.py @@ -0,0 +1,10 @@ +t = int(input()) + +for i in range(t): + n=int(input()) + h=1 + for j in range(1, n+1): + if j%2!=0: + h=h*2 + else: h+=1 + print(h) diff --git a/learn1/79.py b/learn1/79.py new file mode 100644 index 0000000..e54e19f --- /dev/null +++ b/learn1/79.py @@ -0,0 +1,11 @@ +a = list(map(int, input().split())) +a=a[1] +b = list(map(int, input().split())) + +n=0 +for i in b: + if i<=0: + n+=1 +if n>=a: + print("NO") +else:print("YES") diff --git a/learn1/8.py b/learn1/8.py new file mode 100644 index 0000000..ca6b701 --- /dev/null +++ b/learn1/8.py @@ -0,0 +1,9 @@ +def fact(a): + if a<=1: + return 1 + return a*fact(a-1) + +a = int(input()) +for i in range(a): + n=int(input()) + print(fact(n)) diff --git a/learn1/80.py b/learn1/80.py new file mode 100644 index 0000000..bad5ffa --- /dev/null +++ b/learn1/80.py @@ -0,0 +1,10 @@ +a = list(map(int, input().split())) +b = int(input()) + +a = int("".join(str(i) for i in a))+b + +l = [] +for i in str(a): + l.append(int(i)) + +print(l) diff --git a/learn1/9.py b/learn1/9.py new file mode 100644 index 0000000..cefebbe --- /dev/null +++ b/learn1/9.py @@ -0,0 +1,22 @@ +''' +def fan(a, index, y): + if index==len(a): + return y + if int(a[index])%2==0: + return y+fan(a, index+1, y+1) + if int(a[index])%2!=0: + return fan(a, index+1, y) + + +print(fan(a, 0, 0)) +''' + +a = '574674546476' +c = '' +for b in range(len(a)-1, -1, -1): + y=0 + for i in a[b::]: + if int(i)%2==0: + y+=1 + c+=" "+str(y) +print(c[::-1]) diff --git a/learn1/longeststring.py b/learn1/longeststring.py new file mode 100644 index 0000000..0dc02f8 --- /dev/null +++ b/learn1/longeststring.py @@ -0,0 +1,3 @@ +a = 'ABABC' +b = 'BABCA' +c = [] diff --git a/learn1/longgestcommonsequenec.py b/learn1/longgestcommonsequenec.py new file mode 100644 index 0000000..af90410 --- /dev/null +++ b/learn1/longgestcommonsequenec.py @@ -0,0 +1,13 @@ +def lcs(X, Y, m, n): + + if m == 0 or n == 0: + return 0; + elif X[m-1] == Y[n-1]: + return 1 + lcs(X, Y, m-1, n-1); + else: + return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); + + +X = "ACBDEF" +Y = "EFGD" +print ("Length of LCS is ", lcs(X , Y, len(X), len(Y)))