Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Binary file modified base_app/.DS_Store
Binary file not shown.
Binary file modified base_app/static/.DS_Store
Binary file not shown.
70 changes: 68 additions & 2 deletions chap6.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,75 @@
# Enter your answrs for chapter 6 here
# Name:
# Name: Elizabeth Tenorio


# Ex. 6.6
def first(word):
return word[0]
def last(word):
return word[-1]
def middle(word):
return word[1:-1]
#print first('biology')
#print last('biology')
# print middle('biology')
# print middle('hit')
# print middle('hi')
# print middle('a')
# print middle('')

# Type these functions into a file named palindrome.py and test them out.
# What happens if you call middle with a string with two letters?
#=> returns an empty string
# One letter?
#=> returns an empty string
# What about the empty string, which is written '' and contains no letters?
#=> returns an empty string

# Write a function called is_palindrome that takes a string argument and returns
# True if it is a palindrome and False otherwise.
# Remember that you can use the built-in function len to check the length of a string.

def is_palindrome(word):
if len(word) <=1:
return True
if first(word) != last(word):
return False
return is_palindrome(middle(word))

# print is_palindrome('cat') #=>False
# print is_palindrome('noon') #=>True
# print is_palindrome('radar') #=> True



# Ex 6.8
# Write a function called gcd that takes parameters
# a and b and returns their greatest common divisor.

def gcd(a, b):
if a == 0:
return b
if b == 0:
return a

elif a > b:
r = a % b
if r == 0:
return b
else:
# print r, b
return gcd(b, r)
elif b > a:
a, b = b, a
return gcd(a,b)


#print gcd(0, 4)
#print gcd(4, 0)
#print gcd(4, 2)
#print gcd(21, 9)
#print gcd(13, 17)
#print gcd(125, 4300)



# Ex 6.8
44 changes: 42 additions & 2 deletions chap7.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
# Enter your answrs for chapter 7 here
# Name:
# Name: Elizabeth Tenorio


# Ex. 7.5
# The mathematician Srinivasa Ramanujan found an infinite series
# that can be used to generate a numerical approximation of pi:

# Write a function called estimate_pi that uses this formula to
# compute and return an estimate of pi.
# You can check the result by comparing it to math.pi.

import math
# print eval('math.pi')

def factorial(n):
if n == 0:
return 1
else:
recurse = factorial(n-1)
result = n * recurse
return result
# print factorial(5)

def estimate_pi():
total = 0
k = 0
factor = 2 * math.sqrt(2) / 9801
keep_going = True
while keep_going == True:
numerator = float(factorial(4*k) * (1103 + 26390 * k))
#print 'Num: ', numerator
denominator = float(factorial(k)**4) * 396**(4*k)
#print 'Den: ', denominator
term = factor * float(numerator/denominator)
#print term
total += term
k += 1
if term < 1e-15:
keep_going == False
print "Finished at iteration number", k
break
return 1 / total

print estimate_pi()

# How many iterations does it take to converge?
# Finished at iteration number 3

# How many iterations does it take to converge?
24 changes: 24 additions & 0 deletions gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def gcd(a, b):
if a == 0:
return b
if b == 0:
return a

elif a > b:
r = a % b
if r == 0:
return b
else:
# print r, b
return gcd(b, r)
elif b > a:
a, b = b, a
return gcd(a,b)


#print gcd(0, 4)
#print gcd(4, 0)
#print gcd(4, 2)
#print gcd(21, 9)
#print gcd(13, 17)
#print gcd(125, 4300)
Binary file added my_app/.DS_Store
Binary file not shown.
73 changes: 73 additions & 0 deletions my_app/base_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Week2 web app
# Name: Elizabeth Tenorio

import flask, flask.views
import os
import functools

app = flask.Flask(__name__)

app.secret_key = "dan"
users = {'beth':'dan'}

class Main(flask.views.MethodView):
def get(self):
return flask.render_template('index.html')

def post(self):
if 'logout' in flask.request.form:
flask.session.pop('username', None)
return flask.redirect(flask.url_for('index'))
required = ['username', 'passwd']
for r in required:
if r not in flask.request.form:
flask.flash("Error: {0} is required.".format(r))
return flask.redirect(flask.url_for('index'))
username = flask.request.form['username']
passwd = flask.request.form['passwd']
if username in users and users[username] == passwd:
flask.session['username'] = username
else:
flask.flash("Username does not exist or incorrect password")
return flask.redirect(flask.url_for('index'))

def login_required(method):
@functools.wraps(method)
def wrapper(*args, **kwargs):
if 'username' in flask.session:
return method(*args, **kwargs)
else:
flask.flash("Login is required")
return flask.redirect(flask.url_for('index'))
return wrapper

class Remote(flask.views.MethodView):
@login_required
def get(self):
return flask.render_template('remote.html')

@login_required
def post(self):
result = eval(flask.request.form['expression'])
flask.flash(result)
return flask.redirect(flask.url_for('remote'))

class Music(flask.views.MethodView):
@login_required
def get(self):
songs = os.listdir('static/music')
return flask.render_template("music.html", songs=songs)


app.add_url_rule('/',
view_func=Main.as_view('index'),
methods=['GET', 'POST'])
app.add_url_rule('/remote',
view_func=Remote.as_view('remote'),
methods=['GET', 'POST'])
app.add_url_rule('/music',
view_func=Music.as_view('music'),
methods=['GET'])

app.debug = True
app.run()
23 changes: 23 additions & 0 deletions my_app/remote.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends "base.html" %}
{% block body %}
<h2>Remote Control</h2>
Enter Python to execute:
<form action="{{url_for('remote')}}" method="post">
<input type="text" name="expression" />
<input type="submit" value="Execute" />
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
Results:
<pre>
{% for message in messages %}
{{ message }}
{% endfor %}
</pre>
{% endif %}
{% endwith %}
{% endblock %}
{% block nav %}
<li><a href="{{url_for('index')}}">Back</a></li>

{% endblock %}
Binary file added my_app/static/.DS_Store
Binary file not shown.
Loading