theme | _class | paginate | backgroundColor | footer | marp |
---|---|---|---|---|---|
gaia |
lead |
true |
Computational Thinking and Social Science | :copyright: Matti Nelimarkka | 2023 | Sage Publishing |
true |
- Identify the differences among variables, lists, and dictionaries
- Be able to add items to lists and dictionaries, and remove them
- Use for statements with lists and dictionaries
- Simplify problem solving using listsand dictionaries as abstractions
- Examine the computational complexity of an algorithm
- Evaluate whether solving a given problem computationally is feasible through back-of-envelope calculations.
Lists allow storing several values in one variable.
Each value is stored in a pre-defined index.
Lists can
- add new values to it
- remove old values from it
- recall or change values on it
- go through all values stored on it
Lists are indexed.
names_list <- list( 'Cat', 'Dog', 3.1415, 'Pie')
for( name in names_list ){
print( paste( "Say hello to", name, sep = ' ' ) )
print( paste( name, "says: Hi.", sep = ' ' ) )
}
List help with
- conducting the same operation to various variables
- cleaning and making the code more readable
- storing unknown number of variables
- working on data which is indexed
Dictionaries store key, value pairs. It is easy to access the value by a given key.
Dictionaries can
- add new values to it
- remove old values from it
- recall or change values on it
- go through all values stored on it
Dictionaries have keys and corresponding values.
postcodes <- list(
'14853' = 'Ithaca',
'94305' = 'Palo Alto',
'27708' = 'Durham'
)
for( code in names( postcodes ) ) {
city <- postcodes[[ code ]]
print( paste( 'ZIP code', code, 'refers to', city ) )
}
Remember to manage cases where a key does not have a corresponding value.
- maintaining a static mapping between two related ideas
- serving as a meta variable, such as gatherer when there are several different cases
- Computing resources are limited: there are time and memory constrains that impact how we solve a problem.
- As the number of items in the list grows, it takes more and more time to go through the entire list through.
-
$\mathcal{O}$ measures the order of growth, algorithm’s resource requirements in relation to input size (often$n$ ). - Analysis is simplified to the highest polynomial only, as it is the only significant factor when
$n$ is high.
- Code optimisations: choosing better data structures, examining if approaches can be improved
- Profiling code: examining where code bottlenecks are and improving them specially
- What operations does a list involve?
- What operations does a dictionary perform and allow?
- What are the similarities and differences between lists and dictionaries?
- In what circumstances do you need dynamic data structures?
- In what kinds of cases would you use these data structures for working with networks?
- In which types of cases would you use them for analysing data algorithmically?
- In what cases should you pay attention to the algorithmic complexity of code?
- List the following levels of algorithmic complexity in order from lowest to highest:
$\mathcal{O}(n2)$ ,$\mathcal{O}(n)$ ,$\mathcal{O}(2n)$ , $$\mathcal{O}(1)$. - Go through five pieces of example code and estimate their algorithmic complexity.
- Write pseudocode that has
$\mathcal{O}(n)$ complexity and a piece of pseudocode that has$\mathcal{O}(n3)$ .