Python optimization

Asked

Viewed 26 times

-3

Is there any way to optimize the code below? I feel like I used many "Fors".

texto = ''
num = 10
for c in range(1, num):
    linha = ''
    for v in range(1, num - c):
       linha += ' '
    linha += '/'
    for g in range(1, c*2-1):
        linha += ' '
    linha += '\\'
    texto += linha + '\n'
print(texto)

Output:

        /\
       /  \
      /    \
     /      \
    /        \
   /          \
  /            \
 /              \
/                \
  • 3

    We don’t even know what the code should do. Or the criteria. There is a way that is extremely optimized, just print the result without doing any processing, gives the same result.

  • 1

    All lines are equal, what changes is the amount of spaces before the /, between the bars and after the \\. If you parameterize this by making the solution more generic, your solution will be more optimized. Good luck.

  • 1

    Optimized not to use for: print(" /\\\n / \\\n / \\\n / \\\n / \\\n / \\\n / \\\n / \\\n/ \\") - See working here: IDEONE

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.