Clarifying "if all(...)" line: Project Euler #7 - Python [duplicate] - python

This question already has answers here:
How do Python's any and all functions work?
(10 answers)
Closed 7 months ago.
Thanks to this video I managed to further optimize the time for my code for Problem 7 on Project Euler. However, as someone who's getting more familiar with Python, I want to understand line 105 in the image here. What is this line of code trying to do? Also, any suggestions on how to optimize this solution even more are welcome.
if all(x%i for i in primeList): ...
image of Problem 7 solution

Hi this line of code is checking if the new x value is divisible by each value on primeLisl. for example:
x=4
primes = [2,3]
list = [x%i for i in primes]
in this case list returns: [0, 1] (4 is divisible by 2 and not by 3)
when applying the all function checks if any value in the list is equal to 0 and in this case returns fales. If all the values of the list are diferent to 0 returns true.

Related

Who can explain how this code is not an error? [duplicate]

This question already has answers here:
Recursive function in simple english [duplicate]
(6 answers)
Closed 11 months ago.
I was making some exercise to train myself and the exercise asked me to do a program that calculates fractals, very simple, i've done in about 1-2 minutes and it work, but looking at his solution it return x multiplicated by the function itself? how does this run? I know maybe it's a stupid question but i think it might be useful.
def fract(x):
if x == 0:
return 1
return x * fract(x - 1)
print(fract(int(input())))
Here's a walk through of whats going on.
First, you call fract(int(input())). The input method gets a response from the user and parses that to an int. and then calls fract on that int.
Say we enter 3. So our print statement evaluates to fract(3).
fract(3) returns 3 * fract(2)
fract(2) is called and that returns 2 * fract(1)
fract(1) is called and that returns 1
So putting it altogether and substituting function calls for what they return we get fract(3) returns 3 * (2 * (1)).

What does "if 2 < a < b: do someting" mean in Python? [duplicate]

This question already has answers here:
Simplify Chained Comparison
(2 answers)
Closed 2 years ago.
I am learning Python and trying to understand the following if statement. May I know what does it mean?
if 2 < a < b:
do something.
Thank you!
An if statement is a check of a condition and if this condition is true it will execute the code after the : .
In this case the Code prior to your line should have two variables a and b which both contain numbers.
If a is greater value than 2 and b is an even greater value than a, for example a is 3 and b is 4, or a is 10 and b is 200, the following Code will be executed. „Do something“ is just a place holder for the code that should be executed.
The best for you would be to search and read about if and if else statements for python to find some real life examples.

FizzBuzz question, I'm unable to solve this code. Read the below output and give the answer please [duplicate]

This question already has answers here:
Python FizzBuzz [closed]
(11 answers)
Closed 2 years ago.
It takes an input n and outputs the numbers from 1 to n.
For each multiple of 3, print "Fizz instead of the number.
For each multiple of 5, prints "Buzz" instead of the number.
For numbers which are multiples of both 3 and 5, output "FizzBuzz".
You need to make the code to skip the even numbers, so that the logic only applies to odd numbers in the range
Adding on to what #dratenik referred, for that last condition to skip for even numbers,
you just need to add following :
def fizz_buzz_main(N):
for i in range(1, N+1):
if N & 1: # check for odd number, then only call, otherwise skip
fizz_buzz() # function already shown by #dratenik
I hope it was clear !
Sample run :
input : n = 10
output : ["1","Fizz","Buzz","7","Fizz"]

python "all" function making list empty [duplicate]

This question already has answers here:
Why can't I iterate twice over the same iterator? How can I "reset" the iterator or reuse the data?
(5 answers)
Closed 4 years ago.
I am new to python and was learning its builtins but function all()
is not behaving as expected i don't know why.Here is my code
n=map(int,input().strip().split())
print(all([j>0 for j in n]))
print(list(n)) #this line returning empty list
Here are my inputs:
1 2 3 4 5 -9
And my output:
False
Does function all changes the original map object(values)? but something like this is not mentioned in given function definition on the docs link.
Thanks in advance
Map returns a generator object which you exhausted in your all function. Therefore when you call list on n, since n is empty/exhausted, it returns an empty list.
To fix just make n a list in the first place.
n=list(map(int,input().strip().split()))
print(all([j>0 for j in n]))
print(n)

Python min function tuples with arbitrary tuples [duplicate]

This question already has answers here:
How does tuple comparison work in Python?
(4 answers)
Closed 5 years ago.
What is this python code doing?
min((2,3),(6,'f',1))
Output: (2, 3)
I am not able to follow the documentation.
Can someone explain why the output in (2,3) and not an error?
Because (2,3) < (6,'f',1)
Meaning tuples are compared itemwise, therefore 2 < 6 yields that the first tuple is less than the second one
While this code works on Python 2 and Python 3, it should fail on Python 3 if both items in 1st place were the same. Because it would compare 3 to the string 'f' and such comparison is now invalid.
The min function will call the comparator methods of the objects you pass. In this case, all tuples. It is returning the minimum tuple with respect to lexicographic order.

Categories