Function that returns even numbers from a list and halves them [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 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]

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

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)

Tuple into list to find max value and if there is a duplicate [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am creating a function that takes any quantity of numbers, and tell you what is the max value or if there is a tie for largest. I am wondering what I could do to simplify what I have.
def max_num(*args):
nums = []
nums_1 = []
nums.append(args)
i = 0
while i < len(nums[0]):
nums_1.append(nums[0][i])
i += 1
c = max(nums_1)
nums_1.remove(c)
if c in nums_1:
print("It's a tie!")
else:
print(c)
max_num(-10, 0, 10, 10)
So when I initially make a list with the arguments given, it gives me a tuple inside the list. This is why I create a new list to dissect the tuple into separate values. I have the feeling that wasn't necessary, and that there is a much simpler way to do this. Any advice would be great.
Just get the max, and count how many times it appears in your data:
def max_num(*args):
maxi = max(args)
if args.count(maxi) == 1:
print(maxi)
else:
print('Tie')
max_num(2, 5, 1)
#5
max_num(2, 5, 1, 5)
#Tie

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