Task for printing table of numbers (patterns) - python

The task is to print this table of digits. I am a total beginner. How can I approach it, and are there any "lifehacks" for doing that kind of pattern? Right below I have attached my code - but this one is not what I need.
0
1 0 9
2 1 0 9 8
3 2 1 0 9 8 7
4 3 2 1 0 9 8 7 6
Code:
for start in range(10,14):
for i in range(start,6,-1):
print(i%10, end='')
print()

You have to find the pattern.
In each row the starting number is increased by 1. In each column the numbers are decreased by 1 and then modulo 10 is applied. The number of columns can be calculated from the row number.
for row in range(5):
for col in range(row * 2 + 1):
print((row - col) % 10, end = ' ')
print()
The code is flexible enough to support an arbitrary number of rows. Running the code with for row in range(12): will give you the following result
0
1 0 9
2 1 0 9 8
3 2 1 0 9 8 7
4 3 2 1 0 9 8 7 6
5 4 3 2 1 0 9 8 7 6 5
6 5 4 3 2 1 0 9 8 7 6 5 4
7 6 5 4 3 2 1 0 9 8 7 6 5 4 3
8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1
0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9

Here's a solution:
print(0)
for row in range(1, 10):
for i in range(row, 0-1, -1):
print(i, end=' ')
for i in range(1, row+1):
print(10-i, end=' ')
print()
As for lifehacks - you need to try and master easier problems first. Try these questions or these questions
And for writing the shortest code using some advanced tricks:
print(0)
for row in range(1, 10):
print(*range(row, 0, -1), end=' ')
print(0, end=' ')
print(*range(10-1, 10-row-1, -1), end=' ')
print()

Related

modifiedSentence = a solved sudoku well it will print it close to each other i want it to be every 3 on col or row make a advanced space it means boxs

for i in range(9):
for j in range(9):
print(modifiedSentence[i][j], end=" ")
print()
modifiedSentence =
7 8 1 2 6 9 3 4 5
3 2 4 7 1 5 6 9 8
5 6 9 4 3 8 1 2 7
2 9 7 3 5 4 8 1 6
4 5 3 1 8 6 9 7 2
8 1 6 9 7 2 5 3 4
9 3 8 6 4 7 2 5 1
6 4 2 5 9 1 7 8 3
1 7 5 8 2 3 4 6 9
and i want it :
7 8 1 2 6 9 3 4 5
3 2 4 7 1 5 6 9 8
5 6 9 4 3 8 1 2 7
2 9 7 3 5 4 8 1 6
4 5 3 1 8 6 9 7 2
8 1 6 9 7 2 5 3 4
9 3 8 6 4 7 2 5 1
6 4 2 5 9 1 7 8 3
1 7 5 8 2 3 4 6 9
means every box in this sudoku has a emptiness in his colum and row as a box
You need to add a space every third iteration of the inner loop, and a newline every 3rd iteration of the outer loop:
for i in range(9):
if i > 0 and i % 3 == 0:
print("")
for j in range(9):
if j > 0 and j % 3 == 0:
print("", end = " ")
print(modifiedSentence[i][j], end=" ")
print()

Table in python

In python I tried to make table like this
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
I tried this code:
for i in range(1, 11):
for j in range(1,10):
print(j, end=" ")
print(i)
but it outputs:
1 2 3 4 5 6 7 8 9 1
1 2 3 4 5 6 7 8 9 2
1 2 3 4 5 6 7 8 9 3
1 2 3 4 5 6 7 8 9 4
1 2 3 4 5 6 7 8 9 5
1 2 3 4 5 6 7 8 9 6
1 2 3 4 5 6 7 8 9 7
1 2 3 4 5 6 7 8 9 8
1 2 3 4 5 6 7 8 9 9
1 2 3 4 5 6 7 8 9 10
Your outer loop is only used to count the number of rows that will be printed. Seems like you want a 10x10 table, so you have 10 rows.
for row in range(10):
Then you want the numbers 1 through 10 printed on each row:
for column in range(1,11):
print(column, end=' ')
Then you want to write a newline so that each row shows up on a new line.
print()
That should do it.
for i in range(10):
for j in range(1,11):
print(j, end=" ")
print()
or
for i in range(10):
for j in range(1,11):
print(j, end=" ")
print()
You don't actually want to print i in the outer loop. You only need the print statement there for your line break. You can change how many columns there are y modifying the range in the inner loop (j loop)

Simple Python number generation

I'm missing something very simple. I'm a new Python student, so bear with me.
for i in range(10):
print("\n")
for x in range(10):
print(x, end = " ")
Sample output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
...
The "i" variable is setting the size of what I want to output, and in the above example I am showing the range of 0-9, 10 times. How would I alter this to display the following without using the print command by way of a string or an array? I'm trying to use two nested "for" statements, but I'm drawing a blank on what to use in place of the range command.
Desired output:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
...
You are printing the variable from your inner loop (x) instead of the variable from the outer loop (i). Do it this way:
for i in range(10):
print("\n")
for x in range(10):
print(i, end = " ")
why to loop twice, we know str*n give n times str
>>> for x in range(10):
... print((str(x) + ' ')*10)
...
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
demo:
>>> 'a'*5
'aaaaa'
>>> 'a'*10
'aaaaaaaaaa'
>>> 'hello**'*10
'hello**hello**hello**hello**hello**hello**hello**hello**hello**hello**'
>>> '9'*10
'9999999999'
This also works:
for i in range(10):
print ' '.join(str(i) * 10)

Nested Loops Python

Hi I am working with nested loops but can not figure out a way to start a new row for my nested loop below:
for i in range(0,10):
for j in range(10):
print(i,end =" ")
Output
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9
The output I want is:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
and so on
Thank you but I found the answer!
for i in range(0,10):
for j in range(10):
print(i, end=" ")
print()
This (and minor variations) also work:
for i in range(10):
print((('%s '%i)*10)[:-1])

How can I print this?

I am trying to teach myself python using interactivepython.org. I have come across a problem that I can not figure out. I have the slope and the spacing correct. I need it to print one less number every time. Could anybody help a newbie out?...
The code I have written:
numLines = 10
for i in range(numLines):
for k in range(i):
print(' ', end = ' ')
for j in range(1, numLines):
print(j, end = ' ')
print()
print(" ")
Prints:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Want to Print:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Well, The above answers are perfectly fine. But this is my way of doing things... :)
Code:
l = map(str,range(0,10))
for i in range(10):
print ' '.join(l[:len(l)-i]).rjust(20)
Output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Hope this helps :)
Try this:
numLines = 10
for i in range(numLines, 0, -1):
for j in range(0, numLines - i):
print " ",
for k in range(0, i):
print k,
print
How about this
numLines = 10
for i in range(numLines):
print "".join (" " for j in range(i)) + " ".join (str(j) for j in range(numLines - i))
Output
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Never mind I figured it out. I had to decrement numLines by one inside the first for loop.

Categories