Where is the infite loop and how to fix it? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a practice quiz question for a course I'm taking. Need to fix the code.
def is_power_of_two(n):
# Check if the number can be divided by two without a remainder
while n % 2 == 0:
n = n / 2
# If after dividing by two the number is 1, it's a power of two
if n == 1:
return True
return False
print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False

for n = 0, each cycle n is 0, and it will run into next cycle
while n % 2 == 0:
n = n / 2

Related

How do I make a function read a row and a column with only zeros without using libraries [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
This post was edited and submitted for review 12 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm new to programming in general... I have to check all the rows and columns of an matrix and in case any of them are complete with zeros, return a True value. I made this code in a silly attempt but it doesn't work for the purpose of the question itself. The matrix will always be a square and a list of lists.
def determinanteEhNulo(matriz):
contador = 0
for i in matriz:
for j in range(len(matriz)):
if i[j] == 0:
contador += 1
if contador >= int(len(matriz)):
return True
return False
This seems to do what you need.
def determinanteEhNulo(matriz):
for i in matriz:
if all(x==0 for x in i):
return True
for i in zip(*matriz):
if all(x==0 for x in i):
return True
return False
count = 0
def determinanteEhNulo(matrix):
global count
for i in matrix:
if all([j ==0 for j in i]):
count += 1
if count == len(matrix):
return True
else:
return False

Function that returns even numbers from a list and halves them [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So I am having trouble concatenating this, I am not allowed to use .append(), and right now Im getting the error 'int' object not iterable.
def halveEvens(l):
num = []
for n in l:
if n % 2 == 0:
num += (n // 2)
return num
print(halveEvens([10,21,32,42,55]))```
sum(x//2 for x in numbers if x%2 == 0)
is probably how I would do it
if you just want to collect them (without summing them)
generator (x//2 for x in numbers if x%2 == 0) would be evaluated as you iterate it
or list comprehension that is evaluated immediatly
[x//2 for x in numbers if x%2 == 0]

Please explain the output of this python code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
>>> 0 < 10 != 1 < 5
True
Why is it?? 0<10 is true. 1<5 is also true.True != True should be false 🤔. Then why the the output is True ???
Because of operations priorities meaning of your expression is different. You need to put parentheses: (0 < 10) != (1 < 5), to have what you want.
Otherwise your original expression means same as (0 < 10) and (10 != 1) and (1 < 5) which is not what you expected. (thanks to #TomKarzes)

recursive function that goes through a built in list of numbers [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 4 years ago.
Improve this question
need help with this question. Much appreciated.
Write a recursive function big_numbers that takes a Python built-in list of numbers and returns True if each number in the list is greater than 100 and False otherwise.
def isGreater(myList, i=0):
if(i == len(myList)): # Termination True case
return True
if myList[i] > 100:
return isGreater(myList, i+1)
else:
return False # Termination False case
Demonstration:
test = [101, 129, 130]
print(isGreater(test))
True

Python: how to display all possible cases to place brackets [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 6 years ago.
Improve this question
Given a number of brackets pairs. I want to display all correct combinations of these brackets. By correct I mean each bracket should be opened before closing in each combination. For example, if the number of brackets is 2 the output should be:
(())
()()
For 3:
((()))
()()()
(()())
(())()
()(())
The order of the output lines doesn't matter.
How can I do it with Python?
Try this code, please:
def __F(l, r, pref):
if r < l or l < 0 or r < 0:
return
if r == 0 and l == 0:
print(pref)
return
__F(l - 1, r, pref + "(")
__F(l, r - 1, pref + ")")
def F(n):
__F(n, n, "")
F(2)
F(3)

Categories