s = "lady gaga rules"

s[0:4]

s[0]+s[5]+s[10]

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

s = "lady gaga rules"

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

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

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

scrambled

unscrambled = [' ']*15

unscrambled

list(range(15))

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)))


"".join(unscrambled)

"""
Explanation: Use Lists to Build Cypher
End of explanation
"""


!easy_install pip

!pip3 install tutormagic

%load_ext tutormagic

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


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


'i' in 'pilon'

"""
Explanation: Loops
End of explanation
"""


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


flipside

%%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")

def hours_to_minutes(x):
    return x*60

hours_to_minutes(12)

"""
Explanation: Functions
End of explanation
"""


import turtle

wn = turtle.Screen()

alex = turtle.Turtle()

alex.forward(50)

alex.left(90)

alex.forward(30)

alex.position()

wn.mainloop()

wn = turtle.Screen()

wn.bgcolor("red")

wn.title("Hello")

tess = turtle.Turtle()

tess.color("blue")

tess.pensize(3)

tess.forward(50)

tess.left(120)

tess.forward(50)

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)

"""
Explanation: Turtle Graphics
End of explanation
"""


my_friends = {}

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


#!pip install tutormagic
%load_ext tutormagic

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


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

my_other_friends["zulfia"] = "zulfia_this"

my_other_friends["zulfia"]

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

print("hgsdadgsah")

print("zulfiya" in my_other_friends)

for z in y:
    print(z in my_other_friends)


type(my_other_friends)

my_other_friends.keys()

my_other_friends

y

y[0]

y[3]

my_other_friends["fthhgfghf"]

"fthhgfghf" in my_other_friends

"""
Explanation: Associative Arrays (Python dict)
End of explanation
"""


import turtle

wn = turtle.Screen()

kuzya = turtle.Turtle()

kuzya.pensize(10)

alex = kuzya

alex.forward(60)
alex.left(90)
alex.forward(60)
alex.left(90)
alex.forward(60)
alex.left(90)
alex.forward(60)
alex.left(90)

wn.mainloop()

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

alex

"""
Explanation: Loops with Turtle
End of explanation
"""


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

"""
Explanation: Golden Ratio
End of explanation
"""


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

"""
Explanation: Link to Wiki Page
Fibonacchi Numbers
End of explanation
"""


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(40)

%%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)

"""
Explanation: Link to Web Page for Fibonacci Numbers
Recursion
End of explanation
"""


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

F_clos(40)

"""
Explanation: Closed form expression
End of explanation
"""


F_clos(41)/F_clos(40)

"""
Explanation: 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
End of explanation
"""


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)

fibonacci_word(7)

main_fib(7)

turtle.bye()

main_fib(15)

"""
Explanation: 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 f_n 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.
Link to article on Fibonacci Word
End of explanation
"""


