Created
March 10, 2016 22:00
-
-
Save maritaria/f883a9b727831eb176f2 to your computer and use it in GitHub Desktop.
Pick n items from a set of items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
public class Class1 | |
{ | |
public static IEnumerable<Class1> SomeMethodThatReturnsAllCardsAvailable() | |
{ | |
return null; | |
} | |
public static int RandomNumberGenerator(int minInclusive, int maxExclusive) | |
{ | |
new Random().Next();//dont actually use this | |
return 0; | |
} | |
public static IEnumerable<Class1> PickCards(int amount) | |
{ | |
List<Class1> pickedCards = new List<Class1>(); | |
var remainingCards = SomeMethodThatReturnsAllCardsAvailable(); | |
var count = remainingCards.Count(); | |
while (amount > 0 && count > 0) | |
{ | |
//Invariants: | |
//we still want to draw cards (amount > 0) | |
//there are cards that we can draw (_.Count() > 0) | |
var index = RandomNumberGenerator(0, remainingCards.Count()); | |
var card = remainingCards.ElementAt(index); | |
pickedCards.Add(card); | |
remainingCards = remainingCards.Except(card); | |
amount--; | |
//Invariant: | |
//count will describe the cards available | |
count = remainingCards.Count(); | |
} | |
return pickedCards; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment