Skip to content

Commit

Permalink
Merge pull request #31 from exercism/circular-buffer
Browse files Browse the repository at this point in the history
Implement exercise 'circular-buffer'
  • Loading branch information
ingydotnet authored Oct 2, 2024
2 parents 09954ab + c04c6b4 commit 4c3e630
Show file tree
Hide file tree
Showing 15 changed files with 579 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,14 @@
"prerequisites": [],
"difficulty": 7
},
{
"slug": "circular-buffer",
"name": "Circular Buffer",
"uuid": "ebb62daf-c09a-4ee6-99a1-4f68be5152e4",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "minesweeper",
"name": "Minesweeper",
Expand Down
7 changes: 7 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ exercises:
prerequisites: []
difficulty: 7

- slug: circular-buffer
name: Circular Buffer
uuid: ebb62daf-c09a-4ee6-99a1-4f68be5152e4
practices: []
prerequisites: []
difficulty: 8

- slug: minesweeper
name: Minesweeper
uuid: 4e7dd5dd-a6b6-4d8c-be9c-7a5995a86de9
Expand Down
58 changes: 58 additions & 0 deletions exercises/practice/circular-buffer/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Instructions

A circular buffer, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.

A circular buffer first starts empty and of some predefined length.
For example, this is a 7-element buffer:

```text
[ ][ ][ ][ ][ ][ ][ ]
```

Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer):

```text
[ ][ ][ ][1][ ][ ][ ]
```

Then assume that two more elements are added — 2 & 3 — which get appended after the 1:

```text
[ ][ ][ ][1][2][3][ ]
```

If two elements are then removed from the buffer, the oldest values inside the buffer are removed.
The two elements removed, in this case, are 1 & 2, leaving the buffer with just a 3:

```text
[ ][ ][ ][ ][ ][3][ ]
```

If the buffer has 7 elements then it is completely full:

```text
[5][6][7][8][9][3][4]
```

When the buffer is full an error will be raised, alerting the client that further writes are blocked until a slot becomes free.

When the buffer is full, the client can opt to overwrite the oldest data with a forced write.
In this case, two more elements — A & B — are added and they overwrite the 3 & 4:

```text
[5][6][7][8][9][A][B]
```

3 & 4 have been replaced by A & B making 5 now the oldest data in the buffer.
Finally, if two elements are removed then what would be returned is 5 & 6 yielding the buffer:

```text
[ ][ ][7][8][9][A][B]
```

Because there is space available, if the client again uses overwrite to store C & D then the space where 5 & 6 were stored previously will be used not the location of 7 & 8.
7 is still the oldest element and the buffer is once again full.

```text
[C][D][7][8][9][A][B]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YS_VERSION := 0.1.75
28 changes: 28 additions & 0 deletions exercises/practice/circular-buffer/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.79

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

YS_LOCAL_BIN := $(YS_LOCAL_PREFIX)/bin

YS_BIN := $(YS_LOCAL_BIN)/ys-$(YS_VERSION)

TEST_FILE ?= $(wildcard *-test.ys)


export PATH := $(YS_LOCAL_BIN):$(PATH)

export YSPATH := $(BASE)


default:

test: $(YS_BIN)
prove -v $(TEST_FILE)

$(YS_BIN):
curl -s https://yamlscript.org/install | \
BIN=1 VERSION=$(YS_VERSION) PREFIX=$(YS_LOCAL_PREFIX) bash >/dev/null
78 changes: 78 additions & 0 deletions exercises/practice/circular-buffer/.meta/circular-buffer-test.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env ys-0

require ys::taptest: :all

use: circular-buffer

test::
- name: Reading empty buffer should fail
code: run(1 [{"operation" "read", "should_succeed" false}])
want: true
uuid: 28268ed4-4ff3-45f3-820e-895b44d53dfa

- name: Can read an item just written
code: run(1 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 1}])
want: true
uuid: 2e6db04a-58a1-425d-ade8-ac30b5f318f3

- name: Each item may only be read once
code: run(1 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 1} {"operation" "read", "should_succeed" false}])
want: true
uuid: 90741fe8-a448-45ce-be2b-de009a24c144

- name: Items are read in the order they are written
code: run(2 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "write", "item" 2, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 1} {"operation" "read", "should_succeed" true, "expected" 2}])
want: true
uuid: be0e62d5-da9c-47a8-b037-5db21827baa7

- name: Full buffer can't be written to
code: run(1 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "write", "item" 2, "should_succeed" false}])
want: true
uuid: 2af22046-3e44-4235-bfe6-05ba60439d38

- name: A read frees up capacity for another write
code: run(1 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 1} {"operation" "write", "item" 2, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 2}])
want: true
uuid: 547d192c-bbf0-4369-b8fa-fc37e71f2393

- name: Read position is maintained even across multiple writes
code: run(3 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "write", "item" 2, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 1} {"operation" "write", "item" 3, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 2} {"operation" "read", "should_succeed" true, "expected" 3}])
want: true
uuid: 04a56659-3a81-4113-816b-6ecb659b4471

- name: Items cleared out of buffer can't be read
code: run(1 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "clear"} {"operation" "read", "should_succeed" false}])
want: true
uuid: 60c3a19a-81a7-43d7-bb0a-f07242b1111f

- name: Clear frees up capacity for another write
code: run(1 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "clear"} {"operation" "write", "item" 2, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 2}])
want: true
uuid: 45f3ae89-3470-49f3-b50e-362e4b330a59

- name: Clear does nothing on empty buffer
code: run(1 [{"operation" "clear"} {"operation" "write", "item" 1, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 1}])
want: true
uuid: e1ac5170-a026-4725-bfbe-0cf332eddecd

- name: Overwrite acts like write on non-full buffer
code: run(2 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "overwrite", "item" 2} {"operation" "read", "should_succeed" true, "expected" 1} {"operation" "read", "should_succeed" true, "expected" 2}])
want: true
uuid: 9c2d4f26-3ec7-453f-a895-7e7ff8ae7b5b

- name: Overwrite replaces the oldest item on full buffer
code: run(2 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "write", "item" 2, "should_succeed" true} {"operation" "overwrite", "item" 3} {"operation" "read", "should_succeed" true, "expected" 2} {"operation" "read", "should_succeed" true, "expected" 3}])
want: true
uuid: 880f916b-5039-475c-bd5c-83463c36a147

- name: Overwrite replaces the oldest item remaining in buffer following a read
code: run(3 [{"operation" "write", "item" 1, "should_succeed" true} {"operation" "write", "item" 2, "should_succeed" true} {"operation" "write", "item" 3, "should_succeed" true} {"operation" "read", "should_succeed" true, "expected" 1} {"operation" "write", "item" 4, "should_succeed" true} {"operation" "overwrite", "item" 5} {"operation" "read", "should_succeed" true, "expected" 3} {"operation" "read", "should_succeed" true, "expected" 4} {"operation" "read", "should_succeed" true, "expected" 5}])
want: true
uuid: bfecab5b-aca1-4fab-a2b0-cd4af2b053c3

- name: Initial clear does not affect wrapping around
code: run(2 [{"operation" "clear"} {"operation" "write", "item" 1, "should_succeed" true} {"operation" "write", "item" 2, "should_succeed" true} {"operation" "overwrite", "item" 3} {"operation" "overwrite", "item" 4} {"operation" "read", "should_succeed" true, "expected" 3} {"operation" "read", "should_succeed" true, "expected" 4} {"operation" "read", "should_succeed" false}])
want: true
uuid: 9cebe63a-c405-437b-8b62-e3fdc1ecec5a

done: 14
49 changes: 49 additions & 0 deletions exercises/practice/circular-buffer/.meta/circular-buffer.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
!yamlscript/v0

defn run(capacity operations):
buffer =: M(:B V(capacity * [nil]), :P 0)
reduce _ buffer operations:
fn(buffer op):
op item pass want =:
op.slice('operation' 'item'
'should_succeed' 'expected')
args =:
condp eq op:
-'clear' : -[buffer]
-'read' : L(buffer pass want)
-'write' : L(buffer pass item)
-'overwrite' : L(buffer pass item)
value("op-$op"): args*
=>: true

defn op-clear(buffer):
M: :B V(buffer.B.# * [nil]) :P 0

defn op-read(buffer pass want):
cell =: buffer.B.nth(buffer.P)
when pass:
when cell:nil?:
die: S("Can't read:\ " buffer)
when cell != want:
die: "Bad read:\ $cell != $want ($buffer)"
M: :B update-in(buffer.B [buffer.P] constantly(nil))
:P (buffer.P.++ % buffer.B.#)

defn op-write(buffer pass item):
B P =: buffer.slice(q(B P))
I =:
reduce _ nil concat(range(P B.#) range(0 P)):
fn(idx i): idx || (not(B.$i) && i)

if I:
update-in: buffer [:B I] constantly(item)
when pass: die('Full buffer')

defn op-overwrite(buffer pass item):
if some(nil? buffer.B):
op-write: buffer pass item
else:
buffer =:
update-in: buffer [:B buffer.P] constantly(item)
P =: buffer.P.++ % buffer.B.#
update-in: buffer [:P] constantly(P)
23 changes: 23 additions & 0 deletions exercises/practice/circular-buffer/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"ingydotnet"
],
"files": {
"solution": [
"circular-buffer.ys"
],
"test": [
"circular-buffer-test.ys",
"GNUmakefile",
"Makefile",
".yamlscript/exercise.mk",
".yamlscript/exercism-ys-installer"
],
"example": [
".meta/circular-buffer.ys"
]
},
"blurb": "A data structure that uses a single, fixed-size buffer as if it were connected end-to-end.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Circular_buffer"
}
52 changes: 52 additions & 0 deletions exercises/practice/circular-buffer/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[28268ed4-4ff3-45f3-820e-895b44d53dfa]
description = "reading empty buffer should fail"

[2e6db04a-58a1-425d-ade8-ac30b5f318f3]
description = "can read an item just written"

[90741fe8-a448-45ce-be2b-de009a24c144]
description = "each item may only be read once"

[be0e62d5-da9c-47a8-b037-5db21827baa7]
description = "items are read in the order they are written"

[2af22046-3e44-4235-bfe6-05ba60439d38]
description = "full buffer can't be written to"

[547d192c-bbf0-4369-b8fa-fc37e71f2393]
description = "a read frees up capacity for another write"

[04a56659-3a81-4113-816b-6ecb659b4471]
description = "read position is maintained even across multiple writes"

[60c3a19a-81a7-43d7-bb0a-f07242b1111f]
description = "items cleared out of buffer can't be read"

[45f3ae89-3470-49f3-b50e-362e4b330a59]
description = "clear frees up capacity for another write"

[e1ac5170-a026-4725-bfbe-0cf332eddecd]
description = "clear does nothing on empty buffer"

[9c2d4f26-3ec7-453f-a895-7e7ff8ae7b5b]
description = "overwrite acts like write on non-full buffer"

[880f916b-5039-475c-bd5c-83463c36a147]
description = "overwrite replaces the oldest item on full buffer"

[bfecab5b-aca1-4fab-a2b0-cd4af2b053c3]
description = "overwrite replaces the oldest item remaining in buffer following a read"

[9cebe63a-c405-437b-8b62-e3fdc1ecec5a]
description = "initial clear does not affect wrapping around"
1 change: 1 addition & 0 deletions exercises/practice/circular-buffer/.yamlscript/exercise.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YS_VERSION := 0.1.75
Loading

0 comments on commit 4c3e630

Please sign in to comment.