Home > Software engineering >  Print the appropriate number of characters on each line according to the input list [closed]
Print the appropriate number of characters on each line according to the input list [closed]

Time:09-17

symbol = '#'

Input: 2 3 6 5 2 Output: ## ### ###### ##### ##

CodePudding user response:

Try this:

>>> lst_rep = [2, 3, 6, 5, 2]
>>> print(' '.join('#'*rep for rep in lst_rep))
## ### ###### ##### ##

With the below code check runtime of two approach:

from timeit import repeat
import random
lst_rep = random.sample(range(1, 11), 10)
approach = [
    "' '.join(['#' * rep for rep in lst_rep])",
    "' '.join('#'*rep for rep in lst_rep) "
]
for _ in range(3):
    for appr in approach:
        number = 100
        times = sorted(repeat(appr, globals=globals(), number=number, repeat=3))
        print(*('M ns ' % (t / number * 1e9) for t in times), appr)

Result:

1422 ns  1460 ns  1970 ns  ' '.join(['#' * rep for rep in lst_rep])
1634 ns  1634 ns  1703 ns  ' '.join('#'*rep for rep in lst_rep) 

2370 ns  2460 ns  3190 ns  ' '.join(['#' * rep for rep in lst_rep])
3009 ns  5153 ns  6705 ns  ' '.join('#'*rep for rep in lst_rep) 

2337 ns  2421 ns  2743 ns  ' '.join(['#' * rep for rep in lst_rep])
2809 ns  2835 ns  3344 ns  ' '.join('#'*rep for rep in lst_rep) 

CodePudding user response:

symbol = '#'
user = input()
users = user.split(" ")
users = [int(x) for x in users]
for numbers in users:
    print(numbers * symbol)

i figured it out

CodePudding user response:

please try this and let me know

symbol = '#'

inputNum = [2, 3, 6, 5, 2]

outputNum = ""
for i in inputNum:
    for j in range(i):
        outputNum  = symbol
    outputNum  = " "
    
print (outputNum)

This gives out the output ## ### ###### ##### ##

for if u want a function which take in a symbol and input number and returns the line with that number of symbols then try this

def linecreator (smb, ipnum):
    outputNum = ""
    if isinstance(ipnum, int):
        for i in range(ipnum):
            outputNum  = smb
            
    return outputNum

if __name__ == "__main__":
    line1 = linecreator("A",3)
    line2 = linecreator("B",5)
    line3 = linecreator("C",7)
    
    print (line1)
    print (line2)
    print (line3)

this gives the following outputs

AAA
BBBBB
CCCCCCC
  • Related