Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 51
Good morning! Here's your coding interview problem for today.

This problem was asked by Facebook.

Given a function that generates perfectly random numbers between 1 and k (inclusive), where k is an input, write a function that shuffles a deck of cards represented as an array using only swaps.

It should run in O(N) time.

Hint: Make sure each one of the 52! permutations of the deck is equally likely.

Pull request:
Day51
Resolves #112.

Patch:
diff --git a/README.md b/README.md
index c5d62de..b4fbcce 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ problems from
 * [Day 48](https://github.com/vaskoz/dailycodingproblem-go/issues/109)
 * [Day 49](https://github.com/vaskoz/dailycodingproblem-go/issues/107)
 * [Day 50](https://github.com/vaskoz/dailycodingproblem-go/issues/110)
+* [Day 51](https://github.com/vaskoz/dailycodingproblem-go/issues/112)
 * [Day 52](https://github.com/vaskoz/dailycodingproblem-go/issues/113)
 * [Day 53](https://github.com/vaskoz/dailycodingproblem-go/issues/114)
 * [Day 54](https://github.com/vaskoz/dailycodingproblem-go/issues/118)
diff --git a/day51/problem.go b/day51/problem.go
new file mode 100644
index 0000000..c70ff98
--- /dev/null
+++ b/day51/problem.go
@@ -0,0 +1,41 @@
+package day51
+
+import (
+	"math/rand"
+	"time"
+)
+
+// Suit represents the 4 suits: Hearts, Spades, Diamonds and Clubs.
+type Suit string
+
+// Rank represents the face of the card.
+type Rank string
+
+// Card represents a playing card with a Suit and Rank.
+type Card struct {
+	s Suit
+	r Rank
+}
+
+// Shuffle randomly shuffles the cards using a random function
+// that returns values between 1 and k inclusive.
+func Shuffle(cards []Card, k int) {
+	r := rand.New(rand.NewSource(time.Now().UnixNano()))
+	r.Shuffle(len(cards), func(i, j int) {
+		cards[i], cards[j] = cards[j], cards[i]
+	})
+}
+
+// NewDeck returns a deterministic deck of cards.
+func NewDeck() []Card {
+	suits := []Suit{"clubs", "diamonds", "spades", "hearts"}
+	ranks := []Rank{"two", "three", "four", "five", "six", "seven",
+		"eight", "nine", "ten", "jack", "queen", "king", "ace"}
+	cards := make([]Card, 0, 52)
+	for _, suit := range suits {
+		for _, rank := range ranks {
+			cards = append(cards, Card{suit, rank})
+		}
+	}
+	return cards
+}


Test patch:
diff --git a/day51/problem_test.go b/day51/problem_test.go
new file mode 100644
index 0000000..429616f
--- /dev/null
+++ b/day51/problem_test.go
@@ -0,0 +1,32 @@
+package day51
+
+import "testing"
+
+func TestShuffle(t *testing.T) {
+	t.Parallel()
+	cards := NewDeck()
+	Shuffle(cards, 5)
+}
+
+func TestNewDeck(t *testing.T) {
+	t.Parallel()
+	suits := []Suit{"clubs", "diamonds", "spades", "hearts"}
+	ranks := []Rank{"two", "three", "four", "five", "six", "seven",
+		"eight", "nine", "ten", "jack", "queen", "king", "ace"}
+	index := 0
+	cards := NewDeck()
+	for _, suit := range suits {
+		for _, rank := range ranks {
+			if expected := (Card{suit, rank}); cards[index] != expected {
+				t.Errorf("Expected %v, got %v", expected, cards[index])
+			}
+			index++
+		}
+	}
+}
+
+func BenchmarkNewDeck(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		NewDeck()
+	}
+}