iT邦幫忙

2025 iThome 鐵人賽

DAY 15
0
Software Development

clojure 30 days系列 第 15

clojure 30 days - day 13

  • 分享至 

  • xImage
  •  

Problem Description

Simple, given a string of words, return the length of the shortest word(s).

String will never be empty and you do not need to account for different data types.

Note

  • clojure.string/split -> It splits a string into substrings, based on a regular expression (regex).

    • #"" -> In Clojure, #"" defines a regular expression literal.
    • \s -> any whitespace character (space, tab, newline, etc.)
    • + -> “one or more” of the previous element.

Implementation

; implement
(defn find_shortest [words]
  (->> (clojure.string/split words #"\s+")
       (map count)
       sort
       first))

; test
; execute implement function
(defn tester [arg exp]
  (= (find_shortest arg) exp))

; args & exception
(comment
  (tester "bitcoin take over the world maybe who knows perhaps" 3)
  (tester "turns out random test cases are easier than writing out basic ones" 3)
  (tester "lets talk about javascript the best language" 3)
  (tester "i want to travel the world writing code one day" 1)
  (tester "Lets all go on holiday somewhere very cold" 2))



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

尚未有邦友留言

立即登入留言