Skip to content

Instantly share code, notes, and snippets.

@deanebarker
Last active October 13, 2022 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deanebarker/5641341476939e76516457b399c106df to your computer and use it in GitHub Desktop.
Save deanebarker/5641341476939e76516457b399c106df to your computer and use it in GitHub Desktop.
Code to process the NY Times Bestseller List
void Main()
{
// This code depends on the FlatFiles Nuget package: https://github.com/jehugaleahsa/FlatFiles
// Download the dataset from here: https://view.data.post45.org/nytfull
var pathToFile = @"[path to file]";
var mapper = DelimitedTypeMapper.Define<BookRanking>();
mapper.Property(c => c.Year).ColumnName("year");
mapper.Property(c => c.Week).ColumnName("week").InputFormat("yyyy-MM-dd");
mapper.Property(c => c.Rank).ColumnName("rank");
mapper.Property(c => c.TitleId).ColumnName("title_id");
mapper.Property(c => c.Title).ColumnName("title");
mapper.Property(c => c.Author).ColumnName("author");
var rankings = new List<BookRanking>();
using (var reader = new StreamReader(File.OpenRead(pathToFile)))
{
var options = new DelimitedOptions() { IsFirstRecordSchema = true, Separator = "\t" };
rankings = mapper.Read(reader, options).ToList();
}
rankings
.Where(r => r.Rank == 1)
.GroupBy(r => r.TitleId)
.OrderByDescending(t => t.Count())
.Select(t => $"{t.First().Title} ({t.Count()})")
.Take(10)
.ToList()
.ForEach(t => Console.WriteLine(t));
/* My results:
1. THE DA VINCI CODE (59)
2. WHERE THE CRAWDADS SING (54)
3. HAWAII (49)
4. THE CAINE MUTINY (48)
5. THE SOURCE (41)
6. LOVE STORY (41)
7. JONATHAN LIVINGSTON SEAGULL (38)
8. THE BRIDGES OF MADISON COUNTY (38)
9. TRINITY (36)
10. THE SPY WHO CAME IN FROM THE COLD (34)
*/
}
public class BookRanking
{
public string Title { get; set; }
public string Author { get; set; }
public int Rank { get; set; }
public int Year { get; set; }
public DateTime Week { get; set; }
public int TitleId { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment