Python Spaced Triangle

前端 未结 4 1705
感情败类
感情败类 2021-01-22 00:17

I\'m supposed to write a program that ends up such as this:

*     *
 *   *
  * *
   *

I have the code written for a regular one, but I\'m not s

4条回答
  •  臣服心动
    2021-01-22 00:44

    Let's label some areas in a line:

    startSpaces   *   middleSpaces   *   endSpaces
    

    For a given line you want startSpaces + 1 + middleSpaces + 1 + endSpaces to equal a constant. This constant is 2*(i+t) + 1

    line 1 will have t=0 spaces before the *
    the final line will have t=i spaces before the * (here I'm using the original i, I know it changes through recursion)

    So can you find a pattern for startSpaces, middleSpaces and endSpaces that will give you the proper pattern?

    Keep in mind that you will need an additional else if case for i==1 so that you can handle the row with only one *


    This should be enough for you to get a lot closer to solving your problem. I'm assuming it's homework so I won't solve it for you, if you get stuck ask for more clues.

提交回复
热议问题