Skip to content

Commit 90bede3

Browse files
committed
Lessons 1-2 completed.
0 parents  commit 90bede3

File tree

11 files changed

+492
-0
lines changed

11 files changed

+492
-0
lines changed

1-intro/Basics.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Write a function called "show_excitement" where the string
2+
# "I am super excited for this course!" is returned exactly
3+
# 5 times, where each sentence is separated by a single space.
4+
# Return the string with "return".
5+
# You can only have the string once in your code.
6+
# Don't just copy/paste it 5 times into a single variable!
7+
8+
9+
def show_excitement():
10+
# Your code goes here!
11+
here = ''
12+
for i in range(5):
13+
here += "I am super excited for this course! "
14+
return here[:-1]
15+
print show_excitement()s

1-intro/Efficiency.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""input manatees: a list of "manatees", where one manatee is represented by a dictionary
2+
a single manatee has properties like "name", "age", et cetera
3+
n = the number of elements in "manatees"
4+
m = the number of properties per "manatee" (i.e. the number of keys in a manatee dictionary)"""
5+
6+
def example1(manatees):
7+
for manatee in manatees:
8+
print manatee['name']
9+
10+
#O(n)
11+
12+
def example2(manatees):
13+
print manatees[0]['name']
14+
print manatees[0]['age']
15+
16+
#O(1)
17+
18+
def example3(manatees):
19+
for manatee in manatees:
20+
for manatee_property in manatee:
21+
print manatee_property, ": ", manatee[manatee_property]
22+
23+
#O(n*m)
24+
25+
def example4(manatees):
26+
oldest_manatee = "No manatees here!"
27+
for manatee1 in manatees:
28+
for manatee2 in manatees:
29+
if manatee1['age'] < manatee2['age']:
30+
oldest_manatee = manatee2['name']
31+
else:
32+
oldest_manatee = manatee1['name']
33+
print oldest_manatee
34+
35+
#O(n^2)

1-intro/Practice.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""You can use this class to represent how classy someone
2+
or something is.
3+
"Classy" is interchangable with "fancy".
4+
If you add fancy-looking items, you will increase
5+
your "classiness".
6+
Create a function in "Classy" that takes a string as
7+
input and adds it to the "items" list.
8+
Another method should calculate the "classiness"
9+
value based on the items.
10+
The following items have classiness points associated
11+
with them:
12+
"tophat" = 2
13+
"bowtie" = 4
14+
"monocle" = 5
15+
Everything else has 0 points.
16+
Use the test cases below to guide you!"""
17+
18+
class Classy(object):
19+
def __init__(self):
20+
self.items = []
21+
def addItem(self, item):
22+
self.items.append(item)
23+
def getClassiness(self):
24+
totalClassiness = 0
25+
classyDict = {"tophat":2,"bowtie":4,"monocle":5}
26+
for item in self.items:
27+
try:
28+
totalClassiness += classyDict[item]
29+
except KeyError:
30+
pass
31+
return totalClassiness
32+
33+
# Test cases
34+
me = Classy()
35+
36+
# Should be 0
37+
print me.getClassiness()
38+
39+
me.addItem("tophat")
40+
# Should be 2
41+
print me.getClassiness()
42+
43+
me.addItem("bowtie")
44+
me.addItem("jacket")
45+
me.addItem("monocle")
46+
# Should be 11
47+
print me.getClassiness()
48+
49+
me.addItem("bowtie")
50+
# Should be 15
51+
print me.getClassiness()
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""The LinkedList code from before is provided below.
2+
Add three functions to the LinkedList.
3+
"get_position" returns the element at a certain position.
4+
The "insert" function will add an element to a particular
5+
spot in the list.
6+
"delete" will delete the first element with that
7+
particular value.
8+
Then, use "Test Run" and "Submit" to run the test cases
9+
at the bottom."""
10+
11+
class Element(object):
12+
def __init__(self, value):
13+
self.value = value
14+
self.next = None
15+
16+
class LinkedList(object):
17+
def __init__(self, head=None):
18+
self.head = head
19+
20+
def append(self, new_element):
21+
current = self.head
22+
if self.head:
23+
while current.next:
24+
current = current.next
25+
current.next = new_element
26+
else:
27+
self.head = new_element
28+
29+
def get_position(self, position):
30+
"""Get an element from a particular position.
31+
Assume the first position is "1".
32+
Return "None" if position is not in the list."""
33+
current = self.head
34+
if current == None:
35+
return None
36+
pos = 1
37+
while pos < position:
38+
if current.next == None:
39+
return None
40+
current = current.next
41+
pos += 1
42+
return current
43+
44+
def insert(self, new_element, position):
45+
"""Insert a new node at the given position.
46+
Assume the first position is "1".
47+
Inserting at position 3 means between
48+
the 2nd and 3rd elements."""
49+
current = self.head
50+
pos = 1
51+
while pos < position - 1:
52+
current = current.next
53+
pos += 1
54+
new_element.next = current.next
55+
current.next = new_element
56+
57+
def delete(self, value):
58+
"""Delete the first node with a given value."""
59+
current = self.head
60+
if current.value == value:
61+
if self.head.next:
62+
self.head = self.head.next
63+
else:
64+
self.head = None
65+
if self.head:
66+
while current.next:
67+
if current.next.value == value:
68+
current.next = current.next.next
69+
break
70+
current = current.next
71+
72+
# Test cases
73+
# Set up some Elements
74+
e1 = Element(1)
75+
e2 = Element(2)
76+
e3 = Element(3)
77+
e4 = Element(4)
78+
79+
# Start setting up a LinkedList
80+
ll = LinkedList(e1)
81+
ll.append(e2)
82+
ll.append(e3)
83+
84+
# Test get_position
85+
# Should print 3
86+
print ll.head.next.next.value
87+
# Should also print 3
88+
print ll.get_position(3).value
89+
90+
# Test insert
91+
ll.insert(e4,3)
92+
# Should print 4 now
93+
print ll.get_position(3).value
94+
95+
# Test delete
96+
ll.delete(1)
97+
# Should print 2 now
98+
print ll.get_position(1).value
99+
# Should print 4 now
100+
print ll.get_position(2).value
101+
# Should print 3 now
102+
print ll.get_position(3).value
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Element(object):
2+
def __init__(self, value):
3+
self.value = value
4+
self.next = None
5+
6+
class LinkedList(object):
7+
def __init__(self, head=None):
8+
self.head = head
9+
10+
def append(self, new_element):
11+
current = self.head
12+
if self.head:
13+
while current.next:
14+
current = current.next
15+
current.next = new_element
16+
else:
17+
self.head = new_element
18+
19+
def get_position(self, position):
20+
counter = 1
21+
current = self.head
22+
if position < 1:
23+
return None
24+
while current and counter <= position:
25+
if counter == position:
26+
return current
27+
current = current.next
28+
counter += 1
29+
return None
30+
31+
def insert(self, new_element, position):
32+
counter = 1
33+
current = self.head
34+
if position > 1:
35+
while current and counter < position:
36+
if counter == position - 1:
37+
new_element.next = current.next
38+
current.next = new_element
39+
current = current.next
40+
counter += 1
41+
elif position == 1:
42+
new_element.next = self.head
43+
self.head = new_element
44+
45+
def delete(self, value):
46+
current = self.head
47+
previous = None
48+
while current.value != value and current.next:
49+
previous = current
50+
current = current.next
51+
if current.value == value:
52+
if previous:
53+
previous.next = current.next
54+
else:
55+
self.head = current.next
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Make a Queue class using a list!
2+
Hint: You can use any Python list method
3+
you'd like! Try to write each one in as
4+
few lines as possible.
5+
Make sure you pass the test cases too!"""
6+
7+
class Queue:
8+
def __init__(self, head=None):
9+
self.storage = [head]
10+
11+
def enqueue(self, new_element):
12+
self.storage.append(new_element)
13+
14+
def peek(self):
15+
return self.storage[0]
16+
17+
def dequeue(self):
18+
return self.storage.pop(0)
19+
20+
# Setup
21+
q = Queue(1)
22+
q.enqueue(2)
23+
q.enqueue(3)
24+
25+
# Test peek
26+
# Should be 1
27+
print q.peek()
28+
29+
# Test dequeue
30+
# Should be 1
31+
print q.dequeue()
32+
33+
# Test enqueue
34+
q.enqueue(4)
35+
# Should be 2
36+
print q.dequeue()
37+
# Should be 3
38+
print q.dequeue()
39+
# Should be 4
40+
print q.dequeue()
41+
q.enqueue(5)
42+
# Should be 5
43+
print q.peek()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""Add a couple methods to our LinkedList class,
2+
and use that to implement a Stack.
3+
You have 4 functions below to fill in:
4+
insert_first, delete_first, push, and pop.
5+
Think about this while you're implementing:
6+
why is it easier to add an "insert_first"
7+
function than just use "append"?"""
8+
9+
# insert first function is easier for a linked list based stack
10+
# because if you take out the first object, you only need to change the first,
11+
# whereas taking out the last object requires changing the .next of the previous node.
12+
13+
class Element(object):
14+
def __init__(self, value):
15+
self.value = value
16+
self.next = None
17+
18+
class LinkedList(object):
19+
def __init__(self, head=None):
20+
self.head = head
21+
22+
def append(self, new_element):
23+
current = self.head
24+
if self.head:
25+
while current.next:
26+
current = current.next
27+
current.next = new_element
28+
else:
29+
self.head = new_element
30+
31+
def insert_first(self, new_element):
32+
"Insert new element as the head of the LinkedList"
33+
new_element.next = self.head
34+
self.head = new_element
35+
36+
def delete_first(self):
37+
"Delete the first (head) element in the LinkedList as return it"
38+
first = self.head
39+
if self.head:
40+
self.head = self.head.next
41+
first.next = None
42+
return first
43+
44+
class Stack(object):
45+
def __init__(self,top=None):
46+
self.ll = LinkedList(top)
47+
48+
def push(self, new_element):
49+
"Push (add) a new element onto the top of the stack"
50+
self.ll.insert_first(new_element)
51+
52+
def pop(self):
53+
"Pop (remove) the first element off the top of the stack and return it"
54+
return self.ll.delete_first()
55+
56+
def printme(self):
57+
curr = self.ll.head
58+
print curr.value
59+
while curr.next:
60+
curr = curr.next
61+
print curr.value
62+
63+
# Test cases
64+
# Set up some Elements
65+
e1 = Element(1)
66+
e2 = Element(2)
67+
e3 = Element(3)
68+
e4 = Element(4)
69+
70+
# Start setting up a Stack
71+
stack = Stack(e1)
72+
73+
# Test stack functionality
74+
stack.push(e2)
75+
stack.push(e3)
76+
print stack.pop().value
77+
print stack.pop().value
78+
print stack.pop().value
79+
print stack.pop()
80+
stack.push(e4)
81+
print stack.pop().value

0 commit comments

Comments
 (0)