-1

symbol = '#'

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

Flux So2
  • 55
  • 3
  • 3
    This isn't a question. Grammatically it reads like a command for Stack Overflow to do your homework for you. It doesn't work that way. Please read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822). – John Coleman Sep 11 '21 at 11:11

3 Answers3

1

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(*('%4d 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) 
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 2
    you can use `' '.join()` to join the list tho instead of using `*` except you don't need a list then: `print(' '.join('#' * i for i in lst_rep))` – Matiiss Sep 11 '21 at 11:21
  • 1
    however I just tested and found out that a list comprehension would be faster than generator (at the cost of using more memory which for such small amounts of data doesn't really matter), so a faster method would be this: `' '.join(['#' * rep for rep in lst_rep])` – Matiiss Sep 11 '21 at 11:35
  • @Matiiss Thanks a lot for you comment and time, TIL i learn from you and defineatly `list` is faster that `print` but `list` in not OP's expected out but I edit answer now and add you comment and check runtime for two approach, again, thanks a lot – I'mahdi Sep 11 '21 at 11:39
  • 2
    @Matiiss "at the cost of using more memory which for such small amounts of data doesn't really matter" -- and the extra microseconds do matter? In either case OP will see the output before their fingertip is a millimeter from the enter key. In any event, in any context in which such things matter, the time saved in not needing to first construct a list in memory will pay for the overhead in using the generator. – John Coleman Sep 11 '21 at 11:40
  • @JohnColeman thanks a lot `MAN` for your good advice, WOW ;))) – I'mahdi Sep 11 '21 at 11:42
  • @Matiiss edited answer and add check runtime of two approach – I'mahdi Sep 11 '21 at 12:10
  • that is not what I meant, I meant comparing this: `' '.join('#'*rep for rep in lst_rep)` with this: `' '.join(['#'*rep for rep in lst_rep])` because obviously `print` takes longer, also still better to use the generator because of the saved memory space – Matiiss Sep 11 '21 at 12:14
  • @Matiiss I check this, why using `list` reduce `runtime`? – I'mahdi Sep 11 '21 at 12:22
  • @user1740577 I don't know exactly why, maybe because the whole list is saved into RAM and therefore easier to access or sth? I really don't know, however generators do save memory so they are usually preferred if they can be used, here is a related question tho which may answer that: https://stackoverflow.com/questions/53336815/generator-is-slower-compared-to-list-comprehension – Matiiss Sep 11 '21 at 12:24
  • @Matiiss thanks a lot again, today I learn so much from you and John :) – I'mahdi Sep 11 '21 at 12:26
-1

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
t.abraham
  • 108
  • 1
  • 9
-1
symbol = '#'
user = input()
users = user.split(" ")
users = [int(x) for x in users]
for numbers in users:
    print(numbers * symbol)

i figured it out

Flux So2
  • 55
  • 3
  • 3
    But your code doesn't match the output in your own question since it is inserting newline characters rather than spaces between the blocks of symbols. – John Coleman Sep 11 '21 at 11:32