The code doesn't give any output - python

I'm trying to do the following in my code:
A man is trying to shoot some balloons with a rock.
a is the number of balloons in a row.
b is the speed of the rock the man is trying to shoot with.
c is the speed that the rock loses after hitting each balloon.
In the end I want to know how many balloon have been shot by the rock.
The problem is that the code doesn't want to give me any output. Please help!
def Balloons(a, b, c):
d = 0
for i in (0, a):
b - c
d = d+1
if d == int(a):
print(d)
if d > int(a):
d = d-1
print(d)
if int(b) <= 0:
print(d)
x = int(input('give me the number of test cases: '))
for i in range (0, x):
a = int(input())
b = int(input())
c = int(input())
Balloons(a, b, c)

Problems
There is no output because in your definition of your code, it you wrote b - c which does nothing. You need to tell Python what to do with the result of b - c.
What you should do instead is to use to subtraction assignment operator:
b -= c
Which is equivalent to:
b = b - c
This will give you some output but not the right output. Another problem with your code is that you missed out the keyword range in the for loop of your function:
for i in (0, a):
What this means in the loop is that i will be the value of 0 and a then the loop will end. It should be changed to:
for i in range(0, a):
Lastly, you may want to add some break statements which exit the for loop so that you only have one output:
if d == int(a):
print(d)
break
if d > int(a):
d -=1
print(d)
break
if int(b) <= 0:
print(d)
break
Final solution
The resulting code using subtraction assignment and addition assignment operators would be:
def Balloons(a, b, c):
d = 0
for i in range(0, a):
b -= c
d += 1
if d == int(a):
print(d)
break
if d > int(a):
d -=1
print(d)
break
if int(b) <= 0:
print(d)
break
x = int(input('give me the number of test cases: '))
for i in range(0, x):
a = int(input())
b = int(input())
c = int(input())
Balloons(a, b, c)
Alternative method
However, looking at your problem, it could be solved with basic math, math.ceil and the min function.
The number of balloons shot through is equals to the smaller of:
ceil(Initial speed/Speed loss)
Number of balloons
Hence, you can have a much smaller function:
import math
def Balloons(a, b, c):
return min(math.ceil(b/c), a)

b-c
I think you want to write b -= c

This should work
def Balloons(a, b, c):
d = 0
for i in range(0, a):
b=b-c
d=d+1
if d==int(a):
print(d)
if d>int(a):
d=d-1
print(d)
if int(b)<=0:
print(d)
x = int(input('give me the number of test cases: '))
for i in range(0, x):
a=int(input())
b=int(input())
c=int(input())
Balloons(a, b, c)

Related

Arranging Integers using Conditional statements

I was wondering if someone would be able to help me regarding my coding problem. I'm still a beginner and I may have missed something here.
Basically, the instruction says that I need to arrange 3 integers in ascending order.
e.g Input : 5 2 4 ------> Output : 2 4 5
I need to utilize the conditional statements for this. Here is what I've done so far.
a, b, c = input().split()
a = int (a)
b = int (b)
c = int (c)
if a < b and a < c:
smallest = a
elif b < a and b < c:
smallest = b
else:
smallest = c
if a > b and a < c:
middle = a
elif a < b and a > c:
middle = a
elif b > a and b < c:
middle = b
elif b < a and b > c:
middle = b
else:
middle = c
if a > b and a > c:
largest = a
elif b > a and b > c:
largest = b
else:
largest = c
print(smallest, middle, largest)
This is on CodeChum btw, I'm stuck in this because I can't figure out why it doesn't accept the code I did. Basically there are 5 tests that the code needs in order to submit the whole activity. I managed to do test 1-4 but for some reason it fails on test 5.
The problem is that your code does not react well if two numbers are identical. You should replace your strict inequalities with non-strict ones.
a = 3
b = 3
c = 1
if a > b and a < c:
middle = a
elif a < b and a > c:
middle = a
elif b > a and b < c:
middle = b
elif b < a and b > c:
middle = b
else:
middle = c
print(middle) # 1 when it should be 3
As a == b, none of the 4 conditions can be True, and therefore the middle number will always be c.
In order to support the case where a == b, we can add another condition:
elif a == b:
middle = a
else:
middle = c
we can simplify this code by replacing the strict inequalities with non-strict ones (⩽ , <= in Python):
if b <= a < c:
middle = a
elif b >= a > c:
middle = a
elif a < b < c:
middle = b
elif a > b > c:
middle = b
else:
middle = c
We simply said that if a == b, the middle is always a. I also simplified the structure for better readability.
Why don't you simply use sorted function?
This one results in the exact thing you are looking for.
smallest, middle, largest = sorted(map(int, input().split()))
print(smallest, middle, largest)
Note that you don't need the map to int function for this. But you if don't do that, the smallest, middle, largest variables will remain str objects.
As the input is guaranteed to contain exactly 3 numbers (separated by whitespace) you can just do this:
if (inval := input()):
if len(nums := inval.split()) == 3:
try:
lo, mid, hi = sorted(map(int, nums))
print(lo, mid, hi)
except ValueError:
pass

Ascending and descending order in Python 3

Just starting to learn Python and this is my problem:
I am taking input a and b and if a < b then then output is a to b in ascending order. If a > b then the output is in descending order. When I put in a < b it works but it gives me nothing when a >b. This is the code:
a = int(input('input a number for a: '))
b = int(input('input a number for b: '))
numbers = list(range(a, b + 1))
if a < b:
print(numbers)
else:
numbers.sort(reverse=True)
print(numbers)
This is the output when a > b:
input a number for a: 10
input a number for b: 1
[]
Process finished with exit code 0
When a is 10 and b is 2 your code is doing:
>>> numbers = list(range(10, 2))
>>> numbers
[]
Maybe you want to do:
numbers = list(range(min(a, b), max(a, b) + 1))
Okay so what you want to do is this....
a = int(input('input a number for a: '))
b = int(input('input a number for b: '))
list = []
if a > b:
while a > b:
list.append(a)
a -= 1
How about
a = int(input('input a number for a: '))
b = int(input('input a number for b: '))
print(list(range(a, b + (b >= a) - (a > b), 1 - 2 * (a > b))))
You can change numbers to the following:
numbers = list(range(a, b + 1)) if a < b else list(range(b, a + 1))
and your code will work just fine. You can also shorten your code by doing it like this:
a = int(input('input a number for a: '))
b = int(input('input a number for b: '))
numbers = list(range(a, b+1)) if a < b else list(range(b, a+1))
print numbers if a < b else sorted(numbers, reverse=True)
Because the last value range() prints is the biggest value for start + step * i that is less than end. If end is less than start and the step is positive, there is no number which satisfies that condition:
10 + i * 1 will never be smaller than 1 (where i is a positive integer)
Instead, do this before you range:
if a > b:
a, b = b, a
which will ensure a is always the smaller number moving forward, by swapping their values if that is not the case.
If you want to preserve the ascending/descending order:
if a > b:
a, b = b, a
rev = True
Then, after building the list
if rev:
numbers.reverse()

Finding Greatest Common Divisor through iterative solution (python 3)

I am trying to find the great common divisor by using a function and solving it iteratively. Though, for some reason, I am not sure why I am not getting the right output.
The greatest common divisor between 30 & 15 should be 15, however, my output is always giving me the wrong number. I have a strong feeling that my "if" statement is strongly incorrect. Please help!
def square(a,b):
'''
x: int or float.
'''
c = a + b
while c > 0:
c -= 1
if a % c == 0 and b % c == 0:
return c
else:
return 1
obj = square(30,15)
print (obj)
You should return a value only if you finished iterating all numbers and found none of them a divisor to both numbers:
def square(a, b):
c = a + b
while c > 0:
if a % c == 0 and b % c == 0:
return c
c -= 1
return 1
However, the last return will be unneeded in this case, as c would go from a + b to 1, and mod 1 will always bring a common divisor, so the loop will always terminate with 1, for the worst case.
Also, a number greater than a and b can not be a common divisor of them. (x mod y for y > x yields x), and gcd is the formal name for the task, so I would go with
def gcd(a, b):
for c in range(min(a, b), 0, -1):
if a % c == b % c == 0:
return c
for iterational solution.
You might be interested to know that there is a common recursive solution to the GCD problem based on the Euclidian algorighm.
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
print(gcd(30, 15))
# 15

GCD implementation in python

I am solving this problem in SPOJ and it states that :
Problem statement is simple. Given A and B you need to calculate
S(A,B) .
Here, f(n)=n, if n is square free otherwise 0. Also f(1)=1.
Input
The first line contains one integer T - denoting the number of test
cases.
T lines follow each containing two integers A,B.
Output
For each testcase output the value of S(A,B) mod 1000000007 in a
single line.
Constraints
`T <= 1000
1 <= A,B <= 1000000`
Example
Input:
3
42 18
35 1
20 25
Output:
306395
630
128819
I wrote this code for this problem (if I got the the problem right) :
def gcd(a,b): #gcd(a,b)
if b==0:
return a
else:
return gcd(b,a%b)
# print gcd(42,18)
import math
def issquarefree(n): #sqare free number check
i=2
s=i*i
if (n==1 or n==2) or n==3:
return True
while s<=n:
if n%s==0:
i=-1
break
else:
i+=1
s=i*i
if i==-1:return False
else:
return True
for i in range(int(raw_input())): #main program
a,b=map(int,raw_input().split())
g=gcd(a,b)
sa=(a*(a+1))/2 #see below
sb=(b*(b+1))/2 #see below
gc=issquarefree(g)
s=0
if gc== False:
print 0
elif gc==True:
s+=sa*sb*g
print s%1000000007
here I found that so applying this to the problem # S(A,B) I wrote this as (multiplication of sum of first A and B numbers ) multiplied by f(n) which is gcd(a,b) or 0.
But I am not getting the expected output to this problem so is my code wrong or I got the problem wrong
my output vs expected
3
35 1
42 18
20 25
630 630
926478 306395
341250 128819
Writing out the G(a, b) = f(gcd(a, b)) (so that you can use the cited formula) is incorrect since the function is not constant. The proper solution is this:
for i in range(int(raw_input())):
A, B = map(int, raw_input().split())
# proper algorithm
s = 0
for a in xrange(1, A):
for b in xrange(1, B):
s += a * b * G(a, b)
print s % 1000000007
You obviously have to implement G function properly (as returning 0 or gcd(a, b)).
Careful analysis of G might give some optimization insight but it is definitely not a trivial one if any.
Here is a simple optimization:
import fractions
DIVISOR = 1000000007
def is_not_square_free(a):
counter = 1
factor = 1
while factor < a:
counter += 1
factor = counter * counter
if a % factor == 0:
return True
return factor == a
def F(n):
if n == 1:
return 1
if is_not_square_free(n):
return 0
return n
_CACHE = {}
def G(a, b):
a = a % DIVISOR
b = b % DIVISOR
key = (a, b) if a > b else (b, a)
if key not in _CACHE:
_CACHE[key] = (a * b * F(fractions.gcd(a, b))) % DIVISOR
return _CACHE[key]
def S(A, B):
s = 0
for a in range(1, A+1):
for b in range(1, B+1):
s += G(a, b)
return s
for _ in range(int(raw_input())):
A, B = map(int, raw_input().split())
print(S(A, B) % DIVISOR)
def gcd(a, b):
returns greatest common divisor of a and b'''
return gcd(b % a, a) if a and b else max(a, b)
print test gcd should print 6,5, 7, and 9'''
print gcd(48,18)
print gcd(10,5)
print gcd(14,21)
print gcd (9,0)

Python: string formatting and calling functions

So I'm running in to the string formatting error when trying to pass the arguments num1 and num2 to the function gcd. I'm not sure how to fix this. Please bear with me since I'm new to Python programming. Thanks!
#!/usr/bin/python
import sys
from collections import defaultdict
lines = sys.stdin.read()
lineArray = lines.split()
listLength = len(lineArray)
def gcd(a, b):
c = 0
if a > b:
r = a%b
if r == 0:
return b
else:
return gcd(b, r)
if a < b:
c = b
b = a
a = c
return gcd(a, b)
for x in range(0, listLength):
num1 = lineArray[x]
num2 = lineArray[x+1]
print num1, 'and', num2
print gcd(num1, num2)
print 'end'
It's pretty simple. lineArray is not a list containing integers, but strings.
So when you do this:
r = a%b
It tries to format the string a, instead of calculating a%b.
To solve this, convert a and b to integers:
def gcd(a, b):
a,b = int(a),int(b)
c = 0
if a > b:
r = a%b
if r == 0:
return b
else:
return gcd(b, r)
if a < b:
c = b
b = a
a = c
return gcd(a, b)
Also, in your gcd function, the recursion never ends. Hint: You'll have to check if b is 0.
Hope this helps!

Categories

Resources