7-9
def tr(srcstr, dststr, string, distinguish=True):
if not distinguish:
srcstr = srcstr.lower()
dststr = dststr.lower()
string = string.lower()
if len(srcstr) >= len(dststr):
srcstr = srcstr[:len(dststr)]
return string.replace(srcstr, dststr)
else:
return 'invalid input'
7-10
def rot13(letter):
import string
uppercase = string.uppercase
lowercase = string.lowercase
if letter in uppercase:
L_index = uppercase.index(letter)
if L_index <= 12:
letter = uppercase[L_index+13]
else:
letter = uppercase[L_index-13]
if letter in lowercase:
l_index = lowercase.index(letter)
if l_index <= 12:
letter = lowercase[l_index+13]
else:
letter = lowercase[l_index-13]
return letter
def translate(letters):
string = ''
for i in letters:
string += rot13(i)
return string
print translate(raw_input("string you want to be translated: "))
7-13 + 7-14
import random
# create set A
amtA = random.randint(1, 10)
A = set()
for i in range(amtA):
A.add(random.randint(0, 9))
# create set B
amtB = random.randint(1, 10)
B = set()
for i in range(amtB):
B.add(random.randint(0, 9))
# print A and B
print 'A:', A
print 'B:', B
loop = True
n = 0
while loop:
if n == 3:
print A | B, A & B
loop = False
continue
print "try your answer: "
per_or = input("what is A|B? ")
per_and = input("what is A&B? ")
if per_or == A | B and per_and == A & B:
print "you are right!"
break
else:
print "you are wrong"
n += 1
7-15
A = set(input("your set A(like this: 'a',2...): "))
B = set(input("your set B(like this: 'a',2...): "))
prompt = """
How to calculate:
(in, not in, &, |, -, ^, <, <=, >, >=, ==, !=)
Enter:
"""
sign = raw_input(prompt)
cal = {'in': A in B, 'not in': A not in B, '&': A & B, '|': A | B,
'^': A ^ B, '<': A < B, '<=': A <= B, '>': A > B, '>=': A >= B,
'==': A == B, '!=': A != B}
print cal[sign]