In [1]:
s = "lady gaga rules"
In [2]:
s[0:4]
Out[2]:
In [3]:
s[0]+s[5]+s[10]
Out[3]:
In [4]:
"".join([ s[i] for i in [0,5,10] ])
Out[4]:
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]:
In [10]:
unscrambled = [' ']*15
In [11]:
unscrambled
Out[11]:
In [12]:
list(range(15))
Out[12]:
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)))
In [14]:
"".join(unscrambled)
Out[14]:
In [19]:
!easy_install pip
In [18]:
!pip3 install 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]:
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]:
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]:
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]:
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)
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
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]:
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]:
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]:
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]:
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)
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]:
Problem: test which function is faster - F_rec
or F_clos
. Test it for various values of n
.
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]:
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]:
In [82]:
main_fib(7)
In [15]:
turtle.bye()
In [13]:
main_fib(15)
In [ ]: