Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 nested for loop dilemma
#1
Hey guys, so I have been doing some basic tasks on while loops for loops which I completed with some help here and there and I mostly grasped it without too much headache.
The task I have now is proving a major challenge but I think its because I haven't really worked with it before and its my first nested loops assignment.

So, I need to use a nested for loop and create the following numbers pyramid as described below:
I understand that the rows will be defined with the outer loop and the columns with the inner loop.
And the code starts something like this I believe:


for x in range(1,i+1):

Here is what I need to create:

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 47
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81

As yet I am just not getting it, I will really be for ever thankful if someone can give me guidance on this,
I do not want the code written for me, after all, I need to learn from this, I just need some pointers because this is driving me mad. Wall

Thanks in advance
Quote
#2
for row in range(10):
    for brick in range(row + 1):
         print('#', end = ' ')
    print()
YoungGrassHopper likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(Sep-11-2019, 04:44 PM)ichabod801 Wrote:
for row in range(10):
    for brick in range(row + 1):
         print('#', end = ' ')
    print()

Hey ichabod801 thanks a stack for the speedy help you are my saviour! , I will go and try and complete it now and I will return with my coding attempt at the pyramid and post it here
Quote
#4
One could start with observing pyramid and trying to find pattern(s).

We can observe that:

- first column is numbers from 1, 9
- number of items in row equals to the row number/first number in row (row #1 has 1 number, row #2 has two numbers .... row #9 has 9 numbers)
- numbers in row are multipliers of first number of row (a.k.a row number)

It follows that integers i with values 1...9 are emitted, then i is multiplied with values j in range 1, i + 1.

Visually it can be represented:

- > 1 (i) -> 1 * 1 (j)
- > 2 (i) -> 2 * 1 (j), 2 * 2 (j)
- > 3 (i) -> 3 * 1 (j), 3 * 2 (j), 3 * 3 (j)
...
YoungGrassHopper likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#5
(Sep-11-2019, 04:44 PM)ichabod801 Wrote:
for row in range(10):
    for brick in range(row + 1):
         print('#', end = ' ')
    print()

Yay! haha..


for row in range(1,10):
    for column in range(1,row + 1):
         print(row * column , end = ' ')
    print()
        
its the first time I see "end" used like that in the inner print line, how does it work if its not defined in the code? neither do I see its coloured like other commands but yet it has a function? can you please explain how it works if you don't mind?
Quote
#6
just for fun

for i in range(1,10):
    for n in range(i, i ** 2 + 1, i):
        print(n, end=' ')
    print() 

for i in range(1,10):
    print(' '.join(str(n) for n in range(i, i ** 2 + 1, i)))
    

# as oneliner    
print('\n'.join(' '.join(str(n) for n in range(i, i ** 2 + 1, i)) for i in range(1, 10)))
newbieAuggie2019 and YoungGrassHopper like this post
Quote
#7
(Sep-11-2019, 05:45 PM)YoungGrassHopper Wrote: can you please explain how it works if you don't mind?
https://docs.python.org/3/library/functions.html#print
Quote
#8
(Sep-11-2019, 05:29 PM)perfringo Wrote: One could start with observing pyramid and trying to find pattern(s).

We can observe that:

- first column is numbers from 1, 9
- number of items in row equals to the row number/first number in row (row #1 has 1 number, row #2 has two numbers .... row #9 has 9 numbers)
- numbers in row are multipliers of first number of row (a.k.a row number)

It follows that integers i with values 1...9 are emitted, then i is multiplied with values j in range 1, i + 1.

Visually it can be represented:

- > 1 (i) -> 1 * 1 (j)
- > 2 (i) -> 2 * 1 (j), 2 * 2 (j)
- > 3 (i) -> 3 * 1 (j), 3 * 2 (j), 3 * 3 (j)
...

Thanks for your guidance perfringo I appreciate it. I saw the pattern that part was very obvious to me but for some reason I could not lay in down in code, I believe its because this is my very first nested for loop task and its very new to me, I am being hit with a crazy amount of new info the last week I started with a software engineering bootcamp last Thursday and I have never coded prior to that, so really this is a whole new world to me, I am like a neathandral who just found fire for the very first time lol so if I sound very stupid its because Im a super noob and I am trying my best to stay above water, all of the advice I have received so far means a great deal to me you guys are top shelf

(Sep-11-2019, 05:52 PM)buran Wrote:
(Sep-11-2019, 05:45 PM)YoungGrassHopper Wrote: can you please explain how it works if you don't mind?
https://docs.python.org/3/library/functions.html#print

Hey buran Thanks for the sample code and info I appreciate it, I will have a look at it soonest : )
Quote
#9
(Sep-11-2019, 05:50 PM)buran Wrote: just for fun

for i in range(1,10):
    for n in range(i, i ** 2 + 1, i):
        print(n, end=' ')
    print() 

for i in range(1,10):
    print(' '.join(str(n) for n in range(i, i ** 2 + 1, i)))
    

# as oneliner    
print('\n'.join(' '.join(str(n) for n in range(i, i ** 2 + 1, i)) for i in range(1, 10)))

Wow this is pretty cool, I am absolutely jealous , Thanks for enlightening me about the alternative ways to do it buran : )
Quote
#10
This is a problem you need to work out on paper. You need to visualize what the loops are doing to solve it. As you analyze the pyramid, think of what is happening on each loop pass. Your outer loops count from 1 to the depth and the inter loop is a counter.
If I say anymore I'd give it away.

Draw it out on paper and if you figure out the solution you will have really learned something. It is a very important skill to figure out an algorithm to solve a problem. Be patient with it.

Work at it!
John
buran likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Nested Loop for user input Ads 2 174 Dec-30-2019, 11:44 AM
Last Post: Ads
  openpyxl nested for loop help rmrten 3 416 Oct-16-2019, 03:11 PM
Last Post: stullis
  nested for loop dilemma 2 YoungGrassHopper 12 517 Sep-12-2019, 02:06 PM
Last Post: YoungGrassHopper
  Nested while loop problem + turtle DreamingInsanity 3 470 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  Index error using pop in nested loop PerksPlus 3 548 Mar-28-2019, 03:11 PM
Last Post: ichabod801
  Nested for loop strange problem mcva 2 471 Mar-16-2019, 12:53 PM
Last Post: mcva
  Nested while loop in pyramid program. raj55 2 2,181 May-04-2018, 10:05 AM
Last Post: ThiefOfTime
  Nested Loop Problem GSWes 5 1,437 Nov-12-2017, 10:22 PM
Last Post: Larz60+
  Using nested for loop with a single list mikeavison 3 1,104 Aug-12-2017, 08:13 PM
Last Post: ichabod801
  A "Nested Loop" example from python 3.3.4 to 2.6!! sarada2099 6 2,573 Dec-18-2016, 02:56 PM
Last Post: sarada2099

Forum Jump:


Users browsing this thread: 1 Guest(s)