├── README.md ├── active-traders ├── balanced-system-files-partition ├── longest-subarray ├── maximum-cost-of-laptop-count ├── nearly-similar-rectangles ├── parallel-processing ├── password-decryption ├── road-repair ├── string-anagram ├── subarray-sums ├── unexpected-demand ├── usernames-changes └── vowel-substring /README.md: -------------------------------------------------------------------------------- 1 | # Hackerrank Problem Solving(Basic) Certificate test soltions 2 | 3 | Solutions to Certification of Problem Solving Basic on Hackerrank 4 | 5 | 6 | To get a certificate, two problems have to be solved within 90 minutes. 7 | 8 | The following is a list of possible problems per certificate. 9 | 10 | - Problem Solving (Basic) 11 | - [Active Traders](active-traders) 12 | - [Balanced System Files Partition](balanced-system-files-partition) 13 | - [Longest Subarray](longest-subarray) 14 | - [Maximum Cost of Laptop Count](maximum-cost-of-laptop-count) 15 | - [Nearly Similar Rectangles](nearly-similar-rectangles) 16 | - [Parallel Processing](parallel-processing) 17 | - [Password Decryption](password-decryption) 18 | - [Road Repair](road-repair) 19 | - [String Anagram](string-anagram) 20 | - [Subarray Sums](subarray-sums) 21 | - [Unexpected Demand](unexpected-demand) 22 | - [Usernames Changes](usernames-changes) 23 | - [Vowel Substring](vowel-substring) 24 | -------------------------------------------------------------------------------- /active-traders: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | from collections import defaultdict 10 | 11 | # 12 | # Complete the 'mostActive' function below. 13 | # 14 | # The function is expected to return a STRING_ARRAY. 15 | # The function accepts STRING_ARRAY customers as parameter. 16 | # 17 | 18 | def mostActive(customers): 19 | d = defaultdict(int) 20 | for c in customers: 21 | d[c] += 1 22 | return sorted([c for c, cnt in d.items() if cnt / len(customers) >= 0.05]) 23 | 24 | if __name__ == '__main__': 25 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 26 | 27 | customers_count = int(input().strip()) 28 | 29 | customers = [] 30 | 31 | for _ in range(customers_count): 32 | customers_item = input() 33 | customers.append(customers_item) 34 | 35 | result = mostActive(customers) 36 | 37 | fptr.write('\n'.join(result)) 38 | fptr.write('\n') 39 | 40 | fptr.close() 41 | -------------------------------------------------------------------------------- /balanced-system-files-partition: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | # 11 | # Complete the 'mostBalancedPartition' function below. 12 | # 13 | # The function is expected to return an INTEGER. 14 | # The function accepts following parameters: 15 | # 1. INTEGER_ARRAY parent 16 | # 2. INTEGER_ARRAY files_size 17 | # 18 | 19 | def mostBalancedPartition(parent, files_size): 20 | n = len(parent) 21 | children = [[] for _ in range(n)] 22 | for i in range(1, n): 23 | children[parent[i]].append(i) 24 | size_sums = [None for _ in range(n)] 25 | 26 | def size_sums_rec(i): 27 | size_sums[i] = files_size[i] + sum(size_sums_rec(c) for c in children[i]) 28 | return size_sums[i] 29 | 30 | size_sums_rec(0) 31 | return min(abs(size_sums[0] - 2 * ss) for ss in size_sums[1:]) 32 | 33 | if __name__ == '__main__': 34 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 35 | 36 | parent_count = int(input().strip()) 37 | 38 | parent = [] 39 | 40 | for _ in range(parent_count): 41 | parent_item = int(input().strip()) 42 | parent.append(parent_item) 43 | 44 | files_size_count = int(input().strip()) 45 | 46 | files_size = [] 47 | 48 | for _ in range(files_size_count): 49 | files_size_item = int(input().strip()) 50 | files_size.append(files_size_item) 51 | 52 | result = mostBalancedPartition(parent, files_size) 53 | 54 | fptr.write(str(result) + '\n') 55 | 56 | fptr.close() 57 | -------------------------------------------------------------------------------- /longest-subarray: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | # 11 | # Complete the 'longestSubarray' function below. 12 | # 13 | # The function is expected to return an INTEGER. 14 | # The function accepts INTEGER_ARRAY arr as parameter. 15 | # 16 | 17 | def longestSubarray(arr): 18 | n = len(arr) 19 | ans = 0 20 | # O(n^2) is okay because of constraints. 21 | for i in range(n): 22 | w = [] 23 | cnt = 0 24 | for j in range(i, n): 25 | if arr[j] in w: 26 | cnt += 1 27 | continue 28 | if len(w) == 0: 29 | w.append(arr[j]) 30 | elif len(w) == 1: 31 | if abs(w[0] - arr[j]) > 1: 32 | break 33 | else: 34 | w.append(arr[j]) 35 | else: 36 | break 37 | cnt += 1 38 | ans = max(ans, cnt) 39 | return ans 40 | 41 | if __name__ == '__main__': 42 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 43 | 44 | arr_count = int(input().strip()) 45 | 46 | arr = [] 47 | 48 | for _ in range(arr_count): 49 | arr_item = int(input().strip()) 50 | arr.append(arr_item) 51 | 52 | result = longestSubarray(arr) 53 | 54 | fptr.write(str(result) + '\n') 55 | 56 | fptr.close() 57 | -------------------------------------------------------------------------------- /maximum-cost-of-laptop-count: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | 11 | # 12 | # Complete the 'maxCost' function below. 13 | # 14 | # The function is expected to return an INTEGER. 15 | # The function accepts following parameters: 16 | # 1. INTEGER_ARRAY cost 17 | # 2. STRING_ARRAY labels 18 | # 3. INTEGER dailyCount 19 | # 20 | 21 | def maxCost(cost, labels, dailyCount): 22 | ans = 0 23 | cur_cnt = 0 24 | cur_cost = 0 25 | for c, l in zip(cost, labels): 26 | cur_cost += c 27 | if l == "illegal": 28 | continue 29 | cur_cnt += 1 30 | if cur_cnt == dailyCount: 31 | ans = max(ans, cur_cost) 32 | cur_cnt = 0 33 | cur_cost = 0 34 | return ans 35 | 36 | if __name__ == '__main__': 37 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 38 | 39 | cost_count = int(input().strip()) 40 | 41 | cost = [] 42 | 43 | for _ in range(cost_count): 44 | cost_item = int(input().strip()) 45 | cost.append(cost_item) 46 | 47 | labels_count = int(input().strip()) 48 | 49 | labels = [] 50 | 51 | for _ in range(labels_count): 52 | labels_item = input() 53 | labels.append(labels_item) 54 | 55 | dailyCount = int(input().strip()) 56 | 57 | result = maxCost(cost, labels, dailyCount) 58 | 59 | fptr.write(str(result) + '\n') 60 | 61 | fptr.close() 62 | -------------------------------------------------------------------------------- /nearly-similar-rectangles: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | from collections import defaultdict 10 | 11 | # 12 | # Complete the 'nearlySimilarRectangles' function below. 13 | # 14 | # The function is expected to return a LONG_INTEGER. 15 | # The function accepts 2D_LONG_INTEGER_ARRAY sides as parameter. 16 | # 17 | 18 | def nearlySimilarRectangles(sides): 19 | gcd = lambda a, b: gcd(b, a % b) if b > 0 else a 20 | d = defaultdict(int) 21 | for w, h in sides: 22 | z = gcd(w, h) 23 | d[(w // z, h // z)] += 1 24 | return sum((x * (x - 1)) // 2 for x in d.values()) 25 | 26 | if __name__ == '__main__': 27 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 28 | 29 | sides_rows = int(input().strip()) 30 | sides_columns = int(input().strip()) 31 | 32 | sides = [] 33 | 34 | for _ in range(sides_rows): 35 | sides.append(list(map(int, input().rstrip().split()))) 36 | 37 | result = nearlySimilarRectangles(sides) 38 | 39 | fptr.write(str(result) + '\n') 40 | 41 | fptr.close() 42 | -------------------------------------------------------------------------------- /parallel-processing: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | # 11 | # Complete the 'minTime' function below. 12 | # 13 | # The function is expected to return a LONG_INTEGER. 14 | # The function accepts following parameters: 15 | # 1. INTEGER_ARRAY files 16 | # 2. INTEGER numCores 17 | # 3. INTEGER limit 18 | # 19 | 20 | def minTime(files, numCores, limit): 21 | x = [] 22 | y = [] 23 | for f in files: 24 | if f % numCores == 0: 25 | x.append(f) 26 | else: 27 | y.append(f) 28 | x.sort(reverse=True) 29 | return (sum(x[:limit]) // numCores) + sum(x[limit:]) + sum(y) 30 | 31 | if __name__ == '__main__': 32 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 33 | 34 | files_count = int(input().strip()) 35 | 36 | files = [] 37 | 38 | for _ in range(files_count): 39 | files_item = int(input().strip()) 40 | files.append(files_item) 41 | 42 | numCores = int(input().strip()) 43 | 44 | limit = int(input().strip()) 45 | 46 | result = minTime(files, numCores, limit) 47 | 48 | fptr.write(str(result) + '\n') 49 | 50 | fptr.close() 51 | -------------------------------------------------------------------------------- /password-decryption: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | 11 | # 12 | # Complete the 'decryptPassword' function below. 13 | # 14 | # The function is expected to return a STRING. 15 | # The function accepts STRING s as parameter. 16 | # 17 | 18 | def decryptPassword(s): 19 | s = list(s) 20 | i = 0 21 | while i < len(s) and s[i].isdigit() and s[i] != "0": 22 | i += 1 23 | for j, k in enumerate([l for l in range(i, len(s)) if s[l] == "0"]): 24 | s[k] = s[i - j - 1] 25 | for j in range(i, len(s)): 26 | if s[j] == "*": 27 | s[j - 1], s[j - 2] = s[j - 2], s[j - 1] 28 | return "".join(s[i:]).replace("*", "") 29 | 30 | if __name__ == '__main__': 31 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 32 | 33 | s = input() 34 | 35 | result = decryptPassword(s) 36 | 37 | fptr.write(result + '\n') 38 | 39 | fptr.close() 40 | -------------------------------------------------------------------------------- /road-repair: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | # 11 | # Complete the 'getMinCost' function below. 12 | # 13 | # The function is expected to return a LONG_INTEGER. 14 | # The function accepts following parameters: 15 | # 1. INTEGER_ARRAY crew_id 16 | # 2. INTEGER_ARRAY job_id 17 | # 18 | 19 | def getMinCost(crew_id, job_id): 20 | crew_id.sort() 21 | job_id.sort() 22 | return sum(abs(c - j) for c, j in zip(crew_id, job_id)) 23 | 24 | if __name__ == '__main__': 25 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 26 | 27 | crew_id_count = int(input().strip()) 28 | 29 | crew_id = [] 30 | 31 | for _ in range(crew_id_count): 32 | crew_id_item = int(input().strip()) 33 | crew_id.append(crew_id_item) 34 | 35 | job_id_count = int(input().strip()) 36 | 37 | job_id = [] 38 | 39 | for _ in range(job_id_count): 40 | job_id_item = int(input().strip()) 41 | job_id.append(job_id_item) 42 | 43 | result = getMinCost(crew_id, job_id) 44 | 45 | fptr.write(str(result) + '\n') 46 | 47 | fptr.close() 48 | -------------------------------------------------------------------------------- /string-anagram: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | from collections import defaultdict 10 | 11 | # 12 | # Complete the 'stringAnagram' function below. 13 | # 14 | # The function is expected to return an INTEGER_ARRAY. 15 | # The function accepts following parameters: 16 | # 1. STRING_ARRAY dictionary 17 | # 2. STRING_ARRAY query 18 | # 19 | 20 | def stringAnagram(dictionary, query): 21 | d = defaultdict(int) 22 | for w in dictionary: 23 | d["".join(sorted(w))] += 1 24 | ans = [] 25 | for w in query: 26 | w = "".join(sorted(w)) 27 | ans.append(d[w] if w in d else 0) 28 | return ans 29 | 30 | if __name__ == '__main__': 31 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 32 | 33 | dictionary_count = int(input().strip()) 34 | 35 | dictionary = [] 36 | 37 | for _ in range(dictionary_count): 38 | dictionary_item = input() 39 | dictionary.append(dictionary_item) 40 | 41 | query_count = int(input().strip()) 42 | 43 | query = [] 44 | 45 | for _ in range(query_count): 46 | query_item = input() 47 | query.append(query_item) 48 | 49 | result = stringAnagram(dictionary, query) 50 | 51 | fptr.write('\n'.join(map(str, result))) 52 | fptr.write('\n') 53 | 54 | fptr.close() 55 | -------------------------------------------------------------------------------- /subarray-sums: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | # 11 | # Complete the 'findSum' function below. 12 | # 13 | # The function is expected to return a LONG_INTEGER_ARRAY. 14 | # The function accepts following parameters: 15 | # 1. INTEGER_ARRAY numbers 16 | # 2. 2D_INTEGER_ARRAY queries 17 | # 18 | 19 | def findSum(numbers, queries): 20 | a = [0] 21 | b = [0] 22 | for x in numbers: 23 | a.append(a[-1] + x) 24 | b.append(b[-1] + (x == 0)) 25 | return [a[r] - a[l - 1] + x * (b[r] - b[l - 1]) for l, r, x in queries] 26 | 27 | if __name__ == '__main__': 28 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 29 | 30 | numbers_count = int(input().strip()) 31 | 32 | numbers = [] 33 | 34 | for _ in range(numbers_count): 35 | numbers_item = int(input().strip()) 36 | numbers.append(numbers_item) 37 | 38 | queries_rows = int(input().strip()) 39 | queries_columns = int(input().strip()) 40 | 41 | queries = [] 42 | 43 | for _ in range(queries_rows): 44 | queries.append(list(map(int, input().rstrip().split()))) 45 | 46 | result = findSum(numbers, queries) 47 | 48 | fptr.write('\n'.join(map(str, result))) 49 | fptr.write('\n') 50 | 51 | fptr.close() 52 | -------------------------------------------------------------------------------- /unexpected-demand: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | # 11 | # Complete the 'filledOrders' function below. 12 | # 13 | # The function is expected to return an INTEGER. 14 | # The function accepts following parameters: 15 | # 1. INTEGER_ARRAY order 16 | # 2. INTEGER k 17 | # 18 | 19 | def filledOrders(order, k): 20 | order.sort() 21 | ans = 0 22 | for x in order: 23 | if x <= k: 24 | ans += 1 25 | k -= x 26 | else: 27 | break 28 | return ans 29 | 30 | if __name__ == '__main__': 31 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 32 | 33 | order_count = int(input().strip()) 34 | 35 | order = [] 36 | 37 | for _ in range(order_count): 38 | order_item = int(input().strip()) 39 | order.append(order_item) 40 | 41 | k = int(input().strip()) 42 | 43 | result = filledOrders(order, k) 44 | 45 | fptr.write(str(result) + '\n') 46 | 47 | fptr.close() 48 | -------------------------------------------------------------------------------- /usernames-changes: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | 11 | # 12 | # Complete the 'possibleChanges' function below. 13 | # 14 | # The function is expected to return a STRING_ARRAY. 15 | # The function accepts STRING_ARRAY usernames as parameter. 16 | # 17 | 18 | def possibleChanges(usernames): 19 | ans = [] 20 | for u in usernames: 21 | if len(u) <= 1: 22 | ans.append("NO") 23 | for i in range(len(u) - 1): 24 | if u[i] > u[i + 1]: 25 | ans.append("YES") 26 | break 27 | else: 28 | ans.append("NO") 29 | return ans 30 | 31 | if __name__ == '__main__': 32 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 33 | 34 | usernames_count = int(input().strip()) 35 | 36 | usernames = [] 37 | 38 | for _ in range(usernames_count): 39 | usernames_item = input() 40 | usernames.append(usernames_item) 41 | 42 | result = possibleChanges(usernames) 43 | 44 | fptr.write('\n'.join(result)) 45 | fptr.write('\n') 46 | 47 | fptr.close() 48 | -------------------------------------------------------------------------------- /vowel-substring: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | 11 | # 12 | # Complete the 'findSubstring' function below. 13 | # 14 | # The function is expected to return a STRING. 15 | # The function accepts following parameters: 16 | # 1. STRING s 17 | # 2. INTEGER k 18 | # 19 | 20 | def findSubstring(s, k): 21 | vowels = ["a", "e", "i", "o", "u"] 22 | cur = best = sum([c in vowels for c in s[:k]]) 23 | ans = 0 24 | for i in range(k, len(s)): 25 | cur += s[i] in vowels 26 | cur -= s[i - k] in vowels 27 | if cur > best: 28 | best = cur 29 | ans = i - k + 1 30 | if best > 0: 31 | return s[ans:(ans+k)] 32 | else: 33 | return "Not found!" 34 | 35 | if __name__ == '__main__': 36 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 37 | 38 | s = input() 39 | 40 | k = int(input().strip()) 41 | 42 | result = findSubstring(s, k) 43 | 44 | fptr.write(result + '\n') 45 | 46 | fptr.close() 47 | --------------------------------------------------------------------------------