I post simple code in order to understand how solve my problem. The while loop have 2 cycle but in real case have billion cycle.
def uno_trov():
if(1==1):
return True
else:
return False
def due_trov():
if(1==0):
return True
else:
return False
condizioneV = []
condizione = [1, 0] #1 or 0 inside can be change by the user
first_time = False
i=0
while(i<2):
if(first_time == False): #whit the first cycle I build condizioneV (list of functions)
if(condizione[0]==1):
condizioneV.append(uno_trov)
if(condizione[1]==1):
condizioneV.append(due_trov)
first_time = True
print(condizioneV) #I expect [True]
i+=1
else: #second time condizioneV is already builded and I suppose the process will be more fast because the code not check anymore " if(condizione[1]==1)"
print(condizioneV) #I expect [True]
i+=1
#problem is that I obtain "[<function uno_trov at 0x0272DED0>]" two time.
I don't understand the reason but I obtain "[]" two time. They aren't error but I didn't have a list with one or two True/False.
condizioneV.append(uno_trov)
appends the function address you're missing the ()
correction:
condizioneV.append(uno_trov())
General remark: your code is very poorly written and confusing. look at your first_time condition which is reversed for instance. And the indentation is terrible.
That's some pretty convoluted code you have there. I can't imagine what it is trying to do. I'll presume you are a beginner.
But the reason your arrays contains functions is that these two lines are appending the functions themselves into the arrays, the functions are not being executed.
Instead of this:
condizioneV.append(uno_trov)
condizioneV.append(due_trov)
You need to do this:
condizioneV.append(uno_trov())
condizioneV.append(due_trov())
Yes, this is a solution to one cycle but if you try to use it by pass a parameter to a function doesn't work anymore. I add "t=i" in the first if:
def uno_trov(t):
if(1==t):
return True
else:
return False
def due_trov():
if(1==0):
return True
else:
return False
condizioneV = []
condizione = [1, 1] #1 or 0 inside can change by the user
first_time = False
i=0
while(i<2):
if(first_time == False): #whit first cycle I build condizioneV
if(condizione[0]==1):
t=i
condizioneV.append(uno_trov(t))
if(condizione[1]==1):
condizioneV.append(due_trov())
first_time = True
print(condizioneV) #I expect [False]
i+=1
t=i
else: #second time condizioneV is already build but not properly work good
print(condizioneV) #I expect [True] but instead I get [False]
i+=1
t=i
In other words I would like that with first cycle I build the list of functions and from the second cycle to the end, execute the list of functions.
Related
I need to create a function named 'Bernoulli' that should take 2 input variables 'rr' and 'p' and should return a value of 1 if rr is less than or equal to p and a value of 0 if rr is greater than p.
The code I have produced so far is this:
rr=float(input())
p=float(input())
def bernoulli(rr,p):
if rr<=p:
return 'X=1'
else:
return 'X=0'
I am not sure how correct this is.
Upon running tests I get this feedback:
Your program took too long to execute.
Make sure that it isn't waiting for input and that there is no infinite loop.
rr=float(input())
p=float(input())
def bernoulli(rr,p):
if rr<=p:
return 1
else:
return 0
x = bernoulli(rr,p)
print(x)
However, if you are simply checking if one number is bigger than the other, it might make more sense down the line to use True and False because comparing them will be a shorter line of code later on. if x == False That being in the logical sense that we understand true to be positive and false to be negative. You might forget which way round the 1 and the 0 are ordered :)
Swift answered this in the same way I would approach this. The reason your code is not executing, is because it is never used. You must call a function to use it.
Here is how I did it:
rr=float(input())
p=float(input())
def bernoulli(rr,p):
if rr<=p:
return 'X=1'
else:
return 'X=0'
function_response = bernoulli(rr,p)
print(function_response)
def sudoku_solver(sudoku):
l = [0, 0]
if not find_empty_location(sudoku, l):
return True
row = l[0]
col = l[1]
for num in range(1, 10):
if check_location_is_safe(sudoku, row, col, num):
sudoku[row][col] = num
if sudoku_solver(sudoku):
return True,sudoku
sudoku[row][col] = 0
return False
def P():
a = np.zeros((9,9))
if sudoku_solver(sudoku):
a = sudoku_solver(sudoku)[1]
else:
a = 1
return a
There are two returns, True and sudoku. How can I call sudoku only in function P? When I run the function P(), it will show
'bool' object is not subscriptable.
You could make your function return a single value that would be either None or the sudoku data.
Python treats None as false so your condition could then be:
...
if sudoku_solver(sudoku):
a = sudoku_solver(sudoku)
...
Note that if sudoku_solver() is a complex and time consuming function, you will want to place its result in a variable BEFORE testing the condition so that it is not executed twice (as would be the case in the above condition)
...
a = sudoku_solver(sudoku)
if a:
...
Another option would be to systematically return a tuple with the boolean and the sudoku data (or None).
Your return statements would have to be changed to return True,sudoku and return False,None
Then your condition could use indexing directly:
...
if sudoku_solver(sudoku)[0]:
a = sudoku_solver(sudoku)[1]
...
but again, you probably don't want to execute the function twice so :
...
ok,a = sudoku_solver(sudoku)
if ok :
...
Another thing is that, if you're passing sudoku as a parameter, you have to realize that your function will modify the content of the calling variable even when it returns False. That may not be what you want but if it is, then there is no point in returning a second value because the caller's instance of the sudoku variable will already be modified and available.
When you see
return True,sudoku
you can catch the returned values like so
result, sudoku = my_function()
or if you've no interest in result
_, sudoku = my_function()
---Edit---
It seems your question centers around the two returns. Namely
return True,sudoku
versus
return False
That's adds unnecessary complexity. Can I suggest you simplify by instead using
return sudoku
and then later
return None
That means you can check the returned value like so
sudoku = my_function()
if (sudoku):
// Do something here
else:
// Do something else
You can't return multiple objects from a function, but you can return a container object, such as a list or tuple, that contains multiple member objects. Try using
return [ True, sudoku ]
Hi i am new to Python programming . I am writing a python code which uses stack.
There are two function in my python code isdesc() and poisonplant()
When i have called the function poisonplant in there is another function but that function is not get called
Code is indented properly
Here is my code:
def isdesc(arr):
if len(arr) == 0:
return False
for i in range(0,len(arr)):
if arr[i+1] < arr[i]:
return True
else:
return False
def poisonplant(expr):
count=0
pdb.set_trace()
while not isdesc(expr):
s.push(expr[0])
for i in range(0,len(expr)):
if expr[i+1] < expr[i]:
s.push(expr[i+1])
count+=1
del expr[:]
for each_item in s.items:
a=s.pop()
expr.insert(0,a)
return count
input1=[6,5,8,4,7,10,9]
print(poisonplant(input1))
I have only called poisonplant and i think isdec is automatically get called inside poison function.
Can someone help me in this why isdesc is not get called here
isdesc is returning too soon, without looking at all the adjacent elements. It should read
def isdesc(arr):
for i in range(len(arr)-1):
if arr[i] < arr[i+1]:
return False
return True
(I've slightly modified the definition to treat empty lists as descending. If you treat an empty list as non-descending, you'll enter the loop, where you are assuming the list is not empty.)
Your main problem is that you're using return as if it will return each iteration, but you're actually just going to return on the very first one. You could instead return False if any of the sorts are wrong, but a better way to check if the lists are sorting in descending order is to check if the list is equal to a reverse sort of itself.
def isdesc(arr):
if len(arr) == 0:
return False
return arr == sorted(arr, reverse=True)
I am currently working on a BASIC simulator in Python, as the title suggests. Here is my code for this problem:
def getBASIC():
l = []
x = 1
while x == 1:
i = input()
l.append(i)
if len(i.split()) != 3:
x = 0
return l
def findLine(prog, target):
for l in range(0, len(prog)):
progX = prog[l].split()
if progX[0] == target:
return l
def execute(prog):
location = 0
visited = [False] * len(prog)
while True:
T = prog[location].split()[2]
location = findLine(prog, T)
visited[location] = True
if visited[len(visited)-1] == False:
return "infinite loop"
else:
return "success"
The first function does what it is intended to do -- convert input of BASIC code into a list. The second function, findLine also does what it is intended to do, in that it finds the item which contains the string equal to the input. The last function, however, I cannot get to work. I know what I have to do, and that is to check whether or not a part of it has been visited twice. I cannot figure out how to do this, due to the existence of the while loop. As a result of this, the second half of that function is just placeholder. If you could help me figure out how to solve this, it would be greatly appreciated. Thanks.
You keep a list of places that have been visited (you already do this) and then when you encounter a goto, you check if it does to a line that already have been visited, and if it has been visited, you exit.
One mistake right now is that you make a list that is as long as the program is. That's pretty pointless. Just keep a list of the visited line numbers instead, and check with
if current_line in visited:
Try adding an if statement declaring a line in the visited list to be true when it is encountered in the loop. This is my solution:
def execute(prog):
location = 0
visited=[False]*len(prog)
while True:
if location==len(prog)-1:
return "success"
if visited[location]==True:
return "infinite loop"
if visited[location]==False:
visited[location]=True
line2strings=prog[location].split()
T=line2strings[-1]
location=findLine(prog, T)
I am trying to write a function which is supposed to compare list structures (the values are indifferent). The problem is that I have two lists which are unequal but the function still returns True even though it actually goes into the else part. I don't understand why and what I did wrong. Here is my code:
def islist(p): #is p a list
return type(p)==type(list())
def ListeIsomorf(a,b):
if len(a)==len(b):
for i,j in zip(a,b):
if islist(i) and islist(j):
ListeIsomorf(i,j)
elif islist(i) or islist(j):
return(False)
return(True)
else:
print(a,"length from the list isn't equal",b)
return(False)
#example lists
ListeE = [[],[],[[]]]
ListeD = [[],[],[[]]]
ListeF = [[[],[],[[]]]]
ListeG = [[],[[]],[[]]]
ListeH = [1,[3]]
ListeI = [1,3]
#tests
print(ListeIsomorf(ListeD,ListeE)) # True
print(ListeIsomorf(ListeD,ListeF)) # False
print(ListeIsomorf(ListeD,ListeG)) # False
print(ListeIsomorf(ListeH,ListeI)) # False
So the problem only occurs with the third print(ListeIsomorf(ListeD,ListeG)) # False. It actually goes into the else part and does print the "length from the list isn't equal" but it doesn't stop and it doesn't give out the return(False). Am I missing something?
The problem is that when your function calls itself recursively:
ListeIsomorf(i,j)
it ignores the returned value.
Thus the comparisons that take place at the second level of recursion have no effect on what the top level returns.
Changing the above to:
if not ListeIsomorf(i,j):
return(False)
fixes the problem.