 Looping function in python | Sololearn: Learn to code for FREE!

+2

# Looping function in python

How do I make a function that does this: squareString(2,6) 23456 34562 45623 56234 62345 Please don’t give the full answer as this is a homework. I would instead like to know what the most useful tools would be. I am treating the numbers as a string and got the first line just fine. Now trying to make a for loop with a counter that goes up to the maximum value (+1) and where in every iteration the first character of the previous line is moved to the end of the string. So far, no luck.

+5

If length of srting == 1, squareString() function should return the actual string else start looping, first make a pattern of this format 23456 3456 456 56 6 Using [:i] then in the loop, reverse the string to become 65432 then slice again using [i:]

+3

That is a case for the modulo operator or the itertools-module.

+3

hello MRJN, i think there is no tool that do this and you have to implement your own method. here is how you can do this: 1- the methode parameters are two integers (int start, int end). 2- you have to test if start is equal to end ( in that case do something ) 3- you have to test if start is less than end ( in that case you have to reverse the algorithm ) 4 - you have to treat your string as an array and get the lenght( ex: if start is 2 and end is 6, the array must be [2,3,4,5,6] and it lenght is 5 ) 4- use the lenght to build a valid loop. 5- for each element in the array, let it be the first element of a new array ( ex : i == 4 ). 6- for that element search for the next elements in the first array until the last one, if the lenght of the first array is different than the new one, get elements from the begining of the first array until equality(equality by lenght). hope it helps. best reguards.

0