Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort exercises by slug before emitting template #2227

Merged
merged 2 commits into from Mar 18, 2024

Conversation

csaltachin
Copy link
Contributor

Should fix #2149.

Note that comparing slugs lexicographically doesn't work because it yields "10" < "2", etc. Here we parse each slug into its numeric part (defaulting to 0) and its rightmost letter (if present) before comparing them as int * string tuples.
Maybe the letter could be instead parsed as a char option or directly as an int, but I hope it's not too convoluted overall.

@csaltachin csaltachin marked this pull request as ready for review March 16, 2024 04:41
Comment on lines 79 to 86
let slug = exercise.slug in
let len = String.length slug in
if len = 0 then (0, "")
else
match slug.[len - 1] with
| 'A' .. 'Z' as c ->
(parse_int (String.sub slug 0 (len - 1)), String.make 1 c)
| _ -> (parse_int slug, "")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let slug = exercise.slug in
let len = String.length slug in
if len = 0 then (0, "")
else
match slug.[len - 1] with
| 'A' .. 'Z' as c ->
(parse_int (String.sub slug 0 (len - 1)), String.make 1 c)
| _ -> (parse_int slug, "")
Scanf.sscanf exercise.slug "%d%[A-Z]" (fun s c -> (s, c))

What do you think of something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wow, I didn't know sscanf was so versatile -- thanks for showing me. I'll change this and remove parse_int.
This could throw at build time if sscanf fails, but I don't imagine the slug format will change drastically in the future. We can always just update the format string + receiver function if so.

Copy link
Collaborator

@cuihtlauac cuihtlauac left a comment

Choose a reason for hiding this comment

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

Thanks @csaltachin, this looks good to me

@cuihtlauac cuihtlauac merged commit c5cd69a into ocaml:main Mar 18, 2024
1 check passed
@csaltachin csaltachin deleted the sort-exercises branch April 19, 2024 10:35
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.

Exercises are not listed in order
2 participants