iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 7
0
自我挑戰組

各種筆記系列 第 7

[Kata] Clojure - Day 7

  • 分享至 

  • xImage
  •  

Beginner Series - Clock

Clock shows 'h' hours, 'm' minutes and 's' seconds after midnight.
Your task is to make 'Past' function which returns time converted to milliseconds.

將 hours, minutes, seconds 轉成 milliseconds

(ns kata.test
  (:require [clojure.test :refer :all]
            [kata         :refer [past]]))
(defn tester [h m s e]
  (testing (str "(past " h " " m " " s ")")
    (is (= (past h m s) e))))
(deftest basic-tests
  (tester 0 1 1 61000)
  (tester 1 1 1 3661000)
  (tester 0 0 0 0)
  (tester 1 0 1 3601000)
  (tester 1 0 0 3600000))

Solution

(ns kata)
(defn past [h m s]
  (* 1000 (+ (* h 3600) (* m 60) s)))

Expressions Matter

Given three integers a ,b ,c, return the largest number obtained after inserting the following operators and brackets: +, , ()
In other words , try every combination of a,b,c with [
+()] , and return the Maximum Obtained

給予三個數字 a, b, c,使用 *, +, () 各種組合,找出結果最大的組合

(ns expression.matter-test
  (:require [expression.matter :refer :all]
            [clojure.test :refer :all]))

(deftest basic-tests
  (testing "(expression-matter 2 1 2)" (is (= (expression-matter 2 1 2) 6)))
  (testing "(expression-matter 2 1 1)" (is (= (expression-matter 2 1 1) 4)))
  (testing "(expression-matter 1 1 1)" (is (= (expression-matter 1 1 1) 3)))
  (testing "(expression-matter 1 2 3)" (is (= (expression-matter 1 2 3) 9)))
  (testing "(expression-matter 1 3 1)" (is (= (expression-matter 1 3 1) 5)))
  (testing "(expression-matter 2 2 2)" (is (= (expression-matter 2 2 2) 8)))
  (testing "(expression-matter 5 1 3)" (is (= (expression-matter 5 1 3) 20)))
  (testing "(expression-matter 3 5 7)" (is (= (expression-matter 3 5 7) 105)))
  (testing "(expression-matter 5 6 1)" (is (= (expression-matter 5 6 1) 35)))
  (testing "(expression-matter 1 6 1)" (is (= (expression-matter 1 6 1) 8)))
  (testing "(expression-matter 2 6 1)" (is (= (expression-matter 2 6 1) 14)))
  (testing "(expression-matter 6 7 1)" (is (= (expression-matter 6 7 1) 48)))
  (testing "(expression-matter 2 10 3)" (is (= (expression-matter 2 10 3) 60)))
  (testing "(expression-matter 1 8 3)" (is (= (expression-matter 1 8 3) 27)))
  (testing "(expression-matter 9 7 2)" (is (= (expression-matter 9 7 2) 126)))
  (testing "(expression-matter 1 1 10)" (is (= (expression-matter 1 1 10) 20)))
  (testing "(expression-matter 9 1 1)" (is (= (expression-matter 9 1 1) 18)))
  (testing "(expression-matter 10 5 6)" (is (= (expression-matter 10 5 6) 300)))
  (testing "(expression-matter 1 10 1)" (is (= (expression-matter 1 10 1) 12))))

Solution

(ns expression.matter)

(defn expression-matter [a b c]
  (max
    (* (+ b c) a)
    (* (* a b) c)
    (+ (* b c) a)
    (* (+ a b) c)
    (+ (+ a b) c)
   )
)

上一篇
[Kata] Clojure - Day 6
下一篇
[Kata] Clojure - Day 8
系列文
各種筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言