Skip to content

Commit a4bf121

Browse files
authored
Merge pull request #727 from KristinaRiemer/prob_decomp
Add code example to problem decomposition lecture
2 parents 338730e + 1723840 commit a4bf121

File tree

1 file changed

+104
-9
lines changed

1 file changed

+104
-9
lines changed

materials/decomposition-R.md

+104-9
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,118 @@ title: Problem Decomposition
55
language: R
66
---
77

8+
### Approaches to solving problems
9+
810
* Real computational tasks are complicated.
911
* To accomplish them effectively you need to **think before you code**.
1012

1113
* Decompose a coding problem:
12-
1. Understand the problem.
13-
* Identify inputs and outputs.
14-
2. Break the problem down into a few pieces.
15-
* Build an outline on paper or as comments.
16-
3. Break those pieces into codeable chunks.
17-
4. Code one chunk at a time.
14+
15+
1. Understand the problem.
16+
* Identify inputs and outputs.
17+
* Input = Portal surveys csv
18+
* Output = plot of yearly abundance for large and small rodents
19+
20+
```
21+
# Overall goal: visualize how many large (>50g) and
22+
# small (<50g) mammals are collected each year
23+
```
24+
25+
2. Break the problem down into a few pieces.
26+
* Bullet list on paper or comments in script.
27+
28+
```
29+
# Subtasks
30+
# Import data
31+
# Get column of small and large categories
32+
# Count number of each per year
33+
# Plot this
34+
```
35+
36+
3. Break those pieces into codeable chunks.
37+
38+
```
39+
# TODO: function that returns size class from weight
40+
```
41+
42+
4. Code one chunk at a time.
43+
44+
### Coding one chunk
45+
46+
* Write function to return size classes
47+
48+
```
49+
get_size_class()
50+
```
51+
52+
* Start with if statement
53+
* Pseudocode
54+
* Write code in English
55+
* “If weight value is bigger than 50, return large; otherwise return small”
56+
57+
```
58+
if(weight > 50){
59+
size_class <- "large"
60+
} else {
61+
size_class <- "small"
62+
}
63+
```
64+
65+
* Test code using values with known output
66+
67+
```
68+
weight <- 10
69+
```
70+
71+
* Turn into function
72+
73+
```
74+
get_size_class <- function(weight){
75+
if(weight > 50){
76+
size_class <- "large"
77+
} else {
78+
size_class <- "small"
79+
}
80+
}
81+
get_size_class(100)
82+
```
83+
84+
* Why doesn’t this work?
85+
86+
```
87+
get_size_class <- function(weight){
88+
if(weight > 50){
89+
size_class <- "large"
90+
} else {
91+
size_class <- "small"
92+
}
93+
return(size_class)
94+
}
95+
```
96+
97+
* Generalize function
98+
* Easier to change values later
99+
* Always test after changing
100+
101+
```
102+
get_size_class <- function(weight, threshold){
103+
if(weight > threshold){
104+
size_class <- "large"
105+
} else {
106+
size_class <- "small"
107+
}
108+
return(size_class)
109+
}
110+
get_size_class(100, 50)
111+
get_size_class(100, 150)
112+
```
113+
114+
* Make one change at a time
18115

19116
* Decompose a code chunk:
20117
1. Think about it.
21118
2. Write it.
22119
3. Test it ( *on it's own* ).
23120
4. Fix any problems.
24121

25-
> Demo [decomposition-example.R]({{ site.baseurl }}/materials/decomposition-example.R).
26-
27-
> Assign remaining exercises.
122+
> Rest of code in [decomposition-example.R]({{ site.baseurl }}/materials/decomposition-example.R).

0 commit comments

Comments
 (0)