Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Last active September 15, 2015 19:29
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 sudipto80/84e5280c4eb1bda427b4 to your computer and use it in GitHub Desktop.
Save sudipto80/84e5280c4eb1bda427b4 to your computer and use it in GitHub Desktop.
Arranging books in your bookshelves
Table books = DataAcquisition.LoadCSV(@"C:\personal\books.csv");
//genre,author,height-genre*author
books.AddColumn("GenreAsNum", books.ValuesOf("Genre"));
books.Transform("GenreAsNum", x => x == "fiction" ? "1" : x == "nonfiction" ?
"2" : x == "philosophy" ?
"3" : x == "science" ?
"4" : x == "tech" ?
"5" :
"-1");
books = books.Transform("Author", x =>
{
if (x.Contains(',')) return x.Substring(0, x.IndexOf(','));
else if (x.Contains(' ')) return x.Substring(0, x.IndexOf(' ')); else return x;
});
books.AddColumn("Shelf", "Floor([Height]/[GenreAsNum])", 0);
int max = books.ValuesOf("Shelf").Select(t => Convert.ToInt32(t)).Max();
books.Transform("Shelf", x => Math.Ceiling((6 * Convert.ToDecimal(Convert.ToDouble(x) / Convert.ToDouble(max)))).ToString());
var shelfs = books.SortBy("Height").ThenBy("Author").ThenBy("Genre").SplitOn("Shelf");
shelfs.ToList().ForEach(t => { t.Value.Pick("Title", "Genre", "Height").PrettyDump(); Console.WriteLine("----------"); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment