Using Python as an example:
for i in range(-5, 5, 1):
print(i % 4, end = " ") ## Will output 0 1 2 3 0 1 2 3...
Is there a nice way do get a reverse cycle with decreasing input including negative numbers? How would I get the following without using conditions?
for i in range(5, -5 , -1):
print('''Something here''', end = " ") ## Will output 3 2 1 0 3 2 1 0...
What's wrong with this simple offset shift:
for i in range(5, -5 , -1):
print((i+2)%4, end = " ")
outputs:
3 2 1 0 3 2 1 0 3 2
Related
I am new to python and I've been trying to wrap my head around this code:
stop = int(input())
result = 0
for a in range(4):
print(a, end=': ')
for b in range(2):
result += a + b
if result > stop:
print('-', end=' ')
continue
print(result, end=' ')
print()
When I input 6, the output is
0: 0 1
1: 2 4
2: 6 -
3: - -
why isn't it
0: 0 1
1: 3 4 --> since we're starting out with a = 1 and b = 2 so result is 1 + 2 = 3.
etc
I feel like I'm missing something fundamental.
Value of b will never be 2.
Each iteration of loop will initialise the scope variables. i.e. while looping first loop, value of b will range between 0 & 1.
Whereas, Value of result (a global variable) will be cumulative (value obtained from prev iteration).
iteration
a
b
result
output
1
0
0
0
0: 0..
2
0
1
1
0: 0 1
3
1
0
2
1: 2..
4
1
1
4
1: 2 4
5
2
0
6
2: 6..
6
2
1
9
2: 6 9
7
3
0
12
3: 12..
8
3
1
16
3: 12 16
when a = 0 and b = 1, result = 0 + 0 + 1 = 1,
so, for a = 1 and b = 0, result = 1 + 1 + 0 = 2
when a = 1 and b = 1, result = 2 + 1 + 1 = 4
so, it will print 1: 2 4
I want to write a Program for the Fibonacci series and need to show series in Pyramid.
Enter (through command line) the number of times the Fibonacci has to iterate.
Ex:
Enter the number of times
6
Fibonacci Series of the number is: 0 1 1 2 3
Below is the expected output -
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
I have tried this code as below -
n= int(input("enter the number of rows: "))
a=0
b=1
for i in range(a,n):
a=0
b=1
print(b,end="")
for j in range(a,i-1):
c=a+b
print(c,end="")
a=b
b=c
print()
But this is giving below output
1
1
11
112
1123
11235
the above output starting from "1" but the expected output should start from "0"
Please help me with correct python code as expected
Thanks
Try:
n = int(input("Enter the number of rows: "))
fib = []
for i in range(n):
fib.append(fib[-2] + fib[-1] if i > 1 else i)
print(' '.join(str(x) for x in fib))
Output:
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
In your code, you are computing the Fibonacci sequence from zero again and again for each row, which is redundant. Instead, you can make a list and add one item at each iteration.
I've used join to insert a blank between entries, which I believe is more "pythonic."
It often makes for simpler code if you separate computation from doing pretty output. So construct your series first:
>>> series = [0,1]
>>> n = 6
>>> while len(series) < n:
series.append(series[-1]+series[-2])
>>> series
[0, 1, 1, 2, 3, 5]
Then do the output:
>>> sseries = [str(s) for s in series]
>>> sseries
['0', '1', '1', '2', '3', '5']
>>> for row in range(len(sseries)+1):
print (" ".join(sseries[:row]))
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
There is some small change that your code will require. You have to make end=" ", so that it will print space there.
n= int(input("enter the number of rows: "))
a=0
b=1
for i in range(a,n):
a=0
b=1
print(a, b,end=" ")
for j in range(a,i-1):
c=a+b
print(c,end=" ")
a=b
b=c
print()
This question already has answers here:
Making a shift function in 2048
(3 answers)
Closed 4 years ago.
So I have some code to shift up a board of numbers in a 2048 game:
data = [0, 2, 4, 8, 0, 2, 8, 0, 0, 0, 0, 2, 4, 2, 2, 0]
def drawBoard(): # Making the board into a 2d array
count = 0
for i in range(16):
print(data[i], end = ' ')
count += 1
if count == 4:
print("")
count = 0
print(drawBoard())
for col in range(4): #Loop to shift numbers up
count = 0
for row in range(4): # read loop
if data[row*4+col] != 0:
data[count*4+col] = data[row*4+col]
count += 1
for row in range(count, 4):
data[row*4+col] = 0
print(drawBoard())
This basically takes data, makes it into a 4 x 4 board and shifts all the non zero numbers up.
Non Shifted Board:
2 2 4 8
0 2 8 0
0 0 0 2
4 2 2 0
Shifted Board:
2 2 4 8
4 2 8 2
0 2 2 0
0 0 0 0
Is there a way using the same format as what I commented "loop to shift numbers up" and make this shift up function into a shift left function?
So the board shifted left looks like:
2 2 4 8
2 8 0 0
2 0 0 0
4 2 2 0
I suggest you to rotate the row/col reference, and the code will be the same. Immagine that you turn the grid counter clockwise so your first column is the last row and your first row is the first column
I'm trying to print a half pyramid that stars on the left side in python.
So far, this is my code
for i in range(1,12):
for j in range(12 - i):
print(" ", end = " ")
for j in range(1, i):
print(j, end = " " )
print("\n")
and my output is
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
However, my output is meant to be in the opposite order:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
How can I make this change?
Just reverse the second loop -- the one that prints that actual numbers:
for j in range(i-1, 0, -1):
The last parameter controls the "step", or how much the variable changes on each loop iteration. Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
...
Reverse the range by adding the third argument (-1). Also format your numbers to use 2 places, so 10 is not pushing the last line to the right. Finally, the last print should probably not have \n, since that is already the default ending character of print:
for i in range(1,12):
for j in range(12 - i):
print(" ", end = "")
for j in range(i-1, 0,-1):
print(str(j).rjust(2), end = "" )
print()
You could just reverse the range that you print out as numbers
for i in range(1,12):
for j in range(12 - i):
print(" ", end = " ")
for j in reversed(range(1, i)):
print(j, end = " " )
print("\n")
The problem is in your second for loop, as you are looping from 1 to i, meaning you start off with 1 being printed first, and every following number until (not including) i.
Fortunately, for loops are able to go in reverse. So, instead of:
for j in range(1, i)
You could write:
for j in range((i-1), 0, -1)
Where the first parameter is signifies where the loop starts, the second is where the loop finishes, and the third signifies how large our jumps are going to be, in this case negative. The reason we are starting at i-1 and finishing at 0 is because loops start at exactly the first given number, and loop until just before the second given number, so in your given code the loop stops just before i, and so this one starts just before i as well, although you could remove the -1 if you wish to include 12 in the pyramid.
In this question I had to create a program that prompts the user for a number and then prompt again for how many rows to create. Something like:
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
This is what I came up with and I have tried many different ways to get the same result but it didn't work.
num=int(input("Enter a number between 1 and 10: "))
rows=int(input("Enter how many rows to of numbers: "))
for i in range(num):
print(i,end=" ")
for x in range(rows):
print (x)
This is the output I came up with:
Enter a number between 1 and 10: 6
Enter how many rows to of numbers: 4
0 1 2 3 4 5 0
1
2
3
You may simply do it like:
num = 5
rows = 4
for i in range(1, num+1):
print('{} '.format(i) * rows)
Output:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
Explanation: When you multiply a str with some number say n, new string is returned with original string repeated n times. Doing this you will eliminate your nested loop
Simple solution: Just use nested for loops:
num = int(input("Enter a number between 1 and 10: "))
rows = int(input("Enter how many rows to of numbers: "))
for i in range(num):
print ('\n')
for x in range(rows):
print (i + 1)
The code above will go through the range 0 to num, printing first a new line and then printing the current number rows times.
rows = 5
side = int(input("Please Enter any Side of a Square : "))
for i in range(side):
for j in range(side):
if(i == 0 or i == side - 1 or j == 0 or j == side - 1):
print(i, end = ' ')
else:
print(i, end = ' ')
print()