This is the real test of the week: a new problem, no guided steps, and the hints closed for as long as you can stand. Its 35 minutes assume the week went smoothly; taking an hour from a blank editor is normal. Don't copy the study planner line by line. Reuse its ideas: records for data, a class for rules, parsing that returns null, totals worked out from stored data, and checks on the boundaries.
The brief
Input is one book per line: pages,finished,title, separated by commas. Pages must be a whole number from 1 to 2000. Finished is exactly yes or no. The title is 1 to 40 characters after trimming, and may contain commas. Invalid lines are rejected, and later lines still run.
Report the books in the order they were read, in the form Reading: Dune (412 pages) or Finished: Emma (320 pages). Afterwards, print accepted books, total pages, pages still unread (which counts only unfinished books), and rejected lines. Finally, print the title of the longest accepted book, the first one in a tie, or Longest: none when there are no books.
Acceptance example, for the lines 412,no,Dune, 320,yes,Emma, abc,no,Broken, 3000,no,Too Long and 412,no,War, Peace and Others:
Books: 3Pages: 1144Unread: 824Rejected: 2Longest: Dune
Before you start, make a list of the rules in the brief and decide how you would test each one. Empty input should produce zeros and Longest: none.
Warm up: choosing the separator
Commas in titles are the new difficulty here. Predict how many parts each split gives. Which one is right for this format?
string line = "412,no,War, Peace and Others";
Console.WriteLine(line.Split(',').Length);
Console.WriteLine(line.Split(',', 3).Length);
Console.WriteLine(line.Split(',', 3)[2]);Check your prediction
4
3
War, Peace and OthersBuild it yourself
The reading log
Implement the whole brief in a single file: a Book record, a ReadingLog class with ParseBook and Add, and the report. Add checks for the page boundaries (1, 2000, 0 and 2001), a bad flag, a blank title, and the empty log. Compare your output with the acceptance example, then try lines of your own.
#nullable enable
string? line = Console.ReadLine();
Console.WriteLine(line);Hint 1: the idea
Everything flows from a list of accepted books. The totals and the longest title can each be worked out from it at the end, so they aren't stored anywhere. For an empty log there is no longest book: x?.Title reads Title only when x isn't null and gives null otherwise, and ?? then supplies a fallback.
Hint 2: a first step
Write ParseBook and check it on its own, with no reading of input at all. Start from the valid line, then add one rule at a time.
Show one solution
#nullable enable
int failures = 0;
Check(ReadingLog.ParseBook("1,no,A") is not null, "accepts 1 page");
Check(ReadingLog.ParseBook("2000,no,A") is not null, "accepts 2000 pages");
Check(ReadingLog.ParseBook("0,no,A") is null, "rejects 0 pages");
Check(ReadingLog.ParseBook("2001,no,A") is null, "rejects 2001 pages");
Check(ReadingLog.ParseBook("10,maybe,A") is null, "rejects a bad flag");
Check(ReadingLog.ParseBook("10,no, ") is null, "rejects a blank title");
Check(ReadingLog.ParseBook("10,no") is null, "rejects a missing title");
Check(new ReadingLog().Longest() is null, "an empty log has no longest book");
var log = new ReadingLog();
int rejected = 0;
string? line;
while ((line = Console.ReadLine()) is not null)
{
Book? book = ReadingLog.ParseBook(line);
if (book is null)
{
rejected++;
continue;
}
log.Add(book);
}
foreach (Book book in log.Books)
{
string state = book.Finished ? "Finished" : "Reading";
Console.WriteLine($"{state}: {book.Title} ({book.Pages} pages)");
}
Console.WriteLine($"Books: {log.Books.Count}");
Console.WriteLine($"Pages: {log.TotalPages()}");
Console.WriteLine($"Unread: {log.UnreadPages()}");
Console.WriteLine($"Rejected: {rejected}");
Console.WriteLine($"Longest: {log.Longest()?.Title ?? "none"}");
void Check(bool passed, string name)
{
if (!passed)
{
Console.WriteLine($"FAILED: {name}");
failures++;
}
}
record Book(string Title, int Pages, bool Finished);
class ReadingLog
{
private readonly List<Book> books = new();
public IReadOnlyList<Book> Books => books;
public static Book? ParseBook(string line)
{
string[] parts = line.Split(',', 3);
if (parts.Length != 3 || !int.TryParse(parts[0], out int pages))
{
return null;
}
if (pages < 1 || pages > 2000 || (parts[1] != "yes" && parts[1] != "no"))
{
return null;
}
string title = parts[2].Trim();
if (title.Length < 1 || title.Length > 40)
{
return null;
}
return new Book(title, pages, parts[1] == "yes");
}
public void Add(Book book)
{
books.Add(book);
}
public int TotalPages()
{
int total = 0;
foreach (Book book in books)
{
total += book.Pages;
}
return total;
}
public int UnreadPages()
{
int total = 0;
foreach (Book book in books)
{
if (!book.Finished)
{
total += book.Pages;
}
}
return total;
}
public Book? Longest()
{
Book? longest = null;
foreach (Book book in books)
{
if (longest is null || book.Pages > longest.Pages)
{
longest = book;
}
}
return longest;
}
}Reading: Dune (412 pages)
Finished: Emma (320 pages)
Reading: War, Peace and Others (412 pages)
Books: 3
Pages: 1144
Unread: 824
Rejected: 2
Longest: DuneThe shape is the planner's: a record for the data, a class that owns the list, a static parser that returns null for a bad line, and a loop that counts rejections. The checks sit at the limits (1 and 2000 pages, a blank title, a missing field) before any input is read. Longest returns Book?, because an empty log has no longest book, and the report turns that into none. If your version differs, compare it on those points.
Compare your approach with the solution, not your text. Where did you put each rule? Did the tie go to the first book? Find where the solution uses ?. and ?? together for the empty log.
Completion is saved in this browser only. It does not measure mastery.