Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

Python: how to get a string (e.g aaaXXX) to the form of a3X3?

OpenStudy (e.mccormick):

Ah, so you are counting. Use a loop to cycle through the string. Count when string[i]==string[i+1]. When they don't match, add some results to the output and reset counters.

OpenStudy (anonymous):

And if I want to do the opposite, meaning d3x3 = dddxxx?

OpenStudy (anonymous):

and how should I define i?

OpenStudy (e.mccormick):

Well, then you need to find the numbers. But it gets tricky. d11x27 for example.

OpenStudy (e.mccormick):

Ever worked with regular expressions?

OpenStudy (anonymous):

Maybe it would help if I inserted my code I have made so far?

OpenStudy (anonymous):

a little

OpenStudy (anonymous):

My code so far: print("Insert random combination of letters (e.g dddXXX) or "), print("random combination of letters and numbers (e.g d3X3).") text = input("Combination: ") if text.isalpha(): i = for i in text[i]==text[i+1]: elif not text: print("You entered nothing, try again:") text = input("Combination: ") else: print("")

OpenStudy (e.mccormick):

OK. So you are finding them, just not doing anything with them yet. Also, you are going to overrun the string that way. You need to find the length and work that way.

OpenStudy (anonymous):

len(text)?

OpenStudy (e.mccormick):

for i in "test" Does does i='t' to start. Well, t+1 is not e. It is invalid. for i in range(len("test")-1) on the other hand, is very workable. Then it does three spots. So when it compares spot 2 to spot 2+1 it won't go past the string.

OpenStudy (e.mccormick):

It has to do with this: string s = 'test' means: s[0] = 't' s[1] = 'e' s[2] = 's' s[3] = 't'

OpenStudy (e.mccormick):

And s[3+1] or s[4] is out of range. That is why you use len(s)-1.

OpenStudy (anonymous):

That makes sense.

OpenStudy (anonymous):

It probably is quite rude of me, but currently I must go away from the computer. So if you have any more pointers, please write them down, else thank you very much for your help.

OpenStudy (e.mccormick):

Well, I think that will give you enough to work on it a little more. I also found a few things for doing the reverse that might help: Use regex to find the numbers: http://www.tutorialspoint.com/python/python_reg_expressions.htm Or http://stackoverflow.com/questions/4289331/python-extract-numbers-from-a-string The tricky thing is, you will want to find one number at a time, then do the work on the letter before it, then start again to find the next number.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!