473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

have to wrte a code for the below program

2
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
Aug 19 '09 #1
1 2255
bvdet
2,851 Expert Mod 2GB
fury30,

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

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

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tommo | last post by:
Hello All, I am a still learning so be easy on me. I am trying to get some code to work that is using JS and Perl/CGI, I am using AS Perl and an Apache Server on XP as the webserver. Can anyone...
8
by: G Patel | last post by:
I wrote the following program to remove C89 type comments from stdin and send it to stdout (as per exercise in K&R2) and it works but I was hoping more experienced programmer would critique the...
8
by: Polaris | last post by:
Hi Experts: The program creates Panels and inicilize them. When I run the code below, the program crashes (at the line commented below). Any help is appriciated. Thanks in Advance! Polaris ...
1
by: peterggmss | last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for...
13
by: cj | last post by:
In a project done in 2003 about a year ago I was told to add the SocketWrench code below into the Windows Form Designer generated code area as shown below. #Region " Windows Form Designer...
5
by: stevewilliams2004 | last post by:
I was wondering if someone could explain the output I am getting for the program below. What I expected from the main program output was "Cat" but instead I see "Mammal". The output is also...
0
by: svgeorge | last post by:
I want to update several tables using one stored procedure. How can i do this I mean the syntax.etc. declaration etc. I know the basic syntax as below CREATE PROCEDURE <Procedure_Name, sysname,...
2
by: fury30 | last post by:
this is the program as below and now what i need to do is add the following to it but i am not able to get how to do it # futval.py # A program to compute the value of an investment # ...
4
by: psyvanz | last post by:
Im a college student, my program is a computerized enrollment system, Im using MS ACCESS database in connecting my VB6 program form. Inside i'd created two tables, named "tbstudentinfo" and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.