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

Re: Scanning a string?

 
0
  #11
Feb 11th, 2007
AWSOME!!! Thank you soo much, ok wow, lol I basically, (i think) was on the same track as you, but I kept getting a syntax error, heres the code n/e ways.

  1. decoderdict = {"aaa": "a", "bbb": "b", "ccc": "c", "tcg": "d"}
  2.  
  3. while True:
  4. dna = raw_input("Please enter the DNA string: ").lower()
  5. if dna == "exit":
  6. break
  7. output = ""
  8. for triplet in range(0,1,3):
  9. decoderdict.get(dna[triple], "error") += finals
  10. if finals == "error":
  11. break
  12. if finals != "error":
  13. print "The decoded string is: ", finals.upper()
  14. else:
  15. print "One of the triples in your code could not be decoded."
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: 134
Reputation: LaMouche is on a distinguished road 
Solved Threads: 20
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Scanning a string?

 
0
  #12
Feb 11th, 2007
Several syntax problems:

- You have "for triplet in range(0,1,3):" but later reference "triple", which is not a variable

- You create output as an empty string, and then never reference it. Either change that to finals or all of the instances of "finals" to "output".

Now that the program works... there are some semantic errors:

- Do you know what "range(0,1,3)" does? It starts with 0 and goes by threes from 0 to 1. So basically it is [0] (list). You want to approach this problem a little different. You don't want to do it by indexes because you are getting a string of THREE letters. I guess you could figure out how to do it by slicing, but I didn't choose to approach it that way.

Note:

The range function works like this:

Range(max) generates the list [0, 1, ... , (max-1)].

Range(min,max) generates the list [min, (min+1), ... , (max-1)].

Range(min,max,step) does the same as the previous except goes by the increment step. step cannot be 0. If step is negative, then the role of min and max are reversed.
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
  #13
Feb 11th, 2007
ok gotcha, lol i read the range thing wrong, I thought that i was saying go up 3 in increments of 1 from 0 lol oops

EDIT: Well, thank you for the help, Ill let you know when the decoder is fininshed, I'm also making a coder too, I have the beta versions out, but they are very cumbersome, so yeah, ttyl and thank you soo much for you help!
Last edited by danizzil14; Feb 11th, 2007 at 1:56 am.
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: 134
Reputation: LaMouche is on a distinguished road 
Solved Threads: 20
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Scanning a string?

 
0
  #14
Feb 11th, 2007
Cool. It was fun to help. I'd love to see the finished products. When you finish them, post in a new post because it won't be about "scanning a string?" ...

Later.
Last edited by LaMouche; Feb 11th, 2007 at 1:58 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Scanning a string?

 
0
  #15
Feb 11th, 2007
Another way:
  1. def group( lst, n ):
  2. ''' splits an iterable in parts with length n, e.g.
  3. group( [1,2,3,4], 2 ) ==> [ (1,2), (3,4) ]
  4. '''
  5. return zip( *[ lst[i::n] for i in range(n) ] )
  6.  
  7. aa_dict = { "AAA": "A", "GGG": "C", "CCC": "B" }
  8.  
  9. s = "AAAGGGCCC"
  10.  
  11. g = [ "".join(t) for t in group(s, 3) ]
  12.  
  13. for triplet in g:
  14. print aa_dict[triplet],

Regards, mawe
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 6
Reputation: A no-name-moose is an unknown quantity at this point 
Solved Threads: 0
A no-name-moose A no-name-moose is offline Offline
Newbie Poster

Re: Scanning a string?

 
0
  #16
Oct 4th, 2008
simplified version of an Encryptor/Decryptor i did a while ago
the parts with the pointy
  1. l='' # a place to attach the results of <i in dictionary> to#
  2. for i in <NameOfInput/TheThingYouWantToEncrypt>:
  3. if i in <NameOfdictionary>:
  4. L=L+<NameOfdictionary>[<NameOfInputInsideBrackets>]
  5. #attach the value of the dictionary to each letter to the empty string#
  6. # can be written as L+= instead of L=L+, and is another way to join/append to a list/tuple
  7. else:
  8. pass
  9. # you can also insert<print i> if u want it to print a char unknown to the dictionary!#
  10. #the pass just tells him to ingore that letter#
  11. print l
  12. #print the final output, if i would put it under the if/else idention, it would have print each step of the encryption on a different line#
Hope that helps,
Regard,
{Anonamemoose}
Last edited by A no-name-moose; Oct 4th, 2008 at 10:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Scanning a string?

 
0
  #17
Oct 5th, 2008
A no-name-moose, rather than writing template looking code with <NameOfDictionary> things, whihc are not in the syntax of python, you should better write a python function with local variables, like this
  1. def crypt(input_sequence, dictionary):
  2. L = ''
  3. for i in input_sequence:
  4. if i in dictionary:
  5. L += dictionary[i]
  6. return L
The advantage is that your code is immediately usable by others !
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


Views: 3086 | Replies: 16
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC