Skip to content

Commit

Permalink
edX python lectures 1-4
Browse files Browse the repository at this point in the history
  • Loading branch information
Blair Hasler committed Jul 22, 2018
1 parent 6388478 commit 4875c97
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

116 changes: 116 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}"
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}",
"port": 3000,
"secret": "my_secret",
"host": "localhost"
},
{
"name": "Python: Terminal (integrated)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Terminal (external)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"debugOptions": [
"RedirectOutput",
"Django"
]
},
{
"name": "Python: Flask (0.11.x or later)",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
]
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "module.name"
},
{
"name": "Python: Pyramid",
"type": "python",
"request": "launch",
"args": [
"${workspaceFolder}/development.ini"
],
"debugOptions": [
"RedirectOutput",
"Pyramid"
]
},
{
"name": "Python: Watson",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/console.py",
"args": [
"dev",
"runserver",
"--noreload=True"
]
},
{
"name": "Python: All debug Options",
"type": "python",
"request": "launch",
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"module": "module.name",
"env": {
"VAR1": "1",
"VAR2": "2"
},
"envFile": "${workspaceFolder}/.env",
"args": [
"arg1",
"arg2"
],
"debugOptions": [
"RedirectOutput"
]
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\ProgramData\\Anaconda3\\python.exe"
}
37 changes: 0 additions & 37 deletions Nest/windows.py

This file was deleted.

19 changes: 19 additions & 0 deletions edX_CS_intro_Python/Lecture4/Hanoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 8 12:31:42 2016
@author: ericgrimson
"""

def printMove(fr, to):
print('move from ' + str(fr) + ' to ' + str(to))

def Towers(n, fr, to, spare):
if n == 1:
printMove(fr, to)
else:
Towers(n-1, fr, spare, to)
Towers(1, fr, to, spare)
Towers(n-1, spare, to, fr)

print(Towers(4, 'P1', 'P2', 'P3'))
120 changes: 120 additions & 0 deletions edX_CS_intro_Python/Lecture4/Lecture4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

# iterate
def iterPower(base, exp):
'''
base: int or float.
exp: int >= 0
returns: int or float, base^exp
'''
result = 1
while exp > 0:
result *= base
exp -= 1
return result

iterPower(3,3)


# recursion
def recurPower(base, exp):
'''
base: int or float.
exp: int >= 0
returns: int or float, base^exp
'''
# Your code here
if exp ==0:
return 1
elif exp == 1:
return base
else:
return base * recurPower(base, exp - 1)

recurPower(3,0)


# Greatest common denominator
# iterate
def gcdIter(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
# Your code here
if a > b:
small = b
smaller = b
large = a
else:
small = a
smaller = a
large = b

while smaller > 0:
if large % smaller == 0 and small % smaller == 0:
return smaller
smaller -= 1
return 1

gcdIter(9,12)


# Greatest common denominator
# recursion
def gcdRecur(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
# Your code here
# gcd(a, b) is the same as gcd(b, a % b)
if b == 0:
return a
else:
return gcdRecur(b, a % b)

gcdRecur(3,6)



# Is character in string
# recursion
# doesn't fully work; see actual assignment for correct def
def isIn(char, aStr):
'''
char: a single character
aStr: an alphabetized string
returns: True if char is in aStr; False otherwise
'''
# Your code here
if char == aStr[len(aStr)//2]:
return True
elif len(aStr) == 1:
return False
elif char > aStr[len(aStr)//2]:
aStr = aStr[len(aStr)//2:len(aStr)]
return isIn(char, aStr[len(aStr)//2:len(aStr)])
else:
aStr = aStr[0:len(aStr//2)]
return isIn(char, aStr[0:len(aStr)//2])


isIn('a', 'abc')


################
# practice problem
import math
def polysum (n, s):
area = 0.25 * n * s ** 2 / (math.tan(math.pi / n))
perim = n * s
ps = round(area + perim ** 2, 4)
return ps

polysum(4, 5)

13 changes: 13 additions & 0 deletions edX_CS_intro_Python/Lecture4/fExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 10 12:59:05 2016
@author: ericgrimson
"""

def f( x ):
x = x + 1
print('in f(x): x =', x)
return x
x = 3
z = f( x )
14 changes: 14 additions & 0 deletions edX_CS_intro_Python/Lecture4/fib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 10 13:06:06 2016
@author: ericgrimson
"""

def fib(x):
"""assumes x an int >= 0
returns Fibonacci of x"""
if x == 0 or x == 1:
return 1
else:
return fib(x-1) + fib(x-2)
21 changes: 21 additions & 0 deletions edX_CS_intro_Python/Lecture4/findElement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 10 13:03:44 2016
@author: ericgrimson
"""

def find_elem_recur(e, L):
if L == []:
return False
elif len(L) == 1:
return L[0] == e
else:
half = len(L)//2
if L[half] > e:
return find_elem_recur(e, L[:half])
else:
return find_elem_recur(e, L[half:])


print(find_elem_recur(1, [1, 2, 3, 5, 7, 8, 9, 15]))
18 changes: 18 additions & 0 deletions edX_CS_intro_Python/Lecture4/funcAsArgs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 8 12:25:20 2016
@author: ericgrimson
"""

def func_a():
print('inside func_a')
def func_b(y):
print('inside func_b')
return y
def func_c(z):
print('inside func_c')
return z()
print(func_a())
print(5 + func_b(2))
print(func_c(func_a))
17 changes: 17 additions & 0 deletions edX_CS_intro_Python/Lecture4/gAndHExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 10 13:01:06 2016
@author: ericgrimson
"""

def g(x):
def h():
x = 'abc'
x = x + 1
print('in g(x): x =', x)
h()
return x

x = 3
z = g(x)
Binary file not shown.
Loading

0 comments on commit 4875c97

Please sign in to comment.