『コードが書けないプログラマ志願者を見分ける手法』(Wikipedia)として知られているFizzBuzz問題だが,こういう問題を見ると,いろんな言語でどれだけ短くかつシンプルに書けるか考えたくなるのが世の常なので(偏見),あらためて自分で考えてみた.
#Schemeの場合
素直に書くと,こんな感じか.Gaucheで確認.
(define FizzBuzz
(lambda (n)
(let loop ((x 1) (r '()))
(cond ((> x n) (reverse r))
((and (zero? (modulo x 3)) (zero? (modulo x 5)))
(loop (+ x 1) (cons 'FizzBuzz r)))
((zero? (modulo x 3))
(loop (+ x 1) (cons 'Fizz r)))
((zero? (modulo x 5))
(loop (+ x 1) (cons 'Buzz r)))
(else
(loop (+ x 1) (cons x r)))))))
(display (FizzBuzz 100))
いや,素直すぎるだろ,ということで,書き直してみた.
(define (ch x)
(define (p? n) (zero? (modulo x n)))
(cond ((p? 15) 'FizzBuzz) ((p? 3) 'Fizz) ((p? 5) 'Buzz) (else x)))
(display (map ch (cdr (iota 101))))
今度は尖り過ぎたか….
#Pythonの場合
まずは素直に.
def FizzBuzz(n):
for x in range(1, n+1):
if x % 3 == 0 and x % 5 == 0:
print('FizzBuzz')
elif x % 3 == 0:
print('Fizz')
elif x % 5 == 0:
print('Buzz')
else:
print(x)
FizzBuzz(100)
これを,Schemeの場合のノリで書き直してみる.
def ch(x):
def p(n):
return (x % n == 0)
return ('FizzBuzz' if p(15) else 'Fizz' if p(3) else 'Buzz' if p(5) else x)
print(list(map(ch, range(1,101))))
よし.と思ったら,リスト内包表記使えば一行で済むのか….【追記】リスト内包表記でなくても一行で済むのか….真偽値を0/1とするのはPythonでもよくあることらしい.
#Prologの場合
SWI-PrologとGNU Prologで確認.betweenとfailの組合せは邪道かなあ.
fb(X,'FizzBuzz') :- X mod 15 =:= 0, !.
fb(X,'Fizz') :- X mod 3 =:= 0, !.
fb(X,'Buzz') :- X mod 5 =:= 0, !.
fb(X,X).
fizzbuzz(N) :- between(1,N,X), fb(X,R), write(R), write(' '), fail.
% ?- fizzbuzz(100).
#Cの場合
とりあえず素直バージョン.
#include <stdio.h>
int main(void)
{
for (int n = 1; n <= 100; n++)
if (n % 3 == 0 && n % 5 == 0) printf("FizzBuzz ");
else if (n % 3 == 0) printf("Fizz ");
else if (n % 5 == 0) printf("Buzz ");
else printf("%d ", n);
return (0);
}
三項演算子を用いて書き換えたもの.演算子としての使い方じゃないけど,少し短くなる.
#include <stdio.h>
int main(void)
{
for (int n = 1; n <= 100; n++)
n % 3 == 0 && n % 5 == 0 ? printf("FizzBuzz ") :
n % 3 == 0 ? printf("Fizz ") :
n % 5 == 0 ? printf("Buzz ") :
printf("%d ", n);
return (0);
}
#考察
1~100の数列を生成して各要素に処理を適用したり,複数の判定式を一行で書けたりすると,短くてシンプルなコードにできそうな感じ.
#備考
##increment, decrement, negative-flagのみを用いた場合
や,なんとなく.無駄が多いことは承知.FizzBuzz記述はこの記事から流用.
;;;; increment, decrement, negative-flag
(define inc (lambda (x) (+ x 1)))
(define dec (lambda (x) (+ x -1)))
(define nn? negative?)
;;;;
(define den (lambda (c1) (cond (c1 #f) (else #t))))
(define coj (lambda (c1 c2) (cond (c1 c2) (else #f))))
(define dij (lambda (c1 c2) (cond (c1 #t) (else c2))))
(define np? (lambda (x) (coj (den (nn? x)) (den (nn? (dec x))))))
(define nz? (lambda (x) (coj (den (np? x)) (den (nn? x)))))
(define add
(lambda (x y)
(cond ((np? y) (add (inc x) (dec y)))
((nn? y) (add (dec x) (inc y)))
(else x))))
(define sub
(lambda (x y)
(cond ((np? y) (sub (dec x) (dec y)))
((nn? y) (sub (inc x) (inc y)))
(else x))))
(define mul-iter
(lambda (x y r)
(cond ((nz? y) r)
(else (mul-iter x (dec y) (add r x))))))
(define mul
(lambda (x y)
(cond ((np? y) (mul-iter x y 0))
((np? x) (mul-iter y x 0))
(else (mul-iter (sub 0 x) (sub 0 y) 0)))))
(define div-iter
(lambda (x y r)
(cond ((nn? x) (dec r)) ((nz? x) r)
(else (div-iter (sub x y) y (inc r))))))
(define div
(lambda (x y)
(cond ((coj (np? x) (np? y)) (div-iter x y 0))
((coj (nn? x) (np? y)) (mul -1 (div-iter (sub 0 x) y 0)))
((coj (np? x) (nn? y)) (mul -1 (div-iter x (sub 0 y) 0)))
(else (div-iter (sub 0 x) (sub 0 y) 0)))))
(define mdl (lambda (x y) (sub x (mul(div x y) y)))) ; positive only
(define ne? (lambda (x y) (nz? (sub x y))))
;;;; FizzBuzz
(define d (lambda (x n) (ne? (mdl x n) 0)))
(define p display)
(define c
(lambda (x)
(cond ((d x 15) (p "FizzBuzz "))
((d x 3) (p "Fizz "))
((d x 5) (p "Buzz "))
(else (p x) (p " ")))))
(define fb
(lambda (x)
(cond ((ne? x 0))
(else (fb (dec x))
(c x)))))
(fb 100)
; => 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
##変更履歴
- 2020-08-09:Pythonの場合に追記
- 2020-07-21:おまけ(inc-dec-nflag.scm)追加
- 2020-07-17:初版公開,Prolog全面書き直し