iT邦幫忙

2025 iThome 鐵人賽

DAY 14
0
Software Development

clojure 30 days系列 第 14

clojure 30 days - day 12

  • 分享至 

  • xImage
  •  

Problem Description

Task
King Arthur and his knights are having a New Years party. Last year Lancelot was jealous of Arthur,
because Arthur had a date and Lancelot did not, and they started a duel.

To prevent this from happening again, Arthur wants to make sure that there are at least as many women
as men at this year's party. He gave you a list of integers of all the party goers.

Arthur needs you to return true if he needs to invite more women or false if he is all set.

Input/Output
[input] integer array L ($a in PHP)
An array (guaranteed non-associative in PHP) representing the genders of the attendees, where -1 represents women and 1 represents men.

2 <= L.length <= 50

[output] a boolean value

true if Arthur need to invite more women, false otherwise.

Implementation

; implement
(defn invite-more-women [xs]
  (pos? (reduce + xs)))

; (defn invite-more-women [xs]
;   (let [men   (count (filter (fn [x] (= 1  x)) xs))
;         women (count (filter (fn [x] (= -1 x)) xs))]
;     (> men women)))

; test
; execute implement function
(defn tester [arg exp]
  (= (invite-more-women arg) exp))

; args & exception
(comment
  (tester [1 -1 1] true)
  (tester [-1 -1 -1] false)
  (tester [1 -1] false)
  (tester [1 1 1] true))


上一篇
clojure 30 days - day 11
下一篇
clojure 30 days - day 13
系列文
clojure 30 days17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言