Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions December-01/python3_ASHIK11ab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import ast

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 = ast.literal_eval(input("arr = "))
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()
27 changes: 27 additions & 0 deletions December-02/python3_ASHIK11ab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 = int(input("n = "))
# 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()
78 changes: 78 additions & 0 deletions December-03/python3_ASHIK11ab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import ast

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

print(self.solution_reached)


def main():
board = ast.literal_eval(input())
name = input("name = ")
obj = Solution(board, name)
obj.solve()


if __name__ == "__main__":
main()
53 changes: 53 additions & 0 deletions December-05/python3_ASHIK11ab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 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()
28 changes: 28 additions & 0 deletions December-06/python3_ASHIK11ab.py
Original file line number Diff line number Diff line change
@@ -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()
84 changes: 84 additions & 0 deletions December-07/python3_ASHIK11ab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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):
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.
planes.append(jet_info)
s = Solution(buildings, planes)
print("\nBuildings that received food:")
s.solve()

if __name__ == "__main__":
main()
31 changes: 31 additions & 0 deletions December-08/python3_ASHIK11ab.py
Original file line number Diff line number Diff line change
@@ -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()
45 changes: 45 additions & 0 deletions December-09/python3_ASHIK11ab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import ast

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 main():
no_understudies = int(input("N = "))
req_understudies = int(input("P = "))
understudies_ability = ast.literal_eval(input("N = "))
s = Solution(no_understudies, req_understudies, understudies_ability)
s.solve()

if __name__ == "__main__":
main()
Loading