diff --git a/Q1/solution.py b/Q1/solution.py index 4429a19..598b8cb 100644 --- a/Q1/solution.py +++ b/Q1/solution.py @@ -1 +1,24 @@ ## Add code below with answer clearly stated +def factorial(n): + if n==1: + return 1 + else: + temp = 1 + for i in range(1, n): + temp *= i + return temp + +def sum_factorial(n): + if n < 1: + print("Invalid Value!!!") + return None + if n == 1: + return 1 + else: + fact_n = factorial(n) + temp = 0 + for i in str(fact_n): + temp += int(i) + return temp + +print("The sum of 100! is ", sum_factorial(100)) \ No newline at end of file diff --git a/Q2/solution.py b/Q2/solution.py index dd114fb..8a11536 100644 --- a/Q2/solution.py +++ b/Q2/solution.py @@ -1,8 +1,83 @@ # Definition for singly-linked list. -# class ListNode: -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next +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]: + def __init__(self) -> None: + pass + def creat_LList(self, arr: list) -> ListNode: + if arr == []: + print("Invalid Entry") + else: + head = ListNode(arr[0]) + for i in arr[1:]: + last_node = head + while(last_node.next): + last_node = last_node.next + last_node.next = ListNode(i) + return head + + def display(self, head: ListNode) -> None: + temp = head + print("[", end="") + while (temp): + if temp.next: + print(temp.val, end=",") + else: + print(temp.val, end="") + temp = temp.next + print("]") + + def remove_nth_value(self, head: ListNode, n: int) -> ListNode: + if n < 1: + print("Invalid Entry") + else: + counter = 1 + temp = head + while counter < n - 1: + temp = temp.next + counter += 1 + if temp == None: + return head + temp.next = temp.next.next + return head + + def reverse_LL(self, head: ListNode) -> ListNode: + current_node = head + prev = None + while current_node: + temp = current_node.next + current_node.next = prev + prev = current_node + current_node = temp + return prev + + + def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: + if head.next == None: + return [] + else: + head_node = self.reverse_LL(head) + head_node = self.remove_nth_value(head_node, n) + head_node = self.reverse_LL(head_node) + return head_node +if __name__ == '__main__': + ll = Solution() + list = [1, 2, 3, 4, 5] + head_node = ll.creat_LList(list) + ll.display(head_node) + head_node = ll.removeNthFromEnd(head_node, 2) + ll.display(head_node) + list = [1] + head_node = ll.creat_LList(list) + ll.display(head_node) + head_node = ll.removeNthFromEnd(head_node, 1) + ll.display(head_node) + list = [1, 2] + head_node = ll.creat_LList(list) + ll.display(head_node) + head_node = ll.removeNthFromEnd(head_node, 1) + ll.display(head_node) diff --git a/Q3/solution.py b/Q3/solution.py index 18ab591..de5b9e5 100644 --- a/Q3/solution.py +++ b/Q3/solution.py @@ -1,3 +1,27 @@ + class Solution: - def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: - + def __init__(self) -> None: + pass + + def findMedianSortedArrays(self, nums1: list, nums2: list) -> float: + nums1.extend(nums2) + n = sorted(nums1) + index = int((len(n) + 1)/ 2) + if len(n) % 2 == 0: + index2 = index + 1 + median_value = (n[index-1] + n[index2-1])/2 + else: + median_value = n[index - 1] + return median_value + +if __name__ == '__main__': + sol = Solution() + num1 = [1, 3] + num2 = [2] + print(sol.findMedianSortedArrays(num1, num2)) + num1 = [1, 2] + num2 = [3, 4] + print(sol.findMedianSortedArrays(num1, num2)) + num1 = [1, 2, 4, 6] + num2 = [3, 6, 7, 8] + print(sol.findMedianSortedArrays(num1, num2)) \ No newline at end of file