Skip to content

Commit

Permalink
Add circular buffer exercise (#584)
Browse files Browse the repository at this point in the history
Added circular buffer
* Updated config.json

* Added tests and solution for Circular Buffer exercise

* Updated tests, metadata, docs for circular buffer exercise

* Fixed missing name attribute for circular buffer in config.json

* Moved solution file to .meta directory for circular buffer

* simplified overwrite  method on circular buffer

* updated example solution file

* updated UUID to version 4 of circular buffer exercise

* updated UUD in config and exercise dir
  • Loading branch information
Bajger authored Sep 10, 2023
1 parent a09213c commit bf19afd
Show file tree
Hide file tree
Showing 13 changed files with 962 additions and 189 deletions.
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,22 @@
"topics": [
"math"
]
},
{
"slug": "circular-buffer",
"name": "Circular Buffer",
"uuid": "158d4bc4-1002-4612-bdaf-5aa711a4f9a4",
"practices": [],
"prerequisites": [],
"difficulty" : 5,
"topics" : [
"arrays",
"conditionals",
"data_structures",
"exception_handling",
"lists",
"list-methods"
]
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion dev/src/BaselineOfExercism/BaselineOfExercism.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ BaselineOfExercism class >> exerciseGoldenTestPackageNames [
BaselineOfExercism class >> exercisePackageNames [
"Answer the list of exercise package names (as we don't yet have proper projects)"

^ #('Exercise@Acronym' 'Exercise@Allergies' 'Exercise@Anagram' 'Exercise@ArmstrongNumbers' 'Exercise@AtbashCipher' 'Exercise@Binary' 'Exercise@Bowling' 'Exercise@Clock' 'Exercise@CollatzConjecture' 'Exercise@Darts' 'Exercise@Diamond' 'Exercise@Die' 'Exercise@Etl' 'Exercise@FlattenArray' 'Exercise@Forth' 'Exercise@GradeSchool' 'Exercise@Grains' 'Exercise@Hamming' 'Exercise@HelloWorld' 'Exercise@HighScores' 'Exercise@IsbnVerifier' 'Exercise@Isogram' 'Exercise@Leap' 'Exercise@Luhn' 'Exercise@MatchingBrackets' 'Exercise@Matrix' 'Exercise@Minesweeper' 'Exercise@Pangram' 'Exercise@Proverb' 'Exercise@Raindrops' 'Exercise@ResistorColorDuo' 'Exercise@ReverseString' 'Exercise@RobotSimulator' 'Exercise@RomanNumerals' 'Exercise@SecretHandshake' 'Exercise@Sieve' 'Exercise@SpaceAge' 'Exercise@SumOfMultiples' 'Exercise@Tournament' 'Exercise@TwelveDays' 'Exercise@TwoFer' 'Exercise@Welcome' 'Exercise@WordCount')
^ #('Exercise@Acronym' 'Exercise@Allergies' 'Exercise@Anagram' 'Exercise@ArmstrongNumbers' 'Exercise@AtbashCipher' 'Exercise@Binary' 'Exercise@Bowling' 'Exercise@CircularBuffer' 'Exercise@Clock' 'Exercise@CollatzConjecture' 'Exercise@Darts' 'Exercise@Diamond' 'Exercise@Die' 'Exercise@Etl' 'Exercise@FlattenArray' 'Exercise@Forth' 'Exercise@GradeSchool' 'Exercise@Grains' 'Exercise@Hamming' 'Exercise@HelloWorld' 'Exercise@HighScores' 'Exercise@IsbnVerifier' 'Exercise@Isogram' 'Exercise@Leap' 'Exercise@Luhn' 'Exercise@MatchingBrackets' 'Exercise@Matrix' 'Exercise@Minesweeper' 'Exercise@Pangram' 'Exercise@Proverb' 'Exercise@Raindrops' 'Exercise@ResistorColorDuo' 'Exercise@ReverseString' 'Exercise@RobotSimulator' 'Exercise@RomanNumerals' 'Exercise@SecretHandshake' 'Exercise@Sieve' 'Exercise@SpaceAge' 'Exercise@SumOfMultiples' 'Exercise@Tournament' 'Exercise@TwelveDays' 'Exercise@TwoFer' 'Exercise@Welcome' 'Exercise@WordCount')
]

{ #category : #baselines }
Expand Down
89 changes: 89 additions & 0 deletions dev/src/Exercise@CircularBuffer/CircularBuffer.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"
I represent circular buffer - a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. I support operations like read, write, overwrite, clear.
"
Class {
#name : #CircularBuffer,
#superclass : #Object,
#instVars : [
'buffer',
'capacity'
],
#category : #'Exercise@CircularBuffer'
}

{ #category : #private }
CircularBuffer >> buffer [

^ buffer
]

{ #category : #private }
CircularBuffer >> buffer: anObject [

buffer := anObject
]

{ #category : #private }
CircularBuffer >> bufferFull [

^ self buffer size >= self capacity
]

{ #category : #accessing }
CircularBuffer >> capacity [

^ capacity
]

{ #category : #accessing }
CircularBuffer >> capacity: anObject [

capacity := anObject
]

{ #category : #operations }
CircularBuffer >> clear [

self buffer removeAll
]

{ #category : #initialization }
CircularBuffer >> initialize [

super initialize.
self buffer: OrderedCollection new
]

{ #category : #operations }
CircularBuffer >> overwrite: aValue [

self bufferFull ifTrue: [ self buffer removeFirst ].
^ self write: aValue
]

{ #category : #'error signalling' }
CircularBuffer >> raiseEmptyBufferOnRead [

^ Error signal: 'Cannot read from empty buffer!'
]

{ #category : #'error signalling' }
CircularBuffer >> raiseFullBufferOnWrite [

^ Error signal: 'Cannot write to full buffer!'
]

{ #category : #operations }
CircularBuffer >> read [

self buffer ifEmpty: [ ^ self raiseEmptyBufferOnRead ].
^ self buffer removeFirst

]

{ #category : #operations }
CircularBuffer >> write: aValue [

self bufferFull ifTrue: [ ^ self raiseFullBufferOnWrite ].
^ self buffer addLast: aValue.
]
Loading

0 comments on commit bf19afd

Please sign in to comment.