5

today I bring you an apparently simple question, that it's not so simple as it seems(for me at least)!

Imagine I have the following list of integers:

num = [3,1,1,2]

And I want to print "$" corresponding with the height i.e:


&
&     & 
& & & &



for i in num:
    print("#"*i)


prints this:


& & &
&
&
& &


However I want the former displayed!

I tried this:


for i in range(1, max(num)+1): # loops through the rows 
   for j in num:
       if j == i:
            print("#")
       else:
            print("")

But after a while I understood that the condition doesn't make any sense, because I'm comparing row numbers with the height!

I tried other stuff but none of them worked properly, I would appreciate if someone could help me out! Thanks

5
  • 1
    you can store them in a matrix and then transpose it and print.
    – vb_rises
    Commented Sep 10, 2019 at 15:48
  • And how would you store this values in the matrix ? (without numpy) Commented Sep 10, 2019 at 15:52
  • ok, can you show the expected output for the input [1, 3, 1, 1, 2, 5] ? Commented Sep 10, 2019 at 15:53
  • @RomanPerekhrest, each number on the list correspond to the number of vertical symbols, so you'd have 1 symbol , 3 symbols , 1 symbol ... m 5 symbols all displayed horizontally Commented Sep 10, 2019 at 15:56
  • Are you using $, &, or # your question is insconsistent the entire way. Commented Sep 10, 2019 at 17:07

2 Answers 2

5

I would just iterate backwards from the max number, checking each element in your list if it is greater to or equal to that number, and printing the desired character, else printing a space.

>>> for x in range(max(num), 0, -1):
...     print(''.join(['&' if i >= x else ' ' for i in num]))
...
&
&  &
&&&&
6
  • There should be a space between horizontal &'s
    – user10732646
    Commented Sep 10, 2019 at 15:58
  • it should be ' '.join(... you can delete space inside & to make it more readable.
    – user10732646
    Commented Sep 10, 2019 at 16:00
  • @joumaico happy now? :P
    – Chris
    Commented Sep 10, 2019 at 16:01
  • @joumaico it just messes w/the spacing in the print statement so it lines up wrong if i do that.
    – Chris
    Commented Sep 10, 2019 at 16:06
  • It's not displaying correctly row number 2, should have $ in the first columns and last column, you have the symbol in the 3rd column, it should be on the 4th (last)! Commented Sep 10, 2019 at 16:08
1

A great question! Here is my take on it without using any packages

def printH(nList):
    m = max(nList)
    while m > 0:
        for l in nList:
            if(l>=m):
                print('#',end = ' ')
            else:
                print(' ',end = ' ')
        print('')
        m-=1
num = [5,3,1,1,2]
printH(num)

Expected output:

#
#
# #
# #     #
# # # # #
1
  • I'm only comparing max value in the first pass of the loop (so the max height). In this pass: all columns with max height are printed with '#'. In the next run I'm checking it with max height -1 (refer to m-=1), this prints out all '#' with max height -1. This goes on until we reach m =0 (i.e., no more height). Try modifying my code by print(m) after 'for l in nList' so that you can track m. Let me know if this explanation does not make sense.
    – Harsha
    Commented Sep 10, 2019 at 16:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.