Skip to content

Add grade-school exercise #199

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

Open
atk opened this issue Apr 11, 2025 · 8 comments
Open

Add grade-school exercise #199

atk opened this issue Apr 11, 2025 · 8 comments

Comments

@atk
Copy link
Contributor

atk commented Apr 11, 2025

Again the output format is a conundrum. While roster() is best expressed as JSON string, we would usually use a string with a line-break after each name for grade(number). I wouldn't want to mix and match, so I see two ways to go about this.

  1. Go JSON all the way
  2. Go formatted strings all the way

JSON would be obviously the exact same output that is expected from JS. A formatted string would introduce the next grade as a line containing only a number and a line-break, followed by all names ended with line-breaks, e.g.

3
Chelsea
7
Logan

Please share your opinion about which way to go.

@keiravillekode
Copy link
Contributor

Here is another possible approach:

Copy (func $initialize) from resistor-color.wat

The next two functions are adapted from C example.h

addStudent(name offset, name length, grade)

getGrade(grade)
returns (offset, length) with a newline appearing after each student's name

@atk
Copy link
Contributor Author

atk commented Apr 11, 2025

The C version keeps the current roster as input to the function and returns the changed roster. This would mean we would be forced to define the internal data format for the users, which is something that I really dislike, because it reduces the chances to be creative. In this case, you could have an u32 value for each student: [u16 offset, u8 length, u8 grade], similar to what I did for the tournament exercise.

Also, most other assembly dialects are being called from C while WASM is usually called from JS. So it makes a lot more sense to look at JS than looking at C for the output.

@keiravillekode
Copy link
Contributor

My suggestion omits the roster_t definition and argument, so the internal data format is unspecified, and there is only one roster at a time. bowling.wat takes that same approach.

Test cases would be something like:

{
// "Cannot add same student to multiple grades in the roster"

// $initialize()

addStudent("Blair", 2) expect true
addStudent("James", 2) expect true
addStudent("James", 3) expect false
addStudent("Paul", 3) expect true
}
{
// "Student not added to multiple grades"

// $initialize()

addStudent("Blair", 2) 
addStudent("James", 2)
addStudent("James", 3)
addStudent("Paul", 3)

getGrade(2) expect "Blair\nJames\n"
}

Perhaps instead of $initialize() it should be reset(), or it could be omitted like in bowling.wat

@atk
Copy link
Contributor Author

atk commented Apr 12, 2025

But there is no way to get the full roster then, so one of the asks of the introductions would have to be removed, which I would argue against.

@keiravillekode
Copy link
Contributor

The canonical roster is simply a list of names, sorted by (grade, name)

@atk
Copy link
Contributor Author

atk commented Apr 13, 2025

Then we could use the same format as the grade function. However, I haven't seen a language actually using that output. It is also pretty ambiguous.

I would probably prefer the solution where the numbers of the grades are within their own lines, since it's simple enough to distinguish those from the names.

@atk
Copy link
Contributor Author

atk commented Apr 13, 2025

Then let's keep that format and take an string of line-break-terminated names for both.

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

No branches or pull requests

2 participants