Scanning a string?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 68
Reputation: danizzil14 is an unknown quantity at this point 
Solved Threads: 1
danizzil14's Avatar
danizzil14 danizzil14 is offline Offline
Junior Poster in Training

Scanning a string?

 
0
  #1
Feb 10th, 2007
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?
Blender is by far the best free 3D computer Graphics program and it is expandable with python! GoTo www.blender3d.org
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Scanning a string?

 
0
  #2
Feb 10th, 2007
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:

  1. d = {'a':'a','g':'c','c':'b'} # key is actually talking about a triple 'aaa':'a'
  2.  
  3. while True:
  4. stuff = raw_input("string: ").lower()
  5. if stuff == "exit":
  6. break
  7. s = ""
  8. for triple in range(0,len(stuff),3):
  9. decoded_str = d.get(stuff[triple],"error")
  10. if decoded_str == "error":
  11. s = "error"
  12. break
  13. s += decoded_str
  14. if s != "error":
  15. print s.upper()
  16. else:
  17. 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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 68
Reputation: danizzil14 is an unknown quantity at this point 
Solved Threads: 1
danizzil14's Avatar
danizzil14 danizzil14 is offline Offline
Junior Poster in Training

Re: Scanning a string?

 
0
  #3
Feb 10th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Scanning a string?

 
0
  #4
Feb 10th, 2007
Originally Posted by danizzil14 View Post
I have 26 if/elif/else statements to decode each letter, with 10 of them outputting different variables
Yeah, ouch. That's a lot less efficient. Look up dictionaries.

Do you need me to explain mine, or do you understand?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 68
Reputation: danizzil14 is an unknown quantity at this point 
Solved Threads: 1
danizzil14's Avatar
danizzil14 danizzil14 is offline Offline
Junior Poster in Training

Re: Scanning a string?

 
0
  #5
Feb 10th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 68
Reputation: danizzil14 is an unknown quantity at this point 
Solved Threads: 1
danizzil14's Avatar
danizzil14 danizzil14 is offline Offline
Junior Poster in Training

Re: Scanning a string?

 
0
  #6
Feb 10th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Scanning a string?

 
1
  #7
Feb 11th, 2007
Okay.

Code:

  1. d = {'a':'a','g':'c','c':'b'} # key is actually talking about a triple 'aaa':'a'
  2.  
  3. while True:
  4. stuff = raw_input("string: ").lower()
  5. if stuff == "exit":
  6. break
  7. s = ""
  8. for triple in range(0,len(stuff),3):
  9. decoded_str = d.get(stuff[triple],"error")
  10. if decoded_str == "error":
  11. s = "error"
  12. break
  13. s += decoded_str
  14. if s != "error":
  15. print s.upper()
  16. else:
  17. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 68
Reputation: danizzil14 is an unknown quantity at this point 
Solved Threads: 1
danizzil14's Avatar
danizzil14 danizzil14 is offline Offline
Junior Poster in Training

Re: Scanning a string?

 
0
  #8
Feb 11th, 2007
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.
One little problem, and its my fault for not telling you, the worksheet that i got this idea from, had triplets like:

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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 68
Reputation: danizzil14 is an unknown quantity at this point 
Solved Threads: 1
danizzil14's Avatar
danizzil14 danizzil14 is offline Offline
Junior Poster in Training

Re: Scanning a string?

 
0
  #9
Feb 11th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Scanning a string?

 
0
  #10
Feb 11th, 2007
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:

  1. d = {'aaa':'a','ggg':'c','ccc':'b','agt':'g'}
  2.  
  3. while True:
  4. stuff = raw_input("string: ").lower()
  5. if stuff == "exit":
  6. break
  7. s = ""
  8. code_str = ""
  9. for letter in stuff:
  10. code_str += letter
  11. if len(code_str) == 3:
  12. decoded_str = d.get(code_str,"error")
  13. if decoded_str == "error":
  14. s = "error"
  15. break
  16. s += decoded_str
  17. code_str = ""
  18. if s != "error":
  19. print s.upper()
  20. else:
  21. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC