Skip to content

Instantly share code, notes, and snippets.

@perjerz
Created October 26, 2018 14:16
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 perjerz/f41bdd4642c12d8efca2dc9174b23ac7 to your computer and use it in GitHub Desktop.
Save perjerz/f41bdd4642c12d8efca2dc9174b23ac7 to your computer and use it in GitHub Desktop.
YWC question
package cardat
import (
"fmt"
)
func cardAt(n int) string {
if n < 0 || n > 51 {
return "Out of range"
}
suits := []byte{'C', 'D', 'H', 'S'}
ranks := []byte{'2', '3', '4', '5', '6', '7', '8', '9', 'O', 'J', 'Q', 'K', 'A'}
return fmt.Sprintf("%c%c", ranks[n%13], suits[n/13])
}
package cardat
import (
"fmt"
"testing"
)
func TestCardAt(t *testing.T) {
testCases := []struct {
n int
expected string
}{
{
n: -1,
expected: "Out of range",
},
{
n: 0,
expected: "2C",
},
{
n: 1,
expected: "3C",
},
{
n: 34,
expected: "OH",
},
{
n: 35,
expected: "JH",
},
{
n: 52,
expected: "Out of range",
},
}
for _, tC := range testCases {
desc := fmt.Sprintf("cardAt should return %s when n = %d", tC.expected, tC.n)
t.Run(desc, func(t *testing.T) {
actual := cardAt(tC.n)
if actual != tC.expected {
t.Errorf("expected %s but got %s", tC.expected, actual)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment