The python code, given in a lab for a class I'm in when executed through pycharm, ouputs 0. However, upon looking at the code, it should be 1. Why is it 0?
X = 4
C = 0
while X > 0:
if X % 2 == 0:
C = C + 1
else:
C = C - 1
X = X - 1
print(C)
The code is fine. Your X will go from 4 to 1 and on X = 0, the program will leave the loop. Printing X-C for each iteration gives the values:
X-C
4-1
3-0
2-1
1-0
If you want it to go till 0, make the condition as:
while X >= 0:
The loop executes 4 times: When X = 4, C = 1; when X = 3, C = 0; when X = 2, C = 1; when X = 1, C = 0.
Related
When I put this python code with input 12, the answer is 0, 6, 18. I dont know how to calculate that and I keep visualizing it as code snippet 2, with the answers 0,0,1,3,6,6,8,12.
How does this loop work?
stop=int(input())
result=0
for a in range(5):
for b in range(4):
result += a * b
print(result)
if result > stop:
break
What I calculate
stop=int(input())
result=0
for a in range(5):
for b in range(4):
result += a * b
print(result)
if result > stop:
break
picture of my calculations
I'll walk you through your for a in range(5) loop.
First, a = 0, result = 0.
This loops 4 times and result remains at 0 because 0 * b = 0
0 is printed
Next, a = 1, result = 0.
Result += 1x0 + 1x1 + 1x2 + 1x3
So result = 0 + 6
6 is printed
Finally, a = 2, result = 6.
Result += 2x0 + 2x1 + 2x2 + 2x3
So result = 6 + 12 = 18
18 is printed
if result > stop evaluates to true so the loop is broken.
I'm just trying to get the logic straight and using Python to help me do it. Ultimately, I need to solve this problem using ImageJ macro language.
I have no idea if I'm using the right term, but I'd like to create a "snake" counter.
x = 1
number = 12
maxVal = 3
minVal = 1
for i in xrange(number):
%do something
x = incrementSnakeCounter(x, maxVal, minVal)
print("i = ", i)
print("x = ", x)
The "snake" part is making the counter go up only to the maxVal, repeating that number on the next iteration, counting down to the minVal, repeating that value on the next iteration, and repeating the process.
For instance, in the above
I'd like the following to happen :
i = 0
x = 1
i = 1
x = 2
i = 2
x = 3
i = 3
x = 3
i = 4
x = 2
i = 5
x = 1
i = 6
x = 1
i = 7
x = 2
i = 8
x = 3
i = 9
x = 3
i = 10
x = 2
i = 11
x = 1
You will find some useful utils in itertools:
from itertools import chain, cycle
def snake(lower, upper):
return cycle(chain(range(lower, upper+1), range(upper, lower-1, -1)))
> s = snake(1,3)
> [next(s) for _ in range(10)]
[1, 2, 3, 3, 2, 1, 1, 2, 3, 3]
Here's a silly mathematical solution:
def snake(low, high, x):
k = (high-low+1)
return k - int(abs(x % (2*k) + low - k - 0.5))
[snake.snake(1,3,x) for x in range(8)]
[1, 2, 3, 3, 2, 1, 1, 2]
Add a conditional to determine if x should be increasing or decreasing at any given point within the loop.
x = 1
number = 12
maxVal = 3
minVal = 1
for i in xrange(number):
%do something
if(xIsIncreasing)
x = incrementSnakeCounter(x, maxVal, minVal)
else
x = decrementSnakeCounter(x, maxVal, minVal)
print("i = ", i)
print("x = ", x)
Then inside your incrementSnakeCounter() change the value of xIsIncreasing to false when x == maxVal and inside your decrementSnakeCounter() to true when x == minVal (you'll have to do some work to make sure that you're staying at the same value twice in a row, I don't have time right now to solve that part for you).
You can write a little custom generator.
The key is to create a list of the pattern you want to repeat [1, 2, 3, 3, 2, 1] and then index that with the modulo of the length to get the repeating behavior:
def snake(x, max_v=3, min_v=1):
cnt=0
sn=list(range(min_v, max_v+1,1))+list(range(max_v, min_v-1,-1))
while cnt<x:
yield cnt, sn[cnt%len(sn)]
cnt+=1
Then:
for i,x in snake(12):
print("i=",i)
print("x=",x)
print()
Prints:
i= 0
x= 1
i= 1
x= 2
i= 2
x= 3
i= 3
x= 3
i= 4
x= 2
i= 5
x= 1
i= 6
x= 1
i= 7
x= 2
i= 8
x= 3
i= 9
x= 3
i= 10
x= 2
i= 11
x= 1
Write a program that displays 12 digits,
each digit is equal to three times the digit before him.
I tried to code like this
a , b , c = 1 , 1 , 1
print(c)
while c < 12 : # for looping
c = c + 1 # c for counting
b = a+b
y = 3*b
print(c,y)
can any one help me to correct the result
You can use power operator for that:
from itertools import islice
def numbers(x, base=3):
n = 0
while True:
yield x * base ** n
n += 1
for n in islice(numbers(1), 12):
print(n)
Or if you really like your way of doing that, here's a fixed version of your code:
b, c = 1, 0
while c < 12:
print(c, b)
b *= 3
c += 1
You can start with a list where first element is the beginning of the multiples:
-- start with 1 or the number you like
multiples = [1]
for i in range(1, 12):
multiples.append(3 * multiples[i - 1])
print(multiples)
-- Output : [1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147]
I am attempting to solve Project Euler: Problem 3, and I am using the following function to test for primality,
def check_prime(x):
i = 1
b = 0
while b == 0 :
i += 1
if i >= x :
return True
b += 1
elif x%i == 0 :
return False
b += 1
Which I call from the rest of my program
a = 3
z = []
number_used = 600851475143
while a < number_used/2 :
if check_prime(a) and number_used%a == 0 :
z.append(a);
a += 2
else :
a += 2
print z
But, the code is not printing the prime factors needed for the third Euler problem. Is my code too inefficient to manage it?
What is the thought process when evaluating a loop? I really have no idea how the shell gets these answers (A: 12, B: 2, C: 4, D: 6).
A, B, C, D = 0, 0, 0, 0
while A <= 10:
A += 2
if A%3 == 0:
B += 1
else:
C += 1
D += 1
Perhaps you can read it more easily if you break it down:
A = 0
while A <= 10:
A += 2
Can you read this? Do you understand how it gets to 12?
A, D = 0, 0
while A <= 10:
A += 2
D += 1
Also including D should not make it any harder.
Can you read and understand the if-statement by itself?
if A%3 == 0:
B += 1
else:
C += 1
How about when it is inside the loop?
A, B, C, D = 0, 0, 0, 0
while A <= 10:
A += 2
if A%3 == 0:
B += 1
else:
C += 1
D += 1
B and C are related; exactly one of them are incremented in each iteration, so they should add up to the same as D, which they do.
Do you have any specific problems reading and understanding this now? :)
The other answers are good. I would highly recommend going through things with a pen and paper to make sure you understand what's going on.
Using print inside the loop is also useful to see what is going on while your program runs.
A,B,C,D = 0,0,0,0
while A <= 10:
A += 2
if A%3 == 0:
B += 1
else:
C += 1
D += 1
print "A =", A, " B =", B, " C =", C, " D =", D
The output shows you the values of A, B, C, D at the end of every loop iteration.
A = 2 B = 0 C = 1 D = 1
A = 4 B = 0 C = 2 D = 2
A = 6 B = 1 C = 2 D = 3
A = 8 B = 1 C = 3 D = 4
A = 10 B = 1 C = 4 D = 5
A = 12 B = 2 C = 4 D = 6
You can see that:
A gets incremented by 2 every loop iteration
B gets incremented by 1 IF A is divisible by 3, that is A%3 == 0
C gets incremented by 1 IF A is NOT divisible by 3
D gets incremented by 1 every loop iteration
When it comes to loops, you can think of the collection of indented code as a single "chunk" of code that gets executed once for every repetition of the loop. The formal term for this code chunk is a block. It also applies to if/else statements.
The body of the while loop will execute 6 times (for A=0,2,4,6,8,10).
At each iteration, A is incremented by 2, so after the first statement
within the loop it has values 2,4,6,8,10,12.
B is incremented by one twice (when A=6 and A=12);
C is incremented by one for the remaining values of A.
D is incremented every time round the loop.
Hence, after the loop, A=12, B=2, C=4 and D=6.