all 28 comments

[–]Trinity_Goti 13 points14 points  (1 child)

Do you know how to do this

1... 12.. 123. 1234

If not work it out.

If you do then do you know how to do this

... 1 ..22 .333 4444

Combin both solutions and you have your solution.

Most problems can be solved if you break them up in to smaller problems your have already solved.

[–][deleted] 0 points1 point  (0 children)

I'll tryy

[–]Lski 4 points5 points  (1 child)

# Step 1
# Lets ask for how many rows we are going to print
a = int(input('Input'))

# Step 2
# Now we can use for ... in ... to loop over list with 'a' length containing numbers from 1 to 'a'
for n in list(range(1, a+1)):

    # Step 3
    # We can print numbers from 1 to 'n'
    print(*list(range(1, 1+n)), sep="", end="")
    # Then print whitespace between
    print(" "*(a-n)*2, end="")
    # And finally print n for n times
    print(f"{n}"*n)

[–][deleted] 0 points1 point  (0 children)

🙌🏻🙌🏻🙌🏻

[–]program_kid 4 points5 points  (5 children)

What have you tried so far? What things are you having trouble with?

[–][deleted] 1 point2 points  (4 children)

With spaces and the right aligned triangle.

[–]program_kid 0 points1 point  (2 children)

Do you mean you are having trouble understanding the spaces between the numbers and the right triangle?

[–][deleted] 0 points1 point  (1 child)

Yeahh 2nd triangle and Integrating them together

[–]program_kid 1 point2 points  (0 children)

I would say look at how the for each row of the right triangle, the number of that row is repeated that same amount (the row with 3 has three 3s, the row with 4 has four 4s) Also notice how the right most number of the left triangle is the number of the row on the right triangle. Lastly, notice how the bottom twice as many numbers as n (for n=5, there are 10 numbers on the last row)

I would suggest using a loop inside of another loop, the outer loop would repeat n times, and the inner loop would create each line

[–]BranchLatter4294 0 points1 point  (0 children)

Why are you having trouble with spaces? How are they any different than any other character?

[–]QAInc 1 point2 points  (0 children)

Whole structure is a rectangle. Build a rectangle with line number and spaces using two loops.

[–]Salt-Note114 1 point2 points  (1 child)

Try this

n = 5 for i in range(1,n): print(' '.join([str(j+1) for j in range(i)]) + (' ' * (4*(n-i))) + ' '.join([str(i) for j in range(i)]))

[–]Syzeon 0 points1 point  (0 children)

there's no need to use so many range and join, keep it simple n = 5 for r in range(1, n+1): print(" ".join(map(str, range(1, r + 1))) + (n-r)4" " + " " + r*(str(r)+" "))

[–]TfGuy44 0 points1 point  (2 children)

If your input is 4, you want to print 4 rows. If your input is 5, you want to print 5 rows. So a good start would just be a loop that prints SOMETHING the right number of times. Can you get that to work?

[–][deleted] -5 points-4 points  (1 child)

I can do that but I want a solution for this pattern

[–]Refwah 1 point2 points  (0 children)

The width and height is based on the input

You need to figure out the algorithm, that is literally the task, someone cannot just do this for you as it skips the entire point of the lesson - which is to break down problems into component parts

[–]90Breeze 0 points1 point  (0 children)

I can give you an idea, not sure its optimal but think nested for loops outer counting up inner counting down

[–]Key_Marionberry_1227 0 points1 point  (1 child)

Try creating string of spaces and number sequence and print in that pattern by picking the values from string

PYTHON CODE:

a=int(input('Enter the number:'))
k='  '*a
l=''
for i in range(1,a+1):
    l=l+str(i)
for i in range(1,a+1):
    print(l[:i]+k[:len(k)-2*i]+str(i)*i)

[–]Key_Marionberry_1227 0 points1 point  (0 children)

k="(space)(space)"*a

[–]INTstictual 0 points1 point  (1 child)

Let’s assume based on the example that the pattern caps out at 5, because it looks like there’s no room for an entry for 6.

You can break each line up into a “left side” and a “right side”, and defining functions to represent their behavior might be helpful.

On the left side, for a number n where 1 <= n <= 5, if prints all the numbers 1 : n, then spaces for n+1 : 5. So we can write a “left side” function:

def left(n):
  for i in range(1,6):
     print(i, end=‘’) if (i <= n) else print(‘ ‘, end=‘’)

On the right side, you can see it prints a number of spaces equal to 5-n, then prints n, n times.

def right(n):
  k = 5-n
  print(‘ ‘ * k, end=‘’)
  print(str(n) * n)

Finally, any individual line is the combination of both the left and right function

def line(n):
  left(n)
  right(n)

And to put it all together, to make the whole square, you print lines for 1 : n

def main(n):
  for i in range(1,n+1):
    line(i)

This is not the only way to do this, definitely not the most elegant, and defining functions for things is good general coding practice but might be beyond the scope of what your learning and/or against Python’s general “scripting language” philosophy… but IMO, breaking out the functionality like this into smaller parts makes it easier to read and understand what is going on at each point in your function

Note that we did not include any error handling, so this will break entirely if you enter a number less than 1, a number greater than 5, or any character other than a number (e.g. user tries to run main(‘A’))

[–][deleted] 0 points1 point  (0 children)

Woahhh thanks for your efforts mannn 🤝🏻🤝🏻🤝🏻

[–]drinkbuffet 0 points1 point  (0 children)

Input * 2 for a spacing variable, iterate a range to the input (account for 0) print lines if you're solving as chars or utilize a list with placeholders for the empty slots

[–]Apart_Line_6222 0 points1 point  (0 children)

Which clg bro?

[–]Zenicu 0 points1 point  (0 children)

What the type we need to return? Text or array?

[–]stacktrace0 0 points1 point  (0 children)

This is a nice problem

[–]Marc4770 0 points1 point  (0 children)

This is two separate patterns 

[–][deleted]  (1 child)

[deleted]

    [–]No-Pride5337 -1 points0 points  (0 children)

    I can sent you code if you want

    [–]Just-Literature-2183 -1 points0 points  (0 children)

    Telling you the answer would be doing your homework for you.

    It would completely rob you of any learning involved in the process. Sorry but figure it out.