CS 106A
Stanford University
Chris Gregg
Where have you been?
Where are you going?
When you started this course, you may never have programmed a line of code in your life!
We stared simple: Karel!
Karel is a great robot...but doesn't know much!
But with functions, Karel learns quickly!
# Our first program!
def main():
move()
pick_beeper()
turn_left()
move()
turn_right()
move()
move()
put_beeper()
move()
def turn_right():
turn_left()
turn_left()
turn_left()
Your first non-Karel assignment!
Assignment 3: Ghost and Quilt
Week 3 diagnostic!
Assignment 4: Sand!
Assignment 5: Strings and Word Guessing
Assignment 6: Baby Names
Assignment 7: Bajillion!
CS106A is just the beginning!
Data Structures courses (like CS106B) teach you more programming, but they also teach you about efficiency. We never really cared if your programs were particularly efficient in CS106A, but if they aren't efficient in CS106B, your programs may never finish!
For example, does it matter if we remove elements from the front or the back of a list?
It depends on what you care about -- we often care about how long our programs take to run. So, let's check this question in Python. Take a look at this function:
def remove_from_list(big_list, front):
if front:
for i in range(len(big_list)):
big_list.pop(0) # pop from the front
else:
for i in range(len(big_list)):
big_list.pop() # pop from the back
We either remove from the front, or from the back, depending on the argument.
In Python, we can use the timeit library to time how long a function takes to run. This function times our function from the previous slide for larger and larger numbers, so we can create a graph:
import timeit
def multiple_tests():
print('num_elements,pop_front(s),pop_back(s)')
num_elements = 100
while num_elements < 1000000:
big_list = [x for x in range(num_elements)]
runtime_front = timeit.timeit(lambda: remove_from_list(big_list, True),
number=1)
big_list = [x for x in range(num_elements)]
runtime_back = timeit.timeit(lambda: remove_from_list(big_list, False),
number=1)
print(f"{num_elements},{runtime_front},{runtime_back}")
num_elements *= 2
(the program has a fancy lambda function, which is necessary to time the function using our parameters)
Here is the output when we run the test:
num_elements,pop_front(s),pop_back(s)
100,1.2403999999993642e-05,7.804999999999618e-06
200,1.997599999999794e-05,1.2365000000000292e-05
400,4.2077000000001474e-05,2.5162000000002183e-05
800,9.651799999999683e-05,4.8698000000006736e-05
1600,0.00023973700000000375,9.68699999999989e-05
3200,0.0006860179999999966,0.0002778829999999996
6400,0.0026121519999999995,0.00036841399999999747
12800,0.009626186000000002,0.0006882139999999995
25600,0.043316131,0.001345416000000002
51200,0.189158414,0.0026960799999999896
102400,0.8873830819999999,0.005571295000000198
204800,3.6363927990000002,0.01142525800000005
409600,15.318506804,0.02394107599999984
819200,75.538091239,0.051048672999996825
Let's graph the data in Excel
Here is the graph of the test:
If you remove from the front, the time is much slower! Why? Every time you remove from the front of a list, Python has to move all the other elements backwards! When you remove from the back of a list, no movement is necessary.
So, CS106B involves data structures and efficiency
What else does CS106B teach you?
def count_down(n):
print(n)
if n == 0:
return
else:
count_down(n - 1)
def main():
count_down(10)
if __name__ == "__main__":
main()
10
9
8
7
6
5
4
3
2
1
0
Output:
So, CS106B involves data structures and efficiency
What else does CS106B teach you?
CS106B is more challenging than CS106A, but of course you already know more! What is more challenging about it?
How can you prepare for CS106B?
You are prepared for it! CS106A is a great preparation for CS106B!
However, if you do want to start learning something now, I would suggest taking a look at a C++ tutorial (e.g., here), and learning a bit about C++. Here is an example Python program, and then the same program in C++:
def main():
for i in range(5):
print(f"Python {i}")
if __name__ == "__main__":
main()
#include<iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
cout << "C++ " << i << endl;
}
return 0;
}
Python 0
Python 1
Python 2
Python 3
Python 4
C++ 0
C++ 1
C++ 2
C++ 3
C++ 4
Output:
Here is another example:
// Assumes we are using the Stanford Library
#include<iostream>
#include<string>
#include "vector.h" // Stanford vector, similar to a list in Python
#include "simpio.h"
using namespace std;
int main() {
// create a vector
Vector<string> vec;
while (true) {
string s = getLine("Please enter a name (blank line to end): ");
if (s == "") {
break;
}
vec.add(s);
}
cout << "You entered:" << endl;
for (string s : vec) {
cout << s << endl;
}
return 0;
}
C++ does do things differently! (See the next slide for the equivalent Python program)
Equivalent Python program:
def main():
my_list = []
while True:
s = input("Please enter a name (blank line to end): ")
if s == '':
break
my_list.append(s)
print("You entered:")
for s in my_list:
print(s)
if __name__ == "__main__":
main()
Python programs tend to be shorter than C++ programs. But, there are some similarities:
Some things are different in C++
if
statements and while
statements require the boolean value to be inside of parentheses, e.g., if (x == 4) {
In the end: C++ is a new language and will take some time to learn, but it is a powerful and fast language, and a good one to have in your programming toolbox.
You don't have to take other official courses to learn more computer science!
Things to learn on your own:
You don't have to take other official courses to learn more computer science!
Things to learn on your own:
iOS / Android Programming: learn to program your phone!
Hardware: Raspberry Pi, Arduino, FPGA: Hardware is awesome!
It is a great time to be in computer science!