Skip to content
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
179 changes: 179 additions & 0 deletions polynomial_expansion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import re

# def polynomial_expansion(strParam):
# letter= re.search("[a-z]|[A-Z]", strParam).group()
# m1= strParam.split(')(')
# m2= []
# tempsol= []
# for i,j in enumerate(m1):
# ns= j.replace('(', '').replace(')', '')
# m1[i]= ns

# for i in m1:
# t1= re.match("(-|/+|)\d+\w(\^)(-|/+|)\d+", i)
# if t1 == None:
# t1= re.match("(-|/+|)\d+\w", i)
# if t1 == None:
# t1= re.match("(-|/+|)\d+", i)

# t2= i.replace(t1.group(), '')
# if t2 != '':
# m2.append([t1.group(), t2])
# else:
# m2.append([t1.group()])

# for i in m2[0]:
# if re.match("(-|/+|)\d+\w(\^)(-|/+|)\d+", i):
# td2= int(re.search("((-|/+|)\d+)$", i).group())
# td1= int(re.match("(-|/+|)\d+", i).group())
# elif re.match("(-|/+|)\d+\w", i):
# td1= int(re.match("(-|/+|)\d+", i).group())
# td2= 1
# else:
# td1= int(i)
# td2= 0

# for j in m2[1]:
# if re.match("(-|/+|)\d+\w(\^)(-|/+|)\d+", j):
# ed2= int(re.search("((-|/+|)\d+)$", j).group())
# ed1= int(re.match("(-|/+|)\d+", j).group())
# elif re.match("(-|/+|)\d+\w", j):
# ed1= int(re.match("(-|/+|)\d+", j).group())
# ed2= 1
# else:
# ed1= int(j)
# ed2= 0

# ttd1= ed1 * td1
# ttd2= ed2 + td2
# tempsol.append([ttd1, ttd2])

# for i,j in enumerate(tempsol):
# for k, l in enumerate(tempsol):
# if j[1] == l[1] and k != i:
# tempsol[i][0]= tempsol[i][0] + tempsol[k][0]
# tempsol.pop(k)
# break

# def sortcriteria(e):
# return e[1]
# tempsol.sort(key=sortcriteria, reverse=True)

# solu= ""
# for i in tempsol:
# if solu != "" and i[0] > 0:
# solu= solu + "+"
# if i[1] > 1 or i[1] < 0:
# if i[0] == 1:
# solu= solu + letter + "^" + str(i[1])
# else:
# solu= solu + str(i[0]) + letter + "^" + str(i[1])
# elif i[1] == 1:
# if i[0] == 1:
# solu= solu + letter
# else:
# solu= solu + str(i[0]) + letter
# elif i[1] == 0:
# solu= solu + str(i[0])



# print(m1)
# print(m2)
# print(tempsol)
# print(solu)


def polynomial_expansion(strParam):
letter= re.search("[a-z]|[A-Z]", strParam).group()
m1= strParam.split(')(')
m2= []
tempsol= []
for i,j in enumerate(m1):
ns= j.replace('(', '').replace(')', '')
m1[i]= ns

for i in m1:
term= i
termlist= []

while term != "":
t= re.match("(-|\+|)\d+\w(\^)(-|/+|)\d+", term)
if t == None:
t= re.match("(-|\+|)\d+\w", term)
if t == None:
t= re.match("(-|\+|)\d+", term)
termlist.append(t.group())
term= term.replace(t.group(), '', 1)

m2.append(termlist)

for i in m2[0]:
if re.match("(-|\+|)\d+\w(\^)(-|/+|)\d+", i):
td2= int(re.search("((-|\+|)\d+)$", i).group())
td1= int(re.match("(-|\+|)\d+", i).group())
elif re.match("(-|\+|)\d+\w", i):
td1= int(re.match("(-|\+|)\d+", i).group())
td2= 1
else:
td1= int(i)
td2= 0

for j in m2[1]:
if re.match("(-|\+|)\d+\w(\^)(-|/+|)\d+", j):
ed2= int(re.search("((-|\+|)\d+)$", j).group())
ed1= int(re.match("(-|\+|)\d+", j).group())
elif re.match("(-|\+|)\d+\w", j):
ed1= int(re.match("(-|\+|)\d+", j).group())
ed2= 1
else:
ed1= int(j)
ed2= 0

ttd1= ed1 * td1
ttd2= ed2 + td2
tempsol.append([ttd1, ttd2])

for i,j in enumerate(tempsol):
for k, l in enumerate(tempsol):
if j[1] == l[1] and k != i:
tempsol[i][0]= tempsol[i][0] + tempsol[k][0]
tempsol.pop(k)
break

def sortcriteria(e):
return e[1]
tempsol.sort(key=sortcriteria, reverse=True)

solu= ""
for i in tempsol:
if solu != "" and i[0] > 0:
solu= solu + "+"
if i[1] > 1 or i[1] < 0:
if i[0] == 1:
solu= solu + letter + "^" + str(i[1])
else:
solu= solu + str(i[0]) + letter + "^" + str(i[1])
elif i[1] == 1:
if i[0] == 1:
solu= solu + letter
else:
solu= solu + str(i[0]) + letter
elif i[1] == 0:
solu= solu + str(i[0])



print(m1)
print(m2)
print(tempsol)
print(solu)



# polynomial_expansion("(1x)(2x^-2+1)")
# polynomial_expansion("(2x^2+4)(6x^3+3)")
# polynomial_expansion("(2x^2+4)(6x^2+3)")
# polynomial_expansion("(1x^5-4x^2+3)(2x^2+3)")
# polynomial_expansion("(1x)(2x^-2+1)")
polynomial_expansion("(-1p^1+3)(-1p^2-1p^2)")
16 changes: 16 additions & 0 deletions week15/kayode/gas_station.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
Author: Kayode
---

Gas Station
Have the function GasStation(strArr) take strArr which will be an an array consisting of the following elements: N which will be the number of gas stations in a circular route and each subsequent element will be the string g:c where g is the amount of gas in gallons at that gas station and c will be the amount of gallons of gas needed to get to the following gas station.

For example strArr may be: ["4","3:1","2:2","1:2","0:1"]. Your goal is to return the index of the starting gas station that will allow you to travel around the whole route once, otherwise return the string impossible. For the example above, there are 4 gas stations, and your program should return the string 1 because starting at station 1 you receive 3 gallons of gas and spend 1 getting to the next station. Then you have 2 gallons + 2 more at the next station and you spend 2 so you have 2 gallons when you get to the 3rd station. You then have 3 but you spend 2 getting to the final station, and at the final station you receive 0 gallons and you spend your final gallon getting to your starting point. Starting at any other gas station would make getting around the route impossible, so the answer is 1. If there are multiple gas stations that are possible to start at, return the smallest index (of the gas station). N will be >= 2.


Examples
Input: ["4","1:1","2:2","1:2","0:1"]
Output: impossible

Input: ["4","0:1","2:2","1:2","3:1"]
Output: 4
42 changes: 42 additions & 0 deletions week15/kayode/gas_station.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def gas_station(arr):
stops= int(arr.pop(0))
route= []
result1= []
result2= []
for i in arr:
s= i.split(':')
route.append([int(j) for j in s])

for i,j in enumerate(route):
fuel= 0
run= 0

for k in range(stops):
ind= i + k
if ind >= stops:
ind= ind - stops
fuel= route[ind][0] + fuel
if route[ind][1] <= fuel:
fuel= fuel - route[ind][1]
run= run + 1
else:
result1.append([i, False])
break


if run == stops:
result1.append([i, True])

for i in result1:
if i[1]:
result2.append(i[0] + 1)

if result2 == []:
return print("impossible")
else:
return print(min(result2))


gas_station(['4', '3:1', '2:2', '1:2', '0:1'])
# gas_station(["4","1:1","2:2","1:2","0:1"])
# gas_station(["4","0:1","2:2","1:2","3:1"])
17 changes: 17 additions & 0 deletions week16/kayode/polynomial_expansion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
Author: Kayode
---

Polynomial Expansion

Have the function PolynomialExpansion(str) take str which will be a string representing a polynomial containing only (+/-) integers, a letter, parenthesis, and the symbol "^", and return it in expanded form. For example: if str is "(2x^2+4)(6x^3+3)", then the output should be "12x^5+24x^3+6x^2+12". Both the input and output should contain no spaces. The input will only contain one letter, such as "x", "y", "b", etc. There will only be four parenthesis in the input and your output should contain no parenthesis. The output should be returned with the highest exponential element first down to the lowest.

More generally, the form of str will be: ([+/-]{num}[{letter}[{^}[+/-]{num}]]...[[+/-]{num}]...)(copy) where "[]" represents optional features, "{}" represents mandatory features, "num" represents integers and "letter" represents letters such as "x".


Examples
Input: "(1x)(2x^-2+1)"
Output: x+2x^-1

Input: "(-1x^3)(3x^3+2)"
Output: -3x^6-2x^3