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
41 changes: 38 additions & 3 deletions chap6.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
# Enter your answrs for chapter 6 here
# Name:
# Name: Sidd Tewari


# Ex. 6.6
# Ex. 6.6 - Palindrome

def first(word):
return word[0]

def last(word):
return word[-1]

# Ex 6.8
def middle(word):
return word[1:-1]

def is_palindrome(word):
if not isinstance(word, str):
return 'Sorry! Palindrome test is only applicable to strings'
elif (len(word) <= 1):
return True
else:
if first(word).lower() != last(word).lower():
return False
return is_palindrome(middle(word))

def tests():
myTestsList = ['Nitttotttin', '123', 123, 'Cat on no tac', 'Cat on the no tac']
for i in range (len(myTestsList)):
print "Is '%s' a palindrome? " % myTestsList[i] , is_palindrome(myTestsList[i])

tests()

# Q.1. - how to differentiate between the string and int 123 in the result print statements?
# Q.2. - how can I pass a list or array as to the tests function? Just DID!!! WOO!!! How to make it better?

# Ex 6.8

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

print gcd (7,21)
25 changes: 25 additions & 0 deletions circleArea.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# practice - Sidd Tewari
#6.3 Composition

import math

def area(radius):
temp = math.pi * radius**2
return temp

def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
dsquared = dx**2 + dy**2
result = math.sqrt(dsquared)
return result

def circleArea(xc,yc,xp,yp):
return area(distance(xc, yc, xp, yp))

# radius = distance(xc,yc,xp,yp)
# result = area(radius)
# print result
# return result

circleArea(1,2,1,6)
20 changes: 20 additions & 0 deletions compareTwo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# practice - Sidd Tewari

#EXERCISE 6.1 Write a compare function that returns 1 if x > y, 0 if x == y, and-1 if x < y.
#compare function

def compareTwo(x,y):
if x > y:
return 1
elif x < y:
return -1
elif x == y:
return 0

a = compareTwo(2,3)
b = compareTwo(3,2)
c = compareTwo(3,3)

print a
print b
print c
49 changes: 49 additions & 0 deletions factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# practice - Sidd Tewari
# Page 56 - Think Python - Factorial function

def factorial(n):
if not isinstance(n, int):
print 'Factorial is only defined for integers'
return None
elif n < 0:
print 'Factorial is not defined for negative integers'
return None
elif n == 0:
return 1
else:
return n * factorial(n-1)

def func_call():
x = factorial(3)
print 'x is ', x
y = factorial(2)
print y
z = factorial(0)
print z
l = factorial(1.5)
#print l
m = factorial('Sidd')
#print m
n = factorial(60-85)
#print n

func_call()

def fun_factorial(n):
space = ' ' * (4 * n)
print space, 'factorial', n
if n == 0:
print space, 'returning 1'
return 1
else:
recurse = fun_factorial(n-1)
result = n * recurse
print space, 'returning', result
return result

def func_call_fun():
print '\n --------------------------------------------------'
fun_factorial(10)

func_call_fun()

17 changes: 17 additions & 0 deletions hypotenuse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# practice # Sidd

# Exercise 6.2. Use incremental development to write a function returns the length
# of the hypotenuse of a right triangle given the lengths of the two legs as arguments.
# Record each stage of the development process as you go.

import math
def hypotenuse(x,y):
# xsquared = x**2
# ysquared = y**2
# sumTotal = xsquared + ysquared
#hypSide = math.sqrt(sumTotal)
hypSide = math.sqrt(x**2 + y**2)
print hypSide
return 0.0

hypotenuse(3,4)
14 changes: 14 additions & 0 deletions incrementalDev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# practice - Sidd Tewari
#6.2 Incremental development

import math

def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
dsquared = dx**2 + dy**2
result = math.sqrt(dsquared)
print result
return result

distance(1, 2, 4, 6)
31 changes: 31 additions & 0 deletions is_between.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# practice
# Ex 6.3 Page 55, Think Python, Sidd Tewari


#Exercise 6.3 Write a function is_between(x, y, z) that returns True
# if x <= y <= z or False otherwise.

# if is_divisible(x, y):
# print 'x is divisible by y'

# def is_between(x,y,z):
# if (x <= y <= z):
# print 'True'
# else:
# print 'False'
# return 0

# is_between(1,2,3)


def is_between(x,y,z):
return (x <= y <= z)

def call_fn():
a = is_between(1,2,3)
print 'a is', a
b = is_between(4,2,8)
print b

call_fn()
#is_between(1,2,3)
Binary file added my_first_app/.DS_Store
Binary file not shown.
79 changes: 79 additions & 0 deletions my_first_app/my_first_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# This is whre you can start you python file for your week1 web app
# Name: Sidd Tewari

import flask, flask.views
import os
import functools

app = flask.Flask(__name__)

app.secret_key = "bacon"

users = {'Sidd':'bacon'} #dictionary

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 doesn't 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("A login is required to see the page!")
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']) #"Whatever you want, OCTO thorp!!" #
flask.flash(result)
return flask.redirect(flask.url_for('remote')) #return self.get() # return the result of the function self.get()

class music(flask.views.MethodView):
"""docstring for music"""
@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.add_url_rule('/hello_Sidd', view_func=View.as_view('main'), methods=['GET', 'POST'])

app.debug = True
app.run()
Binary file added my_first_app/static/.DS_Store
Binary file not shown.
Loading