neex
4/14/2017 - 6:43 PM

Code snippets and solutions

Code snippets and solutions

# prime numbers
def is_prime(num):
    isPrime = True
    for divider in range(2, num):
        #print(divider)
        if num%divider == 0:
            isPrime = False
    return isPrime
############################################
# easter
def easterDate(year):
    a = year % 19
    b = year % 4
    c = year % 7
    d = (19 * a + 24) % 30
    e = (2 * b + 4 * c + 6 * d + 5) % 7
    result = 22 + d + e
    
    if year in (1954, 1981, 2049, 2076):
        result -= 7
        
    if result > 31:
        result -= 31

    if 1900 <= year <= 2099:
        return result 
    else:
        return "Wrong Date"
    
print(easterDate(2017))
print(easterDate(1983))
print(easterDate(2000))
print(easterDate(2018))
print(easterDate(2100))
print(easterDate(1900))
print(easterDate(2111))
#####################################################
# not full leap Year
def isLeap(year):
    """A year is a leap year if it is divisible by 4 unless it is a century that
    is not divisible by 400. Write a function that takes a year as a parameter
    and returns True if the year is a leap year, False otherwise."""
    year = float(year)
    if year % 4 == 0:
        if year % 400 != 0 and year % 100 == 0:
            return False
        else:
            return True
    else:
        return False

# Below is a set of tests so you can check if your code is correct.
# Do not copy this part into Vocareum.
from test import testEqual

testEqual(isLeap(1944), True)
testEqual(isLeap(2011), False)
testEqual(isLeap(1986), False)
testEqual(isLeap(1956), True)
testEqual(isLeap(1957), False)
testEqual(isLeap(1800), False)
testEqual(isLeap(1900), False)
testEqual(isLeap(1600), True)
testEqual(isLeap(2056), True)
import turtle

window = turtle.Screen()
window.bgcolor("lightgreen")

alex = turtle.Turtle()
alex.speed(0)
alex.pencolor("red")
alex.pensize(3)

def drawSquare(turtle, step_size):
    for i in range(4):
        turtle.forward(step_size)
        turtle.left(90)
        
def drawPattern(t, base_distance, pattern_number): 
    t.up()
    t.backward(base_distance/2)
    t.down()
    for i in range(base_distance, (pattern_number + 1) * base_distance * 2, base_distance * 2):        
        drawSquare(t, i)
        t.up()
        t.backward(base_distance)
        t.right(90)
        t.forward(base_distance)
        t.left(90)
        t.down()

drawPattern(alex, 20, 6)
##################################################################
import turtle

window = turtle.Screen()
window.bgcolor("green")

alex = turtle.Turtle()
alex.speed(0)

def drawShape(turtle, step, amount, angle):
    step_size = step
    for i in range(amount):
        turtle.forward(step_size)
        turtle.right(angle)
        step_size += step
        
drawShape(alex, 2, 300, 89)

##################################################################
import turtle

window = turtle.Screen()
alex = turtle.Turtle()

def draw_star(turtle, length):
    for num in range(5):
        turtle.forward(length)
        turtle.right(360 * 2 / 5)
    
draw_star(alex, 100)

##################################################################
import turtle

window = turtle.Screen()
alex = turtle.Turtle()

def draw_star(turtle, length):
    for num in range(5):
        turtle.forward(length)
        turtle.right(360 * 2 / 5)

def draw_many(turtle, length, num_drawings):
    for i in range(num_drawings):
        turtle.up()
        turtle.forward(350)
        turtle.right(144)
        turtle.down()
        draw_star(turtle, 100)

alex.up()
alex.backward(180)
alex.left(90)
alex.forward(50)
alex.right(90)
alex.speed(0)
draw_many(alex, 100, 5)
##################################################################

##################################################################

##################################################################

##################################################################

##################################################################

##################################################################

##################################################################
# Think Python
# Chapter 3. Debugging
# https://runestone.launchcode.org/runestone/static/thinkcspy/PythonTurtle/Exercises.html

import turtle

window = turtle.Screen()

tut = turtle.Turtle()
tut.left(90)
tut.up()
tut.forward(180)
tut.left(270)
tut.backward(50)
tut.down()

for i in [3,4,6,8]:
    for number in range(i):
        tut.forward(100)
        tut.right(360/i)

##############################################
import turtle
window = turtle.Screen()

tut = turtle.Turtle()

for i in range(5):
    tut.forward(100)
    tut.left(180-36)


#############################################
import turtle
window = turtle.Screen()
window.bgcolor("green")

tut = turtle.Turtle()
tut.up()

for i in range(12):
    tut.forward(100)
    tut.down()
    tut.forward(10)
    tut.up()
    tut.forward(15)
    tut.stamp()
    tut.backward(125)
    tut.right(360/12)

################################################
import turtle

window = turtle.Screen()

amber = turtle.Turtle()
amber.color("red")
amber.speed(0)
amber.up()
amber.left(90)
amber.forward(100)
amber.right(90)
amber.down()

for i in range(180):
    for a in range(4):
        amber.forward(70)
        amber.right(90)
    amber.right(2)

amber.color("yellow")
for i in range(180):
    for a in range(4):
        amber.forward(50)
        amber.right(90)
    amber.right(2)

amber.color("green")
for i in range(180):
    for a in range(4):
        amber.forward(25)
        amber.right(90)
    amber.right(2)

    
    
amber.color("black")
amber.right(90)
amber.forward(200)
#############################################
# Think Python
# Chapter 3. Debugging
# https://runestone.launchcode.org/runestone/static/thinkcspy/Debugging/Exercises.html
"""
You have a thermostat that allows you to set the room to any temperature between 40 and 90 degrees.
The thermostat can be adjusted by turning a circular dial. If you turn the dial all the way to the left, you will set the temperature to 40 degrees. If you turn to the right by one click, you will get 41 degrees. As you continue to turn to the right, the temperature goes up, and the temperature gets closer and closer to 90 degrees. But as soon as you complete one full rotation (50 clicks), the temperature cycles back around to 40 and starts over.
Write a program that calculates the temperature based on how much the dial has been turned. You should prompt the user for a number of clicks-to-the-right (from the starting point of 40 degrees). Then you should print the current temperature.
Here is an example of how your program should behave (When you see >>>, that line represents what the user is typing in):
"""

clicks_str = input("By how many clicks has the dial been turned?")
clicks = int(clicks_str)

clicks = clicks % 50 # will work for both, plus and minus, returns the data going the other way to get to the point

temperature = (40 + clicks) % 90
print(temperature)

# TODO calculate the temperature, and report it back to the user
Solutions to Think Python book