Conversation
|
I had a look through what is here and I couldn't find anything to comment on in its present form. I would suspect a fairly simple exercise could be created for this. |
I find your optimism encouraging! I'll go ahead and finish Need for Speed to pair with Lists, assuming Names can be filled in later. We're going to have a few disconnected concepts on the syllabus tree temporarily, but I'm not too worried at this early stage. |
|
I was just going through some of the current practice exercises and resistor-color seems like it could be a good candidate to work with this concept if we expanded it. My colors <- c("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white")
bands <- 0:9
names(bands) <- colors
color_code <- function(color) {
unname(bands[color])
}Note: |
|
For clarification: you're suggesting an expanded exercise based on Could work, but we need to think about the setails. |
|
Yeah, I was imagining something like colors <- c("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white")
color_code <- function(color) which(colors == color) - 1Of course, I'm unsure if this is beating a dead horse, because there are already a few resistor themed exercises, but I thought it had a good amount of potential. I could try to throw together an example if you think it's an okay idea. |
| [1] 365 # no meaningful names possible | ||
| ``` | ||
|
|
||
| ## More advanced topics |
There was a problem hiding this comment.
Just a thought, now that I've thought more about it... It could be a useful "advanced topic" to talk about performance. I'm not sure if what I found is accurate, but it looks like indexing by name is a linear operation (rather than being stored as something akin to a hash map).
There was a problem hiding this comment.
I'm fairly ignorant about R performance details, but this sounds at least plausible. Worth looking into, and mentioning if we can find a good link.
There was a problem hiding this comment.
I'm not sure about a solid link (as my searching might be failing me again), but I'm feeling more confident that it is a linear search to match, which would explain why having identical names will return only the first instance (and indeed why identical names are even possible):
colors <- c("black", "brown", "black")
bands <- 1:3
names(bands) <- colors
bands["black"]
# => 1If you're feeling adventurous and read C, here's the relevant source code, or a StackOverflow conversation which mentions the source code (in EDIT2 of the top answer)
In the end, names are just an attribute, so I don't think it changes the underlying data structure.
There was a problem hiding this comment.
If you're feeling adventurous and read C
Not today. I seem to have eaten (or drunk) something which has left ... side effects. Better days will come!
There was a problem hiding this comment.
I hope you feel better soon!
Before I write the whole thing, here's a quick idea of what I'm thinking of for an exemplar:
exemplar.R for named-resistor-colors
colors <- c("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white")
resistor_values <- 0:9
names(resistor_values) <- colors
value <- function(resistor_color) {
unname(resistor_values[resistor_color])
}
values <- function(resistor_colors) {
sum(c(10, 1) * resistor_values[resistor_colors])
}
ohms <- function(resistor_colors) {
values(resistor_colors[1:2]) * 10^value(resistor_colors[3])
}Admittedly, this is fairly unimaginative, since it effectively combines the three resistor-colors exercises, so I'm not sure how you feel about it. I would then link the original resistor-colors series as practicing other concepts, like vector-filtering, so this would be like an enforced approach.
The above exemplar.R maps to four tasks.
There was a problem hiding this comment.
Though I'm instinctively nervous about muddling concept exercises with practice exercises, I think this could be made to work if we emphasize the particular implementation. I certainly don't have any better ideas!
For example, we could test if names(resistor_values) has length 10, and order() returns the correct vector of indices, without providing a solution in the tests.
Sounds good. I'll try to get to opening a PR with it soon then. As an aside: I was doing some |
Another one I'll park here as draft, because I'm not sure what to do with it. It's tentatively a prereq for Lists, but if necessary we could add it in at the top of that concept. Though it could then get a bit long.
I'll try to PR Lists later today, so it's clearer how they relate. It's setting the scene for dataframes (and Tibbles), where column names get really important for navigation.