Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a list of heights with "#" symbol

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

like image 303
José Rodrigues Avatar asked Sep 10 '19 15:09

José Rodrigues


1 Answers

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]))
...
&
&  &
&&&&
like image 88
Chris Avatar answered Sep 18 '22 00:09

Chris