I'm returning a defaultdict(list), but randomly choosing between the two, why does it return nothing sometimes? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
output = ""
numberList = [0, 1]
print(random.choice(numberList))
if(random.choice(numberList) == 0):
if len(slots) > 0:
output = templates[state][0].replace("<num_classes>", str(slots[0][1]))
else:
output = templates[state][0]
elif(random.choice(numberList) == 1):
if len(slots) > 0:
output = templates2[state][0].replace("<num_classes>", str(slots[0][1]))
else:
output = templates2[state][0]
return output
expected answer
sometimes get this
Sometimes it returns nothing or no dictionary... Why?

With
elif(random.choice(numberList) == 1)
you will again choose a brand new random number. And if that isn't 1 then there's no else that will set output.
Instead of elif you should have a plain else.

Related

How to get list of palindrome in text? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I have the following interview question that may require traversing through the entire string.
Problem I searched about find Problem and most people do it like this def palindrome(s): return s==s[::-1] But this task is diffrent?
palindrome is a word the can read from both directions like 'mam', 'dad'. Your task is to get a list of palindrome in a given string.
def palindrome(s):
stack = []
return ['aaa']
exampls
palindrome('aaa') #['aaa']
palindrome('abcbaaaa') #['abcba','aaaa']
palindrome('xrlgabccbaxrlg') #['abccba']
palindrome('abcde') #['']
Let's try to avoid checking all the possible combinations :). My idea is, start from the extremities and converge:
def palindrome(s):
out = [''] #we need the list to not be empty for the following check
for main_start in range(len(s) - 1):
for main_end in range(len(s) - 1, main_start, -1):
start = main_start
end = main_end
while (end - start) > 0:
if s[start] == s[end]: ##may be palindrome
start += 1
end -= 1
else:
break
else:
if s[main_start:main_end + 1] not in out[-1]: #ignore shorter ("inner") findings
out.append(s[main_start:main_end + 1])
return out[1:] #skip the dummy item

How to code this condition in Python? I am new to python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am new to python. so any help will be appreciated.
I have two arrays A = [1,2,4,2,3,5,3] and B = [0,4,4,4,1,1,1]
for the function if I give A, B as input then I should get output as = [1,(2+4+2),(3+5+3)] = [1,8,11](if numbers are repeating in B then corresponding values in A should be added together).
This should do the trick:
def bla(list1:list, list2:list):
prev = list2[0] - 1
final_list = []
for ele, pos in zip(list1, list2):
if prev != pos:
final_list.append(ele)
else:
final_list[-1] += ele
prev = pos
return final_list

Find matching numbers in an array and print result to new array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Trying to figure out why the code I have produced is not the answer to the following question:
Given two arrays, one with user inputs and another of correct inputs, return an array that denotes when the user input matches the correct input with the number 1, and incorrect inputs with -1.
My code below
newArr = []
def correct_stream(user, correct):
for i in user:
for j in correct:
if i == j:
newArr.append(1)
else:
newArr.append(-1)
return newArr
Your return statement is inside of your second for loop and will only execute once.
newArr = []
def correct_stream(user, correct):
for i in user:
for j in correct:
if i == j:
newArr.append(1)
else:
newArr.append(-1)
return newArr #this executes after the if statement runs only once, leading to an array with size 1
Python is built upon proper syntax, and while some aspects are flexible, indentation is strict. Brackets don't split apart scopes, indentation does.
newArr = []
def correct_stream(user, correct):
for i in user:
for j in correct:
if i == j:
newArr.append(1)
else:
newArr.append(-1)
return newArr #this now executes after the whole for loop above is finished

How to fix the recursive function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
It's a simple question of recursive function, which extracting every digits from a positive number. As the comment mentioned, I have known the mistake is because of global variable, but I still have no idea to fix it to get expected result. Thanks.
def getdigits(n):
if n == 0:
return list_1[::-1]
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120)) # output = [1,2,0]
print(getdigits(5)) # output = [5,1,2,0]
But the expected output should be
print(getdigits(120)) # expected output = [1,2,0]
print(getdigits(5)) # expected output = [5]
You are using the existing LIST that already have values from the previous function call.
First clear the list and then try to call for another value.\
Try it
def getdigits(n):
if n == 0:
return list_1[::-1]
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120))
list_1.clear() #clear list values
print(getdigits(5))
def getdigits(n):
global list_1
if n == 0:
listTemp = list_1
list_1 = []
return listTemp
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120)) # output = [1,2,0]
print(getdigits(5)) # output = [5]
you need to declare list 1 as a global varible inside your function so you can clear list one within th function
i have tested it and got that output

While loop testing equality with 0 will not break [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This while loop won't end. Any suggestions for this Python program?
[Edit: Upon request I quoted the entire program. It's supposed to find the largest palindrome produced by two n digit decimals.]
def palindrome(n):
first = 1
second = 1
largestPalindrome = 1
palindrome = True
while(first < 10**n):
while(second < 10**n):
number = []
candidate = 1
while candidate!=0:
number.append(candidate%10)
candidate = candidate // 10
print("in")
i = 0
ub = len(number)//2
while(i<ub):
if(number[i]!=number[len(number)-1-i]):
palindrome = False
i += 1
if palindrome == True:
largestPalindrome = first*second
print(largestPalindrome)
Your external while loops
while(first < 10**n):
while(second < 10**n):
...
are checking if the variables first and second are under a certain value (10**n). The problem is that, inside these loops, you never increment either first or second, so the condition is always satisfied and your loops keep going on forever.

Categories