Skip to content

Instantly share code, notes, and snippets.

@maritaria
Created March 10, 2016 22:00
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 maritaria/f883a9b727831eb176f2 to your computer and use it in GitHub Desktop.
Save maritaria/f883a9b727831eb176f2 to your computer and use it in GitHub Desktop.
Pick n items from a set of items
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