943,722 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3856
  • Python RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Feb 11th, 2007
0

Re: Scanning a string?

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.

Python Syntax (Toggle Plain Text)
  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."
Reputation Points: 16
Solved Threads: 1
Junior Poster in Training
danizzil14 is offline Offline
68 posts
since Jul 2005
Feb 11th, 2007
0

Re: Scanning a string?

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.
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Feb 11th, 2007
0

Re: Scanning a string?

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.
Reputation Points: 16
Solved Threads: 1
Junior Poster in Training
danizzil14 is offline Offline
68 posts
since Jul 2005
Feb 11th, 2007
0

Re: Scanning a string?

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.
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Feb 11th, 2007
0

Re: Scanning a string?

Another way:
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Oct 4th, 2008
0

Re: Scanning a string?

simplified version of an Encryptor/Decryptor i did a while ago
the parts with the pointy
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
A no-name-moose is offline Offline
6 posts
since Oct 2008
Oct 5th, 2008
0

Re: Scanning a string?

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
python Syntax (Toggle Plain Text)
  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 !
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: A Little WordGame problem :)
Next Thread in Python Forum Timeline: GUI Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC