Return a list of vegans - python-3.x

def vegan(something):
list of Foods is returned
data = []
for line in something:
if line.is_vegan == True:
data.append(line)
return data
How can I make this into a recursive function, anything i try just makes it worse, this is the older function.

A simple version could be:
def veggies(foods):
if not foods:
return []
if foods[0].is_vegetarian:
return [foods[0]] + veggies(foods[1:])
return veggies(foods[1:])
Basically you treat the first element, then pass the rest to the next call of the function, until there is no element.

Here is another way
f = open("foods.txt",'r')
def doStuff(f):
line = f.readline()
if line: # While there are lines to read, readline
if "True" in line:
print(line)
# Do formatting and storing into object here
doStuff(f)
doStuff(f)

Hope this helps,
veggie= []
counter = 0
def foodie(counter, foods):
if counter < len(foods):
if foods[counter].split("|")[2] == 'True':
veggie.append(foods[counter])
counter = counter + 1
foodie(counter, foods)
else:
return;
foodie(foods)
Thanks,

Related

CODEFORCES 1744B, how could I fix runtime error?

I'm trying to solve the next problem: https://codeforces.com/contest/1744/problem/B
When I run the code in my terminal, it works with the example given in the exercise; but it doesn't when I submit it in CodeForces, it cause a runtime error which I cannot figure out.
def increment(arr, length, option, add):
for i in range(length):
if(option == '0') and (arr[i]%2 == 0):
arr[i] += add
elif(option == '1') and (arr[i]%2 != 0):
arr[i] += add
else:
pass
return arr
def main():
quantityOperation = int(input())
while quantityOperation > 0:
length, times = input().split()
length = int(length)
times = int(times)
arr = [int(x) for x in input().split()]
while times > 0:
opt, add = input().split()
add = int(add)
res = sum(increment(arr, length, opt, add))
print(res)
times -= 1
quantityOperation -= 1
main()
The loop inside your main function doesn't end. You should put quantityOperation -= 1 inside the while loop.
However, your code will still become time limit exceeded after fixing this. The correct idea is to precalculate odd and even sum and modify them according to the queries instead of looping all elements inside increment function every time. You can check the editorial for this problem.

For loop, if statement and Zip python

a= ("Kiran","Narut","Sasue"]
b= ["Kiran","Naruto","Sasuke"]
def quality_check(x,y):
for i,j in zip(x,y):
if i == j:
return "Good to go"
else:
return "wrong names"
quality_check(a, b) # Good to go
It shows unexpected result. The results are shown just for the first element of the lists.
As you are using return It will only check for the first elements.
A return statement is used to end the execution of the function, Replace this with print() will loop over all the elements.
You should use yield instead of return
a= ["Kiran", "Narut","Sasue"]
b= ["Kiran", "Naruto","Sasuke"]
def quality_check(x,y):
for i,j in zip(x,y):
if i == j:
yield "good to go"
else:
yield "wrong"
for element in quality_check(a, b):
print(element)
this will return required results
every time you yield from function it returns a iterable object
Your function quality_check() is returning after the if i == j statement in the first iteration of your for loop. That if statement is comparing "Kiran" == "Kiran" (which is True).
Try checking for only 'Wrong names' in the for loop. Then return 'Good to go' once the loop completes and no Wrong names have been found.
a = ["Kiran", "Narut","Sasue"]
b = ["Kiran", "Naruto","Sasuke"]
c = ["Kiran", "Narut","Sasue"]
def quality_check(x,y):
for i,j in zip(x,y):
if i != j:
return "Wrong names"
return "Good to go"
print(quality_check(a,b))
#> Wrong names
print(quality_check(a,c))
#> Good to go

why does memo[targetsum] = shortestcombination creats an error in my code

Hi I was doing some dynamic programing exercises and stubbled upon the Bestsum Problem where you are given a Targetnumber and a array of numbers and you have to find the shortest sum using the number in the array to get the Target number for example given the function bestsum(7,[1,2,4]) it schoud return [4,2,1]
def howsum(targetsum, numbers, memo = {}):
if targetsum in memo:
return memo[targetsum]
if targetsum == 0:
return []
if targetsum < 0:
return None
shortestcombination = None
for n in numbers:
remainder = targetsum - n
result = howsum(remainder, numbers, memo)
if result != None:
combination = []
combination = result
combination.append(n)
if shortestcombination == None or len(combination) < len(shortestcombination):
shortestcombination = combination
memo[targetsum] = shortestcombination
return shortestcombination
if __name__ == '__main__':
print(howsum(7, [1, 4, 2]))
Here is my code witch gives the solution right when I comment out memo[targetsum] = shortestcombination and i dont know what went wrong with my memoization so thanks in advance if you can help
I Found The problem and it has to do with how list operation in python works. The issue is in the line "combination.append(n)" append modifies the list in place which is what produces the bug in memo. so the solution is to use the syntax "combination = combination + [n]" so that the code creates a new list and it assigns it to combination and That fixes the bug.
If you want more details here is another thread that explains += operator for list vs = +
Why does += behave unexpectedly on lists?

How to convert this nested string list into the actual list

How to convert string into new_list without using eval(), exec() or any other libraries?
string = '[1,2,3,[4,5,6],7,[8,[9,10]],11]'
new_list = [1,2,3,[4,5,6],7,[8,[9,10]],11]
The biggest problem you're facing with this question is that you cannot say how deep the list will nest, therefore the best option is to make a recursive function that evaluates the string and calls itself whenever it encounters a new list.
Another issue you will run in to is that a number can be multiple digits long and it would not be a clean solution to only assume it might be max 2 digits. A while loop to find all consecutive digits is probably the best solution for that. Here is my implementation:
def get_number(str, idx):
"""Find all consecutive digits and return them and the updated index"""
number = ""
while str[idx].isdigit():
number += str[idx]
idx += 1
return int(number), idx
def recursive_list(str, idx):
"""Transform a string to list, recursively calling this function whenever a nested list is encountered."""
lst = []
# Loop over the entire string
while idx < len(str):
char = str[idx]
# When encountering a digit, get the entire number
if char.isdigit():
number, idx = get_number(str, idx)
lst.append(number)
idx += 1
# When encountering a closing bracket, return the (sub-)list
elif char == ']':
return lst, idx + 1
# When encountering an opening bracket, append the nested list
elif char == '[':
nested, idx = recursive_list(str, idx + 1)
lst.append(nested)
else:
# Go to the next character if we encounter anything else
idx += 1
return lst, idx
def main():
"""Transform a string to a list."""
str = "[1,2,3,[4,5,6],7,[8,[9,10]],11]"
new_list, _ = recursive_list(str, 1)
print(new_list)
if __name__ == "__main__":
main()

Question about returning a value at the end of a recursive call

In the function fp, I am basically checking whether the difference between arguments of succesive recursive calls is less than a certain number and if it is so, I would like to return the last argument. Here is the code:
pX = []
for n in range(100):
pX.append(1/((n+1)*(n+2)))
def phi1(p):
def phi2(u):
sum = 0
for k in range(len(p)):
sum += p[k]*(u**k)
return sum
return phi2
def fp(pt):
temp = phi1(pX)(pt)
print(temp)
if(abs(pt-temp) > 0.01):
fp(temp)
else:
return temp
print(fp(0.1))
The output, without print(temp) is none, with print(temp) is:
0.5175535907956329
0.619369692490415
0.6561427816319753
0.6714277125785142
0.6781654579021761
None
Desired output is 0.678. May I know where I went wrong?
You aren't returning anything when your function recurses. Add a return to where fp is called inside fp:
def fp(pt):
temp = phi1(pX)(pt)
print(temp)
if(abs(pt-temp) > 0.01):
return fp(temp)
else:
return temp

Resources