Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs220-lecture-material
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CDIS
Computer Sciences
Courses
CS220
cs220-lecture-material
Commits
360d7777
Commit
360d7777
authored
1 year ago
by
Anna Meyer
Browse files
Options
Downloads
Patches
Plain Diff
review
parent
ab25a19e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
sum23/lecture_materials/07_Strings/Exam_1_review.ipynb
+43
-0
43 additions, 0 deletions
sum23/lecture_materials/07_Strings/Exam_1_review.ipynb
with
43 additions
and
0 deletions
sum23/lecture_materials/07_Strings/Exam_1_review.ipynb
+
43
−
0
View file @
360d7777
...
@@ -274,6 +274,49 @@
...
@@ -274,6 +274,49 @@
"# Discuss: given two possible versions of a function (where one is correct),\n",
"# Discuss: given two possible versions of a function (where one is correct),\n",
"# what is the general strategy for figuring out which one is correct?"
"# what is the general strategy for figuring out which one is correct?"
]
]
},
{
"cell_type": "markdown",
"id": "d1ba721c",
"metadata": {},
"source": [
"## Iteration"
]
},
{
"cell_type": "markdown",
"id": "851c3e95",
"metadata": {},
"source": [
"### Example 1 (Reimann sums)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85e203c9",
"metadata": {},
"outputs": [],
"source": [
"# Let's try the values from 1 to 5\n",
"start_x = 1\n",
"end_x = 5\n",
"total_area = 0\n",
"current_x = start_x\n",
"# Try out increasing values of width, make sure to comment the other width values\n",
"# delta_x = 1\n",
"delta_x = 0.1\n",
"# delta_x = 0.01\n",
"# delta_x = 0.001\n",
"\n",
"while current_x <= end_x:\n",
" y = ??? # TODO: use f(x) defined previously\n",
" rect_area = ???\n",
" total_area += ???\n",
" current_x += delta_x\n",
" \n",
"print(\"Area found using approximation is:\", total_area)"
]
}
}
],
],
"metadata": {
"metadata": {
...
...
%% Cell type:markdown id:482016d5 tags:
%% Cell type:markdown id:482016d5 tags:
# Exam 1 review
# Exam 1 review
%% Cell type:markdown id:96330469 tags:
%% Cell type:markdown id:96330469 tags:
## Arguments
## Arguments
%% Cell type:code id:138a92cc tags:
%% Cell type:code id:138a92cc tags:
```
python
```
python
def
person
(
name
,
age
,
color
=
'
blue
'
,
animal
=
'
cat
'
):
def
person
(
name
,
age
,
color
=
'
blue
'
,
animal
=
'
cat
'
):
print
(
name
,
"
is
"
,
age
,
"
years old
"
,
end
=
""
)
print
(
name
,
"
is
"
,
age
,
"
years old
"
,
end
=
""
)
print
(
"
their favorite color is
"
,
color
,
end
=
""
)
print
(
"
their favorite color is
"
,
color
,
end
=
""
)
print
(
"
and their favorite animal is
"
,
animal
)
print
(
"
and their favorite animal is
"
,
animal
)
```
```
%% Cell type:code id:a5f2edbc tags:
%% Cell type:code id:a5f2edbc tags:
```
python
```
python
# TODO: what are all the ways we can (or cannot) call person()?
# TODO: what are all the ways we can (or cannot) call person()?
```
```
%% Cell type:markdown id:6f623b4b tags:
%% Cell type:markdown id:6f623b4b tags:
## Function Scope
## Function Scope
%% Cell type:markdown id:e54320ef tags:
%% Cell type:markdown id:e54320ef tags:
### Example 1 -- basic function scope
### Example 1 -- basic function scope
%% Cell type:code id:7e4d570a tags:
%% Cell type:code id:7e4d570a tags:
```
python
```
python
pi
=
3.14
pi
=
3.14
alpha
=
4
alpha
=
4
def
my_func
(
alpha
,
beta
):
def
my_func
(
alpha
,
beta
):
# TODO: what variables are accessible at line 5? what values do they have?
# TODO: what variables are accessible at line 5? what values do they have?
delta
=
alpha
+
beta
delta
=
alpha
+
beta
gamma
=
delta
**
2
gamma
=
delta
**
2
return
gamma
return
gamma
answer
=
my_func
(
alpha
,
pi
)
answer
=
my_func
(
alpha
,
pi
)
answer2
=
my_func
(
answer
,
alpha
)
answer2
=
my_func
(
answer
,
alpha
)
# TODO what variables are accessible here? What values do they have?
# TODO what variables are accessible here? What values do they have?
```
```
%% Cell type:markdown id:33a302bf tags:
%% Cell type:markdown id:33a302bf tags:
### Example 2 -- global vs. local
### Example 2 -- global vs. local
%% Cell type:code id:7a5ed848 tags:
%% Cell type:code id:7a5ed848 tags:
```
python
```
python
two
=
3.14
two
=
3.14
alpha
=
4
alpha
=
4
def
my_func1
(
alpha
,
beta
):
def
my_func1
(
alpha
,
beta
):
# TODO: what variables are accessible at line 5? what values do they have?
# TODO: what variables are accessible at line 5? what values do they have?
delta
=
alpha
+
beta
delta
=
alpha
+
beta
gamma
=
delta
*
two
gamma
=
delta
*
two
return
gamma
return
gamma
answer
=
my_func
(
alpha
,
two
)
answer
=
my_func
(
alpha
,
two
)
# TODO what variables are accessible here? What values do they have?
# TODO what variables are accessible here? What values do they have?
```
```
%% Cell type:code id:04428811 tags:
%% Cell type:code id:04428811 tags:
```
python
```
python
two
=
2
two
=
2
alpha
=
14
alpha
=
14
def
my_func2
(
alpha
,
beta
):
def
my_func2
(
alpha
,
beta
):
# TODO: what variables are accessible at line 5? what values do they have?
# TODO: what variables are accessible at line 5? what values do they have?
delta
=
alpha
+
beta
delta
=
alpha
+
beta
two
=
7
two
=
7
gamma
=
delta
*
two
gamma
=
delta
*
two
return
gamma
return
gamma
answer
=
my_func
(
alpha
,
two
)
answer
=
my_func
(
alpha
,
two
)
# TODO what variables are accessible here? What values do they have?
# TODO what variables are accessible here? What values do they have?
```
```
%% Cell type:code id:51696c45 tags:
%% Cell type:code id:51696c45 tags:
```
python
```
python
two
=
2
two
=
2
alpha
=
14
alpha
=
14
def
my_func3
(
alpha
,
beta
):
def
my_func3
(
alpha
,
beta
):
# TODO: what variables are accessible at line 5? what values do they have?
# TODO: what variables are accessible at line 5? what values do they have?
delta
=
alpha
+
beta
delta
=
alpha
+
beta
gamma
=
delta
*
two
gamma
=
delta
*
two
two
=
7
two
=
7
return
gamma
return
gamma
answer
=
my_func
(
alpha
,
two
)
answer
=
my_func
(
alpha
,
two
)
# TODO what variables are accessible here? What values do they have?
# TODO what variables are accessible here? What values do they have?
```
```
%% Cell type:code id:6cdb7a02 tags:
%% Cell type:code id:6cdb7a02 tags:
```
python
```
python
two
=
2
two
=
2
alpha
=
14
alpha
=
14
def
my_func3
(
alpha
,
beta
):
def
my_func3
(
alpha
,
beta
):
# TODO: what variables are accessible at line 5? what values do they have?
# TODO: what variables are accessible at line 5? what values do they have?
delta
=
alpha
+
beta
delta
=
alpha
+
beta
global
two
global
two
gamma
=
delta
*
two
gamma
=
delta
*
two
two
=
7
two
=
7
return
gamma
return
gamma
answer
=
my_func
(
alpha
,
two
)
answer
=
my_func
(
alpha
,
two
)
# TODO what variables are accessible here? What values do they have?
# TODO what variables are accessible here? What values do they have?
```
```
%% Cell type:markdown id:2ebbfa8b tags:
%% Cell type:markdown id:2ebbfa8b tags:
## Refactoring
## Refactoring
%% Cell type:markdown id:c61c0d46 tags:
%% Cell type:markdown id:c61c0d46 tags:
### Example 1
### Example 1
%% Cell type:code id:7e25ba85 tags:
%% Cell type:code id:7e25ba85 tags:
```
python
```
python
def
some_func
():
def
some_func
():
if
some_cond1
:
if
some_cond1
:
if
some_cond2
:
if
some_cond2
:
if
some_cond3
:
if
some_cond3
:
return
True
return
True
return
False
return
False
```
```
%% Cell type:code id:d6dc250a tags:
%% Cell type:code id:d6dc250a tags:
```
python
```
python
# TODO how can we rewrite some_func() to only use 1 line of code?
# TODO how can we rewrite some_func() to only use 1 line of code?
def
some_func_short
():
def
some_func_short
():
pass
pass
```
```
%% Cell type:markdown id:b9da2d00 tags:
%% Cell type:markdown id:b9da2d00 tags:
### Example 2
### Example 2
%% Cell type:code id:678f8ec2 tags:
%% Cell type:code id:678f8ec2 tags:
```
python
```
python
def
some_func_1
(
x
,
y
,
z
):
def
some_func_1
(
x
,
y
,
z
):
if
x
>=
7
:
if
x
>=
7
:
return
True
return
True
if
y
<
18
:
if
y
<
18
:
return
True
return
True
if
z
==
32
:
if
z
==
32
:
return
True
return
True
else
:
else
:
return
False
return
False
```
```
%% Cell type:code id:d1bf3938 tags:
%% Cell type:code id:d1bf3938 tags:
```
python
```
python
# TODO which of these refactoring options is correct?
# TODO which of these refactoring options is correct?
def
some_func_1a
(
x
,
y
,
z
):
def
some_func_1a
(
x
,
y
,
z
):
return
x
>=
7
or
y
<
18
or
z
==
32
return
x
>=
7
or
y
<
18
or
z
==
32
def
some_func_1b
(
x
,
y
,
z
):
def
some_func_1b
(
x
,
y
,
z
):
return
x
>=
7
and
y
<
18
and
z
==
32
return
x
>=
7
and
y
<
18
and
z
==
32
```
```
%% Cell type:code id:063829c0 tags:
%% Cell type:code id:063829c0 tags:
```
python
```
python
# TODO
# TODO
# Discuss: given two possible versions of a function (where one is correct),
# Discuss: given two possible versions of a function (where one is correct),
# what is the general strategy for figuring out which one is correct?
# what is the general strategy for figuring out which one is correct?
```
```
%% Cell type:markdown id:d1ba721c tags:
## Iteration
%% Cell type:markdown id:851c3e95 tags:
### Example 1 (Reimann sums)
%% Cell type:code id:85e203c9 tags:
```
python
# Let's try the values from 1 to 5
start_x
=
1
end_x
=
5
total_area
=
0
current_x
=
start_x
# Try out increasing values of width, make sure to comment the other width values
# delta_x = 1
delta_x
=
0.1
# delta_x = 0.01
# delta_x = 0.001
while
current_x
<=
end_x
:
y
=
???
# TODO: use f(x) defined previously
rect_area
=
???
total_area
+=
???
current_x
+=
delta_x
print
(
"
Area found using approximation is:
"
,
total_area
)
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment