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

done with q1,q2,q3 #29

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 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 26 21:33:58 2022

@author: sahab
"""

## Add code below with answer clearly stated

def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n-1)

def sum_of_digit(n):
return 0 if n == 0 else int(n % 10) + sum_of_digit(int(n / 10))

x = sum_of_digit(factorial(10))
assert x == 27

x = sum_of_digit(factorial(100))
assert x == 675
16 changes: 15 additions & 1 deletion Q2/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,18 @@
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:

if self.head is None:
return
index = 0
current = self.head

while current.next and index < n:
previous = current
current = current.next
index += 1

if index == 0:
self.head = self.head.next
else:
previous.next = current.next

22 changes: 22 additions & 0 deletions Q3/solution.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
le1, le2 = len(nums1), len(nums2)
total = le1 + le2
half = total // 2
if le1 < le2:
return self.findMedianSortedArrays(nums2, nums1)
l, r = 0, len(nums1) - 1
while True:
i = (l + r) // 2
j = half - i - 2

aleft = nums1[i]
aright = nums1[i+1]
bleft = nums1[j]
bright = nums1[j+1]

if aleft <= bright and bleft <= aright:
if total % 2 == 0:
return min(aright, bright)
return (max(aleft, bleft) + min(aright, bright)) / 2
elif aleft > bright:
r = i - 1
else:
l = i + 1