diff --git a/Q1/solution.py b/Q1/solution.py index 4429a19..fd5ca2a 100644 --- a/Q1/solution.py +++ b/Q1/solution.py @@ -1 +1,33 @@ -## Add code below with answer clearly stated +import math + + +def sumOfFac(number): + + value = [] + value.append(1) + + for i in range(1, number+1): + getCalcFac(value, i) + + sum = 0 + for i in range(len(value)): + sum += value[i] + return sum + + +def getCalcFac(value, j ): + carry = 0 + + for i in range(len(value)): + result = carry + value[i] * j + value[i] = result % 10 + carry = result //10 + + while carry != 0: + value.append(carry % 10) + carry = carry // 10 + + +if __name__ == "__main__": + print(str(math.factorial(100))) + print(sumOfFac(100)) diff --git a/Q2/solution.py b/Q2/solution.py index dd114fb..90baaa5 100644 --- a/Q2/solution.py +++ b/Q2/solution.py @@ -1,8 +1,56 @@ -# Definition for singly-linked list. -# class ListNode: -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next +from typing import Optional + +class ListNode: + + def __init__(self, val = 0, next = None) -> None: + self.value = 0 + self.next = next + class Solution: - def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + + def __init__(self) -> None: + self.head = None + def insert(self, value): + node = ListNode(val = value) + node.next = self.head + node.value = value + self.head = node + return node + + def removeNthFromEnd(self, head : Optional[ListNode], n:int) ->Optional[ListNode]: + start =self.head + end = self.head + + """Finding relevant index""" + for i in range(n): + if end.next == None: + print("here") + if i == n-1: + self.head = self.head.next + end = end.next + + """Resetting List""" + while(end.next != None): + end = end.next + start = start.next + start.next = start.next.next + + + """Printing""" + start = self.head + while(start != None): + print(start.value, " ") + start = start.next + + +if __name__ == "__main__": + list_link = Solution() + n1 = list_link.insert(1) + n2 =list_link.insert(2) + n3 =list_link.insert(3) + n4 =list_link.insert(4) + n5 =list_link.insert(5) + list_link.removeNthFromEnd([n1,n2,n3,n4,n5], 3) + + diff --git a/Q3/solution.py b/Q3/solution.py index 18ab591..594dbfe 100644 --- a/Q3/solution.py +++ b/Q3/solution.py @@ -1,3 +1,29 @@ +from tkinter import W + + class Solution: - def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + + def findMedianSortedArrays(self, nums1, nums2) -> float: + nums3 = nums1 + nums2 + nums3.sort() + + odd_or_even = len(nums3) + + if odd_or_even % 2 == 0: + half = odd_or_even //2 + even = nums3[half] + even_prior = nums3[half - 1] + return (even + even_prior) /2 + + else: + half = odd_or_even //2 + return nums3[half] + + +if __name__ == "__main__": + + sol = Solution() + print(sol.findMedianSortedArrays([1,3],[2])) + print(sol.findMedianSortedArrays([1,2],[3,4])) +