Cards
Mix.install([
{:kino, github: "livebook-dev/kino", override: true},
{:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
{:vega_lite, "~> 0.1.4"},
{:kino_vega_lite, "~> 0.1.1"},
{:benchee, "~> 0.1"},
{:ecto, "~> 3.7"},
{:math, "~> 0.7.0"},
{:faker, "~> 0.17.0"},
{:utils, path: "#{__DIR__}/../utils"},
{:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])
Navigation
Cards Module
In this exercise, you’re going to create a Cards
module. Work through each step in
the Elixir cell at the bottom.
Step 1: Deck
The Cards
module should be able to create a deck with the deck/0
function.
A deck should be a list of cards. A card should be represented with a string of the suite and number.
We’ll use 11
, 12
, and 13
to represent Jack, Queen, and King so cards will be numbered from 1
to 13
.
We’ll use h
for hearts, s
for spades, c
for clubs, and d
for diamonds.
That means a deck
should be a list of cards with
-
"1h"
to"13h"
-
"1s"
to"13s"
-
"1c"
to"13c"
-
"1d"
to"13d"
Step 2: Shuffle
Create a shuffle/1
function that accepts the deck and returns the cards in random order.
Step 3: Deal
Create a deal/3
function that deals cards to players.
- The first parameter should be the deck.
- The second parameter should be the number of cards to deal.
- The third parameter should be the number of players to deal to.
- It should return separate lists for each player, and the remaining deck.
- Cards should be dealt from the start of the deck to the end while rotating between each hand.
Cards.deal(["1c", "2c", "3c", "4c", "5c", "6c", "7c"], 2, 3)
%{deck: ["7c"], dealt: [["1c", "4c"], ["2c", "5c"], ["3c", "6c"]]}
- if there are not enough cards, deal as many as you can and return empty or reduced lists for the other players.
Cards.deal(["1c", "2c", "3c", "4c", "5c", "6c", "7c"], 3, 3)
%{deck: [], dealt: [["1c", "4c", "7c"], ["2c", "5c"], ["3c", "6c"]]}
Step 4: Sort
create a sort/1
function which accepts a list of cards and sorts them. cards should be
sorted by suite in the order h
-> s
-> c
-> d
. then they should be sorted in chronological order
1
-> 2
-> ...
-> 13
.
Cards.sort(["1c", "5d", "4c", "6d", "2c", "3c", "7d", "5s", "3h", "2h", "4s"], 3, 3)
["2h", "3h", "4s", "5s", "1c", "2c", "3c", "4c", "5d", "6d", "7d"]
Your Solution
Enter your answer in the Elixir cell below.
defmodule Cards do
end
Commit Your Progress
Run the following in your command line from the project folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish deprecated cards exercise"