3

Say I have a code given in a string for example: str(00012333210-1)

The numbers relate to a certain amplitude on a wave I wish to graph.

I want to transform the code into a printable wave where the numbers are replaced by asterisks such as:

 3:     ***
 2:    *   *
 1:   *     *
 0:***       *
-1:           *
-2:
-3:

Any useful tools to go about doing this?

So far I have tried to split the string into individual numbers however am struggling with the negatives.

For example:

file = "000123332101233321000"
x = [int(i) for i in file]
print(x)

I only have a very basic understanding of python so i'm very stuck at the moment.

AJ4314
  • 29
  • 4
  • Welcome to SO. Please have a look at https://stackoverflow.com/help/how-to-ask and provide a [Minimal example](https://stackoverflow.com/help/mcve) of what you have tried so far. – MrLeeh Apr 14 '18 at 08:51

2 Answers2

2

The first thing to recognize is that printing in a terminal goes from left to right, then top to bottom. One line at a time. Since your data are transposed (each value belongs to a column, not a line), you'll need to buffer your entire data set first, before printing it. In pseudocode it should be like this:

  1. Tokenize string into a list of numbers L.
  2. Calculate max(L). Loop from this down to min(L).
  3. In each iteration (which is one line of output), loop over L, and print an asterisk whenever the value in L matches the loop index, and a space otherwise.

The second and third steps are something like this:

for y in range(max(L), min(L) - 1, -1):
    for val in L:
        print('*' if val == y else ' ', end='')
    print() # next line

For your example:

L = [0, 0, 0, 1, 2, 3, 3, 3, 2, 1, 0, 1, 2, 3, 3, 3, 2, 1, 0, 0, 0]

It prints:

     ***     ***     
    *   *   *   *    
   *     * *     *   
***       *       ***
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

You can use the below code to convert the string to a list of integers:

y = '00012333210-1'
nums = []

for i in y:
    if i == '-':
        sym = i
        continue
    nums.append(int(sym+i))
    sym = ''

I am trying to think of better ways to do this.

KBN
  • 153
  • 1
  • 8