Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for Tasks of Kodifly Recruitment Task. #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 33 additions & 1 deletion Q1/solution.py
Original file line number Diff line number Diff line change
@@ -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))
60 changes: 54 additions & 6 deletions Q2/solution.py
Original file line number Diff line number Diff line change
@@ -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)


28 changes: 27 additions & 1 deletion Q3/solution.py
Original file line number Diff line number Diff line change
@@ -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]))