Use Lists to Build Cypher


In [1]:
s = "lady gaga rules"

In [2]:
s[0:4]


Out[2]:
'lady'

In [3]:
s[0]+s[5]+s[10]


Out[3]:
'lgr'

In [4]:
"".join([ s[i] for i in [0,5,10] ])


Out[4]:
'lgr'

In [5]:
s = "lady gaga rules"

In [6]:
cypher = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]

In [7]:
cypher = [14,3,11,4,2,9,12,5,1,13,0,10,6,8,7]

In [8]:
scrambled = "".join([ s[i] for i in cypher ])

In [9]:
scrambled


Out[9]:
'syu d lgaelraag'

In [10]:
unscrambled = [' ']*15

In [11]:
unscrambled


Out[11]:
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']

In [12]:
list(range(15))


Out[12]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

In [13]:
for i in range(15):
    unscrambled[cypher[i]] = scrambled[i]
    print("i=%s     cypher[i]=%s     scrambled[i]=%s \n unscrambled='%s'" % \
          (i,cypher[i],scrambled[i],"".join(unscrambled)))


i=0     cypher[i]=14     scrambled[i]=s 
 unscrambled='              s'
i=1     cypher[i]=3     scrambled[i]=y 
 unscrambled='   y          s'
i=2     cypher[i]=11     scrambled[i]=u 
 unscrambled='   y       u  s'
i=3     cypher[i]=4     scrambled[i]=  
 unscrambled='   y       u  s'
i=4     cypher[i]=2     scrambled[i]=d 
 unscrambled='  dy       u  s'
i=5     cypher[i]=9     scrambled[i]=  
 unscrambled='  dy       u  s'
i=6     cypher[i]=12     scrambled[i]=l 
 unscrambled='  dy       ul s'
i=7     cypher[i]=5     scrambled[i]=g 
 unscrambled='  dy g     ul s'
i=8     cypher[i]=1     scrambled[i]=a 
 unscrambled=' ady g     ul s'
i=9     cypher[i]=13     scrambled[i]=e 
 unscrambled=' ady g     ules'
i=10     cypher[i]=0     scrambled[i]=l 
 unscrambled='lady g     ules'
i=11     cypher[i]=10     scrambled[i]=r 
 unscrambled='lady g    rules'
i=12     cypher[i]=6     scrambled[i]=a 
 unscrambled='lady ga   rules'
i=13     cypher[i]=8     scrambled[i]=a 
 unscrambled='lady ga a rules'
i=14     cypher[i]=7     scrambled[i]=g 
 unscrambled='lady gaga rules'

In [14]:
"".join(unscrambled)


Out[14]:
'lady gaga rules'

Loops


In [19]:
!easy_install pip


Searching for pip
Best match: pip 9.0.1
Adding pip 9.0.1 to easy-install.pth file
Installing pip script to /usr/local/bin
Installing pip2.7 script to /usr/local/bin
Installing pip2 script to /usr/local/bin

Using /usr/local/lib/python2.7/site-packages
Processing dependencies for pip
Finished processing dependencies for pip

In [18]:
!pip3 install tutormagic


Requirement already satisfied: tutormagic in /usr/local/lib/python3.6/site-packages
Requirement already satisfied: jupyter>=1.0 in /usr/local/lib/python3.6/site-packages (from tutormagic)
Requirement already satisfied: jupyter-console in /usr/local/lib/python3.6/site-packages (from jupyter>=1.0->tutormagic)
Requirement already satisfied: nbconvert in /usr/local/lib/python3.6/site-packages (from jupyter>=1.0->tutormagic)
Requirement already satisfied: notebook in /usr/local/lib/python3.6/site-packages (from jupyter>=1.0->tutormagic)
Requirement already satisfied: ipykernel in /usr/local/lib/python3.6/site-packages (from jupyter>=1.0->tutormagic)
Requirement already satisfied: ipywidgets in /usr/local/lib/python3.6/site-packages (from jupyter>=1.0->tutormagic)
Requirement already satisfied: qtconsole in /usr/local/lib/python3.6/site-packages (from jupyter>=1.0->tutormagic)
Requirement already satisfied: jupyter-client in /usr/local/lib/python3.6/site-packages (from jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: pygments in /usr/local/lib/python3.6/site-packages (from jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.0 in /usr/local/lib/python3.6/site-packages (from jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: ipython in /usr/local/lib/python3.6/site-packages (from jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: jupyter-core in /usr/local/lib/python3.6/site-packages (from nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.6/site-packages (from nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.6/site-packages (from nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.6/site-packages (from nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.6/site-packages (from nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: bleach in /usr/local/lib/python3.6/site-packages (from nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: nbformat in /usr/local/lib/python3.6/site-packages (from nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: testpath in /usr/local/lib/python3.6/site-packages (from nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: mistune!=0.6 in /usr/local/lib/python3.6/site-packages (from nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: ipython-genutils in /usr/local/lib/python3.6/site-packages (from notebook->jupyter>=1.0->tutormagic)
Requirement already satisfied: tornado>=4 in /usr/local/lib/python3.6/site-packages (from notebook->jupyter>=1.0->tutormagic)
Requirement already satisfied: terminado>=0.3.3; sys_platform != "win32" in /usr/local/lib/python3.6/site-packages (from notebook->jupyter>=1.0->tutormagic)
Requirement already satisfied: widgetsnbextension>=1.2.6 in /usr/local/lib/python3.6/site-packages (from ipywidgets->jupyter>=1.0->tutormagic)
Requirement already satisfied: pyzmq>=13 in /usr/local/lib/python3.6/site-packages (from jupyter-client->jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: wcwidth in /usr/local/lib/python3.6/site-packages (from prompt-toolkit<2.0.0,>=1.0.0->jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.6/site-packages (from prompt-toolkit<2.0.0,>=1.0.0->jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: simplegeneric>0.8 in /usr/local/lib/python3.6/site-packages (from ipython->jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: appnope; sys_platform == "darwin" in /usr/local/lib/python3.6/site-packages (from ipython->jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: decorator in /usr/local/lib/python3.6/site-packages (from ipython->jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.6/site-packages (from ipython->jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: pickleshare in /usr/local/lib/python3.6/site-packages (from ipython->jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: pexpect; sys_platform != "win32" in /usr/local/lib/python3.6/site-packages (from ipython->jupyter-console->jupyter>=1.0->tutormagic)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.6/site-packages (from jinja2->nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: html5lib!=0.9999,!=0.99999,<0.99999999,>=0.999 in /usr/local/lib/python3.6/site-packages (from bleach->nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /usr/local/lib/python3.6/site-packages (from nbformat->nbconvert->jupyter>=1.0->tutormagic)
Requirement already satisfied: ptyprocess in /usr/local/lib/python3.6/site-packages (from terminado>=0.3.3; sys_platform != "win32"->notebook->jupyter>=1.0->tutormagic)

In [1]:
%load_ext tutormagic

In [16]:
%%tutor --lang python3
x = 3
for i in range(3):
    x = x + 1



In [21]:
%%tutor --lang python3
x = 1
for i in range(3):
    x = x + i



In [22]:
'i' in 'pilon'


Out[22]:
True

Functions


In [23]:
def flipside(s):
    """ flipside(s): swaps s's sides!
        input s: a string
    """
    x = len(s)//2
    return s[x:] + s[:x]

In [24]:
flipside


Out[24]:
<function __main__.flipside>

In [25]:
%%tutor --lang python3
def flipside(s):
    """ flipside(s): swaps s's sides!
        input s: a string
    """
    x = len(s)//2
    return s[x:] + s[:x]

x = 12234
y = flipside("abcdef")



In [26]:
def hours_to_minutes(x):
    return x*60

In [27]:
hours_to_minutes(12)


Out[27]:
720

Turtle Graphics


In [28]:
import turtle

In [29]:
wn = turtle.Screen()

In [30]:
alex = turtle.Turtle()

In [31]:
alex.forward(50)

In [32]:
alex.left(90)

In [33]:
alex.forward(30)

In [34]:
alex.position()


Out[34]:
(50.00,30.00)

In [40]:
wn.mainloop()

In [36]:
wn = turtle.Screen()

In [37]:
wn.bgcolor("red")

In [38]:
wn.title("Hello")

In [ ]:
tess = turtle.Turtle()

In [ ]:
tess.color("blue")

In [ ]:
tess.pensize(3)

In [ ]:
tess.forward(50)

In [ ]:
tess.left(120)

In [ ]:
tess.forward(50)

In [ ]:
alex = turtle.Turtle()
alex.color("green")
alex.pensize(10)
alex.forward(60)
alex.left(90)
alex.forward(60)
alex.left(90)
alex.forward(60)
alex.left(90)
alex.forward(60)
alex.left(90)

Associative Arrays (Python dict)


In [ ]:
my_friends = {}

In [ ]:
import turtle
for name in ["zoyka","maruska","zulfia","gulchatay"]:
    x = turtle.Turtle()
    my_friends[name] = x

In [ ]:
#!pip install tutormagic
%load_ext tutormagic

In [ ]:
%%tutor --lang python3
my_other_friends = {}
for name in ["zoyka","maruska","zulfia","gulchatay"]:
    x = name + "_other"
    my_other_friends[name] = x

In [ ]:
my_other_friends = {}
for name in ["zoyka","maruska","zulfia","gulchatay"]:
    x = name + "_other"
    my_other_friends[name] = x
my_other_friends

In [ ]:
my_other_friends["zulfia"] = "zulfia_this"

In [ ]:
my_other_friends["zulfia"]

In [ ]:
y = ["todd","john","maruska","anna","soyka"]

In [ ]:
print("hgsdadgsah")

In [ ]:
print("zulfiya" in my_other_friends)

In [ ]:
for z in y:
    print(z in my_other_friends)

In [ ]:
type(my_other_friends)

In [ ]:
my_other_friends.keys()

In [ ]:
my_other_friends

In [ ]:
y

In [ ]:
y[0]

In [ ]:
y[3]

In [ ]:
my_other_friends["fthhgfghf"]

In [ ]:
"fthhgfghf" in my_other_friends

Loops with Turtle


In [ ]:
import turtle

In [ ]:
wn = turtle.Screen()

In [ ]:
kuzya = turtle.Turtle()

In [ ]:
kuzya.pensize(10)

In [ ]:
alex = kuzya

In [ ]:
alex.forward(60)
alex.left(90)
alex.forward(60)
alex.left(90)
alex.forward(60)
alex.left(90)
alex.forward(60)
alex.left(90)

In [ ]:
wn.mainloop()

In [ ]:
for i in range(20):
    alex.forward(i*i)
    alex.left(i*i)

In [41]:
alex


Out[41]:
<turtle.Turtle at 0x10636b320>

Golden Ratio


In [43]:
import IPython
url = 'https://en.wikipedia.org/wiki/Golden_ratio'
iframe = '<iframe src=' + url + ' width=100% height=800></iframe>'
IPython.display.HTML(iframe)


Out[43]:

Fibonacchi Numbers


In [85]:
import IPython
url = 'https://en.wikipedia.org/wiki/Fibonacci_number'
iframe = '<iframe src=' + url + ' width=100% height=800></iframe>'
IPython.display.HTML(iframe)


Out[85]:

Recursion


In [2]:
def F_rec(n):
    if n == 0: return 0
    elif n == 1: return 1
    else: return F_rec(n-1)+F_rec(n-2)

In [5]:
F_rec(40)


Out[5]:
102334155

In [73]:
%%tutor --lang python3
def F_rec(n):
    if n == 0: return 0
    elif n == 1: return 1
    else: return F_rec(n-1)+F_rec(n-2)
F_rec(4)


Closed form expression


In [6]:
import math
def F_clos(n):
    return ((1+math.sqrt(5))**n-(1-math.sqrt(5))**n)/(2**n*math.sqrt(5))

In [8]:
F_clos(40)


Out[8]:
102334155.00000013

Problem: test which function is faster - F_rec or F_clos. Test it for various values of n.

How to compute Golden Ratio from Fibonacci Numbers

The golden ratio is the limit of a sequence of the ratios of successive terms of the Fibonacci sequence (or any Fibonacci-like sequence), as originally shown by Kepler

$$\lim_{n\to\infty}\frac{F(n+1)}{F(n)}=\varphi$$

Problem: Write Python function to approximate the Golden Ratio using Kepler's expression above and any of the two implementations that compute Fibinacci numbers


In [9]:
F_clos(41)/F_clos(40)


Out[9]:
1.6180339887498951

Fibonacchi Word

How do you turn a word into a fractal? It helps to start with the right word, and a good one is the Fibonacci word.

By a "word", we mean a sequence of symbols, in this case 0s and 1s. The Fibonacci word is defined by setting f_0 to be the word "1" and f_1 to be the word "0". To define fn for n = 2 and higher, we simply concatenate the words f{n-1} and f{n-2}, with f{n-1} on the left. This gives f_2 = "01", f_3 = "010", f_4 = "01001", and so on. Notice that the number of symbols in each of these partial words f_0, ..., f_4 is given by the sequence 1, 1, 2, 3, 5, ..., which is the famous Fibonacci sequence: each term after the first two is the sum of the previous two terms. The Fibonacci word itself is an infinite sequence f of 0s and 1s obtained by continuing this procedure indefinitely, so that f = 0100101001001010010100100101...

OK, so how do we turn this into a fractal? Well, we interpret the word f as a sequence of instructions, such as one might have fed to a turtle-shaped pen-holding robot in the old computer language LOGO. More specifically, the symbol "1" means "draw a line forward". The symbol "0" means "draw a line forward; then turn left if the 0 is in an even-numbered position, or turn right if the 0 is in an odd-numbered position". The results of applying this procedure to the words f_10 and f_17 are shown in Figure 1. Notice how the longer the word becomes, the more fractal-like the output becomes.


In [16]:
import turtle
def fibonacci_word(n) :
    if n < 0 :
        return None
    elif n == 0 :
        return [1]
    elif n == 1 :
        return [0]
    else :
        f_n_1 = fibonacci_word(n - 1)
        f_n_2 = fibonacci_word(n - 2)
        return f_n_1 + f_n_2

def draw_fibonacci_word(fib_word, step = 10):
    import turtle
    turtle.setworldcoordinates(0, 0, 800, 600)
    turtle.Screen()
    turtle.home()
    for i, symbol in enumerate(fib_word):
        turtle.forward(step)
        if symbol == 0 and i % 2 == 0:
            turtle.left(90)
        elif symbol == 0 and i % 2 == 1:
            turtle.right(90)

def main_fib(n) :
    fib_word_n = fibonacci_word(n)
    draw_fibonacci_word(fib_word_n)

In [84]:
fibonacci_word(7)


Out[84]:
[0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0]

In [82]:
main_fib(7)

In [15]:
turtle.bye()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-af1255bec85b> in <module>()
----> 1 turtle.bye()

NameError: name 'turtle' is not defined

In [13]:
main_fib(15)

In [ ]: