Skip to content

Grade School - Adding Same Student Twice #916

Closed
@czrpb

Description

@czrpb

Hi!

I just did Grade School and my add ended up like 20lines long, with a filter and reduce! 🤯

I reviewed the community solutions and I noticed that pretty much none of them would handle adding the same student to the same grade twice, ie: test #11: Most dont remove dups in the grade roster, they just blindly add.

This caused me to poke into this test further and I found that the current "example" solution:

def add(db, name, grade) do
if name in List.flatten(Map.values(db)) do
db
else
Map.update(db, grade, [name], &[name | &1])
end
end

is deficient when the order of adds in this test is reverse (and why my attempt is so long!):

test "a student cannot be added to more than one grade" do
actual =
@db
|> School.add("Aimee", 2)
|> School.add("Aimee", 1)
assert actual == %{2 => ["Aimee"]}
end

I'd like to:

  1. duplicate this test but with the add order reversed
  2. update the "example", though maybe after someone reviews my solution! there has got to be a better one?!? 😁

Should I continue this?

thx!! << q

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions