Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix binary breakage in CI #254

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bin/verify-exercises
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ run_test() {
mkdir -p ${temp_dir}

cp -r "$1/." $temp_dir
cp $temp_dir/.meta/example.lfe $temp_dir/src/$slug.lfe

outpath=$temp_dir/src/$slug.lfe
if [ "$slug" = "binary" ]; then
outpath=$temp_dir/src/binary-string.lfe
fi

cp $temp_dir/.meta/example.lfe $outpath

(cd /opt/test-runner && bin/run.sh $slug $temp_dir $temp_dir) || exit 1

Expand Down
6 changes: 1 addition & 5 deletions exercises/practice/binary/.meta/example.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@
(export (to-decimal 1)))

(defun to-decimal (string)
(try (element 2 (lists:foldr #'to-decimal/2 #(0 0) string))
(catch (_ 0))))
(list_to_integer string 2))

(defun to-decimal
([#\0 `#(,n ,acc)] `#(,(+ n 1) ,acc))
([#\1 `#(,n ,acc)] `#(,(+ n 1) ,(+ acc (trunc (math:pow 2 n))))))
7 changes: 7 additions & 0 deletions exercises/practice/binary/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,25 @@ description = "binary ignores leading zeros"

[44f7d8b1-ddc3-4751-8be3-700a538b421c]
description = "2 is not a valid binary digit"
include = false

[c263a24d-6870-420f-b783-628feefd7b6e]
description = "a number containing a non-binary digit is invalid"
include = false

[8d81305b-0502-4a07-bfba-051c5526d7f2]
description = "a number with trailing non-binary characters is invalid"
include = false

[a7f79b6b-039a-4d42-99b4-fcee56679f03]
description = "a number with leading non-binary characters is invalid"
include = false

[9e0ece9d-b8aa-46a0-a22b-3bed2e3f741e]
description = "a number with internal non-binary characters is invalid"
include = false

[46c8dd65-0c32-4273-bb0d-f2b111bccfbd]
description = "a number and a word whitespace separated is invalid"
include = false

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
%% -*- erlang -*-
{application, 'binary',
{application, 'binary-string',
[{description, ""},
{vsn, "0.0.1"},
{modules,
['binary']},
['binary-string']},
{registered, []},
{applications,
[kernel, stdlib]},
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/binary/src/binary-string.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(defmodule binary-string
(export (to-decimal 1)))

; please implement the to-decimal function

29 changes: 19 additions & 10 deletions exercises/practice/binary/test/binary-string-tests.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@

(include-lib "ltest/include/ltest-macros.lfe")

(deftest one (check "1"))
(deftest binary-0-is-decimal-0
(is-equal 0 (binary-string:to-decimal "0")))

(deftest two (check "10"))
(deftest binary-1-is-decimal-1
(is-equal 1 (binary-string:to-decimal "1")))

(deftest three (check "11"))
(deftest binary-10-is-decimal-2
(is-equal 2 (binary-string:to-decimal "10")))

(deftest four (check "100"))
(deftest binary-11-is-decimal-3
(is-equal 3 (binary-string:to-decimal "11")))

(deftest nine (check "1001"))
(deftest binary-100-is-decimal-4
(is-equal 4 (binary-string:to-decimal "100")))

(deftest twenty-six (check "11010"))
(deftest binary-1001-is-decimal-9
(is-equal 9 (binary-string:to-decimal "1001")))

(deftest large (check "10001101000"))
(deftest binary-11010-is-decimal-26
(is-equal 26 (binary-string:to-decimal "11010")))

(deftest carrot (is-equal 0 (binary-string:to-decimal "carrot")))
(deftest binary-10001101000-is-decimal-1128
(is-equal 1128 (binary-string:to-decimal "10001101000")))

(deftest binary-ignores-leading-zeros
(is-equal 31 (binary-string:to-decimal "000011111")))

(defun check (string)
(is-equal (list_to_integer string 2) (binary-string:to-decimal string)))