“10分でコーディング”を Squeak Smalltalk で


時間は 10分どころか、かなりたっぷり^^;かけてしまいましたが、べつだんトリッキーなこともせずに、ギリギリつぶやくことができる程度の長さのが書けました。w

#() as: String が空文字列を返してくれたおかげで、カードが足りないときの処理を入れずに済んだのがステキで気に入っています。

| deal |
deal := [:n :cs |
   | gs |
   gs := cs groupsOf: n atATimeCollect: [:g | g].
   (1 to: n) collect: [:i | (gs collect: [:g | g at: i]) as: String]].

#( (3 '123123123') ('111' '222' '333')
   (4 '123123123') ('12' '23' '31' '12')
   (6 '012345012345012345') ('000' '111' '222' '333' '444' '555')
   (4 '111122223333') ('123' '123' '123' '123')
   (1 '012345012345012345') ('012345012345012345')
   (6 '01234') ('' '' '' '' '' '')
   (2 '') ('' '')
) pairsCollect: [:args :shouldBe |
   | result |
   self assert: (result := deal valueWithArguments: args) = shouldBe. result]


"=> #(
   #('111' '222' '333')
   #('12' '23' '31' '12')
   #('000' '111' '222' '333' '444' '555')
   #('123' '123' '123' '123')
   #('012345012345012345')
   #('' '' '' '' '' '')
   #('' '')) "