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

Coding challenges for code review #8

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
def add_string_numbers(s1, s2):
"""
a function for adding two string numbers together and returning the result as a string
takes in s1 and s2 strings
returns final, a string (sum of strings)

"""

#check if atleast one of the numbers are floats so we can convert
if "." not in s1 and "." in s2:
s1 += "."
if "." not in s2 and "." in s1:
s2 += "."

if s1 or s2:
#check to see how many numbers come after the decimal
for i in range(len(s1)):
if s1[i] == ".":
savedigits1 = len(s1[i+1:(len(s1)-1)])
break

for i in range(len(s2)):
if s2[i] == ".":
savedigits2 = len(s1[i+1:(len(s2)-1)])
break

#check which one has the longest decimal
if savedigits1>savedigits2:
diff = savedigits1 - savedigits2
for i in range(diff):
s2 += "0"
elif savedigits2>savedigits1:
diff = savedigits2 - savedigits1
for i in range(diff):
s1 += "0"
carry = 0
result = []
# for i in s1[::-1]:
# for h in s2[::-1]:
#Calculate backwards with carry
for i in range(len(s1)-1,-1,-1):
if s1[i]==".":
result.insert(0,'.')
continue

num1 = int(s1[i])
num2 = int(s2[i])

calc = num1 + num2 +carry
carry = calc//10
result.insert(0,str(calc%10))
if carry:
result.insert(0, str(carry))
final = ''.join(result)

return final
16 changes: 16 additions & 0 deletions src/brain_teasers/python/subarray_sum/src/subarray_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def subarray_sum(array, start, end):
"""
function that iterates through the subarray and calculates the sum of elements
returns (ans) an interger (sum of sub array)
"""
# get indexes of the start and end
storeStartId = array.index(start)
storeEndId = array.index(end)
ans = 0
# add the numbers
for i in range(start, end):
if array[i] == start:
continue
else:
ans += array[i]
return ans
58 changes: 58 additions & 0 deletions src/dynamic_programming/python/fibonacci/src/fib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
def fib_basic(n):

if n <= 0:
return 0
elif n == 1:
Expand All @@ -8,6 +9,7 @@ def fib_basic(n):


def fib_memo(n, memo=dict()):

if n < 1:
return 0
if n <= 2:
Expand All @@ -16,6 +18,7 @@ def fib_memo(n, memo=dict()):


def fib_tab(n):

if n < 1:
return 0

Expand All @@ -29,3 +32,58 @@ def fib_tab(n):
table[i] = table[i - 1] + table[i - 2]

return table[n]

####
### Gloire
def fib_basic(n):
"""
This is a recursive function that finds the nth number in the Fibonacci sequence by recursively
calculating the
(n-1) and (n-2)th numbers in the sequence
returns fib(n)
"""
if n == 0 :
return 0
elif n == 1:
return 1
else:
return fib_basic(n-1)+ fib_basic(n-2)

#fibdic = {}
def fib_memo(n, memo = {}):
"""
This function uses memoization to store the results of subproblems and avoid recalculating them.
It works similarly to the fib_basic() function, but it uses a dictionary to store the results of subproblems
so that they can be reused in future recursive calls.
returns the fib(n)

"""
if n == 0 :
return 0
elif n == 1:
return 1
try:
return memo[n]
except KeyError:
result = fib_memo(n-1, memo) + fib_memo(n-2, memo)
memo[n] = result
return result

def fib_tab(n):
"""
This function uses dynamic programming to store the results of subproblems in a table.
It iteratively calculates the nth number in the Fibonacci sequence by using the values of the (n-1)th
and $(n - 2)th $numbers in the sequence
returns a list of fib numbers
"""

if n<= 1:
return n

table = [0] * (n+1)
table[1] = 1

for i in range(2,n+1):
table[i] = table[i-1]+table[i-2]

return table
11 changes: 11 additions & 0 deletions src/sorting/python/insertion_sort/src/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def insertionsort(li):
"""
Sorts items in a list (li) and returns a sorted list(ascending order)

"""
for i in range(1, len(li)):
k = i
while li[k-1]>li[k] and k>0:
li[k],li[k-1] = li[k-1],li[k]
k -= 1
return li
10 changes: 10 additions & 0 deletions src/sorting/python/selection_sort/src/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def selectionsort_v1(li):
"""
Sorts items in a list (li) and returns a sorted list (li)(ascending order)
"""
for i in range(len(li)):
minid = i
for j in range(i+1,len(li)):
minid = j
li[i],li[minid] = li[minid],li[i]
return li
Loading