replacing of numbers to zero in a sum - python

I have to define a function 'func(a, b, c)' in which there are 3 variables it calculates their sum. I have to check if there value is greater than '13' then the number becomes '0' eg.,
'def func(3,4,14)' ---> 7 (3+4+0)
I've tried this code below:
def no_teen_sum(a, b, c):
if(a>13):
a=0
elif(b>13):
b=0
elif(c>13):
c=0
return a+b+c
But it didn't work. Am I doing wrong somewhere?
Please suggest me the correct way to do it...

Your problem is using elif. You want to use if:
def no_teen_sum(a, b, c):
if a > 13:
a = 0
if b > 13:
b = 0
if c > 13:
c = 0
return a + b + c
To create a general function you could use *args and sum in a variadic function:
def no_teen_sum(*args):
return sum(arg if arg < 13 else 0 for arg in args)
Example:
>>>no_teen_sum(1, 2, 14)
3

a version using sum:
def no_teen_sum(a, b, c):
return sum( i if i <= 13 else 0 for i in (a, b, c))
print(no_teen_sum(1, 2, 3), no_teen_sum(1, 2, 13), no_teen_sum(11, 12, 13))
output:
6 16 36
can be easily adapted if you have to add more than 3 values this way:
def no_teen_sum(lst):
return sum( i if i <= 13 else 0 for i in *lst)
print(no_teen_sum(11, 12, 13, 14, 15))

Related

Python: sum function with for loop: different results

Here is the code:
d = {1 : 1, 2 : 2, 3 : 2}
s, s1 = 0, 0
for e in d:
s += sum(x for x in range(d[e]))
s1 = sum(sum(y for y in range(d[e])) for e in d)
print(f"s: {s}, s1: {s1}")
Why s == 2, but s1 == 3?
I expect s == s1 == 2.
It looks that interpreter knows an internal variable e from the for-loop prior the first run of the sum() function .

Python - Multiple Assignment [duplicate]

This question already has answers here:
Multiple assignment and evaluation order in Python
(11 answers)
Closed 2 years ago.
Recently I was reading through the official Python documentation when I came across the example on how to code the Fibonacci series as follows:
a, b = 0, 1
while a < 10:
print (a)
a, b = b, a + b
which outputs to 0,1,1,2,3,5,8
Since I've never used multiple assignment myself, I decided to hop into Visual Studio to figure out how it worked. I noticed that if I changed the notation to...
a = 0
b = 1
while a < 10:
print (a)
a, b = b, a + b
... the output remains the same.
However, if I change the notation to...
a = 0
b = 1
while a < 10:
print(a)
a = b
b = a + b
... the output changes to 0, 1, 2, 4, 8
The way I understand multiple assignments is that it shrinks what can be done into two lines into one. But obviously, this reasoning must be flawed if I can't apply this logic to the variables under the print(a) command.
It would be much appreciated if someone could explain why this is/what is wrong with my reasoning.
a = 0
b = 1
while a < 10:
print(a)
a = b
b = a + b
In this case, a becomes b and then b becomes the changed a + b
a, b = 0, 1
while a < 10:
print (a)
a, b = b, a+b
In this case, a becomes b and at the same time b becomes the original a + b.
That's why, in your case b becomes the new a + b, which, since a = b, basically means b = b + b. That's why the value of b doubles everytime.
When you do a, b = d, e the order in which assignment happens in from right to left. That is, b is first given the value of e and then the other assignment happens. So when you do a, b = b, a + b what you are effectively writing is,
b = a + b
a = b
Hence the difference.
You can verify this by doing
a = 0
b = 1
while a < 10:
a, b = b, a + b
print(a, b)
the first output is 1 1. So first b becomes 0+1 and then a is given the value of b=a making it also 1.
If you want more details on how this works, you can check out this question.
In a multiple assignment, the right side is always computed first.
In effect,
a, b = b, a + b
is the same as:
b = a + b
a = b

Explain how multiple variable assignment in single line works? (Example: a, b = b, a+b)

Say I have this Python code
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
For n=1000 this prints:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
But I don't understand why it's 1 1 2 3.
The issue is with this line:
a, b = b, a+b
What is the order of execution?
The two options I see are:
1:
a = b
b = a+b
2:
b = a+b
a = b
But neither gives me the correct result when I try it manually.
What am I missing?
None of the two options you shared actually describe the working of:
a, b = b, a+b
Above code assigns a with the value of b. And b with the older value of a+b (i.e. in a+b the older value of a). You may consider it as an equivalent of:
>>> temp_a, temp_b = a, b
>>> a = temp_b
>>> b = temp_a + temp_b
Example: Dual variable assignment in one line:
>>> a, b = 3, 5
>>> a, b = b, a+b
>>> a
5
>>> b
8
Equivalent Explicit Logic:
>>> a, b = 3, 5
>>> temp_a, temp_b = a, b
>>> a = temp_b
>>> b = temp_a + temp_b
>>> a
5
>>> b
8
The order of operations in a, b = b, a+b is that the tuple (b, a+b) is constructed, and then that tuple is assigned to the variables (a, b). In other words, the right side of the assignment is entirely evaluated before the left side.
(Actually, starting with Python 2.6, no tuple is actually constructed in cases like this with up to 3 variables - a more efficient series of bytecode operations gets substituted. But this is, by design, not a change that has any observable differences.)
It's python standard way to swap two variables, Here is a working example to clear your doubt,
Python evaluates expressions from left to right. Notice that while
evaluating an assignment, the right-hand side is evaluated before the
left-hand side.
http://docs.python.org/3/reference/expressions.html#evaluation-order
a=[1,2,3,4,5]
for i,j in enumerate(a):
if i==1:
a[i-1],a[i]=a[i],a[i-1]
print(a)
output:
[1, 2, 3, 4, 5]
For more info , read this tutorial

how to keep reducing several numbers and each stops at a pre determined value

let's say i have several numbers and i want to keep reducing each of them by some given value. each number must stop reducing at a pre determined value. My code is below.
a = b = c = 100
x = y = 1
print a, b, x, y
s = 1
while s:
if a >= 11:
a -= x
if b >= 2:
b -= y
if c >= 21:
c -= y
print a, b, c
if a == 10 and b == 1 and c == 20:
s = 0
Can this be done in a more efficient way?
Why not use python lists, that way you can define any amount of numbers each with their own decrease and their own stop limit, adding a number is as simple as adding the values to each list rather than having to type extra code;
numbers = [10, 100, 1000]
decrease = [1, 10, 100]
stop = [5, 50, 500]
b = True
while b:
b = False
for c, n in enumerate(numbers):
if n <= stop[c]:
continue
numbers[c] = n - decrease[c]
b = True
print numbers # [5, 50, 500]

Pythonic way to do conditionally assign variables

Any suggestions on how to do that in Python?
if x():
a = 20
b = 10
else:
a = 10
b = 20
I can swap them as below, but it's not as clear (nor very pythonic IMO)
a = 10
b = 20
if x():
[a, b] = [b, a]
(a,b) = (20,10) if x() else (10,20)
Swapping values with a, b = b, a is considered idiomatic in Python.
a, b = 10, 20
if x(): a, b = b, a
One nice thing is about this is you do not repeat the 10 and 20, so it is a little DRY-er.

Categories

Resources