├── Fair Rations └── Fair_Rations.py ├── HackerRank-A Very Big Sum └── A_Very_Big_Sum.py ├── HackerRank-Apple and Orange └── Apple_and_Orange.py ├── HackerRank-Beautiful Days at the Movies └── Beautiful_Days_at_the_Movies.py ├── HackerRank-Birthday Cake Candles └── Birthday_Cake_Candles.py ├── HackerRank-Birthday Chocolate └── Birthday_Chocolate.py ├── HackerRank-Bon App-tit └── Bon Appétit.py ├── HackerRank-Breaking the Records └── Breaking_the_Records.py ├── HackerRank-Climbing the Leaderboard └── Climbing_the_Leaderboard.py ├── HackerRank-Compare the Triplets └── Compare_the_Triplets.py ├── HackerRank-Counter game └── Counter_game.py ├── HackerRank-Counting Valleys └── Counting_Valleys.py ├── HackerRank-Day of the Programmer └── Day_of_the_Programmer.py ├── HackerRank-Designer PDF Viewer └── Designer_PDF_Viewer.py ├── HackerRank-Diagonal Difference └── Diagonal_Difference.py ├── HackerRank-Divisible Sum Pairs └── Divisible_Sum_Pairs.py ├── HackerRank-Electronics Shop └── Electronics_Shop.py ├── HackerRank-Encryption └── Encryption.py ├── HackerRank-Extra Long Factorials └── Extra_Long_Factorials.py ├── HackerRank-Find Digits └── Find_Digits.py ├── HackerRank-Grading Students └── Grading_Students.py ├── HackerRank-Jumping on the Clouds └── Jumping_on_the_Clouds.py ├── HackerRank-Kangaroo └── Kangaroo.py ├── HackerRank-Migratory Birds └── Migratory_Birds.py ├── HackerRank-Mini-Max Sum └── Mini-Max_Sum.py ├── HackerRank-Plus Minus └── Plus_Minus.py ├── HackerRank-Simple Array Sum └── Simple_Array_Sum.py ├── HackerRank-Sock Merchant └── Sock_Merchant.py ├── HackerRank-Solve Me First ├── Solve_Me_First.java └── Solve_Me_First.py ├── HackerRank-Staircase └── Staircase.py ├── HackerRank-The Hurdle Race └── The_Hurdle_Race.py ├── HackerRank-Time Conversion └── Time_Conversion.py ├── HackerRank-Utopian Tree └── Utopian_Tree.py ├── HackerRank-Viral Advertising └── Viral_Advertising.py ├── Hackerrank-Manasa_and_Stones └── manasa_and_stones.py ├── Hackerrank-The_Minion_Game └── theMinionGame.py ├── HackkerRank-Angry Professor └── Angry_Professor.py ├── Merge Sort └── merge_sort.py └── README.md /Fair Rations/Fair_Rations.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the fairRations function below. 10 | def fairRations(B): 11 | suml = 0; 12 | current = -1 13 | paired = -1 14 | distance = 0 15 | total = 0; 16 | 17 | for i in B: 18 | suml += i 19 | if(i % 2 == 1 and current == -1): 20 | current = i 21 | paired = -1 22 | distance = 0 23 | elif(current != -1 and i % 2 == 1): 24 | paired = i 25 | current = -1 26 | loavesNeeded = (2 + ((distance + 1) - 1) * 2) 27 | total += loavesNeeded 28 | else: 29 | distance += 1 30 | 31 | if(suml % 2 == 1): 32 | return "NO" 33 | else : 34 | return total 35 | 36 | if __name__ == '__main__': 37 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 38 | 39 | N = int(input()) 40 | 41 | B = list(map(int, input().rstrip().split())) 42 | 43 | result = fairRations(B) 44 | 45 | fptr.write(str(result) + '\n') 46 | 47 | fptr.close() 48 | -------------------------------------------------------------------------------- /HackerRank-A Very Big Sum/A_Very_Big_Sum.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | # Complete the aVeryBigSum function below. 9 | def aVeryBigSum(ar): 10 | sum = 0 11 | for i in range(len(ar)): 12 | sum+=ar[i] 13 | return sum 14 | 15 | if __name__ == '__main__': 16 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 17 | 18 | ar_count = int(input()) 19 | 20 | ar = list(map(int, input().rstrip().split())) 21 | 22 | result = aVeryBigSum(ar) 23 | 24 | fptr.write(str(result) + '\n') 25 | 26 | fptr.close() 27 | -------------------------------------------------------------------------------- /HackerRank-Apple and Orange/Apple_and_Orange.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the countApplesAndOranges function below. 10 | def countApplesAndOranges(s, t, a, b, apples, oranges): 11 | acount = 0 12 | bcount = 0 13 | for i in range(len(apples)): 14 | temp = a+apples[i] 15 | if(temp in range(s,t+1)): 16 | acount+=1 17 | for i in range(len(oranges)): 18 | temp = b+oranges[i] 19 | if(temp in range(s,t+1)): 20 | bcount+=1 21 | print (acount) 22 | print (bcount) 23 | 24 | 25 | 26 | if __name__ == '__main__': 27 | st = input().split() 28 | 29 | s = int(st[0]) 30 | 31 | t = int(st[1]) 32 | 33 | ab = input().split() 34 | 35 | a = int(ab[0]) 36 | 37 | b = int(ab[1]) 38 | 39 | mn = input().split() 40 | 41 | m = int(mn[0]) 42 | 43 | n = int(mn[1]) 44 | 45 | apples = list(map(int, input().rstrip().split())) 46 | 47 | oranges = list(map(int, input().rstrip().split())) 48 | 49 | countApplesAndOranges(s, t, a, b, apples, oranges) 50 | -------------------------------------------------------------------------------- /HackerRank-Beautiful Days at the Movies/Beautiful_Days_at_the_Movies.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | from decimal import * 9 | 10 | # Complete the beautifulDays function below. 11 | def beautifulDays(i, j, k): 12 | count=0 13 | for l in range(i,j+1): 14 | n = (str(l)[::-1]) 15 | if(abs(l-int(n))%k ==0): 16 | count+=1 17 | return count 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | ijk = input().split() 23 | 24 | i = int(ijk[0]) 25 | 26 | j = int(ijk[1]) 27 | 28 | k = int(ijk[2]) 29 | 30 | result = beautifulDays(i, j, k) 31 | -------------------------------------------------------------------------------- /HackerRank-Birthday Cake Candles/Birthday_Cake_Candles.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the birthdayCakeCandles function below. 10 | def birthdayCakeCandles(ar): 11 | count=0 12 | big = max(ar) 13 | for i in range(len(ar)): 14 | if(ar[i]==big): 15 | count+=1 16 | return count 17 | 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | ar_count = int(input()) 23 | 24 | ar = list(map(int, input().rstrip().split())) 25 | 26 | result = birthdayCakeCandles(ar) 27 | 28 | fptr.write(str(result) + '\n') 29 | 30 | fptr.close() 31 | -------------------------------------------------------------------------------- /HackerRank-Birthday Chocolate/Birthday_Chocolate.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the birthday function below. 10 | def birthday(s, d, m): 11 | ans=0 12 | for i in range(len(s)): 13 | n=0 14 | count=0 15 | while(n<(m)): 16 | count+=s[i+n] 17 | n+=1 18 | if(count==d): 19 | ans+=1 20 | if(i+n==len(s)): 21 | break 22 | return ans 23 | 24 | if __name__ == '__main__': 25 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 26 | 27 | n = int(input().strip()) 28 | 29 | s = list(map(int, input().rstrip().split())) 30 | 31 | dm = input().rstrip().split() 32 | 33 | d = int(dm[0]) 34 | 35 | m = int(dm[1]) 36 | 37 | result = birthday(s, d, m) 38 | 39 | fptr.write(str(result) + '\n') 40 | 41 | fptr.close() 42 | -------------------------------------------------------------------------------- /HackerRank-Bon App-tit/Bon Appétit.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the bonAppetit function below. 10 | def bonAppetit(bill, k, b): 11 | count=0 12 | for i in range(len(bill)): 13 | if(i!=k): 14 | count+=bill[i] 15 | pay = count//2 16 | if(pay != b): 17 | print(b-pay) 18 | else: 19 | print("Bon Appetit") 20 | 21 | if __name__ == '__main__': 22 | nk = input().rstrip().split() 23 | 24 | n = int(nk[0]) 25 | 26 | k = int(nk[1]) 27 | 28 | bill = list(map(int, input().rstrip().split())) 29 | 30 | b = int(input().strip()) 31 | 32 | bonAppetit(bill, k, b) 33 | -------------------------------------------------------------------------------- /HackerRank-Breaking the Records/Breaking_the_Records.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the breakingRecords function below. 10 | def breakingRecords(scores): 11 | maxi=scores[0] 12 | mini=scores[0] 13 | maxcount =0 14 | mincount=0 15 | for i in range(len(scores)): 16 | if(scores[i]>maxi): 17 | maxi = scores[i] 18 | maxcount+=1 19 | if(scores[i] index and i >= scores[index]): 17 | index += 1 18 | rank_list.append(n+1-index) 19 | return rank_list 20 | 21 | 22 | if __name__ == '__main__': 23 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 24 | 25 | scores_count = int(input()) 26 | 27 | scores = list(map(int, input().rstrip().split())) 28 | 29 | alice_count = int(input()) 30 | 31 | alice = list(map(int, input().rstrip().split())) 32 | 33 | result = climbingLeaderboard(scores, alice) 34 | 35 | fptr.write('\n'.join(map(str, result))) 36 | fptr.write('\n') 37 | 38 | fptr.close() 39 | -------------------------------------------------------------------------------- /HackerRank-Compare the Triplets/Compare_the_Triplets.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the compareTriplets function below. 10 | def compareTriplets(a, b): 11 | alice=0 12 | bob=0 13 | for i in range(3): 14 | if(a[i]>b[i]): 15 | alice+=1 16 | elif(a[i] 1918) & (year%400 == 0 or ((year%4 == 0) & (year%100 != 0)))): 14 | return '12.09.%s' %year 15 | else: 16 | return '13.09.%s' %year 17 | 18 | if __name__ == '__main__': 19 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 20 | 21 | year = int(input()) 22 | 23 | result = solve(year) 24 | 25 | fptr.write(result + '\n') 26 | 27 | fptr.close() 28 | -------------------------------------------------------------------------------- /HackerRank-Designer PDF Viewer/Designer_PDF_Viewer.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the designerPdfViewer function below. 10 | def designerPdfViewer(h, word): 11 | maxi=0 12 | for i in range(len(word)): 13 | if(maxi< h[ord(word[i])-97]): 14 | maxi = h[ord(word[i])-97] 15 | return maxi*len(word) 16 | 17 | if __name__ == '__main__': 18 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 19 | 20 | h = list(map(int, input().rstrip().split())) 21 | 22 | word = input() 23 | 24 | result = designerPdfViewer(h, word) 25 | 26 | fptr.write(str(result) + '\n') 27 | 28 | fptr.close() 29 | -------------------------------------------------------------------------------- /HackerRank-Diagonal Difference/Diagonal_Difference.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the diagonalDifference function below. 10 | def diagonalDifference(arr): 11 | prim =0 12 | sec=0 13 | length = len(arr[0]) 14 | for count in range(length): 15 | prim += arr[count][count] 16 | sec += arr[count][(length-count-1)] 17 | return abs(prim-sec) 18 | 19 | 20 | if __name__ == '__main__': 21 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 22 | 23 | n = int(input()) 24 | 25 | arr = [] 26 | 27 | for _ in range(n): 28 | arr.append(list(map(int, input().rstrip().split()))) 29 | 30 | result = diagonalDifference(arr) 31 | 32 | fptr.write(str(result) + '\n') 33 | 34 | fptr.close() 35 | -------------------------------------------------------------------------------- /HackerRank-Divisible Sum Pairs/Divisible_Sum_Pairs.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the divisibleSumPairs function below. 10 | def divisibleSumPairs(n, k, ar): 11 | count=0 12 | for i in range(len(ar)): 13 | for j in range(i+1,len(ar)): 14 | if((ar[i]+ar[j])%k==0): 15 | count+=1 16 | return count 17 | 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | nk = input().split() 23 | 24 | n = int(nk[0]) 25 | 26 | k = int(nk[1]) 27 | 28 | ar = list(map(int, input().rstrip().split())) 29 | 30 | result = divisibleSumPairs(n, k, ar) 31 | 32 | fptr.write(str(result) + '\n') 33 | 34 | fptr.close() 35 | -------------------------------------------------------------------------------- /HackerRank-Electronics Shop/Electronics_Shop.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | import sys 5 | 6 | def getMoneySpent(keyboards, drives, b): 7 | return max([sum([x,y]) for x in keyboards for y in drives if sum([x,y]) <= b]+[-1]) 8 | 9 | if __name__ == '__main__': 10 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 11 | 12 | bnm = input().split() 13 | 14 | b = int(bnm[0]) 15 | 16 | n = int(bnm[1]) 17 | 18 | m = int(bnm[2]) 19 | 20 | keyboards = list(map(int, input().rstrip().split())) 21 | 22 | drives = list(map(int, input().rstrip().split())) 23 | 24 | moneySpent = getMoneySpent(keyboards, drives, b) 25 | 26 | fptr.write(str(moneySpent) + '\n') 27 | 28 | fptr.close() 29 | -------------------------------------------------------------------------------- /HackerRank-Encryption/Encryption.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | 6 | # complete the encryption function below. 7 | def encryption(s): 8 | L = len(s) 9 | rows = int(math.floor(L**(0.5))) 10 | columns = int(math.ceil(L**(0.5))) 11 | output = "" 12 | for i in range(columns): 13 | k = i 14 | for j in range(k,L,columns): 15 | output+=s[j] 16 | output+=" " 17 | return output 18 | 19 | 20 | if __name__ == '__main__': 21 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 22 | s = input() 23 | result = encryption(s) 24 | fptr.write(result + '\n') 25 | fptr.close() 26 | -------------------------------------------------------------------------------- /HackerRank-Extra Long Factorials/Extra_Long_Factorials.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the extraLongFactorials function below. 10 | def extraLongFactorials(n): 11 | ans=1 12 | while(n!=1): 13 | ans*=n 14 | n-=1 15 | print (ans) 16 | 17 | if __name__ == '__main__': 18 | n = int(input()) 19 | 20 | extraLongFactorials(n) 21 | -------------------------------------------------------------------------------- /HackerRank-Find Digits/Find_Digits.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the findDigits function below. 10 | def findDigits(n): 11 | count = 0 12 | print(list(str(n))) 13 | for i in list(str(n)): 14 | if int(i) != 0 and n % int(i) == 0: 15 | count += 1 16 | return count 17 | 18 | if __name__ == '__main__': 19 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 20 | 21 | t = int(input()) 22 | 23 | for t_itr in range(t): 24 | n = int(input()) 25 | 26 | result = findDigits(n) 27 | 28 | fptr.write(str(result) + '\n') 29 | 30 | fptr.close() 31 | -------------------------------------------------------------------------------- /HackerRank-Grading Students/Grading_Students.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | import sys 5 | 6 | def gradingStudents(grades): 7 | for i in range(len(grades)): 8 | if(grades[i]>37): 9 | if((grades[i]%5)!=0): 10 | if(5-(grades[i]%5)<3): 11 | grades[i]+=5-(grades[i]%5) 12 | return (grades) 13 | 14 | if __name__ == '__main__': 15 | f = open(os.environ['OUTPUT_PATH'], 'w') 16 | 17 | n = int(input()) 18 | 19 | grades = [] 20 | 21 | for _ in range(n): 22 | grades_item = int(input()) 23 | grades.append(grades_item) 24 | 25 | result = gradingStudents(grades) 26 | 27 | f.write('\n'.join(map(str, result))) 28 | f.write('\n') 29 | 30 | f.close() 31 | -------------------------------------------------------------------------------- /HackerRank-Jumping on the Clouds/Jumping_on_the_Clouds.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the jumpingOnClouds function below. 10 | def jumpingOnClouds(c, k): 11 | e=100 12 | energy=0 13 | i=0 14 | while(i!=len(c)): 15 | if(c[i]==1): 16 | energy=e-3 17 | e=energy 18 | else: 19 | energy=e-1 20 | e=energy 21 | i+=k 22 | return energy 23 | 24 | if __name__ == '__main__': 25 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 26 | 27 | nk = input().split() 28 | 29 | n = int(nk[0]) 30 | 31 | k = int(nk[1]) 32 | 33 | c = list(map(int, input().rstrip().split())) 34 | 35 | result = jumpingOnClouds(c, k) 36 | 37 | fptr.write(str(result) + '\n') 38 | 39 | fptr.close() -------------------------------------------------------------------------------- /HackerRank-Kangaroo/Kangaroo.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the kangaroo function below. 10 | def kangaroo(x1, v1, x2, v2): 11 | for n in range(10000): 12 | if((x1+v1)==(x2+v2)): 13 | return "YES" 14 | x1+=v1 15 | x2+=v2 16 | return "NO" 17 | 18 | if __name__ == '__main__': 19 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 20 | 21 | x1V1X2V2 = input().split() 22 | 23 | x1 = int(x1V1X2V2[0]) 24 | 25 | v1 = int(x1V1X2V2[1]) 26 | 27 | x2 = int(x1V1X2V2[2]) 28 | 29 | v2 = int(x1V1X2V2[3]) 30 | 31 | result = kangaroo(x1, v1, x2, v2) 32 | 33 | fptr.write(result + '\n') 34 | 35 | fptr.close() 36 | -------------------------------------------------------------------------------- /HackerRank-Migratory Birds/Migratory_Birds.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the migratoryBirds function below. 10 | def migratoryBirds(arr): 11 | bird_freq = [0, 0, 0, 0, 0, 0] 12 | for i in range(len(arr)): 13 | bird_freq[arr[i]] += 1 14 | return bird_freq.index(max(bird_freq)) 15 | 16 | if __name__ == '__main__': 17 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 18 | 19 | arr_count = int(input().strip()) 20 | 21 | arr = list(map(int, input().rstrip().split())) 22 | 23 | result = migratoryBirds(arr) 24 | 25 | fptr.write(str(result) + '\n') 26 | 27 | fptr.close() 28 | -------------------------------------------------------------------------------- /HackerRank-Mini-Max Sum/Mini-Max_Sum.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the miniMaxSum function below. 10 | def miniMaxSum(arr): 11 | sum=0 12 | for i in range(len(arr)): 13 | sum+=arr[i] 14 | print ( sum-max(arr), sum-min(arr)) 15 | 16 | if __name__ == '__main__': 17 | arr = list(map(int, input().rstrip().split())) 18 | 19 | miniMaxSum(arr) 20 | -------------------------------------------------------------------------------- /HackerRank-Plus Minus/Plus_Minus.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the plusMinus function below. 10 | def plusMinus(arr): 11 | pos=0 12 | neg=0 13 | neu=0 14 | for i in range(len(arr)): 15 | if(arr[i]>0): 16 | pos+=1 17 | elif(arr[i]==0): 18 | neu+=1 19 | else: 20 | neg+=1 21 | print(pos/len(arr)) 22 | print(neg/len(arr)) 23 | print(neu/len(arr)) 24 | 25 | if __name__ == '__main__': 26 | n = int(input()) 27 | 28 | arr = list(map(int, input().rstrip().split())) 29 | 30 | plusMinus(arr) 31 | -------------------------------------------------------------------------------- /HackerRank-Simple Array Sum/Simple_Array_Sum.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | import os 3 | import sys 4 | 5 | def simpleArraySum(ar): 6 | sum=0; 7 | for i in range(len(ar)): 8 | sum+=ar[i] 9 | return (sum) 10 | 11 | if __name__ == '__main__': 12 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 13 | 14 | ar_count = int(input()) 15 | 16 | ar = list(map(int, input().rstrip().split())) 17 | 18 | result = simpleArraySum(ar) 19 | 20 | fptr.write(str(result) + '\n') 21 | 22 | fptr.close() 23 | -------------------------------------------------------------------------------- /HackerRank-Sock Merchant/Sock_Merchant.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the sockMerchant function below. 10 | def sockMerchant(n, ar): 11 | count = 0 12 | ar.sort() 13 | ar.append('#') 14 | i = 0 15 | while ik): 13 | return maxi-k 14 | return 0 15 | 16 | if __name__ == '__main__': 17 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 18 | 19 | nk = input().split() 20 | 21 | n = int(nk[0]) 22 | 23 | k = int(nk[1]) 24 | 25 | height = list(map(int, input().rstrip().split())) 26 | 27 | result = hurdleRace(k, height) 28 | 29 | fptr.write(str(result) + '\n') 30 | 31 | fptr.close() 32 | -------------------------------------------------------------------------------- /HackerRank-Time Conversion/Time_Conversion.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | import sys 5 | 6 | def timeConversion(s): 7 | time = s.split(":") 8 | if s[-2:] == "PM": 9 | if time[0] != "12": 10 | time[0] = str(int(time[0])+12) 11 | else: 12 | if time[0] == "12": 13 | time[0] = "00" 14 | ntime = ':'.join(time) 15 | return str(ntime[:-2]) 16 | 17 | if __name__ == '__main__': 18 | f = open(os.environ['OUTPUT_PATH'], 'w') 19 | 20 | s = input() 21 | 22 | result = timeConversion(s) 23 | 24 | f.write(result + '\n') 25 | 26 | f.close() 27 | -------------------------------------------------------------------------------- /HackerRank-Utopian Tree/Utopian_Tree.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the utopianTree function below. 10 | def utopianTree(n): 11 | boolean = False 12 | count=0 13 | for i in range(n+1): 14 | if(boolean == False): 15 | count+=1 16 | boolean = True 17 | else: 18 | count*=2 19 | boolean=False 20 | return count 21 | 22 | if __name__ == '__main__': 23 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 24 | 25 | t = int(input()) 26 | 27 | for t_itr in range(t): 28 | n = int(input()) 29 | 30 | result = utopianTree(n) 31 | 32 | fptr.write(str(result) + '\n') 33 | 34 | fptr.close() 35 | -------------------------------------------------------------------------------- /HackerRank-Viral Advertising/Viral_Advertising.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the viralAdvertising function below. 10 | def viralAdvertising(n): 11 | shared =5 12 | cumulative=0 13 | for i in range(1,n+1): 14 | liked = shared//2 15 | cumulative+=liked 16 | shared = liked*3 17 | return cumulative 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | n = int(input()) 23 | 24 | result = viralAdvertising(n) 25 | 26 | fptr.write(str(result) + '\n') 27 | 28 | fptr.close() 29 | -------------------------------------------------------------------------------- /Hackerrank-Manasa_and_Stones/manasa_and_stones.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | from itertools import combinations_with_replacement 9 | 10 | # Complete the stones function below. 11 | def stones(n, a, b): 12 | arr = [] 13 | perm = list(combinations_with_replacement([a, b], n-1)) 14 | for i in perm: 15 | arr.append(sum(i)) 16 | arr = list(set(arr)) 17 | arr.sort() 18 | return arr 19 | 20 | 21 | if __name__ == '__main__': 22 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 23 | 24 | T = int(input()) 25 | 26 | for T_itr in range(T): 27 | n = int(input()) 28 | 29 | a = int(input()) 30 | 31 | b = int(input()) 32 | 33 | result = stones(n, a, b) 34 | 35 | fptr.write(' '.join(map(str, result))) 36 | fptr.write('\n') 37 | 38 | fptr.close() 39 | -------------------------------------------------------------------------------- /Hackerrank-The_Minion_Game/theMinionGame.py: -------------------------------------------------------------------------------- 1 | def minion_game(string): 2 | # your code goes here 3 | 4 | vowels = ['A', 'E', 'I', 'O', 'U'] 5 | kevin = 0 6 | stuart = 0 7 | for i in range(len(string)): 8 | if s[i] in vowels: 9 | kevin = kevin + (len(s)-i) 10 | else: 11 | stuart = stuart + (len(s)-i) 12 | 13 | if stuart > kevin: 14 | print('Stuart '+ str(stuart)) 15 | elif kevin > stuart: 16 | print('Kevin ' + str(kevin)) 17 | else: 18 | print('Draw') 19 | 20 | 21 | if __name__ == '__main__': 22 | s = input() 23 | minion_game(s) -------------------------------------------------------------------------------- /HackkerRank-Angry Professor/Angry_Professor.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the angryProfessor function below. 10 | def angryProfessor(k, a): 11 | count=0 12 | for i in range(len(a)): 13 | if(a[i]<=0): 14 | count+=1 15 | if(count>=k): 16 | return "NO" 17 | return "YES" 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | t = int(input()) 23 | 24 | for t_itr in range(t): 25 | nk = input().split() 26 | 27 | n = int(nk[0]) 28 | 29 | k = int(nk[1]) 30 | 31 | a = list(map(int, input().rstrip().split())) 32 | 33 | result = angryProfessor(k, a) 34 | 35 | fptr.write(result + '\n') 36 | 37 | fptr.close() 38 | -------------------------------------------------------------------------------- /Merge Sort/merge_sort.py: -------------------------------------------------------------------------------- 1 | # Python 3 program to count inversions in an array 2 | 3 | # Function to Use Inversion Count 4 | def mergeSort(arr, n): 5 | # A temp_arr is created to store 6 | # sorted array in merge function 7 | temp_arr = [0] * n 8 | return _mergeSort(arr, temp_arr, 0, n - 1) 9 | 10 | 11 | # This Function will use MergeSort to count inversions 12 | 13 | def _mergeSort(arr, temp_arr, left, right): 14 | # A variable inv_count is used to store 15 | # inversion counts in each recursive call 16 | 17 | inv_count = 0 18 | 19 | # We will make a recursive call if and only if 20 | # we have more than one elements 21 | 22 | if left < right: 23 | # mid is calculated to divide the array into two subarrays 24 | # Floor division is must in case of python 25 | 26 | mid = (left + right) // 2 27 | 28 | # It will calculate inversion counts in the left subarray 29 | 30 | inv_count += _mergeSort(arr, temp_arr, left, mid) 31 | 32 | # It will calculate inversion counts in right subarray 33 | 34 | inv_count += _mergeSort(arr, temp_arr, mid + 1, right) 35 | 36 | # It will merge two subarrays in a sorted subarray 37 | 38 | inv_count += merge(arr, temp_arr, left, mid, right) 39 | return inv_count 40 | 41 | 42 | # This function will merge two subarrays in a single sorted subarray 43 | def merge(arr, temp_arr, left, mid, right): 44 | i = left # Starting index of left subarray 45 | j = mid + 1 # Starting index of right subarray 46 | k = left # Starting index of to be sorted subarray 47 | inv_count = 0 48 | 49 | # Conditions are checked to make sure that i and j don't exceed their 50 | # subarray limits. 51 | 52 | while i <= mid and j <= right: 53 | 54 | # There will be no inversion if arr[i] <= arr[j] 55 | 56 | if arr[i] <= arr[j]: 57 | temp_arr[k] = arr[i] 58 | k += 1 59 | i += 1 60 | else: 61 | # Inversion will occur. 62 | temp_arr[k] = arr[j] 63 | inv_count += (mid - i + 1) 64 | k += 1 65 | j += 1 66 | 67 | # Copy the remaining elements of left subarray into temporary array 68 | while i <= mid: 69 | temp_arr[k] = arr[i] 70 | k += 1 71 | i += 1 72 | 73 | # Copy the remaining elements of right subarray into temporary array 74 | while j <= right: 75 | temp_arr[k] = arr[j] 76 | k += 1 77 | j += 1 78 | 79 | # Copy the sorted subarray into Original array 80 | for loop_var in range(left, right + 1): 81 | arr[loop_var] = temp_arr[loop_var] 82 | 83 | return inv_count 84 | 85 | 86 | # Driver Code 87 | # Given array is 88 | arr = [1, 20, 6, 4, 5] 89 | n = len(arr) 90 | result = mergeSort(arr, n) 91 | print("Number of inversions are", result) 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hackerrank-Problem-Solving-Python-Solutions 2 | This repo consists the solution of hackerrank problem solving solutions in python 3 | --------------------------------------------------------------------------------