'From Squeak3.5 of ''11 April 2003'' [latest update: #5180] on 20 May 2003 at 10:29:52 pm'! Object subclass: #BrickRow instanceVariableNames: 'paintedNumbers shifted ' classVariableNames: '' poolDictionaries: '' category: 'Practice-NumberPaintedBrick'! !BrickRow commentStamp: 'sumim 5/20/2003 21:25' prior: 0! I am a collection of bricks that are Arrays. All bricks are painted two numbers on its left and right side, respectively. You can access number or brick. If you have PrologModoki and its change set using me, you can evaluate such like expressions below. " | n ns | ns _ OrderedCollection new. n _ PrologModoki var new. PrologModoki paintedNumberLeft: n right: 3 and: [ns add: n copy]. ^ ns "" | row | row _ BrickRow newFrom: {1. 2. 3. 4. 5. 6} shifted: true. PrologModoki brickRow: row and: [^ true] "" | x xs row y z | xs _ OrderedCollection new. x _ PrologModoki var new. y _ x copy. z _ x copy. row _ BrickRow newFrom: {x. y. z. 4. 5. 6} shifted: true. PrologModoki brickRow: row and: [xs add: {x value. y value. z value}]. ^ xs "" | n rows aCollection answers | answers _ OrderedCollection new. rows _ OrderedCollection new. n _ PrologModoki var new. aCollection _ { {1. 2. 3. 4. 5. 6}. {2. 3. 4. 5. 6. 1}. {3. 4. 5. 6. 1. 2}. {4. 5. 6. 1. 2. 3}. {n copy. n copy. n copy. n copy. n copy. n copy}. {n copy. n copy. n copy. n copy. n copy. n copy}}. aCollection doWithIndex: [ :each :idx | rows add: (BrickRow newFrom: each shifted: idx odd)]. PrologModoki brickRows: rows n: 1 and: [answers add: rows deepCopy asArray]. answers "" | n rows aCollection | rows _ OrderedCollection new. n _ PrologModoki var new. aCollection _ { {6. n copy. 5. n copy. n copy. n copy}. {n copy. 6. n copy. n copy. n copy. n copy}. {4. n copy. n copy. n copy. n copy. n copy}. {n copy. n copy. n copy. n copy. n copy. 1}. {n copy. n copy. n copy. n copy. 1. n copy}. {n copy. n copy. n copy. 5. n copy. n copy}}. aCollection doWithIndex: [ :each :idx | rows add: (BrickRow newFrom: each shifted: idx odd)]. PrologModoki brickRows: rows n: 1 and: [^ true]. ^ rows "" | n rows aCollection answers | answers _ OrderedCollection new. rows _ OrderedCollection new. n _ PrologModoki var new. aCollection _ { {6. n copy. 5. n copy. n copy. n copy}. {n copy. 6. n copy. n copy. n copy. n copy}. {4. n copy. n copy. n copy. n copy. n copy}. {n copy. n copy. n copy. 4. n copy. 1}. {n copy. n copy. n copy. n copy. 1. n copy}. {n copy. n copy. n copy. 5. n copy. n copy}}. aCollection doWithIndex: [ :each :idx | rows add: (BrickRow newFrom: each shifted: idx odd)]. PrologModoki brickRows: rows n: 1 and: [answers add: rows deepCopy asArray]. ^ answers "! !BrickRow methodsFor: 'accessing' stamp: 'sumim 5/20/2003 15:30'! at: index ^ paintedNumbers at: index! ! !BrickRow methodsFor: 'accessing' stamp: 'sumim 5/20/2003 15:01'! beShifted shifted _ true! ! !BrickRow methodsFor: 'accessing' stamp: 'sumim 5/20/2003 15:32'! brickAt: index ^ self bricks at: index! ! !BrickRow methodsFor: 'accessing' stamp: 'sumim 5/20/2003 15:32'! bricks | aCollection | aCollection _ paintedNumbers asOrderedCollection. self isShifted ifTrue: [ aCollection add: aCollection first. aCollection removeFirst]. ^ aCollection pairsCollect: [ :a :b | {a. b}]! ! !BrickRow methodsFor: 'accessing' stamp: 'sumim 5/20/2003 14:50'! isShifted ^ shifted ifNil: [shifted _ false]! ! !BrickRow methodsFor: 'printing' stamp: 'sumim 5/20/2003 17:28'! printOn: aStream | bricks toIndex | aStream nextPutAll: '#('. self isShifted ifTrue: [ paintedNumbers first printOn: aStream. aStream space]. bricks _ self bricks. toIndex _ shifted ifTrue: [bricks size - 1] ifFalse: [bricks size]. 1 to: toIndex do: [ :idx | (bricks at: idx) printOn: aStream. idx = toIndex ifFalse: [aStream space]]. shifted ifTrue: [ aStream space. paintedNumbers last printOn: aStream]. aStream nextPutAll: ')'! ! !BrickRow methodsFor: 'private' stamp: 'sumim 5/20/2003 15:29'! paintedNumbers: aCollection paintedNumbers _ aCollection! ! !BrickRow methodsFor: 'converting' stamp: 'sumim 5/20/2003 22:20'! asArray ^ paintedNumbers asArray! ! !BrickRow class methodsFor: 'instance creation' stamp: 'sumim 5/20/2003 15:37'! newFrom: aCollection | aBrickRow | aBrickRow _ self new. aBrickRow paintedNumbers: aCollection. ^ aBrickRow! ! !BrickRow class methodsFor: 'instance creation' stamp: 'sumim 5/20/2003 15:38'! newFrom: aCollection shifted: aBoolean | aBrickRow | aBrickRow _ self newFrom: aCollection. aBoolean ifTrue: [aBrickRow beShifted]. ^ aBrickRow! ! !BrickRow class methodsFor: 'instance creation' stamp: 'sumim 5/20/2003 15:55'! newFromBricks: pairCollection | aCollection | aCollection _ OrderedCollection new. pairCollection do: [ :each | each do: [ :itm | aCollection add: itm]]. ^ self newFrom: aCollection! ! !PrologModoki class methodsFor: 'number painted brick' stamp: 'sumim 5/20/2003 22:19'! brickRow: aBrickRow and: aBlock | bricks | bricks _ aBrickRow bricks. bricks isEmpty and: [aBlock value. ^ false]. self paintedNumberLeft: bricks first first right: bricks first last and: [ self brickRow: (BrickRow newFromBricks: bricks allButFirst) and: [ | anArray | anArray _ aBrickRow asArray. [(anArray count: [ :each | (anArray occurrencesOf: each) = 1]) = anArray size] value and: [aBlock value]]]. ^ false! ! !PrologModoki class methodsFor: 'number painted brick' stamp: 'sumim 5/20/2003 22:29'! brickRow: row in: rows at: n and: aBlock "Transcript cr; show: n; show: ((1 to: n) collect: [ :idx | (rows at: idx) asArray]) asArray printString." [row asArray doWithIndex: [ :each :idx | 1 to: n - 1 do: [ :r | ((rows at: r) at: idx) - each = 0 ifTrue: [^ false]]]. true] value and: [aBlock value]. ^ false! ! !PrologModoki class methodsFor: 'number painted brick' stamp: 'sumim 5/20/2003 20:31'! brickRows: rows n: n and: aBlock | row | n > rows size and: [aBlock value. ^ false]. row _ rows at: n. self brickRow: row and: [ self brickRow: row in: rows at: n and: [ self brickRows: rows n: n + 1 and: [aBlock value]]]. ^ false! ! !PrologModoki class methodsFor: 'number painted brick' stamp: 'sumim 5/20/2003 15:10'! paintedNumberLeft: leftNumber right: rightNumber and: aBlock self paintedNumberPair: leftNumber with: rightNumber and: [aBlock value]. self paintedNumberPair: rightNumber with: leftNumber and: [aBlock value]. ^ false! ! !PrologModoki class methodsFor: 'number painted brick' stamp: 'sumim 5/20/2003 15:08'! paintedNumberPair: numberA with: numberB and: aBlock | vars continuation savedVars | vars _ self vars: {numberA. numberB}. savedVars _ vars copy. continuation _ [aBlock value. vars failed: savedVars]. vars = #(1 2) and: continuation. vars = #(1 4) and: continuation. vars = #(1 6) and: continuation. vars = #(2 3) and: continuation. vars = #(2 5) and: continuation. vars = #(3 4) and: continuation. vars = #(3 6) and: continuation. vars = #(4 5) and: continuation. vars = #(5 6) and: continuation. ^ false! ! !PrologModokiVariable methodsFor: 'arithmetic' stamp: 'sumim 5/20/2003 21:16'! - aNumber ^ value - aNumber! !