I have a code that is ridiculously long and I need to use it in many methods. I think there's definitely a better way to write it. But I'm struggling to find it. What the code does is that it:

  1. Takes 12 numbers
  2. It inputs them in a list(let us call it count)
  3. Creates 12 stars variables

It needs to create some sort of matrix that looks like this:

*******     count1
********    count2
********** maxcount (could be any count from count1 to 12, as long as it is the maximum
*****      count5
etc..

So to explain that, we have 12 count variables, I needed to take the largest variable of them, so I put them in a list. After putting them in a list, I selected the max(count) in order to build my astericks representation.

The maximum number of astericks possible is 10. THerefore, the maximum amongst the 12 counts should have 10 stars next to it, and all the others will have their stars relatively.

This is my code, but it doesn't seem optimal at all to me, lots of variables created and initialized as well as it takes some time.

    count = list()
    count.append(count1)
    count.append(count2)
    count.append(count3)
    count.append(count4)
    count.append(count5)
    count.append(count6)
    count.append(count7)
    count.append(count8)
    count.append(count9)
    count.append(count10)
    count.append(count11)
    count.append(count12)

    stars1 = 0
    stars2 = 0
    stars3 = 0
    stars4 = 0
    stars5 = 0
    stars6 = 0
    stars7 = 0
    stars8 = 0
    stars9 = 0
    stars10 = 0
    stars11 = 0
    stars12 = 0

    stars1 = int((count1 * 10) / max(count))
    stars2 = int((count2 * 10) / max(count))
    stars3 = int(count3 * 10 / max(count))
    stars4 = int(count4 * 10 / max(count))
    stars5 = int(count5 * 10 / max(count))
    stars6 = int(count6 * 10 / max(count))
    stars7 = int(count7 * 10 / max(count))
    stars8 = int(count8 * 10 / max(count))
    stars9 = int(count9 * 10 / max(count))
    stars10 = int(count10 * 10 / max(count))
    stars11 = int(count11 * 10 / max(count))
    stars12 = int(count12 * 10 / max(count))

    astericks1 = ""
    astericks2 = ""
    astericks3 = ""
    astericks4 = ""
    astericks5 = ""
    astericks6 = ""
    astericks7 = ""
    astericks8 = ""
    astericks9 = ""
    astericks10 = ""
    astericks11 = ""
    astericks12 = ""

    for i in range(1, 11):
         if (i <= stars1):
             astericks1 += "*"
         else:
             astericks1 += " "
         if (i <= stars2):
             astericks2 += "*"
         astericks2 += " "
         if (i <= stars3):
             astericks3 += "*"
         else:
             astericks3 += " "
         if (i <= stars4):
             astericks4 += "*"
         else:
             astericks4 += " "
         if (i <= stars5):
             astericks5 += "*"
         else:
             astericks5 += " "
         if (i <= stars6):
             astericks6 += "*"
         else:
             astericks6 += " "
         if (i <= stars7):
             astericks7 += "*"
         else:
             astericks7 += " "
         if (i <= stars8):
             astericks8 += "*"
         else:
             astericks8 += " "
         if (i <= stars9):
             astericks9 += "*"
         else:
             astericks9 += " "
         if (i <= stars10):
             astericks10 += "*"
         else:
             astericks10 += " "
         if (i <= stars11):
             astericks11 += "*"
         else:
             astericks11 += " "
         if (i <= stars12):
             astericks12 += "*"
         else:
             astericks12 += " "

2 Answers

0
Devesh Kumar Singh On

As simple as:

# Get 12 numbers as input
s = input("Input 12 numbers separated by a white space")

# Save them in a list and calculate the maximum
nums = [int(item) for item in s.split()]
max_num = max(nums)

for idx, item in enumerate(nums):
    # Calcuate number of stars, ensuring number of stars are never more then 10
    num_stars = int(item * 10 / max_num)

    # print the stars
    print('*' * num_stars, 'count{}'.format(idx))

Sample output will be

Input 12 numbers separated by a white space1 2 3 4 5 6 1 2 3 4 5 6
* count0
*** count1
***** count2
****** count3
******** count4
********** count5
* count6
*** count7
***** count8
****** count9
******** count10
********** count11
4
Alperen Kantarc─▒ On

First of all this kind of if,elif statements and variable creations are really bad coding practice. Secondly, as a little advice you should always think that if you have two different variables for same reasons, you would need list instead.

As i understand from your code you print maximum 10 stars for max value. So the code below should produce same behavior with yours using list comprehensions. Also i should mention that it can be written more efficiently but i wanted to be clear and simple for you.

variable_num = 12
count = [int(input()) for i in range(variable_num)]
max_num = max(count)
count = [(i*10)//max_num for i in count]
for ind,i in enumerate(count):
  print("{} count {}".format(int(i)*"*",ind))