challenge 正しい文(クイズ)

「この文は0が□個,1が□個,...,9が□個あります」
が正しくなるように□を埋めてください.数値は10進数とします.
一般のn(<=16で可)進数でも解いてみてください.

たとえば2進数なら
「この文は0が11個,1が100個あります」
となります.

Posted feedbacks - Smalltalk

Squeak Smalltalk で。適度にはしょった総当たりですが、にもかかわらず 9 より上は実用に耐えられず。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
| nn results range digits digitChars max |
nn := 8.
range := 1 to: nn.
digits := range asArray - 1.
digitChars := digits collect: [:each | each asString asCharacter].
max := (Number readFrom: '11' base: nn) + (nn = 2 ifTrue: [1] ifFalse: [0]).
results := OrderedCollection new.
(0 to: 3) asDigitsToPower: nn - 1 do: [:ary |
    (0 to: max) do: [:mm |
        | bag array |
        bag := Bag withAll: digitChars.
        array := {ary first. mm}, ary allButFirst.
        array do: [:each | bag addAll: (each radix: nn)].
        (range allSatisfy: [:idx | 
                (bag occurrencesOf: (digitChars at: idx)) = (array at: idx)])
            ifTrue: [results add: array copy]]].
^results

shinh さんの #4382 の爆速っぷりにいたく感動したので Squeak Smalltalk の直訳風に。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| x |
x := [:b :a :n :e :z |
    b = n ifTrue: [
        a = e ifTrue: [
            Transcript cr.
            (0 to: b - 1)
                do: [:i | Transcript show: (i radix: b), '*', ((a at: i + 1) radix: b)]
                separatedBy: [Transcript show: ', ']]
    ] ifFalse: [
        (1 to: (b < 5 ifTrue: [5] ifFalse: [
            n > 1 ifTrue: [(4 - n max: 0) + (e at: n + 1)] ifFalse: [99 min: b + 2 - z + n]]))
        do: [:i |
            | f |
            a at: n + 1 put: i.
            f := e copy.
            (i radix: b) do: [:char |
                f at: (Number readFrom: char asString base: b) + 1 incrementBy: 1].
            ((0 to: n) anySatisfy: [:j | (f at: j + 1) > (a at: j + 1)])
                ifFalse: [x copy fixTemps valueWithArguments: {b. a. n + 1. f. z + i}]
        ]
    ]
].

World findATranscript: nil.
2 to: 16 do: [:b |
    | zeros ones |
    Transcript cr; show: b.
    zeros := Array new: b withAll: 0.
    ones := Array new: b withAll: 1.
    x copy fixTemps valueWithArguments: {b. zeros. 0. ones. 0}]

Index

Feed

Other

Link

Pathtraq

loading...