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"]
map-indexed
: applies function to each element with its index
inc
: increments index by 1 (0-based to 1-based)str
: concatenates strings to create formatted output; 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"])