| | |
Scanning a string?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Ok, so from my last post, you might have figured that I'm making a coder/decoder package. Anyways, say I've got a string of letters that i want to decode, such as:
AAAGGGCCC
Which should produce:
ACB
Because a=aaa c=ggg and b=ccc
Currently with my program, you have to input one three letter string at a time, then it translates it to the one letter string. How could I get python to read every three letters in the string in order, then output the result? For instance, with my program as is you have to do the following:
AAA <enter>
GGG <enter>
CCC <enter>
Then it outputs ACB.
How could i do this?
AAAGGGCCC <enter>
Then outputs ACB.
So yeah, how do I do that?
AAAGGGCCC
Which should produce:
ACB
Because a=aaa c=ggg and b=ccc
Currently with my program, you have to input one three letter string at a time, then it translates it to the one letter string. How could I get python to read every three letters in the string in order, then output the result? For instance, with my program as is you have to do the following:
AAA <enter>
GGG <enter>
CCC <enter>
Then it outputs ACB.
How could i do this?
AAAGGGCCC <enter>
Then outputs ACB.
So yeah, how do I do that?
Blender is by far the best free 3D computer Graphics program and it is expandable with python! GoTo www.blender3d.org
Would you mind posting your code so we can see how you did it and change it from there?
Here's a little program I made to do the same thing:
My dictionary keys are the letters in the triple (so "AAA" has a key of "a") and then the values are the decoded values ("GGG" has a value of "c", so the dictionary item is "g":"c"). You can add stuff to the dictionary obviously and get a more advanced translator.
Is that what you were looking for?
Here's a little program I made to do the same thing:
python Syntax (Toggle Plain Text)
d = {'a':'a','g':'c','c':'b'} # key is actually talking about a triple 'aaa':'a' while True: stuff = raw_input("string: ").lower() if stuff == "exit": break s = "" for triple in range(0,len(stuff),3): decoded_str = d.get(stuff[triple],"error") if decoded_str == "error": s = "error" break s += decoded_str if s != "error": print s.upper() else: print "One of the triples in your code could not be decoded."
My dictionary keys are the letters in the triple (so "AAA" has a key of "a") and then the values are the decoded values ("GGG" has a value of "c", so the dictionary item is "g":"c"). You can add stuff to the dictionary obviously and get a more advanced translator.
Is that what you were looking for?
possibly, im going to need to look into using and manipulating dictionaries, but I can't paste my code here, because of the noobish nature of the program, it's over 600 lines long, the program can decode up to 10 triplets or code up to 10 letters, because I have 26 if/elif/else statements to decode each letter, with 10 of them outputting different variables, I know its noobish, and proboably uneccasery, but that was all I know how to do =(
Blender is by far the best free 3D computer Graphics program and it is expandable with python! GoTo www.blender3d.org
I don;t understand yours but I'm going to have a crack at deciphering it later, after I've learned dictionaries.
Blender is by far the best free 3D computer Graphics program and it is expandable with python! GoTo www.blender3d.org
Actually, if you could explain your entire code step by step, that would be appreciated, as long it's ok with you.
Blender is by far the best free 3D computer Graphics program and it is expandable with python! GoTo www.blender3d.org
Okay.
Code:
Line 1 makes d, a new dictionary. This dictionary has three keys: a, g, and c. Their values are a, c, b, respectively. They're all strings. The keys are the code letters, and the values are the decoded letters.
Line 3 is the start of the infinite loop.
Line 4 gets the code (let's take "AAAGGGCCC") and the .lower() on the end there stores the string in all lowercase. So now "stuff" contains "aaagggccc."
Lines 5 and 6 contain a check. If you type in "exit," it ends the program.
Line 7 creates an empty string 's'. This will be the result of the decoding.
The for loop on lines 8-13 decodes your message. So here was my thought process:
- The code is going to have triples of letters, so we only need to check every third letter.
So the for loop starts at 0 and goes to the len of stuff by threes. I used this number as the index of the string "stuff."
Line 9 is simple. The get() command for dictionaries retrieves the value, otherwise it sets the variable at a default value. So here, we get stuff[triple] (triple is the number assigned by the for loop) from the dictionary (we're retrieving key stuff[triple]). If stuff[triple] is not in the dictionary, it returns the default value, which I have set to "error." This is the syntax for get(): var = dictionary.get(key,default). The next several lines end the while loop if there was an error. If there is no error, the decoded value is added to the s string.
This is what happens for stuff = aaagggccc.
First time through the loop, triple = 0.
stuff[0] now equals a
Line 9 retrieves the value of key 'a' in dictionary d. That value is 'a'. (In the dictionary, 'a':'a')
No error... decoded_str ('a' right now) is added to s.
Next time around the loop... the for loop goes by 3, so now triple = 3
stuff[3] now equals g... it gets key 'g' from dictionary d. The value is 'c'.
No error... 'c' is added to s.
Now s equals "ac"
Third time around... triple = 6
stuff[6] = c
After going through the loop, the loops is done.
s = "acb"
There was never an error, so Line 15 runs and prints s in uppercase (that's what .upper() does)
So we get "ACB"
Let's say we put in "HHHAAABBB."
When it the for loop runs for the first time, triple set to 0, so stuff[0] = h. When it looks for key 'h' and finds none, "decoded_str" is set to "error". The condition on Line 10 is True, so s is set to "error" and the for loop ends with the break statement. The condition on Line 14 is false because s IS "error", so Line 17 prints.
I hope that helped. Sorry my explanation is so lengthy.
Code:
python Syntax (Toggle Plain Text)
d = {'a':'a','g':'c','c':'b'} # key is actually talking about a triple 'aaa':'a' while True: stuff = raw_input("string: ").lower() if stuff == "exit": break s = "" for triple in range(0,len(stuff),3): decoded_str = d.get(stuff[triple],"error") if decoded_str == "error": s = "error" break s += decoded_str if s != "error": print s.upper() else: print "One of the triples in your code could not be decoded."
Line 1 makes d, a new dictionary. This dictionary has three keys: a, g, and c. Their values are a, c, b, respectively. They're all strings. The keys are the code letters, and the values are the decoded letters.
Line 3 is the start of the infinite loop.
Line 4 gets the code (let's take "AAAGGGCCC") and the .lower() on the end there stores the string in all lowercase. So now "stuff" contains "aaagggccc."
Lines 5 and 6 contain a check. If you type in "exit," it ends the program.
Line 7 creates an empty string 's'. This will be the result of the decoding.
The for loop on lines 8-13 decodes your message. So here was my thought process:
- The code is going to have triples of letters, so we only need to check every third letter.
So the for loop starts at 0 and goes to the len of stuff by threes. I used this number as the index of the string "stuff."
Line 9 is simple. The get() command for dictionaries retrieves the value, otherwise it sets the variable at a default value. So here, we get stuff[triple] (triple is the number assigned by the for loop) from the dictionary (we're retrieving key stuff[triple]). If stuff[triple] is not in the dictionary, it returns the default value, which I have set to "error." This is the syntax for get(): var = dictionary.get(key,default). The next several lines end the while loop if there was an error. If there is no error, the decoded value is added to the s string.
This is what happens for stuff = aaagggccc.
First time through the loop, triple = 0.
stuff[0] now equals a
Line 9 retrieves the value of key 'a' in dictionary d. That value is 'a'. (In the dictionary, 'a':'a')
No error... decoded_str ('a' right now) is added to s.
Next time around the loop... the for loop goes by 3, so now triple = 3
stuff[3] now equals g... it gets key 'g' from dictionary d. The value is 'c'.
No error... 'c' is added to s.
Now s equals "ac"
Third time around... triple = 6
stuff[6] = c
After going through the loop, the loops is done.
s = "acb"
There was never an error, so Line 15 runs and prints s in uppercase (that's what .upper() does)
So we get "ACB"
Let's say we put in "HHHAAABBB."
When it the for loop runs for the first time, triple set to 0, so stuff[0] = h. When it looks for key 'h' and finds none, "decoded_str" is set to "error". The condition on Line 10 is True, so s is set to "error" and the for loop ends with the break statement. The condition on Line 14 is false because s IS "error", so Line 17 prints.
I hope that helped. Sorry my explanation is so lengthy.
•
•
•
•
The for loop on lines 8-13 decodes your message. So here was my thought process:
- The code is going to have triples of letters, so we only need to check every third letter.
AGT = g
or CTC = o
So what do I do then, just make the keys three letters and eliminate the 0, len(stuff), 3?
Blender is by far the best free 3D computer Graphics program and it is expandable with python! GoTo www.blender3d.org
oh and what does the += and the != mean?
Blender is by far the best free 3D computer Graphics program and it is expandable with python! GoTo www.blender3d.org
Oh. I was unaware of that. I'll have to rework the loop. Gimme a minute.
edit:
x += 1 is equivalent to x = x + 1. It's a shorter method of incrementing the variable.
!= means "does not equal." "1 != 2" is true.
edit2:
For this for loop, it goes through the letters of the string. Each time through the loop, "letter" equals the next letter in the string. The loop goes through three times, adding the letters to code_str. Once it has three letters, it decodes the triple into a character, and adds it to s (as long as there is no error). Then code_str is set to an empty string so that it can fill up with another three (which will then be decoded)...etc.
edit:
x += 1 is equivalent to x = x + 1. It's a shorter method of incrementing the variable.
!= means "does not equal." "1 != 2" is true.
edit2:
python Syntax (Toggle Plain Text)
d = {'aaa':'a','ggg':'c','ccc':'b','agt':'g'} while True: stuff = raw_input("string: ").lower() if stuff == "exit": break s = "" code_str = "" for letter in stuff: code_str += letter if len(code_str) == 3: decoded_str = d.get(code_str,"error") if decoded_str == "error": s = "error" break s += decoded_str code_str = "" if s != "error": print s.upper() else: print "One of the triples in your code could not be decoded."
For this for loop, it goes through the letters of the string. Each time through the loop, "letter" equals the next letter in the string. The loop goes through three times, adding the letters to code_str. Once it has three letters, it decodes the triple into a character, and adds it to s (as long as there is no error). Then code_str is set to an empty string so that it can fill up with another three (which will then be decoded)...etc.
Last edited by LaMouche; Feb 11th, 2007 at 1:30 am. Reason: more
![]() |
Similar Threads
Other Threads in the Python Forum
- Previous Thread: A Little WordGame problem :)
- Next Thread: GUI Question
| Thread Tools | Search this Thread |
abrupt ansi anti approximation array assignment avogadro backend beginner binary bluetooth builtin calculator character chmod code converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary drive dynamic examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse mysqlquery number numbers output parsing path plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion scrolledtext sqlite statistics stdout string strings sum table terminal text textarea thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows write wxpython xlib






