Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 diamond pattern definition

I write down my code as follow but, my teacher said I have to return the pattern. I used print() and I have no idea for this. Please help me.

def diamond(num):

    a = 1
    b = int((num - 1) / 2)
    c = num - 2
    d = 1
    for i in range(0, int((num - 1) / 2)):
        print( " " * b + a * "*")
        a += 2
        b -= 1
    if num % 2 == 1:
        print("*" * num)
        for i in range(0, int((num - 1) / 2)):
            print( " " * d + c * "*")
            c -= 2
            d += 1
    else:
        print("*" * (num - 1))
        print("*" * (num - 1))
        c = num - 3
        for i in range(0, int((num - 1) / 2)):
            print( " " * d + c * "*")
            c -= 2
            d += 1
like image 364
juyong Lee Avatar asked Dec 14 '22 16:12

juyong Lee


1 Answers

I'm deliberately not going to write the code for you, but instead giving you pointers as to how you can solve the problem on your own.

Currently, your function uses multiple print() function calls to write lines to the console. Your teacher wants you to return those lines, as a single value, to the caller of the function.

There are two problems you must overcome:

  1. you must collect all those lines you are now printing into a single string to return.
  2. you must add the newlines that print() would add, manually.

The easiest way to do this is to use a list, to which you append each string you are printing.

  • Put a new variable at the top of your function, assign an empty list. A name like lines would probably be a good idea.
  • Append each line you are now printing, to that list. The list type has a method to append a value to it, you can trivially replace all the print() calls with that method.
  • At the end of the function, use a single string method to concatenate all the values in the list into one long string. You specify a string to put between the values, if you use the '\n' string then Python will use a newline character (\... is an escape sequence, \n produces a newline character)
  • return the resulting single string.

You can find the methods needed for this in the Built-in Types documentation.

You could also use a string variable at the top, then concatenating each string you now print, to that variable, adding a newline each time. But experienced programmers use a list instead, because that's cheaper for a computer program to process. Concatenating a string to another string requires the computer to create a whole new copy for the result, so it has to read all the characters of the old string, all the characters of the concatenated string, and then create a copy of all those characters to put into a new string. You then do the same for the next string you concatenate, but now the result is longer still. You end up making a lot of copies of those initial characters.

Appending a value to a list doesn't have to make a copy, on the other hand, and you then only have to create a copy of all the characters in all the strings in the list once to create the output. In terms of algorithmic complexity, repeatedly concatenating a string takes O(n**2) time (for n strings, concatenating takes quadradic time, add one more string and the time doubles), while appending those same strings to a list only takes O(n) time (for n strings, adding one more takes linear time, add one more string and the time grows proportionally).

For a half a dozen strings, the difference is not going to be noticeable, but when your program has to process thousands of lines, things can get out of hand quickly. Concatenating a thousand strings in a loop can take a thousand times more time than adding those same strings to a list first, then joining them together afterwards. It quickly becomes obvious why programmers have to care about these little details.

like image 156
Martijn Pieters Avatar answered Dec 29 '22 11:12

Martijn Pieters