+ 5
Can anyone suggest better approach ???
11 ответов
+ 2
Regarding some smart tutorial, someone already suggested this:
https://goalkicker.com/PythonBook/
In my opinion is one of the best Python book ever written, and it is completely free!
+ 5
I did not pay attention, corrected
https://code.sololearn.com/c6YID48HG3Ov/?ref=app
+ 3
https://code.sololearn.com/cEEYYUAm6GHn/?ref=app
+ 2
You see, you repeat at each iteration the same pattern.
Increase n, then print each line from n to 5.
for j in range(...) can be replaced by:
for j in range(5 - n), you see ?
You only need tow nested loops, one incrementing n, and the second with j.
I will give you my code if you find the way.
+ 2
Sure!
Central idea of both implementations is the star operator ("iterable unpacker"), which transform a list of elements in a sequence of elements.
So:
print(*[1,2,3]) becomes print(1,2,3)
The list is created using the range() function.
In my implementation, I used the "list comprehension" syntax, used for generating a list l1 starting from another list l:
l1 = [ f(x) for x in l]
In my initial line, I tried only one print on a list of lists, starting a new line at every new element. Something like:
print(*[ *[1,2,3,4], *[2,3,4], *[3,4], *[4]], sep='\n')
This means two nidified list comprehensions.
Unfortunately we cannot use the star inside a list comprehension, so I separated the elements passing from int to str (map) and rejoining them with spaces.
Better and more readable solution: only one list of many prints:
[print(*range(n,5)) for n in range(1,5)]
+ 1
Михаил Горчанюк your program is printing a wrong pattern
+ 1
Bilbo Baggins and portpass can you please explain your approach????
+ 1
And thanks all of you
Bilbo Baggins ,portpass Théophile ,Sousou, Mike for your support . This is a great community indeed.
0
Bilbo Baggins can you suggest some good tutorials where I can learn this stuff???