Comp 112
Your future in computer science
and programming
●
●
Programming: the craft of creating software
Computer science: the scientific study of
computation
Web
●
Communication between the client (web
browser) and server uses language HTTP
GET /index.html HTTP/1.1
Host: www.example.com
●
The content of the web page itself is expressed
in language HTML
<html><body>
<h1>Simple web app</h1>
Hello, world!
</body></html>
A very simple web app
import web
urls = (
'/', 'index'
)
class index:
def GET(self):
return """<html><body>
<h1>Simple web app</h1>
Hello, world!</body></html>"""
app = web.application(urls, globals())
app.run()
A very simple web app
import web
urls = (
'/', 'index' The URL's document name and
the class to handle it
)
class index:
This is called when a browser
def GET(self):
requests the given page
return """<html><body>
<h1>Simple web app</h1>
Hello, world!</body></html>"""
app = web.application(urls, globals())
app.run()
A more interesting web app
Let's build a blog site
We want to be able to create, view, delete, and
edit blog entries
Web app: URLs
import web
### Url mappings
handles URLs like this:
http://www.myblog.com/new
using class 'New'
urls = (
'/', 'Index',
'/new', 'New',
'/view/(\d+)', 'View',
'/delete/(\d+)', 'Delete',
'/edit/(\d+)', 'Edit',
)
handles URLs like this:
http://www.myblog.com/edit/1234
using class 'Edit'
Web app: New blog entry
import model
use this module for database access
class New:
form = web.form.Form(
web.form.Textbox('title', web.form.notnull,
size=30,
description="Title:"),
web.form.Textarea('content', web.form.notnull,
rows=30, cols=80,
description="Content:"),
web.form.Button('Submit Post'),
Describes web form:
)
text entry areas, and button
def GET(self):
form = self.form()
return render.new(form)
Sends data from user to
def POST(self):
database
form = self.form()
if not form.validates():
return render.new(form)
model.new_post(form.d.title, form.d.content)
raise web.seeother('/')
def reverse(a):
acc = []
for item in a:
acc.insert(0, item)
return acc
nums = [10,20,30,40]
result = reverse(nums)
i=0
while i<len(result):
print(result[i])
i+=1
Python
function reverse(a)
{
var acc = [];
for (var i=0; i<a.length; i+=1)
{
var item = a[i];
acc.splice(0, 0, item);
}
return acc;
}
var nums = [10,20,30,40];
var result = reverse(nums);
var i=0;
while (i < result.length)
{
console.log(result[i]);
i+=1;
}
JavaScript
class Example
def reverse(a):
{
acc = []
static List<Integer> reverse(List<Integer> a)
for i in a:
{
ArrayList<Integer> acc = new ArrayList<>();
acc.insert(0, i)
for (int i : a)
return acc
{
acc.add(0, i);
nums = [10,20,30,40]
}
result = reverse(nums)
return acc;
for num in nums:
}
print(num)
public static void main (String[] args)
{
List<Integer> nums = Arrays.asList(10,20,30,40);
List<Integer> result = reverse(nums);
for (int i : result)
{
System.out.println(i);
}
}
}
Python
Java
def reverse(a):
if a == []:
return []
else:
x = a[0]
xs = a[1:]
return reverse(xs) + [x]
nums = [10,20,30,40]
result = reverse(nums)
for num in nums:
print(num)
Python
import Data.Foldable (for_)
reverse [] = []
reverse (x:xs) = reverse xs ++ [x]
main =
let nums = [10,20,30,40]
result = reverse nums
in for_ result print
Haskell
Searching
names = ['Anita', 'Annice',
'Archie', 'Brittany', 'Cecil',
'Cordell', 'Crystal', 'Debora',
'Eliza', 'Emanuel', 'Eneida',
'Garry', 'Herbert', 'Jeannetta',
'Jerilyn', 'Kelly', 'Larry',
'Louise', 'Savanna', 'Ward']
Linear search
# An obvious solution
def linear_search(names, needle):
for name in names:
if needle == name:
return "Found!"
return "Not found!"
# Internally, this does the same thing
def linear_search(names, needle):
if needle in names:
return "Found!"
else:
return "Not found!"
Linear search
names = ['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil', ...]
search(names, "Zeno")
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil', ...]
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil', ...]
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil', ...]
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil', ...]
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil', ...]
Binary search
We're searching for Eliza. We don't know if it's in the list. We do
know the names are sorted.
Initially, all names are possible
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil',
'Cordell', 'Crystal', 'Debora', 'Eliza', 'Emanuel',
'Eneida', 'Garry', 'Herbert', 'Jeannetta',
'Jerilyn', 'Kelly', 'Larry', 'Louise', 'Savanna',
'Ward']
Binary search
There are 20 names in the list, so the 10th item is the middle.
That item is Eneida which is greater than the name we're looking for,
Eliza. Therefore, if Eliza is in the list, it must be before the 10th item.
We no longer consider items after that. Items before Eneida might
contain Debora.
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil',
'Cordell', 'Crystal', 'Debora', 'Eliza', 'Emanuel',
'Eneida', 'Garry', 'Herbert', 'Jeannetta',
'Jerilyn', 'Kelly', 'Larry', 'Louise', 'Savanna',
'Ward']
Binary search
There are now 11 items remaining, so we will again look in the
middle, and we find Cordell. Cordell is less than the name we want,
so we only consider names after that.
Based on our two tests so far, we know that Eliza, if it's there, must
be between Cordell and Eneida.
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil',
'Cordell', 'Crystal', 'Debora', 'Eliza', 'Emanuel',
'Eneida', 'Garry', 'Herbert', 'Jeannetta',
'Jerilyn', 'Kelly', 'Larry', 'Louise', 'Savanna',
'Ward']
Binary search
We can subdivide the search space.
Of the four items remaining, Eliza is in the middle. Our search is
complete.
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil',
'Cordell', 'Crystal', 'Debora', 'Eliza', 'Emanuel',
'Eneida', 'Garry', 'Herbert', 'Jeannetta',
'Jerilyn', 'Kelly', 'Larry', 'Louise', 'Savanna',
'Ward']
Binary search
names = ['Anita', 'Annice', 'Archie', 'Brittany','Cecil', ...]
search(names, "Bob")
['Anita', ..., 'Emanuel', 'Eneida', 'Garry',..., 'Savanna',
'Ward']
['Anita', ..., 'Cecil', 'Cordell', 'Crystal', ..., 'Savanna',
'Ward']
['Anita', 'Annice', 'Archie', 'Brittany', 'Cecil', 'Cordell',
'Crystal', 'Debora', 'Eliza', 'Emanuel', 'Eneida',
'Garry', ...., 'Savanna', 'Ward']
['Anita', 'Annice', 'Archie', 'Brittany',...., 'Ward']
'Archie' is the greatest item in the list, but is less than the
desired item, so 'Bob' isn't there.
Binary search
def binary_search(names, needle):
lower = 0
upper = len(names)
while lower < upper:
x = lower + (upper - lower) // 2
val = names[x]
if needle == val:
return "Found!"
elif needle > val:
if lower == x:
break
lower = x
else:
upper = x
return "Not found!"
Binary search vs linear search
●
●
●
Linear
–
Searching for a not-present items in a list of 20 items
took 20 steps
–
O(n)
Binary
–
Searching for a not-present item in a list of 20 items
took 4 steps
–
O(log n)
What if there were a million items?
Data structures
●
●
You've learned about list and dict
There are lots of other data structures for
different kinds of data, that can be used with
different kinds of trade-offs
Data structure - tree
Data structure - graph
CompSci courses you might take
●
Algorithms and complexity
●
Design of programming languages
●
Networking
●
Distributed systems
●
Computer structure
●
Quantum computing
●
Artificial intelligence
© Copyright 2026 Paperzz