From 43ec51185e6fd1ada9dbda6e235e252b86819ba5 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Fri, 3 Dec 2021 21:13:47 +0530 Subject: [PATCH 01/22] added December-01 solution --- December-01/python3_ASHIK11ab.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 December-01/python3_ASHIK11ab.py diff --git a/December-01/python3_ASHIK11ab.py b/December-01/python3_ASHIK11ab.py new file mode 100644 index 0000000..0f0faec --- /dev/null +++ b/December-01/python3_ASHIK11ab.py @@ -0,0 +1,25 @@ +def find_intersection(necklase_A, necklase_B): + candidate_stones = "" + # Maintain small string as first for performing intersection. + if len(necklase_A) > len(necklase_B): + (necklase_A, necklase_B) = (necklase_B[:], necklase_A[:]) + + for stone in necklase_A: + if stone in necklase_B and stone not in candidate_stones: + candidate_stones += stone + return candidate_stones + + +def main(necklases): + facelifts = "" + index = 0 + while index < len(necklases) - 1: + facelifts = find_intersection(necklases[index], necklases[index+1]) + if len(facelifts) < 0: + break + index += 1 + print(len(facelifts)) + + +if __name__ == "__main__": + main(['abcdde', 'baccd', 'eeabg']) \ No newline at end of file From 703e633111a7612e1a43146e084c16c05ef6168b Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sun, 12 Dec 2021 19:34:09 +0530 Subject: [PATCH 02/22] added december 2 solution --- December-02/python3_ASHIK11ab.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 December-02/python3_ASHIK11ab.py diff --git a/December-02/python3_ASHIK11ab.py b/December-02/python3_ASHIK11ab.py new file mode 100644 index 0000000..f8e082f --- /dev/null +++ b/December-02/python3_ASHIK11ab.py @@ -0,0 +1,26 @@ +def sum_of_squares_digits(number): + digits_sum = 0 + while number != 0: + digit = number % 10 + digits_sum += digit**2 + number = number // 10 + return digits_sum + +def main(num): + # List of numbers for which sum of squares of digits + # is calculated. Used to find occurrence of cycle. + calculated = [] + while num != 1: + calc_sum = sum_of_squares_digits(num) + # Checking for cycle. + if calc_sum in calculated: + print("NO") + return + else: + calculated.append(calc_sum) + num = calc_sum + print("YES") + + +if __name__ == "__main__": + main(19) \ No newline at end of file From 9a7aeb80fbb23a80d0fdffeb93e23a9690955388 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sun, 12 Dec 2021 19:34:29 +0530 Subject: [PATCH 03/22] added december 3 solution --- December-03/python3_ASHIK11ab.py | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 December-03/python3_ASHIK11ab.py diff --git a/December-03/python3_ASHIK11ab.py b/December-03/python3_ASHIK11ab.py new file mode 100644 index 0000000..f7b45db --- /dev/null +++ b/December-03/python3_ASHIK11ab.py @@ -0,0 +1,77 @@ +class Solution: + def __init__(self, board, req_name): + self.board = board + self.req_name = req_name + self.visited_cells = [] + self.solution_reached = False + + # Use cells in board. + def cell_in_board(self, cell): + return cell[0] >= 0 and cell[0] < len(self.board) and \ + cell[1] >= 0 and cell[1] < len(self.board[0]) + + + # Find next required alphabet. + def find_next_character(self, scanning_index, curr_cell): + if scanning_index == len(self.req_name): + self.solution_reached = True + return + + left_cell = (curr_cell[0], curr_cell[1]-1) + top_cell = (curr_cell[0]-1, curr_cell[1]) + right_cell = (curr_cell[0], curr_cell[1]+1) + bottom_cell = (curr_cell[0]+1, curr_cell[1]) + + candidate_cells = [left_cell, top_cell, right_cell, bottom_cell] + + # For a given alphabet the neighbouring cells are left, top + # right, bottom. + for cell in candidate_cells: + if self.cell_in_board(cell) and cell not in self.visited_cells \ + and self.board[cell[0]][cell[1]] == self.req_name[scanning_index]: + scanning_index += 1 + self.visited_cells.append(cell) + self.find_next_character(scanning_index, cell) + + if not self.solution_reached: + # Undo current step if solution is not possible. + scanning_index -= 1 + self.visited_cells.pop() + else: + return + + + def solve(self): + # Scanning index -> index of the alphabet of name to be + # searched in the board. + scanning_index = 0 + # Look for the initial alphabet in the board. + for row in range(len(self.board)): + # Since the dimensions of all rows are same. + for col in range(len(self.board[0])): + if self.board[row][col] == self.req_name[scanning_index]: + scanning_index += 1 + curr_cell = (row, col) + self.visited_cells.append(curr_cell) + self.find_next_character(scanning_index, curr_cell) + + if not self.solution_reached: + # Undo current step if solution is not possible. + scanning_index -= 1 + self.visited_cells.pop() + else: + break + + if self.solution_reached: + print("YES") + else: + print("NO") + + +def main(board, name): + obj = Solution(board, name) + obj.solve() + + +if __name__ == "__main__": + main(board=[["D","J","O","G"],["W","B","H","S"],["T","Z","N","E"]], name="JOHN") \ No newline at end of file From 0308212c13a70f0f190fab11f9d5d59c9f3e499e Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Thu, 23 Dec 2021 19:37:44 +0530 Subject: [PATCH 04/22] December 05 solution complete --- December-05/python3_ASHIK11ab.py | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 December-05/python3_ASHIK11ab.py diff --git a/December-05/python3_ASHIK11ab.py b/December-05/python3_ASHIK11ab.py new file mode 100644 index 0000000..b2fa198 --- /dev/null +++ b/December-05/python3_ASHIK11ab.py @@ -0,0 +1,42 @@ +class Solution: + def __init__(self, customers, biscuits): + self.customers = customers + self.biscuits = biscuits + + def solve(self): + # No of biscuits in stack. + stack_len = len(self.biscuits) + cnt = 0 + + # Complete one round of iteration and if the buiscuits stack + # is changed or not. + while cnt < len(self.customers): + # When customers likes the buiscuit. + if self.customers[0] == self.biscuits[0]: + self.customers = self.customers[1:] + self.biscuits = self.biscuits[1:] + else: + # Customer removed and placed at end of Queue. + curr_customer = self.customers[0] + self.customers = self.customers[1:] + self.customers.append(curr_customer) + cnt += 1 + + # Stack length not changed indicates that no + # customer in the queue prefers the buiscuit at + # the top of the stack. + if len(self.biscuits) == stack_len: + print(f"Customers that are unable to eat = {len(self.customers)}") + else: + # Keep solving until no user prefers the buiscuit at top + # of the stack or until all biscuits are over. + self.solve() + + +def main(customers, biscuits): + obj = Solution(customers=customers, biscuits=biscuits) + obj.solve() + + +if __name__ == "__main__": + main(customers = [1,1,0,0,1,0], biscuits = [0,1,0,1,1,1]) \ No newline at end of file From 667699ec109f1d37e6156e0e0df383875c5a2734 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sat, 25 Dec 2021 21:40:59 +0530 Subject: [PATCH 05/22] added solution december 06 --- December-06/python3_ASHIK11ab.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 December-06/python3_ASHIK11ab.py diff --git a/December-06/python3_ASHIK11ab.py b/December-06/python3_ASHIK11ab.py new file mode 100644 index 0000000..d842066 --- /dev/null +++ b/December-06/python3_ASHIK11ab.py @@ -0,0 +1,28 @@ +class Solution: + def __init__(self, templars): + self.templars = [templar for templar in templars] + + def solve(self): + left = 0 + right = len(self.templars) - 1 + swap_cnt = 0 + # Always maintain `templar U` in left and `templar T` in right. + # If above does not hold then swap and increment swap count by 1. + while left != right: + if self.templars[left] == 'T': + if self.templars[right] == 'U': + (self.templars[left], self.templars[right]) = (self.templars[right], self.templars[left]) + swap_cnt += 1 + else: + right -= 1 + else: + left += 1 + print(swap_cnt) + +def main(): + templars = input() + s = Solution(templars) + s.solve() + +if __name__ == "__main__": + main() \ No newline at end of file From 9b40bada17ec8c7a7d58afbbaec3c6c439e3444e Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sun, 26 Dec 2021 16:03:53 +0530 Subject: [PATCH 06/22] added solution december 07 --- December-07/python3_ASHIK11ab.py | 85 ++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 December-07/python3_ASHIK11ab.py diff --git a/December-07/python3_ASHIK11ab.py b/December-07/python3_ASHIK11ab.py new file mode 100644 index 0000000..80a2a0b --- /dev/null +++ b/December-07/python3_ASHIK11ab.py @@ -0,0 +1,85 @@ +class Solution: + def __init__(self, buildings, planes): + self.buildings = buildings + self.planes = planes + + def solve(self): + # For each plane find the number of buildings which can recieve + # food from the plane. + for plane_index in range(len(self.planes)): + buildings_recieved_food_cnt = 0 + for index in range(len(self.buildings)): + if food_delivery_possible(self.buildings[index], self.planes[plane_index]): + buildings_recieved_food_cnt += 1 + print(buildings_recieved_food_cnt) + + +def food_delivery_possible(building, plane): + direction = plane[0] + point = plane[1] + # Get three poinst of a building. + p1 = building[0] + p2 = building[1] + p3 = building[2] + + # Food on a plane can be delivered to a building if the plane + # flies over the building. + # For a given direction, if plane passes through any one of the + # three sides of the building then the food can be delivered. + + # dir_index -> if plane passes through 'x' take the x coordinate + # of all points of the building or take 'y' coordinate. + # pt (x,y) -> 0 accesses x coordinate, 1 accesses y coordinate + + dir_index = 0 if direction == 'x' else 1 + + if point > min(p1[dir_index], p2[dir_index]) and point < max(p1[dir_index], p2[dir_index]): + delivery_possible = True + elif point > min(p1[dir_index], p3[dir_index]) and point < max(p1[dir_index], p3[dir_index]): + delivery_possible = True + elif point > min(p2[dir_index], p3[dir_index]) and point < max(p2[dir_index], p3[dir_index]): + delivery_possible = True + else: + delivery_possible = False + return delivery_possible + + +def get_coords(line): + """ Parses a line of 6 integers and returns list of three coordinates. """ + building_coords = [] + # Extracting the coordinate points from the line input. + coords_list = [int(pt) for pt in line if pt != ' '] + for index in range(0, len(coords_list), 2): + coord_x = coords_list[index] + coord_y = coords_list[index+1] + coords = (coord_x, coord_y) + building_coords.append(coords) + return building_coords + + +def main(): + buildings_cnt = int(input("Number of Buildings: ")) + buildings = [] + print("Coordinates of the buildings:") + for index in range(buildings_cnt): + line = input() + # Parses the line and finds the three pairs of points + # of a building. + coords = get_coords(line) + buildings.append(coords) + planes_cnt = int(input("Number of jet-planes: ")) + planes = [] + for index in range(planes_cnt): + line = input() + # jet_info -> list having the jet direction ('x' or 'y') and + # the point on which the jet is moving. + jet_info = line.split(' ') + jet_info.remove('=') + jet_info[1] = int(jet_info[1]) + planes.append(jet_info) + s = Solution(buildings, planes) + print("\nBuildings that received food:") + s.solve() + +if __name__ == "__main__": + main() \ No newline at end of file From 04ca161836d359ee824a90e0b26077758628d711 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Mon, 27 Dec 2021 21:28:57 +0530 Subject: [PATCH 07/22] added solution december 08 --- December-08/python3_ASHIK11ab.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 December-08/python3_ASHIK11ab.py diff --git a/December-08/python3_ASHIK11ab.py b/December-08/python3_ASHIK11ab.py new file mode 100644 index 0000000..c6c3ebd --- /dev/null +++ b/December-08/python3_ASHIK11ab.py @@ -0,0 +1,31 @@ +class Solution: + def __init__(self, time): + self.time = time + + def solve(self): + if self.time == 0: + print("counter value: 0") + return + # Counter initial value is 3 but is set as 4 to keep the counter + # value at 3 during the first iteration. + counter_val = 4 + prev_counter_val = 3 + for index in range(0, self.time): + # when counter value is 1 reset it with twice the value + # of the previous round counter's initial value. + if counter_val == 1: + temp = counter_val + counter_val = 2*prev_counter_val + prev_counter_val = counter_val + else: + counter_val -= 1 + + print(f"counter value: {counter_val}") + +def main(): + time = int(input("time: ")) + s = Solution(time=time) + s.solve() + +if __name__ == "__main__": + main() \ No newline at end of file From deedafcc73db1c8c26d57e990db8d5bb8283cc85 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Mon, 27 Dec 2021 21:57:05 +0530 Subject: [PATCH 08/22] updated getting input december 01 --- December-01/python3_ASHIK11ab.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/December-01/python3_ASHIK11ab.py b/December-01/python3_ASHIK11ab.py index 0f0faec..daa72ea 100644 --- a/December-01/python3_ASHIK11ab.py +++ b/December-01/python3_ASHIK11ab.py @@ -9,8 +9,16 @@ def find_intersection(necklase_A, necklase_B): candidate_stones += stone return candidate_stones +def get_input(): + """ Parses the string input and returns list of necklases. """ + arr = input("arr = ") + arr = arr[1:len(arr)-1] + arr = arr.replace(' ', '').split(',') + arr = [ele[1:len(ele)-1] for ele in arr] + return arr -def main(necklases): +def main(): + necklases = get_input() facelifts = "" index = 0 while index < len(necklases) - 1: @@ -22,4 +30,4 @@ def main(necklases): if __name__ == "__main__": - main(['abcdde', 'baccd', 'eeabg']) \ No newline at end of file + main() \ No newline at end of file From 8c58611c644e37822ee8aed6a2b449d1bade3e27 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Mon, 27 Dec 2021 21:58:41 +0530 Subject: [PATCH 09/22] updated getting input december 02 --- December-02/python3_ASHIK11ab.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/December-02/python3_ASHIK11ab.py b/December-02/python3_ASHIK11ab.py index f8e082f..a09f302 100644 --- a/December-02/python3_ASHIK11ab.py +++ b/December-02/python3_ASHIK11ab.py @@ -6,7 +6,8 @@ def sum_of_squares_digits(number): number = number // 10 return digits_sum -def main(num): +def main(): + num = int(input("n = ")) # List of numbers for which sum of squares of digits # is calculated. Used to find occurrence of cycle. calculated = [] @@ -23,4 +24,4 @@ def main(num): if __name__ == "__main__": - main(19) \ No newline at end of file + main() \ No newline at end of file From 37d4d23278001310c60bc3c7154f4c53f1fcead4 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sat, 1 Jan 2022 16:20:53 +0530 Subject: [PATCH 10/22] added december 9 solution --- December-09/python3_ASHIK11ab.py | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 December-09/python3_ASHIK11ab.py diff --git a/December-09/python3_ASHIK11ab.py b/December-09/python3_ASHIK11ab.py new file mode 100644 index 0000000..108936e --- /dev/null +++ b/December-09/python3_ASHIK11ab.py @@ -0,0 +1,51 @@ +class Solution: + def __init__(self, no_understudies, req_understudies, abilities): + self.no_understudies = no_understudies + self.req_understudies = req_understudies + self.abilities = abilities + + def solve(self): + candidate_teams = combinations(self.abilities, self.req_understudies) + base_periods = [] + for team in candidate_teams: + base_period = 0 + for member in team: + base_period += max(team) - member + base_periods.append(base_period) + print(f"Base number of periods required = {min(base_periods)}") + return + + +def combinations(array, k, candidates=[], result=[]): + # When k is 0 (when one of the k item combination is generated) then + # add it to the result. + if k == 0: + result.append(candidates[:]) + return + # Pick an element from the list and find the remaining combinations. + # After finding the result remove the picked element and proceed + # with the next elements. + for i in range(0, len(array)): + candidates.append(array[i]) + combinations(array[i+1:], k-1, candidates, result) + candidates.pop() + return result + + +def get_understudies_ability(): + array = input("N = ") + array = array[1:len(array)-1] + array = array.replace(' ', '').split(',') + understudies_ability = [int(ele) for ele in array] + return understudies_ability + + +def main(): + no_understudies = int(input("N = ")) + req_understudies = int(input("P = ")) + understudies_ability = get_understudies_ability() + s = Solution(no_understudies, req_understudies, understudies_ability) + s.solve() + +if __name__ == "__main__": + main() \ No newline at end of file From 698549db43c6d7fdddc6afc8f55e79a16f688d6c Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sat, 1 Jan 2022 21:08:41 +0530 Subject: [PATCH 11/22] update getting input december 05 --- December-05/python3_ASHIK11ab.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/December-05/python3_ASHIK11ab.py b/December-05/python3_ASHIK11ab.py index b2fa198..86028b8 100644 --- a/December-05/python3_ASHIK11ab.py +++ b/December-05/python3_ASHIK11ab.py @@ -33,10 +33,21 @@ def solve(self): self.solve() -def main(customers, biscuits): +def get_list_from_string(string): + string = string[1:len(string)-1] + string = string.replace(' ', '').split(',') + new_list = [int(ele) for ele in string] + return new_list + + +def main(): + customers = input("customers = ") + customers = get_list_from_string(customers) + biscuits = input("biscuits = ") + biscuits = get_list_from_string(biscuits) obj = Solution(customers=customers, biscuits=biscuits) obj.solve() if __name__ == "__main__": - main(customers = [1,1,0,0,1,0], biscuits = [0,1,0,1,1,1]) \ No newline at end of file + main() \ No newline at end of file From f6d5b0f627f4c158e2dc7741880b1cd783671f20 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sat, 1 Jan 2022 21:13:40 +0530 Subject: [PATCH 12/22] minor update december 08 --- December-08/python3_ASHIK11ab.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/December-08/python3_ASHIK11ab.py b/December-08/python3_ASHIK11ab.py index c6c3ebd..17d6f7d 100644 --- a/December-08/python3_ASHIK11ab.py +++ b/December-08/python3_ASHIK11ab.py @@ -20,10 +20,10 @@ def solve(self): else: counter_val -= 1 - print(f"counter value: {counter_val}") + print(f"counter value = {counter_val}") def main(): - time = int(input("time: ")) + time = int(input("time = ")) s = Solution(time=time) s.solve() From 22b3869b64fb250efdc21697f4f0efb1b5be2ece Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sat, 1 Jan 2022 21:24:19 +0530 Subject: [PATCH 13/22] updated getting input december 03 --- December-03/python3_ASHIK11ab.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/December-03/python3_ASHIK11ab.py b/December-03/python3_ASHIK11ab.py index f7b45db..54ed39e 100644 --- a/December-03/python3_ASHIK11ab.py +++ b/December-03/python3_ASHIK11ab.py @@ -1,3 +1,5 @@ +import ast + class Solution: def __init__(self, board, req_name): self.board = board @@ -62,16 +64,15 @@ def solve(self): else: break - if self.solution_reached: - print("YES") - else: - print("NO") + print(self.solution_reached) -def main(board, name): +def main(): + board = ast.literal_eval(input()) + name = input("name = ") obj = Solution(board, name) obj.solve() if __name__ == "__main__": - main(board=[["D","J","O","G"],["W","B","H","S"],["T","Z","N","E"]], name="JOHN") \ No newline at end of file + main() \ No newline at end of file From b9bde8bea6ee5d6fac8b3f5a3824901eab01ef40 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Thu, 6 Jan 2022 21:25:37 +0530 Subject: [PATCH 14/22] added solution december 10 --- December-10/python3_ASHIK11ab.py | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 December-10/python3_ASHIK11ab.py diff --git a/December-10/python3_ASHIK11ab.py b/December-10/python3_ASHIK11ab.py new file mode 100644 index 0000000..1695e2e --- /dev/null +++ b/December-10/python3_ASHIK11ab.py @@ -0,0 +1,72 @@ +import ast + +class Solution: + def __init__(self, grid): + self.grid = grid + self.max_oranges = 0 + + + def go_to_first_cell(self, row, col, oranges): + + if (row, col) == (0, 0): + self.max_oranges = max(oranges, self.max_oranges) + return + + cell_value = self.grid[row][col] + + if self.grid[row][col] == 1: + oranges += 1 + self.grid[row][col] = 0 + + # Make top move. + if can_make_move(self.grid, row-1, col): + self.go_to_first_cell(row-1, col, oranges) + + + # Make left move. + if can_make_move(self.grid, row, col-1): + self.go_to_first_cell(row, col-1, oranges) + + self.grid[row][col] = cell_value + + + def solve(self, row, col, oranges): + + row_len = len(self.grid) + col_len = len(self.grid[0]) + cell_value = self.grid[row][col] + + if self.grid[row][col] == 1: + oranges += 1 + self.grid[row][col] = 0 + + # Go to first cell if last cell is reached. + if (row, col) == (row_len-1, col_len-1): + self.go_to_first_cell(row, col, oranges) + return + + # Make right move. + if can_make_move(self.grid, row, col+1): + self.solve(row, col+1, oranges) + + # Make down move. + if can_make_move(self.grid, row+1, col): + self.solve(row+1, col, oranges) + # After both right and down paths are taken restore the value + # of the cell so that it will be used by its previous paths. + self.grid[row][col] = cell_value + + +def can_make_move(grid, row, col): + return (row >= 0 and row < len(grid)) and (col >= 0 and col < len(grid[0])) \ + and grid[row][col] != -1 + + +def main(): + grid = ast.literal_eval(input("field = ")) + s = Solution(grid=grid) + s.solve(0, 0, oranges=0) + print(s.max_oranges) + +if __name__ == "__main__": + main() \ No newline at end of file From 6519ca4b95e987036fb6075563938b5d247d9049 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Fri, 7 Jan 2022 22:07:10 +0530 Subject: [PATCH 15/22] added december 11 solution --- December-11/python3_ASHIK11ab.py | 76 ++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 December-11/python3_ASHIK11ab.py diff --git a/December-11/python3_ASHIK11ab.py b/December-11/python3_ASHIK11ab.py new file mode 100644 index 0000000..8159fda --- /dev/null +++ b/December-11/python3_ASHIK11ab.py @@ -0,0 +1,76 @@ +class Solution: + def __init__(self, nodes, target_node, passing_node): + # Initialise graph with no vertices and edges. + self.graph = [] + + for i in range(len(nodes)): + self.graph.append([]) + for _ in range(len(nodes)): + self.graph[i].append(0) + + # Create nodes in graph. + for node1, node2, weight in nodes: + # Following 0 indexing. + self.graph[node1][node2] = weight + self.graph[node2][node1] = weight + + self.no_nodes = len(nodes) + self.target_node = target_node + self.passing_node = passing_node + self.path = None + self.dist = 0 + self.solution_exist = False + self.visited = [False for _ in range(len(nodes))] + + + def dfs(self, starting_node, path=[], dist=0): + if starting_node == self.target_node and \ + self.passing_node in path: + path.append(starting_node) + # For first path found or if a new path with lesser distance + # is found then use the new path and distance. + if not self.solution_exist or dist < self.dist: + self.path = path[:] + self.dist = dist + self.solution_exist = True + + for node in range(starting_node, self.no_nodes): + self.visited[node] = True + for adj_node in range(self.no_nodes): + # For each unvisited adjacent node perform dfs to find target node. + if self.graph[node][adj_node] != 0 and not self.visited[adj_node]: + path.append(node) + dist += self.graph[node][adj_node] + self.dfs(adj_node, path, dist) + dist -= self.graph[node][adj_node] + path.pop() + + + def solve(self, starting_node): + self.dfs(starting_node) + if self.solution_exist: + print(f"\nShortest path = {self.dist}") + print("Path taken = ", end="") + for node in self.path: + print(node+1, end=" ") + else: + print("\nNo path found") + + +def main(): + no_nodes = int(input("N = ")) + lines = [] + print("\nP Q D") + for _ in range(no_nodes): + line = input() + (node1, node2, dist) = line.split(' ') + lines.append((int(node1)-1, int(node2)-1, int(dist))) + print() + starting_node = int(input("A = ")) - 1 + passing_node = int(input("B = ")) - 1 + target_node = int(input("C = ")) - 1 + s = Solution(lines, target_node=target_node, passing_node=passing_node) + s.solve(starting_node) + +if __name__ == "__main__": + main() \ No newline at end of file From 16cdca197cf24f831019758d8a3c42bf1bf12aa7 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sun, 9 Jan 2022 15:40:51 +0530 Subject: [PATCH 16/22] added solution december 12 --- December-12/python3_ASHIK11ab.py | 67 ++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 December-12/python3_ASHIK11ab.py diff --git a/December-12/python3_ASHIK11ab.py b/December-12/python3_ASHIK11ab.py new file mode 100644 index 0000000..e88266b --- /dev/null +++ b/December-12/python3_ASHIK11ab.py @@ -0,0 +1,67 @@ +import ast + +class Stack: + def __init__(self, contents=[]): + self.array = contents + + @property + def top(self): + if self.length == 0: + return None + return self.array[0] + + @property + def length(self): + return len(self.array) + + def push(self, element): + self.array.insert(0, element) + + def pop(self): + element = self.array.pop(0) + return element + + +class Solution: + def __init__(self, cars): + self.cars = cars + + + def solve(self): + stack1 = Stack(contents=self.cars) + stack2 = Stack() + + while stack1.length > 0: + if stack2.length == 0 or both_cars_in_same_direction(stack1, stack2): + # Pop from stack 1 and push to stack 2 + ele = stack1.pop() + stack2.push(ele) + + # Top most car in stack 1 is compared with topmost car in stack2. + # Second car is broken if it's power is less than that of first car. + elif abs(stack1.top) > abs(stack2.top): + stack2.pop() + # First car is broken if it's power is less than that of second car. + elif abs(stack1.top) < abs(stack2.top): + stack1.pop() + # Both the cars are broken if their powers are equal. + else: + stack1.pop() + stack2.pop() + + stack2.array.reverse() + print(stack2.array) + + +def both_cars_in_same_direction(stack1, stack2): + return (stack1.top < 0 and stack2.top < 0) \ + or (stack1.top > 0 and stack2.top > 0) + + +def main(): + cars = ast.literal_eval(input("cars = ")) + s = Solution(cars=cars) + s.solve() + +if __name__ == "__main__": + main() \ No newline at end of file From 3276bd59ddfa23e4b90ab89551656bf06704f662 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sun, 9 Jan 2022 19:49:02 +0530 Subject: [PATCH 17/22] added december 13 solution --- December-13/python3_ASHIK11ab.py | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 December-13/python3_ASHIK11ab.py diff --git a/December-13/python3_ASHIK11ab.py b/December-13/python3_ASHIK11ab.py new file mode 100644 index 0000000..4d2d540 --- /dev/null +++ b/December-13/python3_ASHIK11ab.py @@ -0,0 +1,74 @@ +class Solution: + def __init__(self): + self.diamonds_sold = 0 + + def solve(self, clients, diamonds, diamonds_sold_cnt=0): + # Store the count of previously sold diamonds if their value is + # greater than the aldready sold diamonds count. + self.diamonds_sold = max(self.diamonds_sold, diamonds_sold_cnt) + + # No further solution is possible when there are no clients to buy + # diamonds are when there are no diamonds to buy. + if len(clients) == 0 or len(diamonds) == 0: + return + + for (client_index, client) in enumerate(clients): + # Find diamonds which satisfy the clients requirement. + qualified_diamonds = find_qualified_diamonds(client, diamonds) + + remaining_clients = clients[:client_index] + clients[client_index+1:] + + # Continue with next client if there are no diamonds which satisfy + # the clients requirements. + if len(qualified_diamonds) == 0: + continue + + # Compute the solution by allowing client to choose all diamonds + # one by one and finding the best diamond which can be sold to + # the client to sell maximum number of diamonds among all clients. + for diamond_index in range(len(qualified_diamonds)): + # Remove the diamond chosen by client and compute the solution + # using the remaining diamonds. + remaining_diamonds = qualified_diamonds[:diamond_index] + qualified_diamonds[diamond_index+1:] + + self.solve(remaining_clients, remaining_diamonds, diamonds_sold_cnt+1) + + +def find_qualified_diamonds(client_req, diamonds): + qualified_diamonds = [] + for diamond in diamonds: + # A diamond is qualified if its purity level is greater than client + # requirement purity level and if the diamond price is lesser than or equal + # to the client's requirement price. + if diamond[0] > client_req[0] and diamond[1] <= client_req[1]: + qualified_diamonds.append(diamond) + return qualified_diamonds + + +def main(): + z = int(input("z = ")) + x = int(input("x = ")) + clients_req = [] + diamonds = [] + + print() + for i in range(z): + client_purity_req = int(input(f"k{i} = ")) + client_budget = int(input(f"r{i} = ")) + client_req = (client_purity_req, client_budget) + clients_req.append(client_req) + + print() + for i in range(x): + diamond_purity = int(input(f"m{i} = ")) + diamond_price = int(input(f"n{i} = ")) + diamond_spec = (diamond_purity, diamond_price) + diamonds.append(diamond_spec) + + s = Solution() + s.solve(clients_req, diamonds) + print(f"\nMaximum number of diamonds sold = {s.diamonds_sold}") + + +if __name__ == "__main__": + main() \ No newline at end of file From f23e0dae8efbe3b769c94217f20ccab0fce7c5c0 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Sun, 9 Jan 2022 20:22:00 +0530 Subject: [PATCH 18/22] added december 14 solution --- December-14/python3_ASHIK11ab.py | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 December-14/python3_ASHIK11ab.py diff --git a/December-14/python3_ASHIK11ab.py b/December-14/python3_ASHIK11ab.py new file mode 100644 index 0000000..9dba102 --- /dev/null +++ b/December-14/python3_ASHIK11ab.py @@ -0,0 +1,37 @@ +import ast + +class Solution: + def __init__(self, chapters): + self.chapters = chapters + + def solve(self, deadline): + concepts_to_study = 1 + while True: + req_hours = 0 + for concepts in self.chapters: + req_time = find_req_time_to_study(concepts, concepts_to_study) + req_hours += req_time + if req_hours <= deadline: + print(concepts_to_study) + break + # Increase the value of `y` if the current value does not fit it. + concepts_to_study += 1 + + +def find_req_time_to_study(concepts, concepts_to_study): + hours_cnt = 0 + while concepts > 0: + concepts = concepts - concepts_to_study + hours_cnt += 1 + return hours_cnt + + +def main(): + chapters = ast.literal_eval(input("chapter = ")) + deadline = int(input("x = ")) + s = Solution(chapters=chapters) + s.solve(deadline) + + +if __name__ == "__main__": + main() \ No newline at end of file From 574b7c9b6c73870b40f12727777e2ba575d0f213 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Mon, 10 Jan 2022 15:01:32 +0530 Subject: [PATCH 19/22] added solution december 15 --- December-15/python3_ASHIK11ab.py | 59 ++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 December-15/python3_ASHIK11ab.py diff --git a/December-15/python3_ASHIK11ab.py b/December-15/python3_ASHIK11ab.py new file mode 100644 index 0000000..e7bf132 --- /dev/null +++ b/December-15/python3_ASHIK11ab.py @@ -0,0 +1,59 @@ +import ast + +class Solution: + def __init__(self, orders, deadline): + self.orders = orders + self.deadline = deadline + + + def solve(self): + # `y` -> no of bracelets to be made in a day to deliver all orders + # on time. The starting value of `y` will be >= maximum element in + # the orders list since, no order can be half complete on the end + # of a day. + y = max(self.orders) + + while True: + cont_sub_arrays = find_cont_sub_arrays(self.orders[:], y) + if len(cont_sub_arrays) <= self.deadline: + print(y) + break + else: + y += 1 + + +def find_cont_sub_arrays(array, y): + """ Returns the continuous sub arrays where each sub array sum is <= y. """ + cont_sub_arrays = [] + outer_index = 0 + + while array != [] : + temp = [] + for j in range(len(array)): + cont_sub_array = array[:j+1] + # Store the continuous sub arrays temporarily since we + # are only intrested in the longest continuous sub array + # whose sum is <= y. + if sum(cont_sub_array) <= y: + temp = cont_sub_array + # If a valid sub array found is the last sub array of the array + # then add it to the list of sub arrays. + if j+1 == len(array): + cont_sub_arrays.append(temp) + array = array[j+1:] + else: + cont_sub_arrays.append(temp) + array = array[j:] + break + return cont_sub_arrays + + +def main(): + orders = ast.literal_eval(input("number of bracelets = ")) + no_of_days = int(input("n = ")) + s = Solution(orders=orders, deadline=no_of_days) + s.solve() + + +if __name__ == "__main__": + main() \ No newline at end of file From 4f1016a5bbe167f79458b6aa3b08fac3034058fc Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Mon, 10 Jan 2022 15:05:02 +0530 Subject: [PATCH 20/22] updated getting input december 01 --- December-01/python3_ASHIK11ab.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/December-01/python3_ASHIK11ab.py b/December-01/python3_ASHIK11ab.py index daa72ea..ff8d93e 100644 --- a/December-01/python3_ASHIK11ab.py +++ b/December-01/python3_ASHIK11ab.py @@ -1,3 +1,5 @@ +import ast + def find_intersection(necklase_A, necklase_B): candidate_stones = "" # Maintain small string as first for performing intersection. @@ -9,16 +11,9 @@ def find_intersection(necklase_A, necklase_B): candidate_stones += stone return candidate_stones -def get_input(): - """ Parses the string input and returns list of necklases. """ - arr = input("arr = ") - arr = arr[1:len(arr)-1] - arr = arr.replace(' ', '').split(',') - arr = [ele[1:len(ele)-1] for ele in arr] - return arr def main(): - necklases = get_input() + necklases = ast.literal_eval(input("arr = ")) facelifts = "" index = 0 while index < len(necklases) - 1: From 0800cf90f7eb39ae5420abe1100ab99140f87c6a Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Mon, 10 Jan 2022 15:22:50 +0530 Subject: [PATCH 21/22] updated getting input december 7 --- December-07/python3_ASHIK11ab.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/December-07/python3_ASHIK11ab.py b/December-07/python3_ASHIK11ab.py index 80a2a0b..dcbd3eb 100644 --- a/December-07/python3_ASHIK11ab.py +++ b/December-07/python3_ASHIK11ab.py @@ -70,12 +70,11 @@ def main(): planes_cnt = int(input("Number of jet-planes: ")) planes = [] for index in range(planes_cnt): - line = input() + choice = input("(x or y): ") + jet_direction = int(input(f"{choice} = ")) + jet_info = [choice, jet_direction] # jet_info -> list having the jet direction ('x' or 'y') and # the point on which the jet is moving. - jet_info = line.split(' ') - jet_info.remove('=') - jet_info[1] = int(jet_info[1]) planes.append(jet_info) s = Solution(buildings, planes) print("\nBuildings that received food:") From 8f30a97bd4489a177dba073b82068bcb211821d6 Mon Sep 17 00:00:00 2001 From: Ashik Meeran Mohideen Date: Mon, 10 Jan 2022 15:25:28 +0530 Subject: [PATCH 22/22] updated getting input december 09 --- December-09/python3_ASHIK11ab.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/December-09/python3_ASHIK11ab.py b/December-09/python3_ASHIK11ab.py index 108936e..6d19111 100644 --- a/December-09/python3_ASHIK11ab.py +++ b/December-09/python3_ASHIK11ab.py @@ -1,3 +1,5 @@ +import ast + class Solution: def __init__(self, no_understudies, req_understudies, abilities): self.no_understudies = no_understudies @@ -32,18 +34,10 @@ def combinations(array, k, candidates=[], result=[]): return result -def get_understudies_ability(): - array = input("N = ") - array = array[1:len(array)-1] - array = array.replace(' ', '').split(',') - understudies_ability = [int(ele) for ele in array] - return understudies_ability - - def main(): no_understudies = int(input("N = ")) req_understudies = int(input("P = ")) - understudies_ability = get_understudies_ability() + understudies_ability = ast.literal_eval(input("N = ")) s = Solution(no_understudies, req_understudies, understudies_ability) s.solve()