├── README.md ├── balanced_system_file_partition.py ├── shopping_cart.py ├── string_transformation.py └── vowel_substring.py /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |  4 | 5 | # Hackerrank Certification 6 |
23 | I'll be uploading other solutions of hackerrank certifications questions in this repo. 24 |
25 | -------------------------------------------------------------------------------- /balanced_system_file_partition.py: -------------------------------------------------------------------------------- 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 | # Write your code here 21 | def helper(node, adj, files_size): 22 | queue = [node] 23 | weight = 0 24 | while queue: 25 | index = queue.pop() 26 | weight += files_size[index] 27 | if index in adj: 28 | queue.extend(adj[index]) 29 | return weight 30 | 31 | adj = {} 32 | edges = [] 33 | for index, p in enumerate(parent): 34 | edges.append((p, index)) 35 | if p in adj: 36 | adj[p].append(index) 37 | else: 38 | adj[p] = [index] 39 | 40 | print(adj, edges) 41 | total_weight = sum(files_size) 42 | min_diff = sum(files_size) 43 | for e in edges: 44 | p,c = e 45 | adj[p].remove(c) 46 | w1 = helper(c, adj, files_size) 47 | min_diff = min(min_diff, abs(total_weight - 2*w1)) 48 | adj[p].append(c) 49 | 50 | return min_diff 51 | if __name__ == '__main__': -------------------------------------------------------------------------------- /shopping_cart.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | class Item: 11 | name = '' 12 | price= 0 13 | 14 | def __init__(self,s,p): 15 | self.name = s 16 | self.price = p 17 | pass 18 | 19 | class ShoppingCart: 20 | cart_names = [] 21 | cart_prices= [] 22 | tot = 0 23 | 24 | def __init__(self): 25 | super().__init__() 26 | 27 | def add(self, item): 28 | self.cart_names.append(item.name) 29 | self.cart_prices.append(item.price) 30 | 31 | def total(self): 32 | self.tot = sum(self.cart_prices) 33 | return self.tot 34 | 35 | def __len__(self): 36 | return len(self.cart_names) 37 | pass 38 | 39 | 40 | 41 | if __name__ == '__main__': 42 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 43 | 44 | n = int(input()) 45 | items = [] 46 | for _ in range(n): 47 | name, price = input().split() 48 | item = Item(name, int(price)) 49 | items.append(item) 50 | 51 | cart = ShoppingCart() 52 | 53 | q = int(input()) 54 | for _ in range(q): 55 | line = input().split() 56 | command, params = line[0], line[1:] 57 | if command == "len": 58 | fptr.write(str(len(cart)) + "\n") 59 | elif command == "total": 60 | fptr.write(str(cart.total()) + "\n") 61 | elif command == "add": 62 | name = params[0] 63 | item = next(item for item in items if item.name == name) 64 | cart.add(item) 65 | else: 66 | raise ValueError("Unknown command %s" % command) 67 | 68 | fptr.close() 69 | -------------------------------------------------------------------------------- /string_transformation.py: -------------------------------------------------------------------------------- 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 'transformSentence' function below. 12 | # 13 | # The function is expected to return a STRING. 14 | # The function accepts STRING sentence as parameter. 15 | # 16 | 17 | def transformSentence(sentence): 18 | sentence.strip() 19 | res = '' 20 | ind = 0 21 | for x in sentence: 22 | if(ind==0): 23 | res+= x 24 | ind+=1 25 | elif(x == ' '): 26 | res += x 27 | ind+=1 28 | else: 29 | y = sentence[ind-1] 30 | if(y==' '): 31 | res+= x 32 | ind+=1 33 | elif(y.lower() < x.lower()): 34 | res += x.upper() 35 | ind+=1 36 | elif(y.lower()>x.lower()): 37 | res += x.lower() 38 | ind+=1 39 | else: 40 | res+= x 41 | ind+=1 42 | return res 43 | if __name__ == '__main__': -------------------------------------------------------------------------------- /vowel_substring.py: -------------------------------------------------------------------------------- 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 'findSubstring' function below. 12 | # 13 | # The function is expected to return a STRING. 14 | # The function accepts following parameters: 15 | # 1. STRING s 16 | # 2. INTEGER k 17 | # 18 | 19 | def findSubstring(s, k): 20 | j = k 21 | mx = 0 22 | count= 0 23 | max_sub = "" 24 | ln = len(s) 25 | i = 0 26 | for i in s[0:j]: 27 | if i in 'aeiou': 28 | count+=1 29 | if count!=0: 30 | mx = count 31 | max_sub = s[0:j] 32 | i = 0 33 | while(j