Encoding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shing
    New Member
    • Mar 2007
    • 58

    Encoding

    Code:
    a='$'
    b='i'
    c='q'
    d='s'
    e='w'
    f='~'
    g='d'
    h='2'
    i='5'
    j='6'
    k='a'
    l='g'
    m='-'
    n='j'
    o='z'
    p='%'
    q='h'
    r='9'
    s='f'
    t='7'
    u='+'
    v='c'
    w='0'
    x='8'
    y='7'
    z='x'
    
    while True:
        guess = raw_input("Word or Exit?")
        if guess.lower() == 'exit':
                  break
        elif guess.lower() == 'Word':
    
    	print code
    I'm not so sure as how to finish my program. I want the python script to show something like: q$7 after I type in "cat". All help appreciated if anybody can help me on this,
  • Elias Alhanatis
    New Member
    • Aug 2007
    • 56

    #2
    Hello there!!!

    I am a newbee also , but as far as i can tell , you should put all your 'encoding'
    ( 'a'=....., 'b'=... etc ) in a list or even better in a dictionary , and then have the program to look up to that list or dict , and compose a string from the values that correspond to the letters you've entered.
    It is supposed to be a simple task in Python..... ( if you work your way through the basics , of course....)

    Happy coding ,

    Elias

    Comment

    • Elias Alhanatis
      New Member
      • Aug 2007
      • 56

      #3
      Here is the code. Just adopt it to your needs....

      Code:
      ### This is a dictionary...
      MyDict={'a':'$','b':'i','c':'q','d':'s','e':'w','f':'~','g':'d','h':'2',
              'i':'5','j':'6','k':'a','l':'g','m':'-','n':'j','o':'z','p':'%',
              'q':'h','r':'9','s':'f','t':'7','u':'+','v':'c','w':'0','x':'8',
              'y':'7','z':'x'}
      
      ## This is just to show the mapping of the 'encoding'
      for k,v in MyDict.iteritems():
          print k,' will be encoded to.. ',v
          
      ## Here we ask for the word to be encoded
      ## Notice that i use the '.lower' method in case
      ##    the user inserts any uppercase letters
          
      Word=raw_input("What is the word you want to encode?").lower()
      
      ## This is an empty string to begin with
      Encoded_Word=''
      
      ## Now we iterate over the inserted word to check each letter
      for letter in Word:
          if MyDict.has_key(letter):
              ## Insert to the string the coresponding character..
              Encoded_Word=Encoded_Word+MyDict.get(letter)
      
      ## Print the encoded word
      print Encoded_Word
      
      ## Or , if you want even more details...
      print 'The word',Word,' was encoded to..',Encoded_Word
      Hope i helped!!!!

      Elias

      Comment

      • elcron
        New Member
        • Sep 2007
        • 43

        #4
        I recommend using the string module. Here's an example for caesar shift 2:
        [code=python]
        import string
        def shift(text):
        """
        caesar shift 2
        """
        # the 0 index of inputt will be mapped to the 0 index of output ...
        inputt = 'abcdefghijklmn opqrstuvwxyz'
        output = 'cdefghijklmnop qrstuvwxyzab'

        # the actual mapping
        table = string.maketran s(inputt, output)

        # translating the text
        result = string.translat e(text, table)

        return result
        [/code]

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          Fantastic solution elcron. I did not know that the string module had this capability.

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            you can use other encoding as well, some examples
            Code:
            >>> "string".encode("rot13")
            'fgevat'
            >>> "string".encode("zip")
            'x\x9c+.)\xca\xccK\x07\x00\tB\x02\x98'
            >>> "string".encode("uu")
            'begin 666 <data>\n&<W1R:6YG\n \nend\n'
            >>> "string".encode("base64")
            'c3RyaW5n\n'
            >>>

            Comment

            • shing
              New Member
              • Mar 2007
              • 58

              #7
              That's a great script you've got Elias, do you think you could add in the "Space bar"? Bartonc's seems good too, but I'm afraid he's too complicated for me to understand it :P

              Comment

              • Elias Alhanatis
                New Member
                • Aug 2007
                • 56

                #8
                Of course you can add a "Space Bar". If you want for example
                the spaces to be translated to asteriscs (*), just add to "MyDict"
                the following : ' ':'*'
                (Notice that betwen the first two ' i have a space.... )

                As an advice for the future , i would recomend that you try to
                experiment!!! Take small pieces of code , try to modify them
                ( a bit at a time ) and see how they work. See what results
                your changes have brought , and you will be able to fully
                understand how everything works.

                Happy Coding!!!

                Elias

                Comment

                • shing
                  New Member
                  • Mar 2007
                  • 58

                  #9
                  Hey elias i tried switching around some of your code to make a decoder, but that seems to come up with errors...

                  Code:
                  ### This is a dictionary...
                  MyDict={'a':'$','b':'i','c':'q','d':'s','e':'w','f':'~','g':'d','h':'2',
                          'i':'5','j':'6','k':'a','l':'g','m':'-','n':'j','o':'z','p':'%',
                          'q':'h','r':'9','s':'f','t':'7','u':'+','v':'c','w':'0','x':'8',
                          'y':'7','z':'x'}
                  
                  ## This is just to show the mapping of the 'encoding'
                  for k,v in MyDict.iteritems():
                      print v,' will be decoded to.. ',k
                      
                  ## Here we ask for the word to be decoded
                  ## Notice that i use the '.lower' method in case
                  ##    the user inserts any uppercase letters
                      
                  Word=raw_input("What is the word you want to decode?").lower()
                  
                  ## This is an empty string to begin with
                  Decoded_Word=''
                  
                  ## Now we iterate over the inserted word to check each letter
                  for letter in Word:
                      if MyDict.has_key(letter):
                          ## Insert to the string the coresponding character..
                          Decoded_Word+MyDict=Decoded_Word.get(letter)
                  
                  ## Print the encoded word
                  print Decoded_Word
                  Any ideas?

                  Comment

                  • Stubert
                    New Member
                    • Dec 2007
                    • 20

                    #10
                    Hey I know nothing of Python but maybe one of your errors is coming from the declaration of the letter 'f' in your dictionary, you've only used one ' and not closed it.

                    Comment

                    • Elias Alhanatis
                      New Member
                      • Aug 2007
                      • 56

                      #11
                      And also check again line 24 of your code , i think you have mixed up things
                      a liitle bit there...

                      Be very carefull and double-check everything in your code. Also read carefully from the error report in which line was the error. If no error comes from the program , and you just get strange results , check again your code. Computers are never wrong ,
                      people are!!!

                      Elias

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Originally posted by shing
                        Hey elias i tried switching around some of your code to make a decoder, but that seems to come up with errors...

                        Code:
                        ### This is a dictionary...
                        MyDict={'a':'$','b':'i','c':'q','d':'s','e':'w','f':'~','g':'d','h':'2',
                                'i':'5','j':'6','k':'a','l':'g','m':'-','n':'j','o':'z','p':'%',
                                'q':'h','r':'9','s':'f','t':'7','u':'+','v':'c','w':'0','x':'8',
                                'y':'7','z':'x'}
                        
                        ## This is just to show the mapping of the 'encoding'
                        for k,v in MyDict.iteritems():
                            print v,' will be decoded to.. ',k
                            
                        ## Here we ask for the word to be decoded
                        ## Notice that i use the '.lower' method in case
                        ##    the user inserts any uppercase letters
                            
                        Word=raw_input("What is the word you want to decode?").lower()
                        
                        ## This is an empty string to begin with
                        Decoded_Word=''
                        
                        ## Now we iterate over the inserted word to check each letter
                        for letter in Word:
                            if MyDict.has_key(letter):
                                ## Insert to the string the coresponding character..
                                Decoded_Word+MyDict=Decoded_Word.get(letter)
                        
                        ## Print the encoded word
                        print Decoded_Word
                        Any ideas?
                        This reverses the dictionary:[code=Python]
                        revDict = dict(zip(MyDict .values(), MyDict.keys()))
                        Decoded_Word = ''.join([revDict.get(let ter, letter) for letter in Encoded_Word])
                        print Decoded_Word[/code]This works well and is good exercise, but I prefer the string.maketran s() - string.translat e() solution.

                        Comment

                        • shing
                          New Member
                          • Mar 2007
                          • 58

                          #13
                          says that the encoded word is not defined.

                          Code:
                          ### This is a dictionary...
                          MyDict={'a':'$','b':'i','c':'q','d':'s','e':'w','f':'~','g':'d','h':'2',
                                  'i':'5','j':'6','k':'a','l':'g','m':'-','n':'j','o':'z','p':'%',
                                  'q':'h','r':'9','s':'f','t':'7','u':'+','v':'c','w':'0','x':'8',
                                  'y':'7','z':'x'}
                          
                          ## This is just to show the mapping of the 'encoding'
                          for k,v in MyDict.iteritems():
                              print v,' will be decoded to.. ',k
                              
                          ## Here we ask for the word to be decoded
                          ## Notice that i use the '.lower' method in case
                          ##    the user inserts any uppercase letters
                              
                          Word=raw_input("What is the word you want to decode?").lower()
                          
                          ## This is an empty string to begin with
                          Decoded_Word=''
                          
                          revDict = dict(zip(MyDict.values(), MyDict.keys()))
                          Decoded_Word = ''.join([revDict.get(letter, letter) for letter in Encoded_Word])
                          print Decoded_Word

                          Comment

                          • Elias Alhanatis
                            New Member
                            • Aug 2007
                            • 56

                            #14
                            Change line 15 to:

                            Encoded_Word=ra w_input("What is the word you want to decode?").lower ()

                            Comment

                            Working...