├── Problem Solving (Basic) ├── AVeryBigSum.py ├── Alternating Characters.py ├── Angry Professor.py ├── AppleandOrange.py ├── Beautiful Days at the Movies.py ├── Beautiful Triplets.py ├── Between Two Sets.py ├── BillDivision.py ├── BirthdayCakeCandles.py ├── BreakingtheRecords.py ├── Caesar Cipher.py ├── CamelCase.py ├── CatsandaMouse.py ├── Chocolate Feast.py ├── Circular Array Rotation.py ├── ComparetheTriplets.py ├── Correctness and the Loop Invariant.py ├── Counting Sort 1.py ├── Counting Valleys.py ├── Cut the sticks.py ├── DayoftheProgrammer.py ├── DesignerPDFViewer.py ├── DiagonalDifference.py ├── DivisibleSumPairs.py ├── DrawingBook.py ├── Electronics Shop.py ├── Equalize the Array.py ├── Fair Rations.py ├── Find Digits.py ├── Game of Thrones - I.py ├── Gemstones.py ├── GradingStudents.py ├── HackerRank in a String.py ├── Halloween Sale.py ├── Intro to Tutorial Challenges.py ├── Jumping on the Clouds.py ├── Jumping on the Clouds: Revisited.py ├── Library Fine.py ├── Lisa's Workbook.py ├── Marc's Cakewalk.py ├── Mark and Toys.py ├── Mars Exploration.py ├── MigratoryBirds.py ├── Mini-MaxSum.py ├── Minimum Distances.py ├── NumberLineJumps.py ├── Pangrams.py ├── Picking Numbers.py ├── PlusMinus.py ├── Repeated String.py ├── Running Time of Algorithms.py ├── SalesbyMatch.py ├── Save the Prisoner!.py ├── Sequence Equation.py ├── Service Lane.py ├── Sherlock and Squares.py ├── SimpleArraySum.py ├── Staircase.py ├── StrangeCounter.py ├── String Construction.py ├── StrongPassword.py ├── SubarrayDivision.py ├── SuperReducedString.py ├── Taum and B'day.py ├── The Love-Letter Mystery.py ├── TheHurdleRace.py ├── TimeConversion.py ├── Two Strings.py ├── UtopianTree.py ├── Viral Advertising.py ├── acmTeam.py ├── anagram.py ├── balancedSums.py ├── beautifulBinaryString.py ├── bigSorting.py ├── cavityMap.py ├── climbingLeaderboard.py ├── closestNumbers.py ├── countSort.py ├── countingSort.py ├── decentNumber.py ├── fibonacciModified.py ├── findMedian.py ├── findZigZagSequence.py ├── funnyString.py ├── gameOfStones.py ├── gridChallenge.py ├── gridSearch.py ├── happyLadybugs.py ├── insertionSort1.py ├── insertionSort2.py ├── is_smart_number.py ├── jimOrders.py ├── kaprekarNumbers.py ├── lonelyinteger.py ├── luckBalance.py ├── makingAnagrams.py ├── maxMin.py ├── maximizingXor.py ├── maximumPerimeterTriangle.py ├── minimumAbsoluteDifference.py ├── missingNumbers.py ├── palindromeIndex.py ├── quickSort.py ├── stones.py ├── strings_xor.py ├── timeInWords.py ├── towerBreakers.py ├── toys.py └── twoArrays.py ├── Problem Solving (Intermediate) ├── extraLongFactorials.py ├── flatlandSpaceStations.py └── icecreamParlor.py └── README.md /Problem Solving (Basic)/AVeryBigSum.py: -------------------------------------------------------------------------------- 1 | # Complete the aVeryBigSum function below. 2 | def aVeryBigSum(ar): 3 | return sum(ar) 4 | 5 | 6 | if __name__ == '__main__': 7 | 8 | ar_count = int(input()) 9 | 10 | ar = list(map(int, input().rstrip().split())) 11 | 12 | result = aVeryBigSum(ar) 13 | 14 | print(result) 15 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Alternating Characters.py: -------------------------------------------------------------------------------- 1 | def alternatingCharacters(s): 2 | res = 0 3 | for i in range(1, len(s)): 4 | if s[i - 1] == s[i]: 5 | res += 1 6 | return res 7 | 8 | 9 | if __name__ == '__main__': 10 | q = int(input()) 11 | 12 | for q_itr in range(q): 13 | s = input() 14 | 15 | result = alternatingCharacters(s) 16 | 17 | print(str(result)) 18 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Angry Professor.py: -------------------------------------------------------------------------------- 1 | def angryProfessor(k, a): 2 | a.sort() 3 | c = 0 4 | for i in a: 5 | if i > 0: 6 | return "YES" 7 | c += 1 8 | if c == k: 9 | return "NO" 10 | 11 | 12 | if __name__ == '__main__': 13 | t = int(input()) 14 | 15 | for t_itr in range(t): 16 | nk = input().split() 17 | 18 | n = int(nk[0]) 19 | 20 | k = int(nk[1]) 21 | 22 | a = list(map(int, input().rstrip().split())) 23 | 24 | result = angryProfessor(k, a) 25 | 26 | print(result + '\n') 27 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/AppleandOrange.py: -------------------------------------------------------------------------------- 1 | def countApplesAndOranges(s, t, a, b, apples, oranges): 2 | countApples, countOranges = 0, 0 3 | for apple in apples: 4 | if s <= apple + a <= t: 5 | countApples += 1 6 | for orange in oranges: 7 | if s <= orange + b <= t: 8 | countOranges += 1 9 | print(countApples, countOranges, sep='\n') 10 | 11 | 12 | if __name__ == '__main__': 13 | st = input().split() 14 | 15 | s = int(st[0]) 16 | 17 | t = int(st[1]) 18 | 19 | ab = input().split() 20 | 21 | a = int(ab[0]) 22 | 23 | b = int(ab[1]) 24 | 25 | mn = input().split() 26 | 27 | m = int(mn[0]) 28 | 29 | n = int(mn[1]) 30 | 31 | apples = list(map(int, input().rstrip().split())) 32 | 33 | oranges = list(map(int, input().rstrip().split())) 34 | 35 | countApplesAndOranges(s, t, a, b, apples, oranges) 36 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Beautiful Days at the Movies.py: -------------------------------------------------------------------------------- 1 | def beautifulDays(i, j, k): 2 | res = 0 3 | for n in range(i, j + 1): 4 | x = int(str(n)[::-1]) 5 | if abs(n - x) % k == 0: 6 | res += 1 7 | return res 8 | 9 | 10 | if __name__ == '__main__': 11 | i, j, k = [int(x) for x in input().split()] 12 | 13 | result = beautifulDays(i, j, k) 14 | 15 | print(str(result) + '\n') 16 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Beautiful Triplets.py: -------------------------------------------------------------------------------- 1 | def beautifulTriplets(d, arr): 2 | res = 0 3 | for i in arr: 4 | if i + d in arr and i + 2 * d in arr: 5 | res += 1 6 | return res 7 | 8 | 9 | if __name__ == '__main__': 10 | nd = input().split() 11 | 12 | n = int(nd[0]) 13 | 14 | d = int(nd[1]) 15 | 16 | arr = list(map(int, input().rstrip().split())) 17 | 18 | result = beautifulTriplets(d, arr) 19 | 20 | print(str(result) + '\n') 21 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Between Two Sets.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def lcm(x, y): 5 | return abs(x * y) // math.gcd(x, y) 6 | 7 | 8 | def lcmList(a): 9 | res = a[0] 10 | for i in a[1:]: 11 | res = lcm(res, i) 12 | return res 13 | 14 | 15 | def gcdList(a): 16 | res = a[0] 17 | for i in a[1:]: 18 | res = math.gcd(res, i) 19 | return res 20 | 21 | 22 | def getTotalX(a, b): 23 | lcm_a = lcmList(a) 24 | gcd_b = gcdList(b) 25 | res = 0 26 | for i in range(lcm_a, gcd_b + 1, lcm_a): 27 | if gcd_b % i == 0: 28 | res += 1 29 | return res 30 | 31 | 32 | if __name__ == '__main__': 33 | first_multiple_input = input().rstrip().split() 34 | 35 | n = int(first_multiple_input[0]) 36 | 37 | m = int(first_multiple_input[1]) 38 | 39 | arr = list(map(int, input().rstrip().split())) 40 | 41 | brr = list(map(int, input().rstrip().split())) 42 | 43 | total = getTotalX(arr, brr) 44 | 45 | print(str(total) + '\n') 46 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/BillDivision.py: -------------------------------------------------------------------------------- 1 | def bonAppetit(bill, k, b): 2 | s = sum(bill) - bill[k] 3 | r = s // 2 4 | diff = b - r 5 | if diff: 6 | print(diff) 7 | else: 8 | print("Bon Appetit") 9 | 10 | 11 | if __name__ == '__main__': 12 | nk = input().rstrip().split() 13 | 14 | n = int(nk[0]) 15 | 16 | k = int(nk[1]) 17 | 18 | bill = list(map(int, input().rstrip().split())) 19 | 20 | b = int(input().strip()) 21 | 22 | bonAppetit(bill, k, b) 23 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/BirthdayCakeCandles.py: -------------------------------------------------------------------------------- 1 | def birthdayCakeCandles(candles): 2 | m = candles[0] 3 | res = 0 4 | for i in candles: 5 | if i == m: 6 | res += 1 7 | elif i > m: 8 | m = i 9 | res = 1 10 | return res 11 | 12 | 13 | if __name__ == '__main__': 14 | candles_count = int(input().strip()) 15 | 16 | candles = list(map(int, input().rstrip().split())) 17 | 18 | result = birthdayCakeCandles(candles) 19 | 20 | print(str(result) + '\n') 21 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/BreakingtheRecords.py: -------------------------------------------------------------------------------- 1 | def breakingRecords(scores): 2 | i, j, m, M = 0, 0, scores[0], scores[0] 3 | for score in scores: 4 | if m > score: 5 | i += 1 6 | m = score 7 | if M < score: 8 | j += 1 9 | M = score 10 | return j, i 11 | 12 | 13 | if __name__ == '__main__': 14 | n = int(input()) 15 | 16 | scores = list(map(int, input().rstrip().split())) 17 | 18 | result = breakingRecords(scores) 19 | 20 | print(' '.join(map(str, result))) 21 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Caesar Cipher.py: -------------------------------------------------------------------------------- 1 | def caesarCipher(s, k): 2 | res = [] 3 | for i in s: 4 | if "A" <= i <= "Z": 5 | res.append(chr(65 + (ord(i) + k - 65) % 26)) 6 | elif "a" <= i <= "z": 7 | res.append(chr(97 + (ord(i) + k - 97) % 26)) 8 | else: 9 | res.append(i) 10 | return "".join(res) 11 | 12 | 13 | if __name__ == '__main__': 14 | n = int(input()) 15 | 16 | s = input() 17 | 18 | k = int(input()) 19 | 20 | result = caesarCipher(s, k) 21 | 22 | print(result + '\n') 23 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/CamelCase.py: -------------------------------------------------------------------------------- 1 | def camelcase(s): 2 | res = 0 3 | if s: 4 | res += 1 5 | for i in s[1:]: 6 | if "A" <= i <= "Z": 7 | res += 1 8 | return res 9 | 10 | 11 | if __name__ == '__main__': 12 | s = input() 13 | 14 | result = camelcase(s) 15 | 16 | print(str(result) + '\n') 17 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/CatsandaMouse.py: -------------------------------------------------------------------------------- 1 | def catAndMouse(x, y, z): 2 | d1 = abs(x - z) 3 | d2 = abs(y - z) 4 | if d1 == d2: 5 | return 'Mouse C' 6 | elif d1 < d2: 7 | return 'Cat A' 8 | return 'Cat B' 9 | 10 | 11 | if __name__ == '__main__': 12 | q = int(input()) 13 | 14 | for q_itr in range(q): 15 | xyz = input().split() 16 | 17 | x = int(xyz[0]) 18 | 19 | y = int(xyz[1]) 20 | 21 | z = int(xyz[2]) 22 | 23 | result = catAndMouse(x, y, z) 24 | 25 | print(result + '\n') 26 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Chocolate Feast.py: -------------------------------------------------------------------------------- 1 | def chocolateFeast(n, c, m): 2 | w = n // c 3 | res = w 4 | while w >= m: 5 | temp = (w // m) 6 | res += temp 7 | w = w % m + temp 8 | return res 9 | 10 | 11 | if __name__ == '__main__': 12 | t = int(input()) 13 | 14 | for t_itr in range(t): 15 | ncm = input().split() 16 | 17 | n = int(ncm[0]) 18 | 19 | c = int(ncm[1]) 20 | 21 | m = int(ncm[2]) 22 | 23 | result = chocolateFeast(n, c, m) 24 | 25 | print(str(result) + '\n') 26 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Circular Array Rotation.py: -------------------------------------------------------------------------------- 1 | def circularArrayRotation(a, k, queries): 2 | res = [] 3 | for i in queries: 4 | res.append(a[(n - (k % n) + i) % n]) 5 | return res 6 | 7 | 8 | if __name__ == '__main__': 9 | nkq = input().split() 10 | 11 | n = int(nkq[0]) 12 | 13 | k = int(nkq[1]) 14 | 15 | q = int(nkq[2]) 16 | 17 | a = list(map(int, input().rstrip().split())) 18 | 19 | queries = [] 20 | 21 | for _ in range(q): 22 | queries_item = int(input()) 23 | queries.append(queries_item) 24 | 25 | result = circularArrayRotation(a, k, queries) 26 | 27 | print('\n'.join(map(str, result))) 28 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/ComparetheTriplets.py: -------------------------------------------------------------------------------- 1 | # Complete the compareTriplets function below. 2 | def compareTriplets(a, b): 3 | ar = [0, 0] 4 | for i in range(3): 5 | if a[i] < b[i]: 6 | ar[1] += 1 7 | elif a[i] > b[i]: 8 | ar[0] += 1 9 | return ar 10 | 11 | 12 | if __name__ == '__main__': 13 | a = list(map(int, input().rstrip().split())) 14 | 15 | b = list(map(int, input().rstrip().split())) 16 | 17 | result = compareTriplets(a, b) 18 | 19 | print(' '.join(map(str, result))) 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Correctness and the Loop Invariant.py: -------------------------------------------------------------------------------- 1 | def insertion_sort(l): 2 | for i in range(1, len(l)): 3 | j = i-1 4 | key = l[i] 5 | while (j >= 0) and (l[j] > key): 6 | l[j+1] = l[j] 7 | j -= 1 8 | l[j+1] = key 9 | 10 | 11 | m = int(input().strip()) 12 | ar = [int(i) for i in input().strip().split()] 13 | insertion_sort(ar) 14 | print(" ".join(map(str, ar))) 15 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Counting Sort 1.py: -------------------------------------------------------------------------------- 1 | def countingSort(arr): 2 | return [arr.count(i) for i in range(100)] 3 | 4 | 5 | if __name__ == '__main__': 6 | n = int(input()) 7 | 8 | arr = list(map(int, input().rstrip().split())) 9 | 10 | result = countingSort(arr) 11 | 12 | print(' '.join(map(str, result))) 13 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Counting Valleys.py: -------------------------------------------------------------------------------- 1 | def countingValleys(steps, path): 2 | res = 0 3 | lvl = 0 4 | for s in path: 5 | if s == 'U': 6 | lvl += 1 7 | if lvl == 0: 8 | res += 1 9 | else: 10 | lvl -= 1 11 | return res 12 | 13 | 14 | if __name__ == '__main__': 15 | steps = int(input().strip()) 16 | 17 | path = input() 18 | 19 | result = countingValleys(steps, path) 20 | 21 | print(str(result) + '\n') 22 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Cut the sticks.py: -------------------------------------------------------------------------------- 1 | def cutTheSticks(arr): 2 | l = len(arr) 3 | res = [] 4 | while l: 5 | res.append(l) 6 | m = min(arr) 7 | arr = [i - m for i in arr if i - m > 0] 8 | l = len(arr) 9 | return res 10 | 11 | 12 | if __name__ == '__main__': 13 | n = int(input()) 14 | 15 | arr = list(map(int, input().rstrip().split())) 16 | 17 | result = cutTheSticks(arr) 18 | 19 | print('\n'.join(map(str, result))) 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/DayoftheProgrammer.py: -------------------------------------------------------------------------------- 1 | def dayOfProgrammer(year): 2 | if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) or (year % 4 == 0 and year < 1918): 3 | return f"12.09.{year}" 4 | if year == 1918: 5 | return f"26.09.{year}" 6 | return f"13.09.{year}" 7 | 8 | 9 | if __name__ == '__main__': 10 | year = int(input().strip()) 11 | 12 | result = dayOfProgrammer(year) 13 | 14 | print(result + '\n') 15 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/DesignerPDFViewer.py: -------------------------------------------------------------------------------- 1 | def designerPdfViewer(h, word): 2 | res = 0 3 | new_word = set(word) 4 | 5 | for letter in new_word: 6 | i = ord(letter) - ord("a") 7 | res = max(res, h[i]) 8 | 9 | return res * len(word) 10 | 11 | 12 | if __name__ == '__main__': 13 | h = list(map(int, input().rstrip().split())) 14 | 15 | word = input() 16 | 17 | result = designerPdfViewer(h, word) 18 | 19 | print(str(result) + '\n') 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/DiagonalDifference.py: -------------------------------------------------------------------------------- 1 | # 2 | # Complete the 'diagonalDifference' function below. 3 | # 4 | # The function is expected to return an INTEGER. 5 | # The function accepts 2D_INTEGER_ARRAY arr as parameter. 6 | # 7 | 8 | def diagonalDifference(arr): 9 | l, r = 0, 0 10 | s = len(arr) 11 | 12 | for i in range(s): 13 | l += arr[i][i] 14 | r += arr[i][s - i - 1] 15 | 16 | return abs(l - r) 17 | 18 | 19 | if __name__ == '__main__': 20 | n = int(input().strip()) 21 | 22 | arr = [] 23 | 24 | for _ in range(n): 25 | arr.append(list(map(int, input().rstrip().split()))) 26 | 27 | result = diagonalDifference(arr) 28 | 29 | print(str(result) + '\n') 30 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/DivisibleSumPairs.py: -------------------------------------------------------------------------------- 1 | def divisibleSumPairs(n, k, ar): 2 | res = 0 3 | for i in range(len(ar) - 1): 4 | for j in range(i + 1, len(ar)): 5 | if (ar[i] + ar[j]) % k == 0: 6 | res += 1 7 | return res 8 | 9 | 10 | if __name__ == '__main__': 11 | nk = input().split() 12 | 13 | n = int(nk[0]) 14 | 15 | k = int(nk[1]) 16 | 17 | ar = list(map(int, input().rstrip().split())) 18 | 19 | result = divisibleSumPairs(n, k, ar) 20 | 21 | print(str(result) + '\n') 22 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/DrawingBook.py: -------------------------------------------------------------------------------- 1 | def pageCount(n, p): 2 | s = n // 2 3 | d = p // 2 4 | return min(s - d, d) 5 | 6 | 7 | if __name__ == '__main__': 8 | n = int(input()) 9 | 10 | p = int(input()) 11 | 12 | result = pageCount(n, p) 13 | 14 | print(str(result) + '\n') 15 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Electronics Shop.py: -------------------------------------------------------------------------------- 1 | def getMoneySpent(keyboards, drives, b, n, m): 2 | keyboards.sort() 3 | drives.sort() 4 | i = 0 5 | res = -1 6 | j = m - 1 7 | while j >= 0: 8 | while i < n: 9 | if keyboards[i] + drives[j] > b: 10 | break 11 | if keyboards[i] + drives[j] > res: 12 | res = keyboards[i] + drives[j] 13 | i += 1 14 | j -= 1 15 | return res 16 | 17 | 18 | if __name__ == '__main__': 19 | bnm = input().split() 20 | 21 | b = int(bnm[0]) 22 | 23 | n = int(bnm[1]) 24 | 25 | m = int(bnm[2]) 26 | 27 | keyboards = list(map(int, input().rstrip().split())) 28 | 29 | drives = list(map(int, input().rstrip().split())) 30 | 31 | moneySpent = getMoneySpent(keyboards, drives, b, n, m) 32 | 33 | print(str(moneySpent) + '\n') 34 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Equalize the Array.py: -------------------------------------------------------------------------------- 1 | import collections 2 | 3 | 4 | def equalizeArray(arr): 5 | return len(arr) - max(collections.Counter(arr).values()) 6 | 7 | 8 | if __name__ == '__main__': 9 | n = int(input()) 10 | 11 | arr = list(map(int, input().rstrip().split())) 12 | 13 | result = equalizeArray(arr) 14 | 15 | print(str(result) + '\n') 16 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Fair Rations.py: -------------------------------------------------------------------------------- 1 | def fairRations(B): 2 | s = sum(B) 3 | if s % 2: 4 | return "NO" 5 | else: 6 | res = 0 7 | for i in range(len(B)): 8 | if B[i] % 2: 9 | B[i] += 1 10 | B[i + 1] += 1 11 | res += 2 12 | return str(res) 13 | 14 | 15 | if __name__ == '__main__': 16 | N = int(input().strip()) 17 | 18 | B = list(map(int, input().rstrip().split())) 19 | 20 | result = fairRations(B) 21 | 22 | print(result + '\n') 23 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Find Digits.py: -------------------------------------------------------------------------------- 1 | def findDigits(n): 2 | count = 0 3 | for i in str(n): 4 | if int(i) != 0 and n % int(i) == 0: 5 | count += 1 6 | return count 7 | 8 | 9 | if __name__ == '__main__': 10 | t = int(input()) 11 | 12 | for t_itr in range(t): 13 | n = int(input()) 14 | 15 | result = findDigits(n) 16 | 17 | print(str(result) + '\n') 18 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Game of Thrones - I.py: -------------------------------------------------------------------------------- 1 | def gameOfThrones(s): 2 | s_set = set(s) 3 | temp = 0 4 | for i in s_set: 5 | if s.count(i) % 2: 6 | temp += 1 7 | if temp >= 2: 8 | return "NO" 9 | return "YES" 10 | 11 | 12 | if __name__ == '__main__': 13 | s = input() 14 | 15 | result = gameOfThrones(s) 16 | 17 | print(result + '\n') 18 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Gemstones.py: -------------------------------------------------------------------------------- 1 | def gemstones(arr): 2 | res = 0 3 | s = set(arr[0]) 4 | for c in s: 5 | count = 0 6 | for st in arr[1:]: 7 | if c in st: 8 | count += 1 9 | if count == len(arr) - 1: 10 | res += 1 11 | return res 12 | 13 | 14 | if __name__ == '__main__': 15 | n = int(input()) 16 | 17 | arr = [] 18 | 19 | for _ in range(n): 20 | arr_item = input() 21 | arr.append(arr_item) 22 | 23 | result = gemstones(arr) 24 | 25 | print(str(result) + '\n') 26 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/GradingStudents.py: -------------------------------------------------------------------------------- 1 | def gradingStudents(grades): 2 | res = [] 3 | for grade in grades: 4 | if grade >= 38 and grade % 5 >= 3: 5 | grade += 5 - grade % 5 6 | res.append(grade) 7 | return res 8 | 9 | 10 | if __name__ == '__main__': 11 | grades_count = int(input().strip()) 12 | grades = [] 13 | for _ in range(grades_count): 14 | grades_item = int(input().strip()) 15 | grades.append(grades_item) 16 | 17 | result = gradingStudents(grades) 18 | 19 | print('\n'.join(map(str, result))) 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/HackerRank in a String.py: -------------------------------------------------------------------------------- 1 | def hackerrankInString(s): 2 | c = "hackerrank" 3 | j = 0 4 | for i in s: 5 | if j < len(c) and i == c[j]: 6 | j += 1 7 | if j == len(c): 8 | return "YES" 9 | return "NO" 10 | 11 | 12 | if __name__ == '__main__': 13 | q = int(input()) 14 | 15 | for q_itr in range(q): 16 | s = input() 17 | 18 | result = hackerrankInString(s) 19 | 20 | print(result + '\n') -------------------------------------------------------------------------------- /Problem Solving (Basic)/Halloween Sale.py: -------------------------------------------------------------------------------- 1 | def howManyGames(p, d, m, s): 2 | count = 0 3 | res = 0 4 | while count + p <= s: 5 | res += 1 6 | count += p 7 | p -= d 8 | if p < m: 9 | p = m 10 | return res 11 | 12 | 13 | if __name__ == '__main__': 14 | pdms = input().split() 15 | 16 | p = int(pdms[0]) 17 | 18 | d = int(pdms[1]) 19 | 20 | m = int(pdms[2]) 21 | 22 | s = int(pdms[3]) 23 | 24 | answer = howManyGames(p, d, m, s) 25 | 26 | print(str(answer) + '\n') 27 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Intro to Tutorial Challenges.py: -------------------------------------------------------------------------------- 1 | def introTutorial(V, arr): 2 | return arr.index(V) 3 | 4 | 5 | if __name__ == '__main__': 6 | V = int(input()) 7 | 8 | n = int(input()) 9 | 10 | arr = list(map(int, input().rstrip().split())) 11 | 12 | result = introTutorial(V, arr) 13 | 14 | print(str(result) + '\n') 15 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Jumping on the Clouds.py: -------------------------------------------------------------------------------- 1 | def jumpingOnClouds(c): 2 | res = 0 3 | i = 0 4 | while i < len(c) - 1: 5 | if c[i] == 0: 6 | if i < len(c) - 2 and c[i + 2] == 0: 7 | i += 1 8 | res += 1 9 | i += 1 10 | 11 | return res 12 | 13 | 14 | if __name__ == '__main__': 15 | n = int(input()) 16 | 17 | c = list(map(int, input().rstrip().split())) 18 | 19 | result = jumpingOnClouds(c) 20 | 21 | print(str(result) + '\n') 22 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Jumping on the Clouds: Revisited.py: -------------------------------------------------------------------------------- 1 | def jumpingOnClouds(c, k): 2 | res = 100 3 | i = k % n 4 | res -= c[i] * 2 + 1 5 | while i: 6 | i = (i + k) % n 7 | res -= c[i] * 2 + 1 8 | return res 9 | 10 | 11 | if __name__ == '__main__': 12 | nk = input().split() 13 | 14 | n = int(nk[0]) 15 | 16 | k = int(nk[1]) 17 | 18 | c = list(map(int, input().rstrip().split())) 19 | 20 | result = jumpingOnClouds(c, k) 21 | 22 | print(str(result) + '\n') 23 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Library Fine.py: -------------------------------------------------------------------------------- 1 | def libraryFine(d1, m1, y1, d2, m2, y2): 2 | if y1 < y2 or (y1 == y2 and m1 < m2) or (y1 == y2 and m1 == m2 and d1 < d2) or (y1 == y2 and m1 == m2 and d1 == d2): 3 | return 0 4 | if y1 > y2: 5 | return (y1 - y2) * 10000 6 | if m1 > m2: 7 | return (m1 - m2) * 500 8 | if d1 > d2: 9 | return (d1 - d2) * 15 10 | 11 | 12 | if __name__ == '__main__': 13 | d1M1Y1 = input().split() 14 | 15 | d1 = int(d1M1Y1[0]) 16 | 17 | m1 = int(d1M1Y1[1]) 18 | 19 | y1 = int(d1M1Y1[2]) 20 | 21 | d2M2Y2 = input().split() 22 | 23 | d2 = int(d2M2Y2[0]) 24 | 25 | m2 = int(d2M2Y2[1]) 26 | 27 | y2 = int(d2M2Y2[2]) 28 | 29 | result = libraryFine(d1, m1, y1, d2, m2, y2) 30 | 31 | print(str(result) + '\n') 32 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Lisa's Workbook.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | import math 3 | 4 | 5 | def workbook(n, k, arr): 6 | res = 0 7 | page = 1 8 | i = 0 9 | for i in arr: 10 | temp = math.ceil(i / k) 11 | for j in range(1, temp): 12 | if (j - 1) * k < page <= j * k: 13 | res += 1 14 | page += 1 15 | if (temp - 1) * k < page <= i: 16 | res += 1 17 | page += 1 18 | return res 19 | 20 | 21 | if __name__ == '__main__': 22 | nk = input().split() 23 | 24 | n = int(nk[0]) 25 | 26 | k = int(nk[1]) 27 | 28 | arr = list(map(int, input().rstrip().split())) 29 | 30 | result = workbook(n, k, arr) 31 | 32 | print(str(result) + '\n') 33 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Marc's Cakewalk.py: -------------------------------------------------------------------------------- 1 | def marcsCakewalk(calorie): 2 | miles = 0 3 | calorie.sort(reverse=True) 4 | for i in range(len(calorie)): 5 | miles += (calorie[i] * 2 ** i) 6 | return miles 7 | 8 | 9 | if __name__ == '__main__': 10 | n = int(input()) 11 | 12 | calorie = list(map(int, input().rstrip().split())) 13 | 14 | result = marcsCakewalk(calorie) 15 | 16 | print(str(result) + '\n') 17 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Mark and Toys.py: -------------------------------------------------------------------------------- 1 | def maximumToys(prices, k): 2 | prices.sort() 3 | res = 0 4 | for price in prices: 5 | if price > k: 6 | break 7 | res += 1 8 | k -= price 9 | return res 10 | 11 | 12 | if __name__ == '__main__': 13 | nk = input().split() 14 | 15 | n = int(nk[0]) 16 | 17 | k = int(nk[1]) 18 | 19 | prices = list(map(int, input().rstrip().split())) 20 | 21 | result = maximumToys(prices, k) 22 | 23 | print(str(result) + '\n') 24 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Mars Exploration.py: -------------------------------------------------------------------------------- 1 | def marsExploration(s): 2 | res = 0 3 | for i in range(0, len(s), 3): 4 | if s[i] != "S": 5 | res += 1 6 | if s[i + 1] != "O": 7 | res += 1 8 | if s[i + 2] != "S": 9 | res += 1 10 | return res 11 | 12 | 13 | if __name__ == '__main__': 14 | s = input() 15 | 16 | result = marsExploration(s) 17 | 18 | print(str(result) + '\n') 19 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/MigratoryBirds.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def migratoryBirds(arr): 5 | count = Counter(arr) 6 | temp, j = count[1], 1 7 | for i in range(2, 6): 8 | if temp < count[i]: 9 | temp = count[i] 10 | j = i 11 | return j 12 | 13 | 14 | if __name__ == '__main__': 15 | arr_count = int(input().strip()) 16 | 17 | arr = list(map(int, input().rstrip().split())) 18 | 19 | result = migratoryBirds(arr) 20 | 21 | print(str(result) + '\n') 22 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Mini-MaxSum.py: -------------------------------------------------------------------------------- 1 | def miniMaxSum(arr): 2 | m = min(arr) 3 | M = max(arr) 4 | s = sum(arr) 5 | print(s - M, s - m) 6 | 7 | 8 | if __name__ == '__main__': 9 | arr = list(map(int, input().rstrip().split())) 10 | 11 | miniMaxSum(arr) 12 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Minimum Distances.py: -------------------------------------------------------------------------------- 1 | def minimumDistances(a): 2 | for d in range(1, n): 3 | for i in range(n - d): 4 | if a[i] == a[i+d]: 5 | return d 6 | return -1 7 | 8 | 9 | if __name__ == '__main__': 10 | n = int(input()) 11 | 12 | a = list(map(int, input().rstrip().split())) 13 | 14 | result = minimumDistances(a) 15 | 16 | print(str(result) + '\n') 17 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/NumberLineJumps.py: -------------------------------------------------------------------------------- 1 | def kangaroo(x1, v1, x2, v2): 2 | if (x1 < x2 and v1 <= v2) or (x2 < x1 and v2 <= v1): 3 | return 'NO' 4 | elif x1 > x2: 5 | x1, x2, v1, v2 = x2, x1, v2, v1 6 | while x1 < x2: 7 | x1 += v1 8 | x2 += v2 9 | if x1 == x2: 10 | return 'YES' 11 | return 'NO' 12 | 13 | 14 | if __name__ == '__main__': 15 | x1V1X2V2 = input().split() 16 | 17 | x1 = int(x1V1X2V2[0]) 18 | 19 | v1 = int(x1V1X2V2[1]) 20 | 21 | x2 = int(x1V1X2V2[2]) 22 | 23 | v2 = int(x1V1X2V2[3]) 24 | 25 | result = kangaroo(x1, v1, x2, v2) 26 | 27 | print(result + '\n') 28 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Pangrams.py: -------------------------------------------------------------------------------- 1 | def pangrams(s): 2 | return "pangram" if len(set(s.lower())) > 26 else "not pangram" 3 | 4 | 5 | if __name__ == '__main__': 6 | s = input() 7 | 8 | result = pangrams(s) 9 | 10 | print(result + '\n') 11 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Picking Numbers.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def pickingNumbers(a): 5 | counter = Counter(a) 6 | res = 0 7 | for key in sorted(set(a)): 8 | res = max(res, counter[key] + counter[key + 1]) 9 | return res 10 | 11 | 12 | if __name__ == '__main__': 13 | n = int(input().strip()) 14 | 15 | a = list(map(int, input().rstrip().split())) 16 | 17 | result = pickingNumbers(a) 18 | 19 | print(str(result) + '\n') 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/PlusMinus.py: -------------------------------------------------------------------------------- 1 | def plusMinus(arr, l): 2 | p, n = 0, 0 3 | for i in arr: 4 | if i < 0: 5 | n += 1 6 | elif i > 0: 7 | p += 1 8 | print(round(p / l, 6)) 9 | print(round(n / l, 6)) 10 | print(round((l - (n + p)) / l, 6)) 11 | 12 | 13 | if __name__ == '__main__': 14 | n = int(input()) 15 | 16 | arr = list(map(int, input().rstrip().split())) 17 | 18 | plusMinus(arr, n) 19 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Repeated String.py: -------------------------------------------------------------------------------- 1 | def repeatedString(s, n): 2 | return (s.count("a") * (n // len(s)) + s[:n % len(s)].count("a")) 3 | 4 | 5 | if __name__ == '__main__': 6 | s = input() 7 | 8 | n = int(input()) 9 | 10 | result = repeatedString(s, n) 11 | 12 | print(str(result) + '\n') 13 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Running Time of Algorithms.py: -------------------------------------------------------------------------------- 1 | def runningTime(arr): 2 | res = 0 3 | for i in range(len(arr)): 4 | j = i - 1 5 | while (j >= 0) and (arr[i] < arr[j]): 6 | arr[i], arr[j] = arr[j], arr[i] 7 | i = j 8 | j -= 1 9 | res += 1 10 | return res 11 | 12 | 13 | if __name__ == '__main__': 14 | n = int(input()) 15 | 16 | arr = list(map(int, input().rstrip().split())) 17 | 18 | result = runningTime(arr) 19 | 20 | print(str(result) + '\n') 21 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/SalesbyMatch.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def sockMerchant(n, ar): 5 | c = Counter(ar) 6 | ans = 0 7 | for _, i in c.items(): 8 | ans += i // 2 9 | return ans 10 | 11 | 12 | if __name__ == '__main__': 13 | n = int(input()) 14 | 15 | ar = list(map(int, input().rstrip().split())) 16 | 17 | result = sockMerchant(n, ar) 18 | 19 | print(str(result) + '\n') 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Save the Prisoner!.py: -------------------------------------------------------------------------------- 1 | def saveThePrisoner(n, m, s): 2 | return (m + s - 2) % n + 1 3 | 4 | 5 | if __name__ == '__main__': 6 | t = int(input()) 7 | 8 | for t_itr in range(t): 9 | nms = input().split() 10 | 11 | n = int(nms[0]) 12 | 13 | m = int(nms[1]) 14 | 15 | s = int(nms[2]) 16 | 17 | result = saveThePrisoner(n, m, s) 18 | 19 | print(str(result) + '\n') 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Sequence Equation.py: -------------------------------------------------------------------------------- 1 | def permutationEquation(p): 2 | res = [] 3 | for i in range(1, len(p) + 1): 4 | d = p.index(i) + 1 5 | res.append(p.index(d) + 1) 6 | return res 7 | 8 | 9 | if __name__ == '__main__': 10 | n = int(input()) 11 | 12 | p = list(map(int, input().rstrip().split())) 13 | 14 | result = permutationEquation(p) 15 | 16 | print('\n'.join(map(str, result))) 17 | print('\n') 18 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Service Lane.py: -------------------------------------------------------------------------------- 1 | def serviceLane(width, cases): 2 | res = [] 3 | for case in cases: 4 | res.append(min(width[case[0]: case[1] + 1])) 5 | return res 6 | 7 | 8 | if __name__ == '__main__': 9 | nt = input().split() 10 | 11 | n = int(nt[0]) 12 | 13 | t = int(nt[1]) 14 | 15 | width = list(map(int, input().rstrip().split())) 16 | 17 | cases = [] 18 | 19 | for _ in range(t): 20 | cases.append(list(map(int, input().rstrip().split()))) 21 | 22 | result = serviceLane(width, cases) 23 | 24 | print('\n'.join(map(str, result))) 25 | print() 26 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Sherlock and Squares.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def squares(a, b): 5 | temp = math.ceil(math.sqrt(a)) 6 | res = 0 7 | while temp ** 2 <= b: 8 | res += 1 9 | temp += 1 10 | return res 11 | 12 | 13 | if __name__ == '__main__': 14 | q = int(input()) 15 | 16 | for q_itr in range(q): 17 | ab = input().split() 18 | 19 | a = int(ab[0]) 20 | 21 | b = int(ab[1]) 22 | 23 | result = squares(a, b) 24 | 25 | print(str(result) + '\n') 26 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/SimpleArraySum.py: -------------------------------------------------------------------------------- 1 | def simpleArraySum(ar): 2 | return sum(ar) 3 | 4 | 5 | if __name__ == '__main__': 6 | ar_count = int(input()) 7 | 8 | ar = list(map(int, input().rstrip().split())) 9 | 10 | print(simpleArraySum(ar)) 11 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Staircase.py: -------------------------------------------------------------------------------- 1 | def staircase(n): 2 | for i in range(1, n + 1): 3 | print(" " * (n - i), "#" * i, sep='') 4 | 5 | 6 | if __name__ == '__main__': 7 | n = int(input()) 8 | 9 | staircase(n) 10 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/StrangeCounter.py: -------------------------------------------------------------------------------- 1 | def strangeCounter(t): 2 | i = 0 3 | k = 0 4 | while k < t: 5 | k += 3 * (2 ** i) 6 | i += 1 7 | return k - t + 1 8 | 9 | 10 | if __name__ == '__main__': 11 | t = int(input()) 12 | 13 | result = strangeCounter(t) 14 | 15 | print(str(result) + '\n') 16 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/String Construction.py: -------------------------------------------------------------------------------- 1 | def stringConstruction(s): 2 | return len(set(s)) 3 | 4 | 5 | if __name__ == '__main__': 6 | q = int(input()) 7 | 8 | for q_itr in range(q): 9 | s = input() 10 | 11 | result = stringConstruction(s) 12 | 13 | print(str(result) + '\n') 14 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/StrongPassword.py: -------------------------------------------------------------------------------- 1 | NUMBERS = "0123456789" 2 | LOWER_CASE = "abcdefghijklmnopqrstuvwxyz" 3 | UPPER_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 4 | 5 | 6 | def minimumNumber(n, password): 7 | # Return the minimum number of characters to make the password strong 8 | d = {'n': 0, 'l': 0, 'u': 0, 's': 0} 9 | for i in password: 10 | if i in NUMBERS: 11 | d['n'] += 1 12 | elif i in LOWER_CASE: 13 | d['l'] += 1 14 | elif i in UPPER_CASE: 15 | d['u'] += 1 16 | else: 17 | d['s'] += 1 18 | res = 0 19 | for _, i in d.items(): 20 | if i == 0: 21 | res += 1 22 | if len(password) < 6: 23 | return max(6 - len(password), res) 24 | return res 25 | 26 | 27 | if __name__ == '__main__': 28 | n = int(input()) 29 | 30 | password = input() 31 | 32 | answer = minimumNumber(n, password) 33 | 34 | print(str(answer) + '\n') 35 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/SubarrayDivision.py: -------------------------------------------------------------------------------- 1 | def birthday(s, d, m): 2 | res = 0 3 | for i in range(len(s) - m + 1): 4 | som = 0 5 | for j in range(m): 6 | som += s[i + j] 7 | if som == d: 8 | res += 1 9 | return res 10 | 11 | 12 | if __name__ == '__main__': 13 | n = int(input().strip()) 14 | 15 | s = list(map(int, input().rstrip().split())) 16 | 17 | dm = input().rstrip().split() 18 | 19 | d = int(dm[0]) 20 | 21 | m = int(dm[1]) 22 | 23 | result = birthday(s, d, m) 24 | 25 | print(str(result) + '\n') 26 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/SuperReducedString.py: -------------------------------------------------------------------------------- 1 | def superReducedString(s): 2 | r = "" 3 | for i in s: 4 | if r == "" or i != r[-1]: 5 | r += i 6 | else: 7 | r = r[:-1] 8 | if len(r) == 0: 9 | return "Empty String" 10 | return r 11 | 12 | 13 | if __name__ == '__main__': 14 | s = input() 15 | 16 | result = superReducedString(s) 17 | 18 | print(result + '\n') 19 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Taum and B'day.py: -------------------------------------------------------------------------------- 1 | def taumBday(b, w, bc, wc, z): 2 | if bc > wc + z: 3 | return w * wc + b * (wc + z) 4 | elif wc > bc + z: 5 | return b * bc + w * (bc + z) 6 | else: 7 | return b * bc + w * wc 8 | 9 | 10 | if __name__ == '__main__': 11 | t = int(input().strip()) 12 | 13 | for t_itr in range(t): 14 | first_multiple_input = input().rstrip().split() 15 | 16 | b = int(first_multiple_input[0]) 17 | 18 | w = int(first_multiple_input[1]) 19 | 20 | second_multiple_input = input().rstrip().split() 21 | 22 | bc = int(second_multiple_input[0]) 23 | 24 | wc = int(second_multiple_input[1]) 25 | 26 | z = int(second_multiple_input[2]) 27 | 28 | result = taumBday(b, w, bc, wc, z) 29 | 30 | print(str(result) + '\n') 31 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/The Love-Letter Mystery.py: -------------------------------------------------------------------------------- 1 | def theLoveLetterMystery(s): 2 | res = 0 3 | if s != s[::-1]: 4 | l = len(s) // 2 5 | for i in range(l): 6 | res += abs(ord(s[i]) - ord(s[i * -1 - 1])) 7 | return res 8 | 9 | 10 | if __name__ == '__main__': 11 | q = int(input()) 12 | 13 | for q_itr in range(q): 14 | s = input() 15 | 16 | result = theLoveLetterMystery(s) 17 | 18 | print(str(result) + '\n') 19 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/TheHurdleRace.py: -------------------------------------------------------------------------------- 1 | def hurdleRace(k, height): 2 | m = max(height) 3 | return 0 if m < k else m - k 4 | 5 | 6 | if __name__ == '__main__': 7 | nk = input().split() 8 | 9 | n = int(nk[0]) 10 | 11 | k = int(nk[1]) 12 | 13 | height = list(map(int, input().rstrip().split())) 14 | 15 | result = hurdleRace(k, height) 16 | 17 | print(str(result) + '\n') 18 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/TimeConversion.py: -------------------------------------------------------------------------------- 1 | def timeConversion(s): 2 | t = s[-2:] 3 | h = int(s[:2]) 4 | if t == "PM" and h != 12: 5 | h += 12 6 | elif t == "AM" and h == 12: 7 | h = 0 8 | return str(h).rjust(2, "0") + s[2:-2] 9 | 10 | 11 | if __name__ == '__main__': 12 | s = input() 13 | 14 | result = timeConversion(s) 15 | 16 | print(result + '\n') 17 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Two Strings.py: -------------------------------------------------------------------------------- 1 | def twoStrings(s1, s2): 2 | if set(s1).intersection(s2): 3 | return "YES" 4 | return "NO" 5 | 6 | 7 | if __name__ == '__main__': 8 | q = int(input()) 9 | 10 | for q_itr in range(q): 11 | s1 = input() 12 | 13 | s2 = input() 14 | 15 | result = twoStrings(s1, s2) 16 | 17 | print(result + '\n') 18 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/UtopianTree.py: -------------------------------------------------------------------------------- 1 | def utopianTree(n): 2 | res = 1 3 | for i in range(n): 4 | if i % 2 == 0: 5 | res *= 2 6 | else: 7 | res += 1 8 | return res 9 | 10 | 11 | if __name__ == '__main__': 12 | t = int(input()) 13 | 14 | for t_itr in range(t): 15 | n = int(input()) 16 | 17 | result = utopianTree(n) 18 | 19 | print(str(result) + '\n') 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/Viral Advertising.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def viralAdvertising(n): 5 | res = 0 6 | p = 5 7 | for i in range(n): 8 | f = math.floor(p / 2) 9 | res += f 10 | p = f * 3 11 | return res 12 | 13 | 14 | if __name__ == '__main__': 15 | n = int(input()) 16 | 17 | result = viralAdvertising(n) 18 | 19 | print(str(result) + '\n') 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/acmTeam.py: -------------------------------------------------------------------------------- 1 | def acmTeam(topic): 2 | max_topics = 0 3 | n = 0 4 | for i in range(len(topic) - 1): 5 | for j in range(i + 1, len(topic)): 6 | temp = 0 7 | for k in range(len(topic[i])): 8 | if topic[i][k] == '1' or topic[j][k] == '1': 9 | temp += 1 10 | if temp == max_topics: 11 | n += 1 12 | elif temp > max_topics: 13 | max_topics = temp 14 | n = 1 15 | return max_topics, n 16 | 17 | 18 | if __name__ == '__main__': 19 | first_multiple_input = input().rstrip().split() 20 | 21 | n = int(first_multiple_input[0]) 22 | 23 | m = int(first_multiple_input[1]) 24 | 25 | topic = [] 26 | 27 | for _ in range(n): 28 | topic_item = input() 29 | topic.append(topic_item) 30 | 31 | result = acmTeam(topic) 32 | 33 | print('\n'.join(map(str, result))) 34 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/anagram.py: -------------------------------------------------------------------------------- 1 | def anagram(s): 2 | if len(s) % 2: 3 | return -1 4 | l = len(s) // 2 5 | res = 0 6 | for x in set(s[l:]): 7 | temp = s[l:].count(x) - s[:l].count(x) 8 | res += temp if (temp > 0) else 0 9 | return res 10 | 11 | 12 | if __name__ == '__main__': 13 | q = int(input().strip()) 14 | 15 | for q_itr in range(q): 16 | s = input() 17 | 18 | result = anagram(s) 19 | 20 | print(str(result)) 21 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/balancedSums.py: -------------------------------------------------------------------------------- 1 | def balancedSums(arr): 2 | s = sum(arr) 3 | l = 0 4 | for x in arr: 5 | s -= x 6 | if l == s: 7 | return 'YES' 8 | l += x 9 | return 'NO' 10 | 11 | 12 | if __name__ == '__main__': 13 | T = int(input().strip()) 14 | 15 | for T_itr in range(T): 16 | n = int(input().strip()) 17 | 18 | arr = list(map(int, input().rstrip().split())) 19 | 20 | result = balancedSums(arr) 21 | 22 | print(result) 23 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/beautifulBinaryString.py: -------------------------------------------------------------------------------- 1 | def beautifulBinaryString(b): 2 | res = 0 3 | while 1: 4 | temp = b.find('010') 5 | if temp == -1: 6 | break 7 | res += 1 8 | b = b[temp + 3:] 9 | 10 | return res 11 | 12 | 13 | if __name__ == '__main__': 14 | n = int(input().strip()) 15 | 16 | b = input() 17 | 18 | result = beautifulBinaryString(b) 19 | 20 | print(str(result)) 21 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/bigSorting.py: -------------------------------------------------------------------------------- 1 | from functools import cmp_to_key 2 | 3 | 4 | def cmp_bigint(x, y): 5 | n = len(x) 6 | m = len(y) 7 | if n == m: 8 | if x < y: 9 | return -1 10 | if n < m: 11 | return -1 12 | return 1 13 | 14 | 15 | def bigSorting(unsorted): 16 | return sorted(unsorted, key=cmp_to_key(cmp_bigint)) 17 | 18 | 19 | if __name__ == '__main__': 20 | n = int(input().strip()) 21 | 22 | unsorted = [] 23 | 24 | for _ in range(n): 25 | unsorted_item = input() 26 | unsorted.append(unsorted_item) 27 | 28 | result = bigSorting(unsorted) 29 | 30 | print('\n'.join(result)) 31 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/cavityMap.py: -------------------------------------------------------------------------------- 1 | def cavityMap(grid): 2 | n = len(grid[0]) 3 | for i in range(1, n - 1): 4 | for j in range(1, n - 1): 5 | if grid[i][j] > max(grid[i - 1][j], grid[i + 1][j], grid[i][j - 1], grid[i][j + 1]): 6 | grid[i] = grid[i][:j] + 'X' + grid[i][j + 1:] 7 | return grid 8 | 9 | 10 | if __name__ == '__main__': 11 | n = int(input().strip()) 12 | 13 | grid = [] 14 | 15 | for _ in range(n): 16 | grid_item = input() 17 | grid.append(grid_item) 18 | 19 | result = cavityMap(grid) 20 | 21 | print('\n'.join(result)) 22 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/climbingLeaderboard.py: -------------------------------------------------------------------------------- 1 | def climbingLeaderboard(ranked, player): 2 | ranked = list(set(ranked)) 3 | ranked.sort(reverse=True) 4 | j = len(ranked) - 1 5 | res = [] 6 | for i in player: 7 | while j >= 0: 8 | if i < ranked[j]: 9 | break 10 | j -= 1 11 | res.append(j + 2) 12 | return res 13 | 14 | 15 | if __name__ == '__main__': 16 | ranked_count = int(input().strip()) 17 | 18 | ranked = list(map(int, input().rstrip().split())) 19 | 20 | player_count = int(input().strip()) 21 | 22 | player = list(map(int, input().rstrip().split())) 23 | 24 | result = climbingLeaderboard(ranked, player) 25 | 26 | print('\n'.join(map(str, result))) 27 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/closestNumbers.py: -------------------------------------------------------------------------------- 1 | def closestNumbers(arr): 2 | arr_set = list(set(arr)) 3 | arr_set.sort() 4 | c = arr_set[1] - arr_set[0] 5 | res = [arr_set[0], arr_set[1]] 6 | for i in range(1, len(arr_set) - 1): 7 | temp = arr_set[i + 1] - arr_set[i] 8 | if c > temp: 9 | c = temp 10 | res.clear() 11 | res.append(arr_set[i]) 12 | res.append(arr_set[i + 1]) 13 | elif c == temp: 14 | res.append(arr_set[i]) 15 | res.append(arr_set[i + 1]) 16 | 17 | return res 18 | 19 | 20 | if __name__ == '__main__': 21 | n = int(input().strip()) 22 | 23 | arr = list(map(int, input().rstrip().split())) 24 | 25 | result = closestNumbers(arr) 26 | 27 | print(' '.join(map(str, result))) 28 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/countSort.py: -------------------------------------------------------------------------------- 1 | def countSort(arr): 2 | res = {} 3 | l = len(arr) // 2 4 | for i in range(len(arr)): 5 | e = arr[i] 6 | if int(e[0]) not in res: 7 | res[int(e[0])] = [] 8 | res[int(e[0])].append('-'if i < l else e[1]) 9 | for k in sorted(res.keys()): 10 | for s in res[k]: 11 | print(s, end=' ') 12 | 13 | 14 | if __name__ == '__main__': 15 | n = int(input().strip()) 16 | 17 | arr = [] 18 | 19 | for _ in range(n): 20 | arr.append(input().rstrip().split()) 21 | 22 | countSort(arr) 23 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/countingSort.py: -------------------------------------------------------------------------------- 1 | def countingSort(arr): 2 | tab = [0] * (max(arr) + 1) 3 | for i in arr: 4 | tab[i] += 1 5 | res = [] 6 | for i in range(len(tab)): 7 | if tab[i]: 8 | for _ in range(tab[i]): 9 | res.append(i) 10 | return res 11 | 12 | 13 | if __name__ == '__main__': 14 | n = int(input().strip()) 15 | 16 | arr = list(map(int, input().rstrip().split())) 17 | 18 | result = countingSort(arr) 19 | 20 | print(' '.join(map(str, result))) 21 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/decentNumber.py: -------------------------------------------------------------------------------- 1 | def decentNumber(n): 2 | num_3 = n // 3 3 | num_5 = (n - 3 * num_3) // 5 4 | while num_3 != -1 and ((num_3 * 3) + (num_5 * 5)) != n: 5 | num_3 -= 1 6 | num_5 = (n - 3 * num_3) // 5 7 | if num_3 != -1: 8 | print('5' * num_3 * 3 + '3' * num_5 * 5) 9 | else: 10 | print('-1') 11 | 12 | 13 | if __name__ == '__main__': 14 | t = int(input().strip()) 15 | 16 | for t_itr in range(t): 17 | n = int(input().strip()) 18 | 19 | decentNumber(n) 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/fibonacciModified.py: -------------------------------------------------------------------------------- 1 | def fibonacciModified(t1, t2, n): 2 | res = [t1, t2] 3 | for _ in range(n - 2): 4 | res.append(res[-2] + res[-1] ** 2) 5 | return res[n - 1] 6 | 7 | 8 | if __name__ == '__main__': 9 | first_multiple_input = input().rstrip().split() 10 | 11 | t1 = int(first_multiple_input[0]) 12 | 13 | t2 = int(first_multiple_input[1]) 14 | 15 | n = int(first_multiple_input[2]) 16 | 17 | result = fibonacciModified(t1, t2, n) 18 | 19 | print(result) 20 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/findMedian.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def findMedian(arr): 5 | return sorted(arr)[math.ceil(len(arr) / 2) - 1] 6 | 7 | 8 | if __name__ == '__main__': 9 | n = int(input().strip()) 10 | 11 | arr = list(map(int, input().rstrip().split())) 12 | 13 | result = findMedian(arr) 14 | 15 | print(str(result) + '\n') 16 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/findZigZagSequence.py: -------------------------------------------------------------------------------- 1 | def findZigZagSequence(a, n): 2 | a.sort() 3 | mid = int(n/2) 4 | a[mid], a[n-1] = a[n-1], a[mid] 5 | 6 | st = mid + 1 7 | ed = n - 2 8 | while(st <= ed): 9 | a[st], a[ed] = a[ed], a[st] 10 | st = st + 1 11 | ed = ed - 1 12 | 13 | for i in range(n): 14 | if i == n-1: 15 | print(a[i]) 16 | else: 17 | print(a[i], end=' ') 18 | return 19 | 20 | 21 | test_cases = int(input()) 22 | for cs in range(test_cases): 23 | n = int(input()) 24 | a = list(map(int, input().split())) 25 | findZigZagSequence(a, n) 26 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/funnyString.py: -------------------------------------------------------------------------------- 1 | def funnyString(s): 2 | a = [ord(x) for x in s] 3 | b = [ord(x) for x in s[::-1]] 4 | for i in range(len(a) - 1): 5 | if abs(a[i] - a[i + 1]) != abs(b[i] - b[i + 1]): 6 | return "Not Funny" 7 | return "Funny" 8 | 9 | 10 | if __name__ == '__main__': 11 | q = int(input().strip()) 12 | 13 | for q_itr in range(q): 14 | s = input() 15 | 16 | result = funnyString(s) 17 | 18 | print(result + '\n') 19 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/gameOfStones.py: -------------------------------------------------------------------------------- 1 | def gameOfStones(n): 2 | return 'First' if n % 7 >= 2 else 'Second' 3 | 4 | 5 | if __name__ == '__main__': 6 | t = int(input().strip()) 7 | 8 | for t_itr in range(t): 9 | n = int(input().strip()) 10 | 11 | result = gameOfStones(n) 12 | 13 | print(result) 14 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/gridChallenge.py: -------------------------------------------------------------------------------- 1 | def gridChallenge(grid): 2 | for i in range(len(grid)): 3 | grid[i] = ''.join(sorted(list(grid[i]))) 4 | for i in range(len(grid[0])): 5 | temp = grid[0][i] 6 | for x in grid: 7 | if x[i] < temp: 8 | return 'NO' 9 | temp = x[i] 10 | return 'YES' 11 | 12 | 13 | if __name__ == '__main__': 14 | t = int(input().strip()) 15 | 16 | for t_itr in range(t): 17 | n = int(input().strip()) 18 | 19 | grid = [] 20 | 21 | for _ in range(n): 22 | grid_item = input() 23 | grid.append(grid_item) 24 | 25 | result = gridChallenge(grid) 26 | 27 | print(result) 28 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/gridSearch.py: -------------------------------------------------------------------------------- 1 | def gridSearch(G, P): 2 | len_g = len(G) 3 | len_p = len(P) 4 | i = 0 5 | while i < len_g: 6 | if P[0] in G[i]: 7 | j = 0 8 | c = 1 9 | while j + len(P[0]) <= len(G[0]): 10 | index = G[i][j:].find(P[0]) 11 | if index == -1: 12 | break 13 | for k in range(1, len_p): 14 | if index == G[i + k][j:].find(P[k]): 15 | c += 1 16 | else: 17 | break 18 | j += index + 1 19 | if c == len_p: 20 | return 'YES' 21 | i += 1 22 | return 'NO' 23 | 24 | 25 | if __name__ == '__main__': 26 | t = int(input().strip()) 27 | 28 | for t_itr in range(t): 29 | first_multiple_input = input().rstrip().split() 30 | 31 | R = int(first_multiple_input[0]) 32 | 33 | C = int(first_multiple_input[1]) 34 | 35 | G = [] 36 | 37 | for _ in range(R): 38 | G_item = input() 39 | G.append(G_item) 40 | 41 | second_multiple_input = input().rstrip().split() 42 | 43 | r = int(second_multiple_input[0]) 44 | 45 | c = int(second_multiple_input[1]) 46 | 47 | P = [] 48 | 49 | for _ in range(r): 50 | P_item = input() 51 | P.append(P_item) 52 | 53 | result = gridSearch(G, P) 54 | 55 | print(result) 56 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/happyLadybugs.py: -------------------------------------------------------------------------------- 1 | def happyLadybugs(b): 2 | for c in set(b.replace("_", "")): 3 | if b.count(c) == 1: 4 | return "NO" 5 | if "_" not in b: 6 | for i in range(1, len(b) - 1): 7 | if b[i - 1] != b[i] and b[i + 1] != b[i]: 8 | return "NO" 9 | return "YES" 10 | 11 | 12 | if __name__ == '__main__': 13 | g = int(input()) 14 | 15 | for g_itr in range(g): 16 | n = int(input()) 17 | 18 | b = input() 19 | 20 | result = happyLadybugs(b) 21 | 22 | print(result + '\n') 23 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/insertionSort1.py: -------------------------------------------------------------------------------- 1 | def insertionSort1(n, arr): 2 | for i in range(len(arr) - 1, 0, -1): 3 | x = arr[i] 4 | j = i - 1 5 | while j >= 0 and arr[j] > x: 6 | arr[j + 1] = arr[j] 7 | j -= 1 8 | print(' '.join(map(str, arr))) 9 | arr[j + 1] = x 10 | print(' '.join(map(str, arr))) 11 | 12 | 13 | if __name__ == '__main__': 14 | n = int(input().strip()) 15 | 16 | arr = list(map(int, input().rstrip().split())) 17 | 18 | insertionSort1(n, arr) 19 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/insertionSort2.py: -------------------------------------------------------------------------------- 1 | def insertionSort2(n, arr): 2 | for i in range(1, n): 3 | while i > 0 and arr[i] < arr[i - 1]: 4 | arr[i], arr[i - 1] = arr[i - 1], arr[i] 5 | i -= 1 6 | print(' '.join(map(str, arr))) 7 | 8 | 9 | if __name__ == '__main__': 10 | n = int(input().strip()) 11 | 12 | arr = list(map(int, input().rstrip().split())) 13 | 14 | insertionSort2(n, arr) 15 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/is_smart_number.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def is_smart_number(num): 5 | val = int(math.sqrt(num)) 6 | if math.sqrt(num) == val: 7 | return True 8 | return False 9 | 10 | 11 | for _ in range(int(input())): 12 | num = int(input()) 13 | ans = is_smart_number(num) 14 | if ans: 15 | print("YES") 16 | else: 17 | print("NO") 18 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/jimOrders.py: -------------------------------------------------------------------------------- 1 | def jimOrders(orders): 2 | dic = {i: orders[i][0] + orders[i][1] for i in range(len(orders))} 3 | return [k + 1 for k, _ in sorted(dic.items(), key=lambda item: item[1])] 4 | 5 | 6 | if __name__ == '__main__': 7 | n = int(input().strip()) 8 | 9 | orders = [] 10 | 11 | for _ in range(n): 12 | orders.append(list(map(int, input().rstrip().split()))) 13 | 14 | result = jimOrders(orders) 15 | 16 | print(' '.join(map(str, result))) 17 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/kaprekarNumbers.py: -------------------------------------------------------------------------------- 1 | def kaprekarNumbers(p, q): 2 | res = [] 3 | for i in range(p, q + 1): 4 | s = str(i * i) 5 | if int(s[:len(s) // 2] or 0) + int(s[len(s) // 2:]) == i: 6 | res.append(i) 7 | if res: 8 | print(' '.join(map(str, res))) 9 | else: 10 | print("INVALID RANGE") 11 | 12 | 13 | if __name__ == '__main__': 14 | p = int(input().strip()) 15 | 16 | q = int(input().strip()) 17 | 18 | kaprekarNumbers(p, q) 19 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/lonelyinteger.py: -------------------------------------------------------------------------------- 1 | def lonelyinteger(a): 2 | for i in set(a): 3 | if a.count(i) == 1: 4 | return i 5 | 6 | 7 | if __name__ == '__main__': 8 | n = int(input().strip()) 9 | 10 | a = list(map(int, input().rstrip().split())) 11 | 12 | result = lonelyinteger(a) 13 | 14 | print(str(result)) 15 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/luckBalance.py: -------------------------------------------------------------------------------- 1 | def luckBalance(k, contests): 2 | contests.sort(reverse=True) 3 | res = 0 4 | for contest in contests: 5 | if contest[1] == 0: 6 | res += contest[0] 7 | elif k > 0: 8 | res += contest[0] 9 | k -= 1 10 | else: 11 | res -= contest[0] 12 | return res 13 | 14 | 15 | if __name__ == '__main__': 16 | first_multiple_input = input().rstrip().split() 17 | 18 | n = int(first_multiple_input[0]) 19 | 20 | k = int(first_multiple_input[1]) 21 | 22 | contests = [] 23 | 24 | for _ in range(n): 25 | contests.append(list(map(int, input().rstrip().split()))) 26 | 27 | result = luckBalance(k, contests) 28 | 29 | print(str(result)) 30 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/makingAnagrams.py: -------------------------------------------------------------------------------- 1 | def makingAnagrams(s1, s2): 2 | u = set(s1).union(set(s2)) 3 | res = 0 4 | for i in u: 5 | res += abs(s1.count(i) - s2.count(i)) 6 | return res 7 | 8 | 9 | if __name__ == '__main__': 10 | s1 = input() 11 | 12 | s2 = input() 13 | 14 | result = makingAnagrams(s1, s2) 15 | 16 | print(str(result) + '\n') 17 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/maxMin.py: -------------------------------------------------------------------------------- 1 | def maxMin(k, arr): 2 | arr.sort() 3 | res = arr[k - 1] - arr[0] 4 | for i in range(1, len(arr) - k + 1): 5 | res = min(res, arr[k + i - 1] - arr[i]) 6 | return res 7 | 8 | 9 | if __name__ == '__main__': 10 | n = int(input().strip()) 11 | 12 | k = int(input().strip()) 13 | 14 | arr = [] 15 | 16 | for _ in range(n): 17 | arr_item = int(input().strip()) 18 | arr.append(arr_item) 19 | 20 | result = maxMin(k, arr) 21 | 22 | print(result) 23 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/maximizingXor.py: -------------------------------------------------------------------------------- 1 | def maximizingXor(l, r): 2 | res = 0 3 | for i in range(l, r + 1): 4 | for j in range(l, r + 1): 5 | if i ^ j > res: 6 | res = i ^ j 7 | return res 8 | 9 | 10 | if __name__ == '__main__': 11 | l = int(input().strip()) 12 | 13 | r = int(input().strip()) 14 | 15 | result = maximizingXor(l, r) 16 | 17 | print(str(result)) 18 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/maximumPerimeterTriangle.py: -------------------------------------------------------------------------------- 1 | def maximumPerimeterTriangle(sticks): 2 | sticks.sort() 3 | i = len(sticks) - 1 4 | while i >= 2 and sticks[i - 2] + sticks[i - 1] <= sticks[i]: 5 | i -= 1 6 | if i >= 2: 7 | return sticks[i - 2: i + 1] 8 | return [-1] 9 | 10 | 11 | if __name__ == '__main__': 12 | n = int(input().strip()) 13 | 14 | sticks = list(map(int, input().rstrip().split())) 15 | 16 | result = maximumPerimeterTriangle(sticks) 17 | 18 | print(' '.join(map(str, result))) 19 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/minimumAbsoluteDifference.py: -------------------------------------------------------------------------------- 1 | def minimumAbsoluteDifference(arr): 2 | arr.sort() 3 | return min([abs(arr[i] - arr[i - 1]) for i in range(1, len(arr))]) 4 | 5 | 6 | if __name__ == '__main__': 7 | n = int(input().strip()) 8 | 9 | arr = list(map(int, input().rstrip().split())) 10 | 11 | result = minimumAbsoluteDifference(arr) 12 | 13 | print(str(result)) 14 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/missingNumbers.py: -------------------------------------------------------------------------------- 1 | def missingNumbers(arr, brr): 2 | res = [] 3 | for x in set(brr): 4 | if brr.count(x) > arr.count(x): 5 | res.append(x) 6 | return res 7 | 8 | 9 | if __name__ == '__main__': 10 | n = int(input().strip()) 11 | 12 | arr = list(map(int, input().rstrip().split())) 13 | 14 | m = int(input().strip()) 15 | 16 | brr = list(map(int, input().rstrip().split())) 17 | 18 | result = missingNumbers(arr, brr) 19 | 20 | print(' '.join(map(str, result))) 21 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/palindromeIndex.py: -------------------------------------------------------------------------------- 1 | def palindromeIndex(s): 2 | if s == s[::-1]: 3 | return -1 4 | i = 0 5 | j = len(s) - 1 6 | 7 | while i < j: 8 | if s[i] != s[j]: 9 | break 10 | i += 1 11 | j -= 1 12 | temp = s[:i] + s[i + 1:] 13 | if temp == temp[::-1]: 14 | return i 15 | temp = s[:j] + s[j + 1:] 16 | if temp == temp[::-1]: 17 | return j 18 | 19 | 20 | if __name__ == '__main__': 21 | q = int(input().strip()) 22 | 23 | for q_itr in range(q): 24 | s = input() 25 | 26 | result = palindromeIndex(s) 27 | 28 | print(str(result)) 29 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/quickSort.py: -------------------------------------------------------------------------------- 1 | def quickSort(arr): 2 | p = arr[0] 3 | l = [] 4 | r = [] 5 | e = [p] 6 | for i in arr[1:]: 7 | if i < p: 8 | l.append(i) 9 | elif i > p: 10 | r.append(i) 11 | else: 12 | e.append(i) 13 | return l + e + r 14 | 15 | 16 | if __name__ == '__main__': 17 | n = int(input().strip()) 18 | 19 | arr = list(map(int, input().rstrip().split())) 20 | 21 | result = quickSort(arr) 22 | 23 | print(' '.join(map(str, result))) 24 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/stones.py: -------------------------------------------------------------------------------- 1 | def stones(n, a, b): 2 | res = set() 3 | l = n - 1 4 | i = 0 5 | while i < n: 6 | res.add(a * (l - i) + b * i) 7 | i += 1 8 | return sorted(res) 9 | 10 | 11 | if __name__ == '__main__': 12 | T = int(input().strip()) 13 | 14 | for T_itr in range(T): 15 | n = int(input().strip()) 16 | 17 | a = int(input().strip()) 18 | 19 | b = int(input().strip()) 20 | 21 | result = stones(n, a, b) 22 | 23 | print(' '.join(map(str, result))) 24 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/strings_xor.py: -------------------------------------------------------------------------------- 1 | def strings_xor(s, t): 2 | res = "" 3 | for i in range(len(s)): 4 | if s[i] == t[i]: 5 | res += '0' 6 | else: 7 | res += '1' 8 | 9 | return res 10 | 11 | 12 | s = input() 13 | t = input() 14 | print(strings_xor(s, t)) 15 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/timeInWords.py: -------------------------------------------------------------------------------- 1 | def timeInWords(h, m): 2 | numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", 3 | "eighteen", "nineteen", "twenty", "twenty one", "twenty two", "twenty three", "twenty four", "twenty five", "twenty six", "twenty seven", "twenty eight", "twenty nine"] 4 | if m <= 30: 5 | if m == 0: 6 | return f"{numbers[h]} o' clock" 7 | elif m == 15: 8 | return f"quarter past {numbers[h]}" 9 | elif m == 30: 10 | return f"half past {numbers[h]}" 11 | elif m == 1: 12 | return f"{numbers[m]} minute past {numbers[h]}" 13 | else: 14 | return f"{numbers[m]} minutes past {numbers[h]}" 15 | else: 16 | if m == 45: 17 | return f"quarter to {numbers[h + 1]}" 18 | else: 19 | return f"{numbers[60 - m]} minutes to {numbers[h + 1]}" 20 | 21 | 22 | if __name__ == '__main__': 23 | h = int(input()) 24 | 25 | m = int(input()) 26 | 27 | result = timeInWords(h, m) 28 | 29 | print(result + '\n') 30 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/towerBreakers.py: -------------------------------------------------------------------------------- 1 | def towerBreakers(n, m): 2 | if m == 1: 3 | return 2 4 | elif n % 2 == 0: 5 | return 2 6 | else: 7 | return 1 8 | 9 | 10 | if __name__ == '__main__': 11 | t = int(input().strip()) 12 | 13 | for t_itr in range(t): 14 | first_multiple_input = input().rstrip().split() 15 | 16 | n = int(first_multiple_input[0]) 17 | 18 | m = int(first_multiple_input[1]) 19 | 20 | result = towerBreakers(n, m) 21 | 22 | print(str(result)) 23 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/toys.py: -------------------------------------------------------------------------------- 1 | def toys(w): 2 | res = 0 3 | w.sort() 4 | i = 0 5 | while i < len(w): 6 | j = i + 1 7 | while j < len(w): 8 | if w[j] > w[i] + 4: 9 | break 10 | j += 1 11 | i = j 12 | res += 1 13 | return res 14 | 15 | 16 | if __name__ == '__main__': 17 | n = int(input().strip()) 18 | 19 | w = list(map(int, input().rstrip().split())) 20 | 21 | result = toys(w) 22 | 23 | print(str(result) + '\n') 24 | -------------------------------------------------------------------------------- /Problem Solving (Basic)/twoArrays.py: -------------------------------------------------------------------------------- 1 | def twoArrays(k, A, B): 2 | A.sort() 3 | B.sort(reverse=True) 4 | for i in range(len(A)): 5 | if A[i] + B[i] < k: 6 | return 'NO' 7 | return 'YES' 8 | 9 | 10 | if __name__ == '__main__': 11 | q = int(input().strip()) 12 | 13 | for q_itr in range(q): 14 | first_multiple_input = input().rstrip().split() 15 | 16 | n = int(first_multiple_input[0]) 17 | 18 | k = int(first_multiple_input[1]) 19 | 20 | A = list(map(int, input().rstrip().split())) 21 | 22 | B = list(map(int, input().rstrip().split())) 23 | 24 | result = twoArrays(k, A, B) 25 | 26 | print(result) 27 | -------------------------------------------------------------------------------- /Problem Solving (Intermediate)/extraLongFactorials.py: -------------------------------------------------------------------------------- 1 | def extraLongFactorials(n): 2 | res = 1 3 | for i in range(2, n + 1): 4 | res *= i 5 | print(res) 6 | 7 | 8 | if __name__ == '__main__': 9 | n = int(input()) 10 | 11 | extraLongFactorials(n) 12 | -------------------------------------------------------------------------------- /Problem Solving (Intermediate)/flatlandSpaceStations.py: -------------------------------------------------------------------------------- 1 | def flatlandSpaceStations(n, c): 2 | c.sort() 3 | res = max(c[0], n - 1 - c[-1]) 4 | for i in range(1, len(c)): 5 | res = max(res, (c[i] - c[i - 1]) // 2) 6 | return res 7 | 8 | 9 | if __name__ == '__main__': 10 | nm = input().split() 11 | 12 | n = int(nm[0]) 13 | 14 | m = int(nm[1]) 15 | 16 | c = list(map(int, input().rstrip().split())) 17 | 18 | result = flatlandSpaceStations(n, c) 19 | 20 | print(str(result)) 21 | -------------------------------------------------------------------------------- /Problem Solving (Intermediate)/icecreamParlor.py: -------------------------------------------------------------------------------- 1 | def icecreamParlor(m, arr): 2 | for i in range(len(arr) - 1): 3 | for j in range(i + 1, len(arr)): 4 | if arr[i] + arr[j] == m: 5 | return i + 1, j + 1 6 | 7 | 8 | if __name__ == '__main__': 9 | t = int(input()) 10 | 11 | for t_itr in range(t): 12 | m = int(input()) 13 | 14 | n = int(input()) 15 | 16 | arr = list(map(int, input().rstrip().split())) 17 | 18 | result = icecreamParlor(m, arr) 19 | 20 | print(' '.join(map(str, result))) 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HackerRank Problem Solving in Python 2 | 3 | ## Description 4 | 5 | Since solving coding problems is one of my favorite things to do, Whenever I get free time, I log into the [HackerRank platform](https://www.hackerrank.com/) and start solving the challenges, so this repository contains my solutions to some selected programming problems on HackerRank, using my favorite programming language Python. 6 | 7 | ## What is HackerRank ? 8 | 9 | [HackerRank](https://www.hackerrank.com/) is a website and a platform for competitive programming, improving coding skills and learning to code which many coders around the world prefer. I started to use this platform for improving my problem solving ability and indulge in competitive programming. It contains coding challenges for practice, tutorials, and coding contests. We can increase our rating and raking by solving challenges present on the website and also by participating in contests and finally improve our credibility. 10 | --------------------------------------------------------------------------------