0

i have a list ["a","b","c"] where `"a" should represent 85% of the output and "b" should represent 10% of the output and "c" should represent 5%

i want to print a array that has these percentage of its size and the array size is variable

for ex: if input size is 20 then "a" should in the array 17 times and "b" should be there 2 times and "c" 1 time

any idea ?

Temo Temo
  • 31
  • 6

1 Answers1

0

If I got your question well, I would start by getting:

  • the ouput number
  • the percentages you want the three letters to represent (unless it's fixed at 85 - 10 - 5)

then proceed to divide said input number by 100 and multiply it by the desired percentage, to initiate a for loop for each of the letters, populating your array (which will stay variable in size).

To represent what I got of your question in Python3:

print("please type an integer, then Enter : ")
# the following integer will be the size of your array. Here I chose to prompt the user so that you can execute and see for yourself that you can change it as you like and the solution will stay valid. 
startingInteger = int(input())
print(f"the startingInteger is {startingInteger}")

print("what is the percentage of a?")
aPercentage = int(input())
print("what is the percentage of b?")
bPercentage = int(input())
print("what is the percentage of c?")
cPercentage = int(input())

array = []

for i in range(int(round(startingInteger/100*aPercentage))):
  array.append("a")

for i in range(int(round(startingInteger/100*bPercentage))):
  array.append("b")

for i in range(int(round(startingInteger/100*cPercentage))):
  array.append("c")

print(array)

You would then need to scramble the resulting array (if that's what you mean by "random"), which should pose no problem if you import the "random" module and use the scrambled(). It's quick and dirty and inelegant but the logic is very explicit this way, hope it illustrates the idea.

WildWilyWilly
  • 121
  • 11
  • 1
    This does only work for one distribution and only for `'a', 'b', 'c'`. In the second `print` statement you missed the `f` in the f-string. You are not supposed to solve the example in a question literally but generally. – Michael Szczesny Nov 10 '20 at 11:46
  • I think you were right, I have edited my answer to be broader in scope. Also dividing by 20 instead of 100 meant multiplying by desired_percentage/5 and didn't add any clarity. – WildWilyWilly Nov 10 '20 at 12:49