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

Muhammad Shariq : All three Recruitment Tasks' Solutions #10

Open
wants to merge 1 commit 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
23 changes: 23 additions & 0 deletions Q1/solution.py
Original file line number Diff line number Diff line change
@@ -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)

68 changes: 62 additions & 6 deletions Q2/solution.py
Original file line number Diff line number Diff line change
@@ -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()
34 changes: 32 additions & 2 deletions Q3/solution.py
Original file line number Diff line number Diff line change
@@ -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))