与えた条件を満たす候補
Posted feedbacks - Smalltalk
Squeak Smalltalk で。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | | input selStream |
input := #(and or not and).
input replaceAll: #and with: #&.
input replaceAll: #or with: #|.
selStream := input readStream.
World findATranscript: nil.
{true. false} asDigitsToPower: 4 do: [:xs |
selStream reset.
(xs allButFirst inject: xs first into: [:result :each |
| selector |
selector := selStream next.
selStream peek = #not ifTrue: [each := each perform: selStream next].
result perform: selector with: each]) ifTrue: [Transcript cr; show: xs]]
"=> #(true true true true)
#(true true false true)
#(true false false true)
#(false true false true)
#(false false false true) "
|
#(not not not) などに対応できる版を Squeak Smalltalk で。
1 2 3 4 5 6 7 8 9 10 | | input numArgs |
input := #(& | not &).
numArgs := (input collect: [:sel | sel numArgs]) sum + 1.
World findATranscript: nil.
{true. false} asDigitsToPower: numArgs do: [:xs |
| xsStream expression |
xsStream := xs readStream.
expression := input inject: xsStream next printString into: [:expStr :sel |
expStr, ' ', sel, (sel == #not ifFalse: [xsStream next printString] ifTrue: [''])].
(Compiler evaluate: expression) ifTrue: [Transcript cr; show: xs]]
|




にしお
#3399()
Rating0/0=0.00
元ネタの 充足可能性問題 - Wikipedia は、 同じリテラル(x1とかnot x2とか)が複数回出てくることを想定しているので、 今回の問題のようにそれぞれ別の変数でだと乗法標準形 - Wikipediaにした場合に、答えが…と色々悩みどころでした。
[ reply ]