have to wrte a code for the below program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fury30
    New Member
    • Aug 2009
    • 2

    have to wrte a code for the below program

    hi i have to write pyhton code for the program below..i have tried doin it but could not get the desired result so if anyone can help me out i would be very thankful.the question is as below..

    A Caesar cipher is a simple substitution cipher based on the idea of shifting
    each character in a message a fixed number of positions in the alphabet (the
    fixed number is called the key). For example “apple” would become “bqqmf”
    with a key of 1, and the initial message could be recovered using a key of -1.

    Write a program program which encodes a text file using the Caesar cipher
    and output the result to a file of the same name with a “.xxx” appended to it.
    Your program will need to prompt the user for the name of the text file
    containing the initial message and the key to be used.

    You will need to use the chr() function to solve this problem. For example if
    you wanted to shift a character (lets call it ch) 2 places you could do so in
    Python with chr(ord(ch)+2).

    Finally modify your Caesar cipher to use two separate keys. The first key
    would shift every odd numbered character and the second key every even numbered character.

    Above is the question what i have to do it is only a single question
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    fury30,

    We are not here to do your homework for you. I can give you some examples that should get you started.

    Code:
    >>> s = "This sentence will be scrambled"
    >>> key = 3
    >>> cipher = [ord(c)+key for c in s]
    >>> print ''.join([chr(i) for i in cipher])
    Wklv#vhqwhqfh#zloo#eh#vfudpeohg
    >>> cipher = [ord(c)-key for c in 'Wklv#vhqwhqfh#zloo#eh#vfudpeohg']
    >>> print ''.join([chr(i) for i in cipher])
    This sentence will be scrambled
    >>>
    Example using string.maketran s:
    Code:
    s = "This sentence will be scrambled"
    >>> import string
    >>> m = string.maketrans('abcdefghijklmnopqrstuvwxyz', 'cdefghijklmnopqrstuvwxyzab')
    >>> s1 = string.translate(s, m)
    >>> s1
    'Tjku ugpvgpeg yknn dg uetcodngf'
    >>>
    Example using a dictionary:
    Code:
    >>> cipher = {"a":"g", "b":"h", "c":"i", "d":"j", "e":"k", "f":"l", "g":"m", "h":"n", \
              "i":"o", "j":"p", "k":"q", "l":"r", "m":"s", "n":"t", "o":"u", "p":"v", \
              "q":"w", "r":"x", "s":"y", "t":"z", "u":"a", "v":"b", "w":"c", "x":"d", \
              "y":"e", "z":"f"}
    
    >>> s = "This sentence will be scrambled"
    >>> print ''.join([cipher.get(c, c) for c in s])
    >>> Tnoy yktzktik corr hk yixgshrkj

    Comment

    Working...