# Review of lists
# Define a new list
list1 = [3,2,4,5]
# access an element
list1
list1[3]
list1[3]=17
list1
# Get length of list
len(list1) # returns length of list
list1.append(81)
list1
# get last elt of list
list1[len(list1)-1]
list1[-1] #shorthand for last element
l# get 2nd to last
ist1[-2]
# code to compute the list corresponding to fibonacci seq:
# 1, 1, 2, 3, 5, 8, 11
# first two elts are 1, then each after is sum of previous two
fib_list = [1,1] # initial condition
for n in range(5):
fib_list.append(fib_list[-1]+fib_list[-2])
fib_list
# CHALLENGE: generate a list of squares of integers 1,4,9,16,...
sq_list = []
for n in range(1,10):
sq_list.append(n^2)
sq_list
# shorthand for above
[n^2 for n in range(1,10)]
# Strings are similar to lists of characters
st = "hello"
# can have space, numbers (also special characters)
st2="Hello World 31332"
# can access individual elts
st[-1]
# can access individual elts
st2[5]
# difference between "c" and c (latter is variable, but it hasn't been defined)
st3="c"
st4 = c
c="g"
st5 = c
st5
# Difference between 3 and '3'
h = 3
h+4
h="3"
h
# Can't add character '3' to number 4
h+4
# Can't change individual elts of a string
st[0] = "s"
st = "this is a string"
st
# can have a list that contains similar info to string
st = ['h','e','l','l','o']
st
# can change individual elt of list
st[0]='s'
st
# Start of Cryptography unit
# "Keyboard cryptosystem"
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
code_alpha = "QWERTYUIOPASDFGHJKLZXCVBNM"
len(code_alpha) # check
alpha.find("C") # function that gives index of c in alpha
# function to "keyboard" encipher, sub A->Q, B->W, C->E, etc
def keyboard_encipher(plaintext):
ciphertext = []
for c in plaintext:
# convert character c into a number, index of c in alpha
numc = alpha.find(c)
codedc = code_alpha[numc]
ciphertext.append(codedc)
return ciphertext
keyboard_encipher("CAB")
keyboard_encipher("ATTACKATDAWN")
# CHALLENGE: write a deciphering function
# code to decipher, switch roles of alpha, code_alpha
# switch roles of ciphertext, plaintext
def keyboard_decipher(ciphertext):
plaintext = []
for c in ciphertext:
# convert character c into a number, index of c in code_alpha
numc = code_alpha.find(c)
decodedc = alpha[numc]
plaintext.append(decodedc)
return plaintext
keyboard_decipher("EQW")
keyboard_decipher(['Q', 'Z', 'Z', 'Q', 'E', 'A', 'Q', 'Z', 'R', 'Q', 'V', 'F'])
# convert list into string (easier to read)
":".join(keyboard_decipher("EQW"))
# Shift/Caesar cipher
# shift by 3 (wrapping around at end of alphabet)
# A->D, B->E, C->F, ..., X->A, Y->B, Z->C
def shift3_cipher(plaintext):
ciphertext = []
for c in plaintext:
# convert character c into a number, index of c in alpha
numc = alpha.find(c)
shiftednumc = (numc + 3) % 26 # mod 26, since wrap around alphabet
codedc = alpha[shiftednumc]
ciphertext.append(codedc)
return ciphertext
shift3_cipher("CAB")
shift3_cipher("ATTACKATDAWN")
# Shift/Caesar cipher
# shift by amount given by shift parameter
def shift_cipher(plaintext, shift):
ciphertext = []
for c in plaintext:
# convert character c into a number, index of c in alpha
numc = alpha.find(c)
shiftednumc = (numc + shift) % 26 # mod 26, since wrap around alphabet
codedc = alpha[shiftednumc]
ciphertext.append(codedc)
return ciphertext
shift_cipher("CAB", 3)
(-3) % 26 # gives number between 0 and 25
# to decipher use -shift
shift_cipher(['F', 'D', 'E'], -3)
shift_cipher(['D', 'W', 'W', 'D', 'F', 'N', 'D', 'W', 'G', 'D', 'Z', 'Q'],-3)