Created
February 12, 2017 14:59
-
-
Save shintakezou/e899d2fecf382a9efbcb6fd043c76afd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"This solves an Italian magazine's quiz, n. 939; | |
you need an implementation for permutationsDo, | |
see miscellanea/gnusmalltalk/permutationsDo.st." | |
|p1 p2 p3 names perm0 bl1 bl2 bl3| | |
p1 := #(2 1 4 3 5). | |
p2 := #(3 4 5 1 2). | |
p3 := #(1 5 3 2 4). | |
names := #('Aldo' 'Berto' 'Carlo' 'Dario' 'Enzo'). | |
perm0 := #(1 2 3 4 5). | |
bl1 := [ :a| | |
((a at: 3) > (a at: 2)) & | |
((a at: 3) < (a at: 5)) ]. | |
bl2 := [ :a| | |
((a at: 1) < (a at: 3)) & | |
((a at: 1) < (a at: 2)) ]. | |
bl3 := [ :a| | |
((a at: 4) < (a at: 3)) & | |
(((a at: 1) > (a at: 2)) | | |
((a at: 1) > (a at: 3)) | | |
((a at: 1) > (a at: 4)) | | |
((a at: 1) > (a at: 5))) | |
]. | |
"wraps our constraints check" | |
OrderedCollection extend [ | |
controlWith: ruleBlock [ | |
^ ruleBlock value: self | |
] | |
]. | |
"I've implemented permutationsDo for OrderedCollection | |
rather than for SequenceableCollection, hence | |
we need to convert the Array into an OrderedCollection." | |
perm0 asOrderedCollection permutationsDo: [:p| | |
|pass1 pass2 pass3 assignedNames| | |
assignedNames := p collect: [:i| names at: i]. | |
pass1 := (p collect: [:i| p1 at: i]) controlWith: bl1. | |
pass2 := (p collect: [:i| p2 at: i]) controlWith: bl2. | |
pass3 := (p collect: [:i| p3 at: i]) controlWith: bl3. | |
pass1 & pass2 & pass3 | |
ifTrue: [ | |
assignedNames displayNl. | |
]. | |
]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment