1. Lab04a.py
– Converting Decimal to Binary
#
# Name:
# File: Lab04a.py
# Date: January 31, 2008
#
# Desc: Converts decimal integer to binary
#
def
main():
# Display
Title
print
print "Decimal to Binary
Conversion"
print
# Read Decimal Value
n = input("Enter
a decimal integer: ")
n0 = n # make a
copy
# Horner's Method
bStr =
"" # initialize binary string to empty
while (n != 0):
bit = chr((n % 2) + 48) # modulo by 2 to get
remainder
# and convert to
character
bStr = bit + bStr; # concatenate to left side of binary string
n = n / 2 #
divide n by 2 and go again
if (n0 == 0): #
Special Case - if initial value == 0
bStr =
"0" # set binary string = "0"
# Display
Results
print
print n0, "base 10 =" , bStr, " base 2"
print
main()
2. Lab04a.py Challenge - Converting
Decimal to base B between 2 and 9
#
# Name:
# File: Lab04ax.py
# Date: January 31, 2008
#
# Desc: Converts decimal
to any base between 2 and 9
#
def main():
# Display Title
print
print
"Decimal to Base B Conversion"
print
#
Get decimal value and target base
n = input("Enter a decimal integer: ")
n0 =
n # save
copy of decimal value
b = input("Enter a new base between 2 and 9: ")
#
Horner's Method
bStr = ""
while (n != 0):
bit = chr((n %
b) + 48) # modulo by base and
# convert remainder
to an integer
bStr = bit + bStr; # concatenate to
the right side
n = n
/ b # divide decimal value by base
if (n0 ==
0): # Special case if n ==
0
bStr = "0"
#
Display Results
print
print n0,
"base 10 =" , bStr,
" base", b
print
main()
3. Lab04a.py Challenge - Decimal to
Hexadecimal Conversion
#
# Name:
# File: Lab04ay.py
# Date: January 31, 2008
#
# Desc: Converts decimal
to hexadecimal
#
def main():
# Display Title
print
print
"Decimal to Base B Conversion"
print
#
Get decimal value and new base
n = input("Enter a decimal integer: ")
n0 =
n # save
copy of decimal value
b = input("Enter a new base between 2 and 16: ")
digitList = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D',
'E', 'F']
# Horner's Method
bStr = ""
while (n != 0):
digit
= digitList[n % b] # modulo by base and
# convert remainder
to a digit
bStr = digit + bStr; # concatenate to
the right side
n = n
/ b # divide decimal value by base
if (n0 ==
0): # Special case if n ==
0
bStr =
"0"
# Display Results
print
print n0,
"base 10 =" , bStr,
" base", b
print
main()
4. Lab04b.py – Date Conversion & Day
of Year
#
# Name:
# File: Lab04b.py
# Date: January 31, 2008
#
# Desc: Variant of Date
Conversion program from page 100
#
of text; computes day of year
import string
def main():
# Display Title
print
print
"Date Conversion"
print
# get the date
dateStr = raw_input("Enter a date (mm/dd/yyyy): ")
# splice into components
monthStr, dayStr, yearStr = string.split(dateStr, "/")
# convert monthStr
to month Name via list look-up
months =
["January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"]
monthName = months[int(monthStr)-1]
# output result in month day, year format
print
"The converted date is:", monthName, dayStr+",",yearStr
# list of number of
days before 1st of the month
daysB4first = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
# look up number of days before 1st of month
and add day
dayNumber = daysB4first[int(monthStr)-1] + int(dayStr)
# check for leap year
if ((int(monthStr) > 2) and (int(yearStr) % 4 == 0)):
dayNumber = dayNumber + 1
# display results
print
"This is day number", dayNumber,"of
the year"
print
main()
5. Lab04c.py – Table Formatting
#
# Name:
# File: Lab04c.py
# Date: January 31, 2008
#
# Desc: Format a Table
#
def main():
# Display Title
print
print
"Compound Interest Table"
print
# Get Present Value,
Annual Interest Rate, and Number of Years
PV = input("Enter present value: ")
apr = input("Enter annual
interest rate as a percent: ");
apr = apr /100.0 #
convert to decimal
nYears = input("Enter the
number of years: ")
# Display Column
Headers
print
print
" | Present | Interest | Future |"
print
" Year | Value | Earned |
Value |"
print
"------+----------+----------+----------+"
#
Generate Table
for i in range(nYears):
interest = PV * apr # compute interest
FV =
PV + interest #
compute future value
print " %2d | %8.2f | %8.2f | %8.2f |" % (i+1, PV,
interest, FV)
PV =
FV #
update present value
main()
6. Lab04d.py – Vigenere
Cipher Program
#
# Name:
# File: Lab04d.py
# Date: January 31, 2008
#
# Desc: Vigenere Cipher
import string
def main():
#
Display Title
print
print
"Vigenere Cipher Program"
print
# Get
Plaintext and key
str1 =
raw_input("Enter
plain text string: ")
key = raw_input("Enter
encryption key: ")
# Extend key to be same length
as Plaintext
str1Length = len(str1)
key =
(key * str1Length)[:str1Length]
# Encode
str2 =
"" # cipher string
for i in range(str1Length):
n1
= ord(str1[i])-65 # convert letters to integer
# values between 0 and 25
k1
= ord(key[i])-65 # do same for key
str2 = str2 + chr(((n1+k1) % 26) + 65)
# Decode
str3 = "" # decoded string
for i in range(str1Length):
n2
= ord(str2[i])-65 # see above
k1
= ord(key[i])-65
str3 = str3 + chr(((n2-k1) % 26) + 65)
# Display Results
print
print
"Plain Text :
", str1
print
"Key ", key
print
"Cipher Text : ", str2
print
"Key ", key
print
"Decoded Text: ", str3
print
main()