How to print modulo of specific value using List comprehension ? - python

I have a list a,
a = ['python', 'django', 'python with django']
What i want to do is ,
if number is divisible by 3 then print 'python'
if number is divisible by 5 then print 'django'
if number is divisible by 3 and 5 both then print 'python with django'
This is my code
for n, i in enumerate(range(20)):
if i % 3 == 0:
print n, a[0]
elif i % 5 == 0:
print n, a[1]
elif i % 3 == 0 and i % 5 == 0:
print n, a[2]
and O/P is
0 python
3 python
5 django
6 python
9 python
10 django
12 python
15 python
18 python
and i need this Result
0 python with django
3 python
5 django
6 python
9 python
10 django
12 python
15 python with django
18 python
Can i use List Comprehension or is there any pythonic way
Thanks in advance.

Your way seems fine but you have your code a little bit incorrect. Here is the correct code:
for n, i in enumerate(range(20)):
if i % 3 == 0 and i % 5 == 0:
print n, a[2]
elif i % 3 == 0:
print n, a[0]
elif i % 5 == 0:
print n, a[1]
You must have it check for if the number is divisible by 3 and 5 first. If you don't and it is divisible by 3 that meets the condition of the first if statement so it will not check any of the elif statements because elif is only checked if the previous if or elif statements were not met. The same goes for if the number is divisible by 5.

This should solve your FizzBuzz task.
a = ['python with django' if x%15 == 0 else ('python' if x%3 == 0 else 'django') for x in xrange(20)]

A list comprehension will probably end up looking too convoluted in this case. Just move the last of your three conditions to the top. Otherwise, if a number is divisble by 3 and by 5, the check for being divisible by 3 alone will be successful first and your elif chain means the combined check will never be reached. And by the way, you need not enumerate the range you iterate over, just use i in place of n.
To make the code a little less verbose (but also a little less straight-forward) you could get away with one check less:
for i in range(20):
if i % 3 == 0:
if i % 5 == 0:
print i, a[2]
else:
print i, a[0]
elif i % 5 == 0 :
print i, a[1]

Related

Why, using "and" in a for loop and "or" in a while loop gives the same result?

I am trying to practice writing these loops, and I had an exercise which asked me to print numbers from 0 to 20 that aren't divisible by 3 or 5.
For the while loop, I wrote this code:
#solution with while
i = 0
while i < 21:
i += 1
if i % 3 == 0 or i % 5 == 0:
continue
print(i)
Whereas, for the for...in loop, I struggled because I found out that I needed to use and instead of or here.
The code is as follows:
#solution with for
for k in range(21):
if k % 3 != 0 and k % 5 != 0:
print(k)
Why did I have to change the logical operator?
In my head, the first rows of the two codes do the same thing, iterate a number from 0 to 20. So the condition, after these, should be equal for both the iterations used.
Can anyone explain to me what am I missing here?
This isn't a loop type problem, the issue is in using different equality operators in your conditions - != instead of ==.
After changing or to and, the result stays the same because you've accidentally used De Morgan's second law (negation of disjunction):
Now, let's look at the code.
In the while loop:
the program doesn't print i:
if it's divisible by 3 (but not necessarily by 5)
or
if it's divisible by 5 (but not necessarily by 3).
In other words:
the program prints i:
if it's not divisible by 3
and
if it's not divisible by 5.
Noe, let's check, what says the condition in the for...in loop:
the program prints k:
if it's not divisible by 3
and
if it's not divisible by 5.
Looks like the same condition, doesn't it?
As you noticed, in the In other words: paragraph, we've naturally used the negation of disjunction.
To sum up, both programs for i / k lower than 21 print:
0
1
2
4
7
8
11
13
14
16
17
19
If you still aren't sure, try to replace the conditions between loops:
i = 0
while i < 21:
i += 1
if i % 3 != 0 and i % 5 != 0:
print(i)
for k in range(21):
if k % 3 == 0 or k % 5 == 0:
continue
print(k)
In while loop.
if i % 3 == 0 or i % 5 == 0: # this means when i is evenly divided by 3 or 5 then don't do anything.
continue
print(i) # means only print i when i is not evenly divided by 3 and 5
And in the for loop
if k % 3 != 0 and k % 5 != 0: # here this means print i when k is not evenly divided by 3 and 5. Which same as while loop
print(k)
You just reverse order and nothing. You can use the same method in both way and you will get the same result.
While loop
i = 0
while i < 21:
i += 1
if i % 3 != 0 and i % 5 != 0:
print(i)
For Loop
for a in range(21):
if a % 3 != 0 and a % 5 != 0:
print(a)
Because you have also changed the conditions for the results in the while loop you have used ==, but in for loop you have used != if you modify the code you will get the same output by or operator in both
for k in range(21):
if k % 3 == 0 or k % 5 == 0:
continue
print(k)
The two loop types are totally equivalent. You had to use and instead of or, and != instead of ==, because you reversed the logic.
In the first case, you are saying "if the number is divisible then skip it, else print it" while in the second one_ "if the number is not divisible then print (else do nothing)"_.

Computational complexity of modulos and FizzBuzz

So I don't want to go into whether this is the most perfect code for the FizzBuzz challenge.
For those unfamiliar with FizzBuzz, there are four rules in printing out a range of 1-100:
Print out all numbers;
If the number is divisible by 3, print "Fizz" instead;
If the number is divisible by 5, print "Buzz" instead;
If the number is
divisible by both 3 and 5, print "FizzBuzz".)
I ran two implementations, to compare their speed:
# Code A
%%timeit
for i in range(1,10001):
if i % 15 == 0:
print('FizzBuzz')
elif i % 3 == 0:
print('Fizz')
elif i % 5 == 0:
print('Buzz')
else:
print(i)
# Code B
%%timeit
for i in range(1,10001):
if i % 5 == 0 and i % 3 == 0:
print('FizzBuzz')
elif i % 3 == 0:
print('Fizz')
elif i % 5 == 0:
print('Buzz')
else:
print(i)
Despite the extra if evaluation of Code B, it consistently ran quicker than Code A. Does a larger number result in a slower modulo? What is the underlying reason for this?
The main reason I could think of is perhaps optimization.
Version B uses the same operation a few times, namely:
i mod 5
i mod 3
A reasonably intelligent compiler/interpreter might spot this, and calculate these intermediate results. By doing so, it would be able to immediately calculate the entire if-block with only 2 mod operations.
In short, I think the code that ends up being executed might look something like this:
for i in range(1,10001):
a = i % 5
b = i % 3
if a == 0 and b == 0:
print('FizzBuzz')
elif b == 0:
print('Fizz')
elif a == 0:
print('Buzz')
else:
print(i)
However, the code in block A is perhaps too complicated. By that, I mean the compiler will always be forced to execute 3 mod operations. Unless it would somehow be able to spot that 15 = 3 * 5, and use that logic to re-work the if-statement.

Trying to add numbers in python loop wrong result

I am new to python. I am trying to write a program that counts a range of values but for multiples of 3 and 4 adds 3 and 4 to them resepectively and when the number is multiple of both adds that number to it. So for instance 1,2,3,4,5,6,7,8,9,10,11,12
should read in the end program as: 1,2,6,8,5,9,7,12,10,11,24
But my code is getting stuck somewhere.
for i in range (1,20):
if i%3==0 and i%4==0:
i=i+12
if i%3==0:
i=i+3
if i%4==0:
i=i+4
print i
This line has a typo
if i%3==0 and 1%4==0: # your second condition is 1 % 4 == 0, which is always false
I think you meant
if i%3==0 and i%4==0:
It is better (accurate and cleaner) if you use a different variable.
for i in range (1,20):
n = i
if i%3==0 and i%4==0:
n=i+12
if i%3==0:
n=i+3
if i%4==0:
n=i+4
print n
Now you will notice this fixed it for the 9 case, but not the 12 case! We now need to add the use of elif. Also, if a number is a multiple of 3 and 4, then it is also a multiple of their lowest common multiple, in this case 12. So you could re-write your first step to just check for multiples of 12. This gives us:
for i in range (1,20):
n = i
if i%12==0
n=i+12 # or possibly i + i
elif i%3==0:
n=i+3
elif i%4==0:
n=i+4
print n
The reason this works is because without the elif the i was getting added to multiple times. For example, with 9, you get to 9%3 ==0, True. Now i is set to 12. Next statement? 12%4 ==0 True. So another 4 is added.
Alternatively, if you would like to do some crazy python stuff:
for i in range(1, 20):
print i + next((n for n in (12, 4, 3) if i % n == 0), 0)
(Shout out to Jon Clements for this 2-liner answer)
You're allowing multiples cases (if conditions) for each iteration.
I think you probably wanted to do exclusive cases.
Maybe something like:
for i in range(1, 20):
print i, "=>",
if i % 3 == 0 and i % 4 == 0:
i += i
elif i % 3 == 0:
i += 3
elif i % 4 == 0:
i += 4
print i

Python FizzBuzz [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 have been given this question to do in Python:
Take in a list of numbers from the user and run FizzBuzz on that list.
When you loop through the list remember the rules:
If the number is divisible by both 3 and 5 print FizzBuzz
If it's only divisible by 3 print Fizz
If it's only divisible by 5 print Buzz
Otherwise just print the number
Also remember elif!
I have the following script created, but it gives me an error at if n%3=True
n = input()
if n % 3 = True:
print("Fizz")
else if n % 5 = True:
print("Buzz")
elif print n
Can anyone help? Thank you very much!
A few issues with your code here. The first issue is that, for comparison, you should be using ==, not =, which is for assignment.
The second issue is that you want to check that the remainder of the divisions (which is what the modulo operator calculates) is zero, not that it's true, which doesn't really make sense.
You should be using elif for "otherwise if..." and else for "otherwise." And you need to fix the formatting of your else clause.
You want:
n=input()
if n%3 == 0:
print("Fizz")
elif n%5 == 0:
print ("Buzz")
else:
print n
Finally, your code does not meet the spec:
1) If the number is divisible by both 3 and 5 print "FizzBuzz"
The above will not do this. This part I'm going to leave to you because I'm not here to solve the assignment for you :)
Based on this
FizzBuzz: For integers up to and including 100, prints FizzBuzz if the integer is divisible by 3 and 5 (15); Fizz if it's divisible by 3 (and not 5); Buzz if it's divisible by 5 (and not 3); and the integer otherwise.
def FizzBuzz():
for i in range(1,101):
print {
3 : "Fizz",
5 : "Buzz",
15 : "FizzBuzz"}.get(15*(not i%15) or
5*(not i%5 ) or
3*(not i%3 ), '{}'.format(i))
The .get() method works wonders here.
Operates as follows
For all integers from 1 to 100 (101 is NOT included),
print the value of the dictionary key that we call via get according to these rules.
"Get the first non-False item in the get call, or return the integer as a string."
When checking for a True value, thus a value we can lookup, Python evaluates 0 to False. If i mod 15 = 0, that's False, we would go to the next one.
Therefore we NOT each of the 'mods' (aka remainder), so that if the mod == 0, which == False, we get a True statement. We multiply True by the dictionary key which returns the dictionary key (i.e. 3*True == 3)
When the integer it not divisible by 3, 5 or 15, then we fall to the default clause of printing the int '{}'.format(i) just inserts i into that string - as a string.
Some of the output
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
n % 3 (or n % any number) does not evaluate to True or False, it's not a Boolean expression. n % 3 == 0 on the other hand, does.
As an aside, what happens when n % 3 == 0 and n % 5 == 0 both evaluate to True?
Here's how I did it using generators.
Explanations are added as comments.
# the factors to check for, along with its
# associated text data.
FACTORS = {
3 : "Fizz",
5 : "Buzz",
}
def fizzbuzz(start, stop):
"""FizzBuzz printer"""
for i in range(start, stop+1):
string = "" # build string
# iterate through all factors
for j, dat in FACTORS.items():
# test for divisibility
if i % j == 0:
# add data to string if divisible
string += dat
if not string:
# if string's length is 0, it means,
# all the factors returned non-zero
# modulus, so return only the number
yield str(i)
else:
# factor had returned modulo as 0,
# return the string
yield string
if __name__ == "__main__":
for each in fizzbuzz(1, 100):
print(each)
This version has the advantage of not depending on any extra factor checks.
one of the shortest answers i have found is
c=1
while c<101:print((c%3<1)*'Fizz'+(c%5<1)*'Buzz'or c);c+=1
61 characters
Make it universal for any integer, positive or negative.
Also make it easily expandable to other keywords for any integer by creating a dictionary of keywords.
def checkdict(divdict, i):
out = ""
for key in divdict:
if key != 0:
if i%key==0:
out+=divdict[key]
if key == 0 and i == 0:
out+=divdict[key]
if out == "":
out = i
print(out)
if __name__ == "__main__":
mydict = {3:"Fizz",5:"Buzz"}
for i in range(-50,50):
checkdict(mydict, i)
def check(num):
finalWord = ''
for k,v in numWordDict.items():
if num % k == 0:
finalWord += v
if not finalWord:
return num
else:
return finalWord
def FizzLoop(start=0, stop=10, step=1):
for i in range(start, stop, step):
print(check(i))
numWordDict = {3:'fizz', 6:'buzz', 5:'fiver'}
FizzLoop(0, 10)
print("----------")
FizzLoop(0, 50, 5)
Numbers = [3, 5]
Labels = ["Fizz", "Buzz"]
for i in range(1, 101):
Output =""
for j in range (len(Numbers) ) :
if i % Numbers[j] == 0:
Output = Output + Labels[j]
if Output =="" :
print(i)
else:
print(Output)
Nothing fancy, using a while loop to achieve the same result:
x,xx = 1,100
xy,xz = 3,5
mylist = []
while x <= xx:
y = (x/xy).is_integer()
z = (x/xz).is_integer()
if y and z:
mylist.append("fizzbuzz")
elif y:
mylist.append("fizz")
elif z:
mylist.append("buzz")
else:
mylist.append(x)
x+=1
print(mylist)
Please find the 1 liner code (list comprehension) code below. Hope this is easy to understand . if it's not please do let me know. I will elaborate.
N = 10 #user_input
print(list(map(lambda n: "Fizz" if (n % 3 == 0) else ( "Buzz" if (n % 5 == 0) else n),[i for i in range(1,N+1)])))
def fizz_buzz(input):
if (input % 3 == 0) and (input % 5 == 0):
return "FizzBuzz"
if input % 3 == 0:
return "Fizz"
if input % 5 == 0:
return "Buzz"
return input
print(fizz_buzz(15))

Numbers without remainder python

I need to print out numbers between 1 and n(n is entered with keyboard) that do not divide by 2, 3 and 5.
I need to use while or for loops and the remainder is gotten with %.
I'm new here and I just don't understand the usage of %?
I tried something like this:
import math
print("Hey. Enter a number.")
entered_number = int(input())
for i in range(1, entered_number):
if i%2 != 0:
print("The number", i, "is ok.")
else:
pass
if i%3 != 0:
print("The number", i, "is ok.")
else:
pass
if i%5 != 0:
print("The number", i, "is ok.")
help?
You need to test for all 3 conditions in one statement, not in 3:
for i in range(1, entered_number):
if i % 2 != 0 and i % 3 != 0 and i % 5 != 0:
print("The number", i, "is ok.")
The and operators here make sure that all three conditions are met before printing.
You are testing each condition in isolation, which means that if the number is, say, 10, you are still printing The number 10 is ok. because it is not divisible by 3. For numbers that are okay, you were printing The number ... is ok. 3 times, as your code tests that it is not divisible by 3 different numbers separately, printing each time.
If something divides by 7 then:
something % 7 == 0
If something divides by 7 and 9 then:
something % 7 == 0 and something % 9 == 0
Conversely, if something divides by 7 or 9 then:
something % 7 == 0 or something % 9 == 0
Something that does not divide by 7 or 9 is given by the expression:
not (something % 7 == 0 or something % 9 == 0)
You don't require the else: pass bits from your code and one if statement with an if-expression that has three %, == bits in it should suffice.
You should probably check the three conditions at the same time:
if i%2 != 0 and i%3 != 0 and i%5 != 0:
print("The number", i, "is ok.")
Otherwise, you would print the same message several times for a single number.
Anyway, for your second question, the% operation is called modulo and it gives you the remainder of a division. For instance, 5%3 = 2 because 5 = 3*1 + 2. And when you check i%2 != 0, you actually check if i can be divided by 2.
print("Hey. Enter a number.")
entered_number = int(input())
for i in range(1, entered_number):
if i%2 != 0 and i%3 !=0 and i%5!=0:
print("The number", i, "is ok.")
a%b returns the remainder when a is divided by b. Example:
>> 5%3
2
What you are doing wrong here is that you are printing after checking a single condition so it will print even if i is divisible by other numbers. For example if i is 3, it will satisfy the first condition and therefore print that the number is ok but it is actually divisible by 3.
I saw you've solved your problem but my answer may worth reading.
This problem is actually doing filtering over a list of numbers 1..n. You can define a base function to test if number x is dividable by number y, and then use this base function to filter the list to get the result.
Here's my version.
import math
from functools import partial
print("Hey. Enter a number.")
entered_number = int(input())
def not_dividable_by(x, y):
return False if x % y == 0 else True
number_list = range(1, entered_number)
for i in [2, 3, 5]:
test_func = partial(not_dividable_by, y=i)
number_list = filter(test_func, number_list)
for number in number_list:
print("%d is OK" % (number,))

Categories

Resources