iT邦幫忙

2025 iThome 鐵人賽

DAY 8
0
Software Development

clojure 30 days系列 第 8

clojure 30 days - day 6

  • 分享至 

  • xImage
  •  

Problem Description

You ask a small girl,"How old are you?" She always says, "x years old", where
x is a random number between 0 and 9.

Write a program that returns the girl's age (0-9) as an integer.

Assume the test input string is always a valid string. For example, the test
input may be "1 year old" or "5 years old". The first character in the string
is always a number.

Note

  • keywords:
    • let
    • first (return java.lang.Character)
    • Integer/parseInt (convert str to int)

Implementation

(defn how-old [old-string]
  (let [old (str (first old-string))]
    (Integer/parseInt old)))

; test
(defn tester [old exp]
  (= (how-old old) exp))

(comment
 (tester "1 year old" 1)
 (tester "6 years old" 6))

Otehr Answers

  • keywords: -> (thread-first)
(defn how-old
  [her-old]
  (-> her-old
      first
      str
      Integer/parseInt))


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

尚未有邦友留言

立即登入留言