I’m trying to use a for loop in order to formulate this:
-
expand('*+123456')should return'1*2+3*4+5*6' -
expand('++123')should return'1+2+3' -
expand('+*1234')should return'1+2*3+4'
A symbol is chosen from the first two characters of the given string in an alternating fashion and placed between the proceeding digits.
Here is what I’ve been working with:
def expand(original):
var = ""
symbols = original[0:2]
for i in range(len(original)):
var = var + symbols + original[i]
return var
I realize that there also must be a original[2:] but I don’t know where I can fit this into in here.
I’m an amateur and I’ve been trying to figure out this question for a long time.
Yes, your function would be improved by adding
[2:]in there, but it could also do with losing therange(len(original)). Whenever you find yourself writingrange(len(something)), you should step back and think about what you’re actually trying to do.In this case, you’re looking for the characters in the string, and you can get those more straightforwardly with
for x in string. Here’s a slightly improved version, taking those ideas into account:This stops you from getting the weird mixture of symbols at the beginning of the output, but it’s still not perfect:
We need to find a way of
We can use itertools.cycle to handle the first of these, and string slicing for the second:
That works, but at some point, someone’s going to pipe up and tell you you shouldn’t use
+or+=to build strings, because it’s inefficient. If we build a list instead, and then usestr.join()to turn that into a string, we can improve things slightly:However, we can do better than that. Rather than having to call
next(symbols)every time, we can usezip()to get the next symbol and next digit a pair at a time:… and that’s probably enough 🙂
EDIT: Since in a comment to another answer, you’ve said you’re not allowed to import anything from the standard library (a rather silly restriction IMO, but there it is), you can use the Python implementation of
cycle()described at the link earlier in this answer:… but you’ll probably have to be prepared to convince your teacher that you understand it, which means you need to understand the yield keyword.