Jump to content

Need some help with python loops

SgtBot

I have been trying to learn python through a book called "Introduction to Computer Science Using Python and Pygame" (it's pretty easy to find the pdf). Anyways, I came across a few questions in the book (on page 53-54) which were called advanced loop questions. It doesn't provide a walkthrough or answers at the end, so I just attempted them myself and was able to get one. I thought I would post the other questions on here if anybody can explain to me how I can complete them. Thanks!

 

Capture.JPG.19aec1c312d0896c4fcb0b19198aa8b2.JPGCapture1.JPG.068c1a1b880135d4f57f5d6b1641008c.JPG

 

I was able to figure out the first 2 fairly easy with some help from forums and google, here is my code for them. I am just unsure on how to continue from there:

Problem 1:

for n in range(10):
    for i in range(10):
        print(i, end=" ")
    print()

Problem 2:

for n in range(11):
    for i in range(n):
        print(i, end=" ")
    print()

Any help would be greatly appreciated! :D

Link to comment
Share on other sites

Link to post
Share on other sites

Update: I have been working on the third problem and I believe i have almost figured it out... here is the code:

for n in range(10):
    for i in range(n):
        print(" ", end=" ")
    for a in range(10):
        print(a, end=" ")
    print()

and this outputs:

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 
      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 
            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 
                  0 1 2 3 4 5 6 7 8 9 

it is almost there, except I can't yet figure out how to make each line print one less number

Link to comment
Share on other sites

Link to post
Share on other sites

I'll just leave hints in spoilers.

 

Spoiler
  1. Already solved
  2. Already solved
  3. Pretend you have a queue of fixed size and the numbers enter from the left and exit from the right
  4. It's a multiplication table (try doing this without a nested loop)
  5. There's a relationship between how many leading spaces you need and how many numbers you need to print
  6. It's #5 and #3. Extra bonus: don't copy and paste code.

EDIT: I was also bored enough to solve these. So I can provide solutions, if you want :B

Edited by M.Yurizaki
Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, SgtBot said:

Update: I have been working on the third problem and I believe i have almost figured it out... here is the code:


for n in range(10):
    for i in range(n):
        print(" ", end=" ")
    for a in range(10):
        print(a, end=" ")
    print()

and this outputs:


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 
      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 
            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 
                  0 1 2 3 4 5 6 7 8 9 

it is almost there, except I can't yet figure out how to make each line print one less number

 

If you combine this with your answer to P2:

4 hours ago, SgtBot said:

Problem 2:


for n in range(11):
    for i in range(n):
        print(i, end=" ")
    print()

then you should be able to do Problem 3.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Question 3 seems to require if statements based on the outer for loop's index. 

Perhaps a subtraction and addition thing going on. 
Psuedo code example spoiler that may be the answer:

Spoiler

for i = 0; i < 10; i++ -- index for each line
	for j = 0; j < (10 - j); j++ -- index for numbers, puts j less than 10 numbers. 
  		for k = 0; k < i; k ++ -- index for spaces
    		print " "
      	print j

line 2 elaboration
For instance on first line, it prints 10-0, 10 numbers. 5th time is 10-5 or 5 numbers. 

I imagine the following ones would be some sort of expansion on this. 
I didn't test it so my logic might be wrong & what I posted isn't proper syntax for any language. It's like C, Python & Lua mixed together. Hopefully my hints before the spoiler help you solve it without you opening my spoiler. 

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, fpo said:

Question 3 seems to require if statements based on the outer for loop's index. 

Perhaps a subtraction and addition thing going on. 
Psuedo code example spoiler that may be the answer:

I imagine the following ones would be some sort of expansion on this. 
I didn't test it so my logic might be wrong & what I posted isn't proper syntax for any language. It's like C, Python & Lua mixed together. Hopefully my hints before the spoiler help you solve it without you opening my spoiler. 

I'm putting my reply in the following spoiler so as to not immediately show what I did.

Spoiler

I created a starter string from 0 - 9, then in the for loop, created a temporary string that gets prepended with spaces depending on what the current iteration is. I found out in Python you can do [char] * [num] to get a string to have that many chars. So 'a' * 4 will put in 'aaaa', and used this to my advantage. Then I appended that string with a substring of the starter.

Though I have a feeling this is more knowledge than what the problems in the OP were expecting a person to know.

 

Link to comment
Share on other sites

Link to post
Share on other sites

That's the fun thing about these puzzles, so many ways to do it.

 

E.g. Using map rather than loops:

Spoiler

print('\n'.join(map(lambda x: '  '*x + ' '.join(map(str, range(10-x))), range(11))))

# 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
                    

 

 

 

15 hours ago, M.Yurizaki said:

[char] * [num] to get a string to have that many chars

Cheers for this, makes its more consise

Link to comment
Share on other sites

Link to post
Share on other sites

These are actually pretty fun. Heres my crack at 3

 

Spoiler
for i in range(9,0,-1):
    print(" "*2*(9-i), end="")
    for j in range(i+1):
        print("%d" % j, end = " ")
    print("")

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×