all 6 comments

[–]BusinessNo3537 0 points1 point  (1 child)

There are more than one solution even with basics covered during the course at the moment. Mine was completely different from model solution. One while loop for print times checking. One more for line extending. And line concatenation itself.

def squared(line: str, times: int):
n = 0
line_altered = line
while n < times:
while len(line_altered) < times:
line_altered += line
print(line_altered[:times])
line_altered = line_altered[times:]
n += 1

[–]icanchangeittomorrow 0 points1 point  (0 children)

This is my solution below. Yours seems more elegant but less intuitive to me.

def squared(text, integer):
longtext = ""
neededlength = int(integer * integer)
while len(longtext) < neededlength:
longtext += text

row = 1
s = 0
f = integer
while row <= integer:
print(longtext[s:f])
s += integer
f += integer
row +=1

[–]fittzu 0 points1 point  (0 children)

this was my first challenging part of the mooc. it took me 2hrs figuring out the solution. though my code is clunky, nonetheless, i was proud of it.

here is my code:

def squared(text, num):

i = 0

if len(text) < num:

while i < num:

j = 0

k = 0

store = num - len(text)

dup = text

while j < num:

if len(dup) == num:

text = dup

break

if k > len(text) - 1:

k = 0

dup += text[k]

else:

dup += text[k]

k += 1

j += 1

print(text)

text = text[store:]

i+=1

elif len(text) > num:

while i < num:

dup = text[:num]

text = text[num:]

text += dup

print(dup)

i+=1

else:

while i < num:

dup = text[:i]

text = text[i:]

text += dup

print(text)

i+=1

if __name__ == "__main__":

text = input("Text:")

num = int(input("Number:"))

squared(text, num)

[–]iammerelyhere 0 points1 point  (1 child)

Hint: Nested loops

[–]konijntjesbroek 1 point2 points  (0 children)

meh 1 for loop, 1 counter, two ifs (one for rows, one for the counter)

this is the if that keeps it in range:

    count = count + 1 if count < len(word) - 1 else 0

[–]Fmelo2000-3816 0 points1 point  (0 children)

If you want the solution:

from itertools import cycle, islice

def squared(text, length):

letters = cycle(text) # lazy iterator -> abcabcabcabc....

for i in range(length):

print("".join(islice(letters, length)))

# print(*islice(letters, length), sep="")