Harold Nelson
2/13/2018
This is the title of a book by Daniel Kahneman. https://www.amazon.com/Thinking-Fast-Slow-Daniel-Kahneman/dp/0374533555/ref=tmm_pap_title_0?_encoding=UTF8&qid=1518538316&sr=8-1
A ball and a bat together cost $11. The bat cost $10 more than the ball. What are the prices of the ball and the bat?
Let \(bat\) be the price of the bat and \(ball\) be the price of the ball.
We have two equations \[ball+bat=11\] \[bat = ball + 10\] Substituting from the second equation into the first, we get \[ball+(ball+10)=11\] Rewriting this we get \[2ball+10=11\] \[2ball=1\] \[ball=.5\] So, \[bat=ball+10=.5+10=10.5\]
If you thought, the ball cost $1, what happens when you substitute this value into the following equation
\[ball+(ball+10)=11\] Did you use System 1 when you needed System 2?
Put yourself in the role of the python interpreter. Lay out your variables on paper and execute the program with your mind, tracking the history of the variables on paper.
Consider the following simple program
s = 0
for i in range(3):
s = s + 2 * i - 1
print(s)
## 3
What does your paper look like when you’re done?
s: 0,-1,0,3
i: 0,1,2
This is a good way to understand what a program is doing.
s = 0
for i in range(3):
print('loc 1',s,i)
s = s + 2 * i - 1
print('loc 2',s,i)
print(s)
## loc 1 0 0
## loc 2 -1 0
## loc 1 -1 1
## loc 2 0 1
## loc 1 0 2
## loc 2 3 2
## 3
We need two functions
Separate the string into the odd-numbered and even-numbered characters.
Concatenate these two.
The following code encrypts the title of this slide.
def scramble2Encrypt(plainText):
evenChars = ""
oddChars = ""
charCount = 0
for ch in plainText:
if charCount % 2 == 0:
evenChars = evenChars + ch
else:
oddChars = oddChars + ch
charCount = charCount + 1
cipherText = oddChars + evenChars
return cipherText
pt = "Code for Encryption"
ct = scramble2Encrypt(pt)
print(ct)
## oefrEcytoCd o nrpin
def scramble2Decrypt(cipherText):
halfLength = len(cipherText) // 2
oddChars = cipherText[:halfLength]
evenChars = cipherText[halfLength:]
plainText = ""
for i in range(halfLength):
plainText = plainText + evenChars[i]
plainText = plainText + oddChars[i]
if len(oddChars) < len(evenChars):
plainText = plainText + evenChars[-1]
return plainText
ct = "oefrEcytoCd o nrpin"
pt = scramble2Decrypt(ct)
print(pt)
## Code for Encryption
The code for these two functions is available in Moodle. There is also a link to the online debugger.
Download the code and create a very short message to walk through the code on paper.
Then use the online debugging tool above to trace the execution as these two programs encrypt and decrypt your message.