How can I print this? - python

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.

Related

irregular spaces making pattern abnormal

I have a pattern which i printed using below code
Code :
n=5
def pyramidupdown(n):
cnt=0
space=2
lst= [str(row) for row in reversed(range(1,n+1))]
for i in range(1,n+1):
if i == 1:
s=' '.join(lst)
print(s)
else:
lst[cnt]=' '
s=' '.join(lst)
print(s)
cnt = cnt + 1
It prints the pattern below as output :
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
But my issue is with spaces when the n value is defined 2 digit like 15
the pattern is not printed properly
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
14 13 12 11 10 9 8 7 6 5 4 3 2 1
13 12 11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Expected output :
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
14 13 12 11 10 9 8 7 6 5 4 3 2 1
13 12 11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
what changes do i need to make in existing code to print properly the pattern
I would just do it like this:
def pyramidupdown(n):
for i in range(n,0,-1): # loop n rows (in descending order)
lst = []
for j in range(n,0,-1): # loop n numbers (in descending order)
s = str(j)
# at the i-th row replace the first i string numbers
# (i.e. where j > i)
# by as many spaces as there are characters in that string
if j <= i:
lst.append(s)
else:
lst.append(' '*len(s))
print(" ".join(lst))
you can even make it a 1-liner (just for the fun):
def pyramidupdown(n):
print('\n'.join([" ".join([str(j) if j <= i else ' '*len(str(j)) for j in range(n,0,-1)]) for i in range(n,0,-1)]))
Now that I understand your code: here's the minimal tweak to make it work:
def pyramidupdown(n):
cnt=0
lst= [str(row) for row in reversed(range(1,n+1))]
for i in range(1,n+1):
if i == 1:
s=' '.join(lst)
print(s)
else:
lst[cnt]=' '*len(lst[cnt]) # here replace by correct number of spaces
s=' '.join(lst)
print(s)
cnt = cnt + 1

how to calculate the given factorial equation of huge numbers?

How can I find the last digit of X^(N!)?
X can go up to 10^9
N can go up to 10^18
I know how to do it when only one of them is large but not both.
ps: execution time is 1 sec
I have no proof for this but...
Let's assume our target function is:
import math
def pow_fact_mod_last_digit_exact(x, y):
return pow(x, math.factorial(y), 10)
but for large values of x and y this would just take too long.
This is actually equivalent to:
import math
def pow_fact_mod_last_digit(x, y):
return pow(x, math.factorial(min(y, 4)), 10)
To test it for the first few hundred numbers:
print(all(
pow_fact_mod_last_digit(x, y) == pow_fact_mod_last_digit_exact(x, y)
for x in range(-300, 300) for y in range(300)))
# True
How did I went for it (empirically)
Let us just observe how pow(x, y, 10) behaves for some values of x and y:
n = 20 # x
m = 24 # y
print(f'{"":2s}', end=' ')
for y in range(m):
print(f'{y:2d}', end=' ')
print()
for x in range(n):
print(f'{x:2d}', end=' ')
for y in range(m):
print(f'{pow(x, y, 10):2d}', end=' ')
print()
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 1 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8
3 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7
4 1 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4
5 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
6 1 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
7 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3
8 1 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2
9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9
10 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
12 1 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8
13 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7
14 1 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4
15 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
16 1 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
17 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3
18 1 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2
19 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9
So, it looks like to get pow(x, y, 10) you only need you only need to know x % 10 (of course) and (y - 1) % 4.
Now, the factorial of a number factorial(n) % k is 0 for n > k and we only need to take care, at most, for the case of n <= k.
For the case of k = 4, we have:
import math
print([(i, math.factorial(i) % 4) for i in range(10)])
# [(0, 1), (1, 1), (2, 2), (3, 2), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0)]
So we do not need to worry for values of y above 4 as they will behave like 4.
EDIT: Apparently this is obvious from Fermat's Little Theorem (but it was not obvious to me O:-) ) and #OneLyner's answer contain essentially the same observations as above, as well as the reference to the theorem in the comments.
Use the fact that
pow(x, k, 10) = pow(x % 10, 1 + (k-1) % 4, 10)
You just need to know the factorial modulo 4.
And obviously, N > 3 => N! % 4 == 0, which should make your life easy.

Task for printing table of numbers (patterns)

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()

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])

Categories