03.01
[4**3,2+3.4**2,(1+1j)**2]
03.02
a = "... but a giant leap for mankind."
num=a.index('leap')
print(a[0:num]+a[num+4:])
03.03
s = "CAGTACCAAGTGAAAGAT"
s.count('A')
a=[1,2,3]
b=[4,5,6]
c=a+b
# or
d=a
d.extend(b)
print (c,d)
04.01
x=3.7
(x>3.4 and x<=6.6) or (x==2)
04.02
dict={'snake':'Schlange','jungle':'Dschungel','lives':'lebt','in':'in','the':'der'}
s="The snake lives in the jungle"
for word in s.split():
print (dict[word.lower()])
05.01
for i in range(2,1000):
is_prime=True
for j in range(2,i):
if (i%j==0):
is_prime=False
if is_prime:
print (i)
05.02: Simply break the loop since it is not necessary to search further if one divisor is found.
for i in range(2,1000):
is_prime=True
for j in range(2,i):
if (i%j==0):
is_prime=False
break
if is_prime:
print (i)
05.03
# very terse:
a, b = 0, 1
while a < 100000:
print(a)
a, b = b, a + b
#Fibonacci sequence with index
#
l=[0]
i=0
x=1
while x<100000:
l.append(x) # add x_i
x=l[i]+l[i+1] # work out x_i+1
i=i+1
print (len(l))
And the square Fibonaccis:
# determine square Fibonaccis
l=[0]
x=1
while x<100000:
l.append(x)
x=l[-2]+l[-1]
# print square Fibonacci numbers
for fibonacci in l:
is_square=False
for j in range(fibonacci):
if j*j==fibonacci:
is_square=True
break
if is_square:
print ("square number =",fibonacci)
square number = 144
s='pbatenghyngvbaf lbh unir fhpprrqrq va qrpelcgvat gur fgevat'
base=ord('a') # ascii code for 'a'
decode=-13
res=''
for character in s.lower():
if character==' ':
t=character
else:
ascii=ord(character)
t=chr(((ascii+decode)-base)%26+base) # (ascii-97)%26 in range 0 ... 25
res+=t
print (decode,res)
The translation of ascii can also be done explicitly:
s='pbatenghyngvbaf lbh unir fhpprrqrq va qrpelcgvat gur fgevat'
base=ord('a') # ascii code for 'a'
decode=-13
res=''
for character in s.lower():
if character==' ':
tt=ord(' ')
else:
ascii=ord(character)
tt=ascii+decode
if tt>=base+26:
tt-=26
elif tt<base:
tt+=26
res+=chr(tt)
print (decode,res)
.... the same as a function:
def caesar_encrypt(message,shift):
base=ord('a')
res=''
for character in message.lower():
if character==' ':
tt=ord(' ')
else:
ascii=ord(character)
tt=ascii+shift
if tt>=base+26:
tt-=26
elif tt<base:
tt+=26
res+=chr(tt)
return res
caesar_encrypt(s,-13)
'congratulations you have succeeded in decrypting the string'
Determine the unkown cipher shift trying out all
s='wxkxmh ngynlcb cqn byjwrbq rwzdrbrcrxw'
for shift in range(-10,20):
print (shift,caesar_encrypt(s,shift))
-10 mnancx dwodbsr sgd rozmhrg hmpthrhshnm -9 nobody expects the spanish inquisition -8 opcpez fyqfdut uif tqbojti jorvjtjujpo -7 pqdqfa gzrgevu vjg urcpkuj kpswkukvkqp -6 qrergb hashfwv wkh vsdqlvk lqtxlvlwlrq -5 rsfshc ibtigxw xli wtermwl mruymwmxmsr -4 stgtid jcujhyx ymj xufsnxm nsvznxnynts -3 tuhuje kdvkizy znk yvgtoyn otwaoyozout -2 uvivkf lewljaz aol zwhupzo puxbpzpapvu -1 vwjwlg mfxmkba bpm axivqap qvycqaqbqwv 0 wxkxmh ngynlcb cqn byjwrbq rwzdrbrcrxw 1 xylyni ohzomdc dro czkxscr sxaescsdsyx 2 yzmzoj piapned esp dalytds tybftdtetzy 3 zanapk qjbqofe ftq ebmzuet uzcgueufuaz 4 abobql rkcrpgf gur fcnavfu vadhvfvgvba 5 bcpcrm sldsqhg hvs gdobwgv wbeiwgwhwcb 6 cdqdsn tmetrih iwt hepcxhw xcfjxhxixdc 7 dereto unfusji jxu ifqdyix ydgkyiyjyed 8 efsfup vogvtkj kyv jgrezjy zehlzjzkzfe 9 fgtgvq wphwulk lzw khsfakz afimakalagf 10 ghuhwr xqixvml max litgbla bgjnblbmbhg 11 hivixs yrjywnm nby mjuhcmb chkocmcncih 12 ijwjyt zskzxon ocz nkvidnc dilpdndodji 13 jkxkzu atlaypo pda olwjeod ejmqeoepekj 14 klylav bumbzqp qeb pmxkfpe fknrfpfqflk 15 lmzmbw cvncarq rfc qnylgqf glosgqgrgml 16 mnancx dwodbsr sgd rozmhrg hmpthrhshnm 17 nobody expects the spanish inquisition 18 opcpez fyqfdut uif tqbojti jorvjtjujpo 19 pqdqfa gzrgevu vjg urcpkuj kpswkukvkqp
Note solution -9 and 17 are equivalent.