iT邦幫忙

2025 iThome 鐵人賽

DAY 24
0
Software Development

clojure 30 days系列 第 24

clojure 30 days - day 22

  • 分享至 

  • xImage
  •  

Problem Description

Your team is writing a fancy new text editor and you've been tasked with implementing the line numbering.

Write a function which takes a list of strings and returns each line prepended by the correct number.

The numbering starts at 1. The format is n: string. Notice the colon and space in between.

Examples: (Input --> Output)

[] --> []
["a", "b", "c"] --> ["1: a", "2: b", "3: c"]

Note

Implementation

; implement
(defn number [lines]
  (map-indexed (fn [idx line]
                 (str (inc idx) ": " line))
               lines))
; test
; execute implement function
(defn tester [arg exp]
  (= (number arg) exp))

; args & exception
(comment
  (tester [] [])
  (tester ["a" "b" "c"] ["1: a" "2: b" "3: c"])
  (tester ["" "" "" "" ""] ["1: " "2: " "3: " "4: " "5: "])
  (tester ["Hello" "World"] ["1: Hello" "2: World"])
  (tester ["single line"] ["1: single line"])


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

尚未有邦友留言

立即登入留言