Guest Book Signatures
Implement a class GuestBook in which every guest signs at most once. Write these methods:
GuestBook()starts with no signatures.sign(name)addsnameto the end of the book and returnstrue. Ifnameis already in the book, nothing changes and it returnsfalse.position_of(name)returns wherenamesigned, counting from1for the first signature, or-1ifnamehas not signed.count_guests()returns how many names are in the book.get_latest_guest()returns the name that was added last, or""if the book is empty.
Names are compared exactly, so "Ana" and "ana" are different guests. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
GuestBook()
sign("Ana") returns true
sign("Ben") returns true
sign("Ana") returns false
position_of("Ben") returns 2
position_of("Cy") returns -1
count_guests() returns 2
get_latest_guest() returns "Ben"
Ana signs only once, so the second sign is refused. Ben signed second, Cy never signed, and the last name added is Ben.
Constraints
namehas1to8letters- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Classes problems
- Odometer With a Trip MeterBeginner
- Panel of Light SwitchesBeginner
- Rectangle on the TableBeginner
- Scoreboard by RoundsBeginner
- Stack With a Size LimitBeginner
- Take a NumberBeginner
- Tally CounterBeginner
- Thermostat Between Two LimitsBeginner