From e8a6316fb0c8b24981b1e5a44e07ed386355c6c1 Mon Sep 17 00:00:00 2001 From: MShariqShoaib <197shariq@gmail.com> Date: Sun, 18 Sep 2022 18:57:03 +0500 Subject: [PATCH] recruitmentTest: all three solutions added --- Q1/solution.py | 23 +++++++++++++++++ Q2/solution.py | 68 +++++++++++++++++++++++++++++++++++++++++++++----- Q3/solution.py | 34 +++++++++++++++++++++++-- 3 files changed, 117 insertions(+), 8 deletions(-) diff --git a/Q1/solution.py b/Q1/solution.py index 4429a19..62bc63b 100644 --- a/Q1/solution.py +++ b/Q1/solution.py @@ -1 +1,24 @@ ## Add code below with answer clearly stated + +#TASK 1 +# Finding factorial using recursive calls + +def calculate_factorial(num): + if num != 1: + return num * calculate_factorial(num-1) + else: + return 1 #Base Case + +# Printing factorial of 100 +fact_100 = calculate_factorial(100) +print('Factorial of 100 is : ',fact_100) + +#TASK 2 +#sum of digits in fact_100 using division + +sum_fact = 0 +while (fact_100 != 0): + sum_fact = sum_fact + (fact_100 % 10) #adding remainder + fact_100 = fact_100//10 # integeral division clip last digit +print(sum_fact) + \ No newline at end of file diff --git a/Q2/solution.py b/Q2/solution.py index dd114fb..bf2d922 100644 --- a/Q2/solution.py +++ b/Q2/solution.py @@ -1,8 +1,64 @@ # Definition for singly-linked list. -# class ListNode: -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next -class Solution: - def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + +class ListNode: + def __init__(self, val=0, next=None): + if val >= 0 and val <= 100: + self.val = val + self.next = next + +class LinkedList: + def __init__(self): + self.head = None + def display(self): + temp = self.head + temp_2 = [] + while temp: + temp_2.append(temp.val) + temp = temp.next + print(temp_2) + + def removeNthFromEnd(self, n: int): + if n > 1: + temp = self.head + node_stack = [] + counter = 0 + while temp: + counter += 1 + node_stack.append(temp) + temp = temp.next + next_node = node_stack.pop() + cur_node = next_node + if n < counter: + for i in range(n-1): + next_node = cur_node + cur_node = node_stack.pop() + node_stack.pop().next = next_node + + def make_ll(self, num_list): + node_stack = [] + if len(num_list) <= 30 : + for i in val: + cur_node = ListNode(i) + ll.head = cur_node if (ll.head == None) else ll.head + node_stack.append(cur_node) + + cur_node = None + next_node = node_stack.pop() + for i in range(len(node_stack)): + cur_node = node_stack.pop() + cur_node.next = next_node + next_node = cur_node + + +val = [1, 2, 3, 4, 5] +k = 2 +ll = LinkedList() +ll.make_ll(val) + +print("Input Linked list:"); +ll.display() +ll.removeNthFromEnd(k) + +print("\nOutput:"); +ll.display() diff --git a/Q3/solution.py b/Q3/solution.py index 18ab591..b51e2f3 100644 --- a/Q3/solution.py +++ b/Q3/solution.py @@ -1,3 +1,33 @@ -class Solution: - def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: +list_1 = [1,2] +list_2 = [3,4,5] + +def findMedianSortedArrays(list_1, list_2): + n = len(list_1) + m = len(list_2) + j = 0 + k = 0 + sorted_list = [] + + for i in range(m+n): + if j >= n and k < m: + sorted_list = sorted_list + (list_2[k:]) + break + elif k >= m and j < n: + sorted_list = sorted_list + (list_1[j:]) + break + elif list_1[j] <= list_2[k]: + sorted_list.append(list_1[j]) + j = j + 1 + else: + sorted_list.append(list_2[k]) + k = k + 1 + + index = len(sorted_list)//2 + print(sorted_list) + if len(sorted_list)%2 == 0: + return (sorted_list[index] + sorted_list[index-1])/2 + else: + return sorted_list[index] + +print(findMedianSortedArrays(list_1, list_2)) \ No newline at end of file