SHARE
TWEET

Code golf - stuffing primes in a box

a guest Mar 25th, 2016 34 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Object subclass: #Insertion
  2.     instanceVariableNames: 'point isHorizontal numberOfOverlaps accidentalPrimes'
  3.     classVariableNames: ''
  4.     poolDictionaries: ''
  5.     category: 'Amos'!
  6.  
  7. !Insertion methodsFor: 'initialization' stamp: 'AMC 3/24/2016 15:29'!
  8. initialize
  9.  
  10.     super initialize.
  11.     numberOfOverlaps := 0.
  12.     accidentalPrimes := OrderedCollection new! !
  13.  
  14.  
  15. !Insertion methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:55'!
  16. point: aPoint
  17.  
  18.     point := aPoint! !
  19.  
  20. !Insertion methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:56'!
  21. numberOfOverlaps: anInteger
  22.  
  23.     numberOfOverlaps := anInteger! !
  24.  
  25. !Insertion methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:29'!
  26. addAccidentalPrime: aPrime
  27.  
  28.     accidentalPrimes add: aPrime! !
  29.  
  30. !Insertion methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:29'!
  31. accidentalPrimes
  32.  
  33.     ^accidentalPrimes! !
  34.  
  35. !Insertion methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:56'!
  36. numberOfOverlaps
  37.  
  38.     ^numberOfOverlaps! !
  39.  
  40. !Insertion methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:55'!
  41. point
  42.  
  43.     ^point! !
  44.  
  45. !Insertion methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:59'!
  46. increaseOverlaps
  47.  
  48.     numberOfOverlaps := numberOfOverlaps + 1! !
  49.  
  50. !Insertion methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:55'!
  51. isHorizontal
  52.  
  53.     ^isHorizontal! !
  54.  
  55. !Insertion methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:55'!
  56. isHorizontal: aBoolean
  57.  
  58.     isHorizontal := aBoolean! !
  59.  
  60.  
  61. !Insertion methodsFor: 'printing' stamp: 'AMC 3/24/2016 16:19'!
  62. printOn: aStream
  63.  
  64.     aStream
  65.         nextPutAll: 'Insertion at: ';
  66.         nextPutAll: point printString;
  67.         nextPut: Character space;
  68.         nextPut: (isHorizontal ifTrue: [$>] ifFalse: [$v]);
  69.         nextPutAll: ' {';
  70.         nextPutAll: (', ' join: accidentalPrimes);
  71.         nextPut: $}! !
  72.  
  73. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  74.  
  75. Insertion class
  76.     instanceVariableNames: ''!
  77.  
  78. !Insertion class methodsFor: 'instance creation' stamp: 'AMC 3/24/2016 15:58'!
  79. at: aPoint isHorizontal: aBoolean
  80.  
  81.     ^self new
  82.         point: aPoint;
  83.         isHorizontal: aBoolean;
  84.         yourself! !
  85.  
  86. Object subclass: #Stuffer
  87.     instanceVariableNames: 'box width height'
  88.     classVariableNames: 'primeLoadDictionary primes'
  89.     poolDictionaries: ''
  90.     category: 'Amos'!
  91.  
  92. !Stuffer methodsFor: 'initialization' stamp: 'AMC 3/24/2016 15:48'!
  93. initializeBox
  94.  
  95.     box := Box width: (width min: 5) height: (height min: 5)! !
  96.  
  97.  
  98. !Stuffer methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:49'!
  99. height: anInteger
  100.  
  101.     height := anInteger! !
  102.  
  103. !Stuffer methodsFor: 'accessing' stamp: 'AMC 3/24/2016 16:07'!
  104. box
  105.  
  106.     ^box! !
  107.  
  108. !Stuffer methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:49'!
  109. width: anInteger
  110.  
  111.     width := anInteger! !
  112.  
  113. !Stuffer methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:48'!
  114. width
  115.  
  116.     ^width! !
  117.  
  118. !Stuffer methodsFor: 'accessing' stamp: 'AMC 3/24/2016 16:07'!
  119. box: aBox
  120.  
  121.     box := aBox! !
  122.  
  123. !Stuffer methodsFor: 'accessing' stamp: 'AMC 3/24/2016 14:49'!
  124. height
  125.  
  126.     ^height! !
  127.  
  128.  
  129. !Stuffer methodsFor: 'testing' stamp: 'AMC 3/24/2016 14:50'!
  130. canGrow
  131.  
  132.     ^width > box width or: [height > box height]! !
  133.  
  134.  
  135. !Stuffer methodsFor: 'stuffing' stamp: 'AMC 3/24/2016 13:23'!
  136. insert: aString at: aPoint horizontalIf: isHorizontal
  137.  
  138.     | point offset |
  139.     point := aPoint.
  140.     offset := isHorizontal ifTrue: [box rightOffset] ifFalse: [box downOffset].
  141.     Transcript crShow: 'Inserting ', aString, ' at ', point printString.
  142.     aString do: [:eachChar |
  143.         box at: point x at: point y put: eachChar.
  144.         point := point + offset
  145.         ].
  146.     Transcript crShow: box! !
  147.  
  148. !Stuffer methodsFor: 'stuffing' stamp: 'AMC 3/24/2016 13:46'!
  149. stuffBox
  150.  
  151.     self stuffCurrentBox.
  152.     [self canGrow] whileTrue: [
  153.         box growMatrixByOneUpTo: width @ height.
  154.         self stuffCurrentBox
  155.         ]! !
  156.  
  157. !Stuffer methodsFor: 'stuffing' stamp: 'AMC 3/24/2016 13:47'!
  158. stuffCurrentBox
  159.  
  160.     self class primes reverseDo: [:eachPrime |
  161.         | string insertion |
  162.         (box primesUsed includes: eachPrime) ifFalse: [
  163.             string := eachPrime asString.
  164.             (insertion := box bestInsertionFor: string)
  165.                 ifNotNil: [
  166.                     Transcript crShow: insertion printString, ' of ', string.
  167.                     box doInsertion: insertion of: eachPrime.
  168.                     Transcript crShow: box
  169.                     ]
  170.                 ]
  171.             ].
  172.     box updatePrimesUsed.! !
  173.  
  174. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  175.  
  176. Stuffer class
  177.     instanceVariableNames: ''!
  178.  
  179. !Stuffer class methodsFor: 'primes' stamp: 'AMC 3/24/2016 16:32'!
  180. primeMaxLength
  181.     "self primeMaxLength"
  182.  
  183.     ^self primes last asString size! !
  184.  
  185. !Stuffer class methodsFor: 'primes' stamp: 'AMC 3/25/2016 18:11'!
  186. primeLoadDictionary
  187.     "self primeLoadDictionary"
  188.  
  189.     primeLoadDictionary ifNil: [
  190.         | sum |
  191.         primeLoadDictionary := IdentityDictionary new.
  192.         sum := 0.
  193.         self primes do: [:eachPrime |
  194.             sum := sum + eachPrime asString size.
  195.             primeLoadDictionary at: sum put: eachPrime
  196.             ]
  197.         ].
  198.     ^primeLoadDictionary! !
  199.  
  200. !Stuffer class methodsFor: 'primes' stamp: 'AMC 3/25/2016 18:05'!
  201. maxPrime
  202.  
  203.     ^20000! !
  204.  
  205. !Stuffer class methodsFor: 'primes' stamp: 'AMC 3/24/2016 14:46'!
  206. resetPrimes
  207.     "self resetPrimes"
  208.  
  209.     primes := nil.
  210.     primeLoadDictionary := nil! !
  211.  
  212. !Stuffer class methodsFor: 'primes' stamp: 'AMC 3/24/2016 14:54'!
  213. primes
  214.     "self primes"
  215.  
  216.     primes isNil ifTrue: [
  217.         primes := (Integer primesUpTo: self maxPrime) asOrderedCollection.
  218.         primes removeAll: #(2 3 5 7)
  219.         ].
  220.     ^primes! !
  221.  
  222.  
  223. !Stuffer class methodsFor: 'instance creation' stamp: 'AMC 3/24/2016 13:09'!
  224. forBox: aBox
  225.  
  226.     ^self new
  227.         box: aBox;
  228.         yourself! !
  229.  
  230. !Stuffer class methodsFor: 'instance creation' stamp: 'AMC 3/24/2016 13:54'!
  231. forDimensions: aPoint
  232.  
  233.     ^self new
  234.         width: aPoint x;
  235.         height: aPoint y;
  236.         initializeBox;
  237.         yourself! !
  238.  
  239.  
  240. Object subclass: #Box
  241.     instanceVariableNames: 'matrix primesUsed'
  242.     classVariableNames: ''
  243.     poolDictionaries: ''
  244.     category: 'Amos'!
  245.  
  246. !Box methodsFor: 'convenience' stamp: 'AMC 3/24/2016 14:32'!
  247. rightOffset
  248.  
  249.     ^1@0! !
  250.  
  251. !Box methodsFor: 'convenience' stamp: 'AMC 3/24/2016 14:18'!
  252. width
  253.  
  254.     ^matrix numberOfColumns! !
  255.  
  256. !Box methodsFor: 'convenience' stamp: 'AMC 3/24/2016 14:20'!
  257. pointsDo: aBlock
  258.  
  259.     1 to: self height do: [:eachY |
  260.         1 to: self width do: [:eachX |
  261.             aBlock value: eachX @ eachY
  262.             ]
  263.         ]! !
  264.  
  265. !Box methodsFor: 'convenience' stamp: 'AMC 3/24/2016 14:02'!
  266. offset: isHorizontal
  267.  
  268.     ^isHorizontal
  269.         ifTrue: [self rightOffset]
  270.         ifFalse: [self downOffset]! !
  271.  
  272. !Box methodsFor: 'convenience' stamp: 'AMC 3/24/2016 13:32'!
  273. downOffset
  274.  
  275.     ^0@1! !
  276.  
  277. !Box methodsFor: 'convenience' stamp: 'AMC 3/24/2016 13:17'!
  278. height
  279.  
  280.     ^matrix numberOfRows! !
  281.  
  282.  
  283. !Box methodsFor: 'initialization' stamp: 'AMC 3/24/2016 14:33'!
  284. initialize
  285.  
  286.     super initialize.
  287.     primesUsed := OrderedCollection new! !
  288.  
  289.  
  290. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:47'!
  291. matrix
  292.  
  293.     ^matrix! !
  294.  
  295. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 16:24'!
  296. primesUsed
  297.  
  298.     ^primesUsed! !
  299.  
  300. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 13:54'!
  301. from: aPoint rightBy: aNumber
  302.  
  303.     ^(matrix atRow: aPoint y)
  304.         copyFrom: aPoint x to: (aPoint x + aNumber min: self width)! !
  305.  
  306. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 16:25'!
  307. primesUsed: somePrimes
  308.  
  309.     primesUsed := somePrimes! !
  310.  
  311. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 13:30'!
  312. at: aRowIndex at: aColumnIndex
  313.  
  314.     ^self at: aColumnIndex @ aRowIndex! !
  315.  
  316. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 13:09'!
  317. primesInColumn: aColumnIndex
  318.  
  319.     ^self class primesIn: (matrix atColumn: aColumnIndex)! !
  320.  
  321. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 13:07'!
  322. primesInRow: aRowIndex
  323.  
  324.     ^self class primesIn: (matrix atRow: aRowIndex)! !
  325.  
  326. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 16:02'!
  327. at: aRowIndex at: aColumnIndex put: aCharacter
  328.  
  329.     ^matrix at: aRowIndex at: aColumnIndex put: aCharacter! !
  330.  
  331. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 15:47'!
  332. matrix: aMatrix
  333.  
  334.     matrix := aMatrix! !
  335.  
  336. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 13:42'!
  337. at: aPoint
  338.  
  339.     ^matrix at: aPoint y at: aPoint x ifInvalid: nil! !
  340.  
  341. !Box methodsFor: 'accessing' stamp: 'AMC 3/24/2016 13:53'!
  342. from: aPoint downBy: aNumber
  343.  
  344.     ^(matrix atColumn: aPoint x)
  345.         copyFrom: aPoint y to: (aPoint y + aNumber min: self height)! !
  346.  
  347.  
  348. !Box methodsFor: 'evaluating' stamp: 'AMC 3/24/2016 17:16'!
  349. updatePrimesUsed
  350.  
  351.     Transcript crShow: primesUsed size printString, ' old primes: ', (', ' join: primesUsed).
  352.     primesUsed := OrderedCollection new.
  353.     1 to: self height do: [:each |
  354.         primesUsed addAll: (self primesInRow: each)
  355.         ].
  356.     1 to: self width do: [:each |
  357.         primesUsed addAll: (self primesInColumn: each)
  358.         ].
  359.     primesUsed asBag sortedCounts first key > 1
  360.         ifTrue: [self error: 'Damn dirty duplicates!!'].
  361.     Transcript crShow: primesUsed size printString, ' new primes: ', (', ' join: primesUsed)! !
  362.  
  363. !Box methodsFor: 'evaluating' stamp: 'AMC 3/24/2016 15:05'!
  364. load
  365.  
  366.     ^((matrix size - (matrix occurrencesOf: Character space)) / matrix size) * 100! !
  367.  
  368.  
  369. !Box methodsFor: 'inserting' stamp: 'AMC 3/24/2016 12:59'!
  370. doInsertion: anInsertion of: aPrime
  371.  
  372.     self
  373.         insert: aPrime printString
  374.         at: anInsertion point
  375.         horizontalIf: anInsertion isHorizontal.
  376.     primesUsed
  377.         add: aPrime;
  378.         addAll: anInsertion accidentalPrimes! !
  379.  
  380. !Box methodsFor: 'inserting' stamp: 'AMC 3/24/2016 11:21'!
  381. growMatrixByOneUpTo: aDimension
  382.  
  383.     | smallMatrix rowOffset columnOffset |
  384.     smallMatrix := matrix copy.
  385.     matrix := Matrix
  386.         rows: (self height + 2 min: aDimension y)
  387.         columns: (self width + 2 min: aDimension x)
  388.         element: Character space.
  389.     rowOffset := (self height - smallMatrix numberOfRows) min: 1.
  390.     columnOffset := (self width - smallMatrix numberOfColumns) min: 1.
  391.     self copy: smallMatrix to: (1 + columnOffset) @ (1 + rowOffset)! !
  392.  
  393. !Box methodsFor: 'inserting' stamp: 'AMC 3/25/2016 17:46'!
  394. bestInsertionFor: aString
  395.  
  396.     | insertions |
  397.     insertions := OrderedCollection new.
  398.     self pointsDo: [:eachPoint |
  399.         insertions
  400.             addIfNotNil: (self insertionOf: aString at: eachPoint isHorizontal: true);
  401.             addIfNotNil: (self insertionOf: aString at: eachPoint isHorizontal: false)
  402.         ].
  403.     insertions removeAllSuchThat: [:each |
  404.         self isInsertion: each invalidWith: aString asInteger
  405.         ].
  406.     ^insertions detectMax: [:each |
  407.         (each accidentalPrimes size * 1) + (each numberOfOverlaps * 2)
  408.         ]! !
  409.  
  410. !Box methodsFor: 'inserting' stamp: 'AMC 3/24/2016 11:08'!
  411. copy: aMatrix to: aPoint
  412.  
  413.     matrix
  414.         atRows: aPoint y
  415.         to: aPoint y + aMatrix numberOfRows - 1
  416.         columns: aPoint x
  417.         to: aPoint x + aMatrix numberOfColumns - 1
  418.         put: aMatrix! !
  419.  
  420. !Box methodsFor: 'inserting' stamp: 'AMC 3/24/2016 12:51'!
  421. insertionOf: aString at: aPoint isHorizontal: isHorizontal
  422.  
  423.     | point insertion |
  424.     point := aPoint.
  425.     (self canFitEndsAt: point length: aString size isHorizontal: isHorizontal)
  426.         ifFalse: [^nil].
  427.     (self isAlreadyThere: aString at: aPoint isHorizontal: isHorizontal)
  428.         ifTrue: [^nil].
  429.     insertion := Insertion at: point isHorizontal: isHorizontal.
  430.     1 to: aString size do: [:eachIndex |
  431.         | eachCharacter |
  432.         eachCharacter := aString at: eachIndex.
  433.         (self canDoInsertion: insertion of: eachCharacter at: point)
  434.             ifTrue: [
  435.                 (self at: point) == eachCharacter
  436.                     ifTrue: [insertion increaseOverlaps].
  437.                 point := point + (self offset: isHorizontal)
  438.                 ]
  439.             ifFalse: [^nil]
  440.         ].
  441.     ^insertion! !
  442.  
  443. !Box methodsFor: 'inserting' stamp: 'AMC 3/24/2016 14:19'!
  444. insert: aString at: aPoint horizontalIf: isHorizontal
  445.  
  446.     | point offset |
  447.     point := aPoint.
  448.     offset := isHorizontal ifTrue: [self rightOffset] ifFalse: [self downOffset].
  449.     aString do: [:eachChar |
  450.         self at: point y at: point x put: eachChar.
  451.         point := point + offset
  452.         ]! !
  453.  
  454.  
  455. !Box methodsFor: 'testing' stamp: 'AMC 3/25/2016 16:11'!
  456. isInsertion: anInsertion invalidWith: aPrime
  457.  
  458.     | otherPrimes |
  459.     otherPrimes := anInsertion accidentalPrimes.
  460.     ^(primesUsed includesAny: otherPrimes)
  461.         or: [(otherPrimes includes: aPrime)
  462.         or: [otherPrimes copy removeDuplicates size < otherPrimes size]]! !
  463.  
  464. !Box methodsFor: 'testing' stamp: 'AMC 3/24/2016 15:39'!
  465. canFitEndsAt: aPoint length: aLength isHorizontal: isHorizontal
  466.  
  467.     | before after |
  468.     before := isHorizontal
  469.         ifTrue: [aPoint - self rightOffset]
  470.         ifFalse: [aPoint - self downOffset].
  471.     after := isHorizontal
  472.         ifTrue: [(aPoint x + aLength) @ aPoint y]
  473.         ifFalse: [aPoint x @ (aPoint y + aLength)].
  474.     ^(self isFreeOrBorder: before) and: [self isFreeOrBorder: after]! !
  475.  
  476. !Box methodsFor: 'testing' stamp: 'AMC 3/24/2016 14:49'!
  477. isFreeOrBorder: aPoint
  478.  
  479.     ^(self isBorder: aPoint)
  480.         or: [self isFreeAt: aPoint]! !
  481.  
  482. !Box methodsFor: 'testing' stamp: 'AMC 3/24/2016 16:24'!
  483. canDoInsertion: anInsertion of: aCharacter at: aPoint
  484.     "Answer whether anInsertion of aCharacter is valid at aPoint. If so,
  485.     also update anInsertion with any accidental insertions."
  486.  
  487.     | perpendicularSequence offset front back |
  488.     (self at: aPoint) == aCharacter
  489.         ifTrue: [^true].
  490.     (self isFreeAt: aPoint)
  491.         ifFalse: [^false].
  492.     perpendicularSequence := OrderedCollection with: aCharacter.
  493.     offset := self offset: anInsertion isHorizontal not.
  494.     front := aPoint - offset.
  495.     back := aPoint + offset.
  496.     [self isFreeOrBorder: front] whileFalse: [
  497.         perpendicularSequence addFirst: (self at: front).
  498.         front := front - offset
  499.         ].
  500.     [self isFreeOrBorder: back] whileFalse: [
  501.         perpendicularSequence addLast: (self at: back).
  502.         back := back + offset
  503.         ].
  504.     ^perpendicularSequence size == 1
  505.         or: [
  506.             | string number |
  507.             string := String withAll: perpendicularSequence.
  508.             number := string asInteger.
  509.             (string first ~~ $0 and: [number isPrime])
  510.                 ifTrue: [anInsertion addAccidentalPrime: number. true]
  511.                 ifFalse: [false]
  512.             ]! !
  513.  
  514. !Box methodsFor: 'testing' stamp: 'AMC 3/24/2016 14:50'!
  515. isBorder: aPoint
  516.  
  517.     ^(aPoint x == 0)
  518.         or: [aPoint x == (self width + 1)
  519.         or: [aPoint y == 0
  520.         or: [aPoint y == (self height + 1)]]]! !
  521.  
  522. !Box methodsFor: 'testing' stamp: 'AMC 3/25/2016 16:46'!
  523. isFreeAt: aPoint
  524.  
  525.     ^(self at: aPoint) == Character space! !
  526.  
  527. !Box methodsFor: 'testing' stamp: 'AMC 3/24/2016 16:08'!
  528. isAlreadyThere: aString at: aPoint isHorizontal: isHorizontal
  529.  
  530.     | currentString |
  531.     currentString := isHorizontal
  532.         ifTrue: [self from: aPoint rightBy: aString size]
  533.         ifFalse: [self from: aPoint downBy: aString size].
  534.     ^aString = currentString! !
  535.  
  536.  
  537. !Box methodsFor: 'printing' stamp: 'AMC 3/24/2016 14:46'!
  538. primesUsedPrintString
  539.  
  540.     ^primesUsed size printString, (' prime' asPluralBasedOn: primesUsed)! !
  541.  
  542. !Box methodsFor: 'printing' stamp: 'AMC 3/24/2016 14:45'!
  543. loadPrintString
  544.  
  545.     ^'load: ', self load asInteger printString, '%'! !
  546.  
  547. !Box methodsFor: 'printing' stamp: 'AMC 3/24/2016 14:44'!
  548. sizePrintString
  549.  
  550.     ^self height printString, 'x', self width printString! !
  551.  
  552. !Box methodsFor: 'printing' stamp: 'AMC 3/25/2016 17:18'!
  553. printOn: aStream
  554.  
  555.     aStream
  556.         nextPutAll: 'Box ';
  557.         nextPutAll: self sizePrintString;
  558.         nextPutAll: ' (';
  559.         nextPutAll: self primesUsedPrintString;
  560.         nextPutAll: ', ';
  561.         nextPutAll: self loadPrintString;
  562.         nextPutAll: '):';
  563.         nextPut: Character cr.
  564.     1 to: self height do: [ :eachY |
  565.         aStream nextPut: $|.
  566.         1 to: self width do: [ :eachX |
  567.             aStream nextPut: (self at: eachX @ eachY)
  568.             ].
  569.         aStream nextPut: $|.
  570.         aStream nextPut: Character cr
  571.         ]
  572. ! !
  573.  
  574. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  575.  
  576. Box class
  577.     instanceVariableNames: ''!
  578.  
  579. !Box class methodsFor: 'instance creation' stamp: 'AMC 3/24/2016 15:48'!
  580. width: aWidth height: aHeight
  581.  
  582.     | matrix |
  583.     matrix := Matrix rows: aHeight columns: aWidth element: Character space.
  584.     ^self newWith: matrix! !
  585.  
  586. !Box class methodsFor: 'instance creation' stamp: 'AMC 3/24/2016 15:47'!
  587. newWith: aMatrix
  588.  
  589.     ^self new
  590.         matrix: aMatrix;
  591.         yourself! !
  592.  
  593.  
  594. !Box class methodsFor: 'primes' stamp: 'AMC 3/24/2016 14:33'!
  595. primesIn: aString
  596.  
  597.     ^((aString splitOn: Character space)
  598.         collect: [:each | (String withAll: each) asInteger])
  599.             select: [:each |
  600.                 each notNil and: [
  601.                     (each > 9 and: [each isPrime not])
  602.                         ifTrue: [self error: 'Caught unexpected non-prime: ', each printString].
  603.                     each > 9
  604.                     ]
  605.                 ]! !
  606. 'From Pharo4.0 of 18 March 2013 [Latest update: #40626] on 25 March 2016 at 10:19:38.616703 pm'!
  607.  
  608. !Collection methodsFor: '*Amos' stamp: 'AMC 3/24/2016 14:52'!
  609. addIfNotNil: anElement
  610.  
  611.     anElement ifNotNil: [self add: anElement]! !
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top