練習 Clojure 基本語法,邊涮邊品嚐,理解它 data 的美味(?)
【學習】
(= (mod base factor) 0)
本身就已經會回傳 true / false,真的不用再搞笑多包一層 if 判斷zero?
的 function【今日湯底】
This function should test if the factor
is a factor of base
.
Return true
if it is a factor or false
if it is not.
測試 factor
是否為 base
的因子(ex. 2 是 6 的因子,4 就不是 6 的因子)
(必須通過以下測試)
(ns kata.check-for-factor.test
(:require [kata.check-for-factor :refer :all]
[clojure.test :refer :all]))
(defn tester [base factor exp]
(testing (str "(check-for-factor " base " " factor ")")
(is (= (check-for-factor base factor) exp))))
(deftest basic-tests
(tester 10 2 true)
(tester 63 7 true)
(tester 2450 5 true)
(tester 24612 3 true)
(tester 9 2 false)
(tester 653 7 false)
(tester 2453 5 false)
(tester 24617 3 false))
【我的答案】
(ns kata.check-for-factor)
(defn check-for-factor [base factor]
(if (= (mod base factor) 0) true false)
)
思路:
每個因數一定都是能被整除的,所以可以用 mod 處理(其實題目也有大方的提示這點XD)
【其他人的答案】
(ns kata.check-for-factor)
(defn check-for-factor [base factor]
(= (mod base factor) 0)
)
(ns kata.check-for-factor)
(defn check-for-factor [base factor]
(zero? (mod base factor))
)