
# 3. How do you change the frequency of a running timer, either increasing or decreasing the frequency? E.g., in the code below,
# we want code at the question marks that changes the timer.

# …
# timer = simplegui.create_timer(1000, timer_handler)
# timer.start()
# …
# ???

import simplegui

def timer_handler():
    print "1"

# register event handlers
timer = simplegui.create_timer(1000, timer_handler)

# start program
timer.start()

# Ans: You can't. But, you can stop this timer, and start a new one with a different frequency and same handler.

timer.stop()

timer = simplegui.create_timer(300, timer_handler)

timer.start()

# 6. Use time.time() to measure the time to run an operation.
import time

time1 = time.time(0)
        # The code being timed
    
time2 = time.time()
print 'Time elapsed:', time2 - time1

# The Epoch is fixed common date shared by all Python installations. 
y = 60*60*24*365.25           # number of seconds for a year
x = time.time()
yrs = round(x/y)              # number of years
epoch = int(2015 - yrs)
print epoch

# 7. The Python code below uses a timer to execute the function update() 10 times, computing a good approximation to a common
# mathematical function. Examine the code, and run it while varying the input value n.
# What is the common name for what this computes?

# Mystery computation in Python
# Takes input n and computes output named result

import simplegui

# global state

result = 1
iteration = 0
max_iterations = 10

# helper functions

def init(start):
    """Initializes n."""
    global n
    n = start
    print "Input is", n
    
def get_next(current):
    """???  Part of mystery computation."""
    return 0.5 * (current + n / current)

# timer callback

def update():
    """???  Part of mystery computation."""
    global iteration, result
    iteration += 1
    # Stop iterating after max_iterations
    if iteration >= max_iterations:
        timer.stop()
        print "Output is", result
    else:
        result = get_next(result)

# register event handlers

timer = simplegui.create_timer(1, update)

# start program
init(13)
timer.start()

# Ans: Square root of n.

# 8. Given any initial natural number, consider the sequence of numbers generated by repeatedly following the rule:

# divide by two if the number is even or
# multiply by 3 and add 1 if the number is odd.
# The Collatz conjecture states that this sequence always terminates at 1. For example, the sequence generated by 23 is:

# 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1

# Write a Python program that takes a global variable n and uses a timer callback to repeatedly apply the rule above to n. 
# Use the code from the previous question (question 7) as a template. I suggest that your code prints out the sequence of
# numbers generated by this rule. Run this program for n = 217. What is the largest number in the sequence generated by 
# this starting value?

# To test your code, starting at n = 23 generates a sequence with a maximum value of 160.
import simplegui

# global state
iteration = 0
current = 217

# helper functions

def init(current):
    """Initializes n."""
    print "Input is", current
    
def sequence(current):
    """Sequence generation rule."""
    if current % 2 == 0:
        return current / 2
    else:
        return current * 3 + 1

# timer callback

def update():
    global iteration, current
    iteration += 1
    # Stop iterating if sequence becomes 1
    if current == 1:
        timer.stop()
        print "Last output is", current
    else:
        current = sequence(current)
        print current
        
# register event handlers
timer = simplegui.create_timer(1, update)

# start program
init(current)
timer.start()

# result
# Input is 217
652
326
163
490
245
736
368
184
92
46
23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1
# Last output is 1

# Ans: 736

# 9. Animation of explosion using 2D sprite sheet

import simplegui

# load 81 frame sprite sheer for explosion - image generated by phaedy explosion generator, source is hasgraphics.com
EXPLOSION_CENTER = [50, 50]
EXPLOSION_SIZE = [100, 100]
EXPLOSION_DIM = [9, 9]
explosion_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/explosion.hasgraphics.png")

# create timer that iterates current_sprite_center through sprite
time = 0

# define draw handler
def draw(canvas):
    global time
    explosion_index = [time % EXPLOSION_DIM[0], (time // EXPLOSION_DIM[0]) % EXPLOSION_DIM[1]]
    canvas.draw_image(explosion_image, 
                    [EXPLOSION_CENTER[0] + explosion_index[0] * EXPLOSION_SIZE[0], 
                     EXPLOSION_CENTER[1] + explosion_index[1] * EXPLOSION_SIZE[1]], 
                     EXPLOSION_SIZE, EXPLOSION_CENTER, EXPLOSION_SIZE)
    time += 1

        
# create frame and size frame based on 100x100 pixel sprite
f = simplegui.create_frame("Asteroid sprite", EXPLOSION_SIZE[0], EXPLOSION_SIZE[1])

# set draw handler and canvas background using custom HTML color
f.set_draw_handler(draw)
f.set_canvas_background("Blue")

# start animation
f.start()

# A modified code version of question 8

import simplegui

# global state
iteration = 0
current = 217
lst = []

# helper functions

def init(current):
    """Initializes n."""
    print "Input is", current
    
def sequence(current):
    """Sequence generation rule."""
    if current % 2 == 0:
        return current / 2
    else:
        return current * 3 + 1

# timer callback

def update():
    global iteration, current
    iteration += 1
    # Stop iterating if sequence becomes 1
    if current == 1:
        timer.stop()
        print "Last output is", current
    else:
        current = sequence(current)
        #print current
        lst.append(current)
        print lst
    print "Max element: " +  str(max(lst))   
# register event handlers
timer = simplegui.create_timer(1, update)

# start program
init(current)
timer.start()

# result
"""
Input is 217
[652]
Max element: 652
[652, 326]
Max element: 652
[652, 326, 163]
Max element: 652
[652, 326, 163, 490]
Max element: 652
[652, 326, 163, 490, 245]
Max element: 652
[652, 326, 163, 490, 245, 736]
Max element: 736
[652, 326, 163, 490, 245, 736, 368]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2]
Max element: 736
[652, 326, 163, 490, 245, 736, 368, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]
Max element: 736
Last output is 1
Max element: 736
"""
