【今日湯底】
My grandfather always predicted how old people would get, and right before he passed away he revealed his secret!
In honor of my grandfather's memory we will write a function using his formula!
Take a list of ages when each of your great-grandparent died.
Multiply each number by itself.
Add them all together.
Take the square root of the result.
Divide by two.
Example
predictAge(65, 60, 75, 55, 60, 63, 64, 45) === 86
Note: the result should be rounded down to the nearest integer.
Some random tests might fail due to a bug in the JavaScript implementation. Simply resubmit if that happens to you.
(必須通過以下測試)
(ns predicter-test
(:require [clojure.test :refer :all]
[predicter :refer :all]))
(deftest predit-age-test1
(is (= (predict-age 65 60 75 55 60 63 64 45) 86)))
(deftest predit-age-test2
(is (= (predict-age 32 54 76 65 34 63 64 45) 79)))
【我的答案】
(ns predicter)
(defn predict-age [& age]
(->> age
(map #(* % %))
(reduce +)
(Math/sqrt)
(* 0.5)
(Math/floor)
(int))
)
思路:
【其他人的答案】
(ns predicter)
(defn predict-age [& ages]
(->> (map #(* % %) ages)
(reduce +)
(Math/sqrt)
(#(/ % 2))
int))