Skip to content

Names attribute#436

Merged
colinleach merged 2 commits intoexercism:mainfrom
colinleach:names-attribute
Feb 14, 2026
Merged

Names attribute#436
colinleach merged 2 commits intoexercism:mainfrom
colinleach:names-attribute

Conversation

@colinleach
Copy link
Copy Markdown
Contributor

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.

@colinleach colinleach mentioned this pull request Feb 10, 2026
36 tasks
@depial
Copy link
Copy Markdown
Contributor

depial commented Feb 13, 2026

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.

@colinleach
Copy link
Copy Markdown
Contributor Author

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.

@colinleach colinleach marked this pull request as ready for review February 13, 2026 22:00
@depial
Copy link
Copy Markdown
Contributor

depial commented Feb 14, 2026

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 names based solution to the rather limited practice exercise already uses some of the major points:

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: unname (or equivalent) is necessary to pass the current tests.

@colinleach
Copy link
Copy Markdown
Contributor Author

colinleach commented Feb 14, 2026

For clarification: you're suggesting an expanded exercise based on resistor-color, rather than adding that as a practice exercise alongside some new concept exercise?

Could work, but we need to think about the setails.

@colinleach colinleach merged commit 3038175 into exercism:main Feb 14, 2026
4 checks passed
@depial
Copy link
Copy Markdown
Contributor

depial commented Feb 14, 2026

Yeah, I was imagining something like named-resistor-color which has more and specific testing for named vectors. The current exercise might not even qualify to practice names since a more straightforward solution is with which:

colors <- c("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white")
color_code <- function(color) which(colors == color) - 1

Of 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
Copy link
Copy Markdown
Contributor

@depial depial Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"]

# => 1

If 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Contributor

@depial depial Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@depial
Copy link
Copy Markdown
Contributor

depial commented Feb 15, 2026

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 easy practice exercises in R and there was a lot of latency in the test runner, with it even timing out occasionally. I'm starting to think it was just a problem with my browser, since when I tried from a new tab, the problem seemed to disappear. If you have some time, could you see if you notice anything out of the ordinary?

@colinleach colinleach deleted the names-attribute branch February 18, 2026 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants