0

I need to get a 5 digit integer as an input, and convert this integer into a 2D array and print it out as a bar graph. The result I am trying to get is:

If the input is 19683, It should return:

  x
  x   x
  x   x
  x x x
  x x x
  x x x
  x x x x
  x x x x
x x x x x
----------

This is what I have written already

x = int(input("Enter a 5 digit integer: "))
digits = [int(n) for n in str(x)]
rows = max(digits)
bar_graph = [[0] * len(digits) for i in range(rows)]

But I don't know what I should do from here.

I just need to find a way to replace the 0s with the xs and in the right order.

4 Answers 4

1

You started off good. You need to have each digit and to know the maximum digit (you got with rows = max(digits)).

Now all you need to do is loop the rows in decreasing order, and for each digit check if it needs to be marked in this row. This will be true when the digit is greater-than or equal-to the row number:

x = int(input("Enter a 5 digit integer: "))
digits = [int(n) for n in str(x)]
rows = max(digits)
bar_graph = []
for row in range(rows, 0, -1):
    bar_graph.append(['x' if digit >= row else ' ' for digit in digits])
for row in bar_graph:
    print(' '.join(row))
print('-'*(2*len(digits)+3))

But note that it's not really necessary to store everything in a 2D list and you can print directly by iterating the rows and digits:

for row in range(rows, 0, -1):
    print(row, ':', ' '.join(['x' if digit >= row else ' ' for digit in digits]))
print('-'*(2*len(digits)+3))

Will give:

Enter a 5 digit integer: 19683
9 :   x      
8 :   x   x  
7 :   x   x  
6 :   x x x  
5 :   x x x  
4 :   x x x  
3 :   x x x x
2 :   x x x x
1 : x x x x x
-------------

Note that this will always "truncate" the graph to the largest digit, for example:

Enter a 5 digit integer: 11132
3 :       x  
2 :       x x
1 : x x x x x
-------------

If you want the graph to always be "complete" (starting with 9), just change this line:

rows = 9

To get:

Enter a 5 digit integer: 11132
9 :          
8 :          
7 :          
6 :          
5 :          
4 :          
3 :       x  
2 :       x x
1 : x x x x x
-------------
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to do this problem using this piece of code too? bar_graph = [[0] * len(digits) for i in range(rows)]
Not sure I understand the question
The question without being edited was: Ask the user to enter a 5 digit integer. Convert the integer to a bar graph stored in a 2- dimensional list of characters. Print the bar graph.
I don't see the reason to store a 2D list when you can print it directly with a simple loop. Sorry to say but if this is a requirement of the exercise, it is a very bad one and not teaching you good coding habits
1

Here you go:

number = input("Enter a number: ")
ls = [9-int(x) for x in s]
for i in range(9,0,-1):
    print(i, ':', *[' ' if v>0 else '*' for v in ls])
    ls = [x-1 for x in ls]
print('-'*(len(s)*2+4))

# input: 19683
# Output:
9 :   *      
8 :   *   *  
7 :   *   *  
6 :   * * *  
5 :   * * *  
4 :   * * *  
3 :   * * * *
2 :   * * * *
1 : * * * * *
-------------

Comments

1

try this:

num = input()
print("\n".join([" ".join([" " if 10-line>int(j) else "x" for j in num]) for line in range(1,10)]))

Comments

1

Come on, and I'll put in my five cents.

x = 196594345345345432

digits = [int(n) for n in str(x)]

for i in reversed(range(1, max(digits) + 1)):
    this_row = ''
    for digit in digits:
        if digit >= i:
            this_row += 'x'
        else:
            this_row += ' '
    print(this_row)

print('-'.rjust(len(digits), '-'))
 x  x             
 x  x             
 x  x             
 xx x             
 xxxx   x  x  x   
 xxxxx xx xx xxx  
 xxxxxxxxxxxxxxxx 
 xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx
------------------

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.