Skip to content

Instantly share code, notes, and snippets.

@hudo
Last active March 15, 2017 13:03
Show Gist options
  • Save hudo/675828b6fdf274e1e863e1f8c72bf688 to your computer and use it in GitHub Desktop.
Save hudo/675828b6fdf274e1e863e1f8c72bf688 to your computer and use it in GitHub Desktop.
Santa gifts
class Program
{
static void Main(string[] args)
{
var children = new List<Child> { new Child("Hudo", true), new Child("Genna", true), new Child("Calippio", false), new Child("Javier", false), new Child("Cristian", true)};
var presents = new List<Present> { new Present("Tannoy speakers", 2), new Present("Candy", 1)};
presents
.SelectMany(x => Enumerable.Range(1, x.Quantity).Select(c => new Present(x.Name, 1)))
.Zip(children.Where(x => x.WasGood), (present, child) => new Assignment(child.Name, present.Name))
.Select(x => x.ToString())
.ToList().ForEach(Console.WriteLine);
}
struct Child
{
public Child(string name, bool wasGood)
{
Name = name;
WasGood = wasGood;
}
public string Name;
public bool WasGood;
}
struct Present
{
public Present(string name, int quantity)
{
Name = name;
Quantity = quantity;
}
public string Name;
public int Quantity;
}
struct Assignment
{
public Assignment(string name, string present)
{
Name = name;
Present = present;
}
string Name;
string Present;
public override string ToString()
{
return $"{Name} gets {Present}";
}
}
}
/*
Given a list of children { name:string, wasGood:boolean
}
And a list of presents { name:string, quantity:number }
Santa needs you to write an assignPresents function that will return a list of present assignments { childName, presentName }.
Bad children get no present(are not in the results).
For example:
children =
[ { name: "Bart" , wasGood: false }
, { name: "Milhouse", wasGood: true }
, { name: "Lisa" , wasGood: true }
, { name: "Maggie" , wasGood: true }
]
presents =
[ { name: "Stacy Malibu doll" , quantity: 2 }
, { name: "Cursed Krusty action-figure", quantity: 5 }
]
assign(children, presents) =
[ { childName: "Milhouse", presentName: "Stacy Malibu doll" }
, { childName: "Lisa" , presentName: "Stacy Malibu doll" }
, { childName: "Maggie" , presentName: "Cursed Krusty action-figure" }
]
Of course, Santa hates mutation(and mutants), so both children and presents must remain unchanged after calling assignPresents
(assignment is not random, if the first present has quantity Q just give that present to the first Q (good) children)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment