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
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:
print()
would add, manually.The easiest way to do this is to use a list, to which you append each string you are printing.
lines
would probably be a good idea.print()
calls with that method.'\n'
string then Python will use a newline character (\...
is an escape sequence, \n
produces a newline character)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With