Browse Source

Reverse strings problem and solution

master
Julio Biason 4 years ago
parent
commit
ca1510f324
  1. 1
      clojure/reverse-string/.exercism/metadata.json
  2. 17
      clojure/reverse-string/README.md
  3. 4
      clojure/reverse-string/project.clj
  4. 21
      clojure/reverse-string/src/reverse_string.clj
  5. 29
      clojure/reverse-string/test/reverse_string_test.clj

1
clojure/reverse-string/.exercism/metadata.json

@ -0,0 +1 @@
{"track":"clojure","exercise":"reverse-string","id":"1d0e36bea1fa400a9423fa17ba1a04fe","url":"https://exercism.io/my/solutions/1d0e36bea1fa400a9423fa17ba1a04fe","handle":"JBiason","is_requester":true,"auto_approve":false}

17
clojure/reverse-string/README.md

@ -0,0 +1,17 @@
# Reverse String
Reverse a string
For example:
input: "cool"
output: "looc"
## Restrictions
You can not use the `reverse` function, but you can use any of the other core functions.
## Source
Introductory challenge to reverse an input string [https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb](https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

4
clojure/reverse-string/project.clj

@ -0,0 +1,4 @@
(defproject reverse-string "0.1.0-SNAPSHOT"
:description "reverse-string exercise."
:url "https://github.com/exercism/clojure/tree/master/exercises/reverse-string"
:dependencies [[org.clojure/clojure "1.10.0"]])

21
clojure/reverse-string/src/reverse_string.clj

@ -0,0 +1,21 @@
(ns reverse-string
; (:require [clojure.string :as str])
)
; This is the easy/cheating way
; (defn reverse-string [s] ;; <- arglist goes here
; (str/reverse s)
; )
; This is the real way :p
(defn reverse-string [s]
(loop [content s
reversed ""]
(let [[head & tails] content]
(if (= tails nil)
(str head reversed)
(recur tails (str head reversed))
)
)
)
)

29
clojure/reverse-string/test/reverse_string_test.clj

@ -0,0 +1,29 @@
(ns reverse-string-test
(:require [clojure.test :refer [deftest is]]
reverse-string))
(deftest empty-string-test
(is (= "" (reverse-string/reverse-string ""))))
(deftest a-letter-test
(is (= "I" (reverse-string/reverse-string "I"))))
(deftest a-word-test
(is (= "tobor" (reverse-string/reverse-string "robot"))))
(deftest capitalised-word-test
(is (= "nemaR" (reverse-string/reverse-string "Ramen"))))
(deftest sentence-with-punctuation-test
(is (= "!yrgnuh m'I" (reverse-string/reverse-string "I'm hungry!"))))
(deftest palindrome-test
(is (= "racecar" (reverse-string/reverse-string "racecar"))))
(deftest even-sized-word-test
(is (= "reward" (reverse-string/reverse-string "drawer"))))
(deftest long-string-test
(let [s (reduce str (repeat 1000 "overflow?"))
rs (reduce str (repeat 1000 "?wolfrevo"))]
(is (= rs (reverse-string/reverse-string s)))))
Loading…
Cancel
Save