Skip to content

Commit c9d3aa2

Browse files
authored
Merge branch 'main' into matrix
2 parents 8310887 + 1281dd3 commit c9d3aa2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4069
-1
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727

2828
steps:
2929
- name: Checkout repository
30-
uses: actions/checkout@v5
30+
uses: actions/checkout@v6
3131

3232
- name: Install free-pascal
3333
run: |

bin/add-practice-exercise

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
3+
# Synopsis:
4+
# Scaffold the files for a new practice exercise.
5+
# After creating the exercise, follow the instructions in the output.
6+
7+
# Example:
8+
# bin/add-practice-exercise two-fer
9+
10+
# Example with difficulty:
11+
# bin/add-practice-exercise -d 5 two-fer
12+
13+
# Example with author and difficulty:
14+
# bin/add-practice-exercise -a foo -d 3 two-fer
15+
16+
set -euo pipefail
17+
scriptname=$0
18+
19+
help_and_exit() {
20+
echo >&2 "Scaffold the files for a new practice exercise."
21+
echo >&2 "Usage: ${scriptname} [-h] [-a author] [-d difficulty] <exercise-slug>"
22+
echo >&2 "Where: author is the GitHub username of the exercise creator."
23+
echo >&2 "Where: difficulty is between 1 (easiest) to 10 (hardest)."
24+
exit 1
25+
}
26+
27+
die() { echo >&2 "$*"; exit 1; }
28+
29+
required_tool() {
30+
command -v "${1}" >/dev/null 2>&1 ||
31+
die "${1} is required but not installed. Please install it and make sure it's in your PATH."
32+
}
33+
34+
require_files_template() {
35+
jq -e --arg key "${1}" '.files[$key] | length > 0' config.json > /dev/null ||
36+
die "The '.files.${1}' array in the 'config.json' file is empty. Please add at least one file. See https://exercism.org/docs/building/tracks/config-json#h-files for more information."
37+
}
38+
39+
required_tool jq
40+
41+
require_files_template "solution"
42+
require_files_template "test"
43+
require_files_template "example"
44+
45+
[[ -f ./bin/fetch-configlet ]] || die "Run this script from the repo's root directory."
46+
47+
author=''
48+
difficulty='5'
49+
while getopts :ha:d: opt; do
50+
case $opt in
51+
h) help_and_exit ;;
52+
a) author=$OPTARG ;;
53+
d) difficulty=$OPTARG ;;
54+
?) echo >&2 "Unknown option: -$OPTARG"; help_and_exit ;;
55+
esac
56+
done
57+
shift "$((OPTIND - 1))"
58+
59+
(( $# >= 1 )) || help_and_exit
60+
61+
slug="${1}"
62+
63+
if [[ -z "${author}" ]]; then
64+
read -rp 'Your GitHub username: ' author
65+
fi
66+
67+
./bin/fetch-configlet
68+
./bin/configlet create --practice-exercise "${slug}" --author "${author}" --difficulty "${difficulty}"
69+
70+
exercise_dir="exercises/practice/${slug}"
71+
files=$(jq -r --arg dir "${exercise_dir}" '.files | to_entries | map({key: .key, value: (.value | map("'"'"'" + $dir + "/" + . + "'"'"'") | join(" and "))}) | from_entries' "${exercise_dir}/.meta/config.json")
72+
73+
./bin/reorder-exercises
74+
./bin/sync-shared.sh
75+
76+
cat << NEXT_STEPS
77+
78+
Your next steps are:
79+
- Create the test suite in $(jq -r '.test' <<< "${files}")
80+
- The tests should be based on the canonical data at 'https://github.com/exercism/problem-specifications/blob/main/exercises/${slug}/canonical-data.json'
81+
- Any test cases you don't implement, mark them in 'exercises/practice/${slug}/.meta/tests.toml' with "include = false"
82+
- Create the example solution in $(jq -r '.example' <<< "${files}")
83+
- Verify the example solution passes the tests by running 'bin/verify-exercises ${slug}'
84+
- Create the stub solution in $(jq -r '.solution' <<< "${files}")
85+
- Update the 'difficulty' value for the exercise's entry in the 'config.json' file in the repo's root
86+
- Validate CI using 'bin/configlet lint' and 'bin/configlet fmt'
87+
NEXT_STEPS

config.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@
4242
"prerequisites": [],
4343
"difficulty": 1
4444
},
45+
{
46+
"slug": "binary",
47+
"name": "Binary",
48+
"uuid": "caaca116-410d-44d0-ba62-d080686bbbab",
49+
"practices": [],
50+
"prerequisites": [],
51+
"difficulty": 2,
52+
"status": "deprecated"
53+
},
54+
{
55+
"slug": "trinary",
56+
"name": "Trinary",
57+
"uuid": "fd61bef1-09d2-433d-ad6c-bb126e19439a",
58+
"practices": [],
59+
"prerequisites": [],
60+
"difficulty": 2,
61+
"status": "deprecated"
62+
},
4563
{
4664
"slug": "acronym",
4765
"name": "Acronym",
@@ -225,6 +243,22 @@
225243
"practices": [],
226244
"prerequisites": [],
227245
"difficulty": 3
246+
},
247+
{
248+
"slug": "line-up",
249+
"name": "Line Up",
250+
"uuid": "7d7ad43c-9978-47d9-b997-cd12ac2641f8",
251+
"practices": [],
252+
"prerequisites": [],
253+
"difficulty": 3
254+
},
255+
{
256+
"slug": "nth-prime",
257+
"name": "Nth Prime",
258+
"uuid": "750d91a2-7a09-465e-9303-1b9b3ddc0c43",
259+
"practices": [],
260+
"prerequisites": [],
261+
"difficulty": 3
228262
},
229263
{
230264
"slug": "nucleotide-count",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Instructions
2+
3+
Convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles.
4+
5+
Implement binary to decimal conversion.
6+
Given a binary input string, your program should produce a decimal output.
7+
The program should handle invalid inputs.
8+
9+
## Note
10+
11+
- Implement the conversion yourself.
12+
Do not use something else to perform the conversion for you.
13+
14+
## About Binary (Base-2)
15+
16+
Decimal is a base-10 system.
17+
18+
A number 23 in base 10 notation can be understood as a linear combination of powers of 10:
19+
20+
- The rightmost digit gets multiplied by 10^0 = 1
21+
- The next number gets multiplied by 10^1 = 10
22+
- ...
23+
- The nth number gets multiplied by 10^_(n-1)_.
24+
- All these values are summed.
25+
26+
So: `23 => 2*10^1 + 3*10^0 => 2*10 + 3*1 = 23 base 10`
27+
28+
Binary is similar, but uses powers of 2 rather than powers of 10.
29+
30+
So: `101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"Binary.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
14+
]
15+
},
16+
"blurb": "Convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles.",
17+
"source": "All of Computer Science",
18+
"source_url": "https://www.wolframalpha.com/examples/mathematics/numbers/base-conversions"
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
unit Binary;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
function Decimal(const binary : string) : longword;
8+
9+
implementation
10+
11+
uses SysUtils;
12+
13+
function Decimal(const binary : string) : longword;
14+
begin
15+
16+
result := length(binary);
17+
18+
end;
19+
20+
end.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[567fc71e-1013-4915-9285-bca0648c0844]
13+
description = "binary 0 is decimal 0"
14+
include = false
15+
16+
[c0824fb1-6a0a-4e9a-a262-c6c00af99fa8]
17+
description = "binary 1 is decimal 1"
18+
19+
[4d2834fb-3cc3-4159-a8fd-da1098def8ed]
20+
description = "binary 10 is decimal 2"
21+
include = false
22+
23+
[b7b2b649-4a7c-4808-9eb9-caf00529bac6]
24+
description = "binary 11 is decimal 3"
25+
include = false
26+
27+
[de761aff-73cd-43c1-9e1f-0417f07b1e4a]
28+
description = "binary 100 is decimal 4"
29+
include = false
30+
31+
[7849a8f7-f4a1-4966-963e-503282d6814c]
32+
description = "binary 1001 is decimal 9"
33+
include = false
34+
35+
[836a101c-aecb-473b-ba78-962408dcda98]
36+
description = "binary 11010 is decimal 26"
37+
include = false
38+
39+
[1c6822a4-8584-438b-8dd4-40f0f0b66371]
40+
description = "binary 10001101000 is decimal 1128"
41+
include = false
42+
43+
[91ffe632-8374-4016-b1d1-d8400d9f940d]
44+
description = "binary ignores leading zeros"
45+
include = false
46+
47+
[44f7d8b1-ddc3-4751-8be3-700a538b421c]
48+
description = "2 is not a valid binary digit"
49+
include = false
50+
51+
[c263a24d-6870-420f-b783-628feefd7b6e]
52+
description = "a number containing a non-binary digit is invalid"
53+
include = false
54+
55+
[8d81305b-0502-4a07-bfba-051c5526d7f2]
56+
description = "a number with trailing non-binary characters is invalid"
57+
include = false
58+
59+
[a7f79b6b-039a-4d42-99b4-fcee56679f03]
60+
description = "a number with leading non-binary characters is invalid"
61+
include = false
62+
63+
[9e0ece9d-b8aa-46a0-a22b-3bed2e3f741e]
64+
description = "a number with internal non-binary characters is invalid"
65+
include = false
66+
67+
[46c8dd65-0c32-4273-bb0d-f2b111bccfbd]
68+
description = "a number and a word whitespace separated is invalid"
69+
include = false
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
unit Binary;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
function Decimal(const binary : string) : longword;
8+
9+
implementation
10+
11+
uses SysUtils;
12+
13+
function Decimal(const binary : string) : longword;
14+
begin
15+
16+
raise ENotImplemented.Create('Please implement your solution.'); result := length(binary);
17+
18+
end;
19+
20+
end.

exercises/practice/binary/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SHELL = /bin/bash
2+
MAKEFLAGS += --no-print-directory
3+
DESTDIR = build
4+
EXECUTABLE = $(DESTDIR)/test
5+
COMMAND = fpc -l- -v0 -g -gl -Sa -Cr -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"
6+
7+
.ONESHELL:
8+
9+
test:
10+
@mkdir -p "./$(DESTDIR)"
11+
@cp -r ./lib "./$(DESTDIR)"
12+
@$(COMMAND) && ./$(EXECUTABLE) $(test)
13+
14+
clean:
15+
@rm -fr "./$(DESTDIR)"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
unit TestCases;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses Classes, SysUtils, FPCUnit, TestRegistry, FPCUnitTestUtils;
8+
9+
type
10+
BinaryTest = class(TTestCase)
11+
published
12+
procedure binary_1_is_decimal_1;
13+
end;
14+
15+
implementation
16+
17+
uses Binary;
18+
19+
// c0824fb1-6a0a-4e9a-a262-c6c00af99fa8
20+
procedure BinaryTest.binary_1_is_decimal_1;
21+
begin
22+
TapAssertTrue(Self, 'binary 1 is decimal 1', 1, Binary.Decimal('1'));
23+
end;
24+
25+
initialization
26+
RegisterTest(BinaryTest);
27+
28+
end.

0 commit comments

Comments
 (0)