Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Created January 4, 2021 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericnormand/18a6858ea77c0a306bc75e4b3d7c5ce6 to your computer and use it in GitHub Desktop.
Save ericnormand/18a6858ea77c0a306bc75e4b3d7c5ce6 to your computer and use it in GitHub Desktop.
409 PurelyFunctional.tv Newsletter

First before second

Write a function that takes a string and two letters. The function should return true if every instance of the first letter comes before every instance of the second letter.

Examples

(first-before? "A rabbit jumps joyfully" \a \j) ;=> true
(first-before? "A jolly rabbit jumps joyfully" \a \j) ;=> false
(first-before? "Don't tread on me" \t \m) ;=> true
(first-before? "Every React podcast" \t \d) ;=> false

Notes

  • Letters will be mixed case. You should treat \A the same as \a.
  • You can assume the strings will contain instances of both letters.

Thanks to this site for the challenge idea where it is considered Hard in Python.

Please submit your solutions as comments on this gist.

@chopmo
Copy link

chopmo commented Jan 16, 2021

(defn first-before? [s c1 c2]
  (let [regex           (re-pattern (str c1 \| c2))
        matches         (re-seq regex (str/lower-case s))
        [group1 group2] (partition-by identity matches)]
    (and (= (str c1) (first group1))
         (= (str c2) (first group2)))))

@chopmo
Copy link

chopmo commented Jan 16, 2021

@diavoletto76 Your solution is my favorite by far! I didn't even consider leaning on those clojure.string functions. Only nitpick is that you seem to have forgotten to make it case insensitive :)

@diavoletto76
Copy link

@chopmo You are right. Thank you for pointing the case sensitivity issue out.

@luissantos
Copy link

(defn first-before?
  [s a b]
  (let [ lc clojure.string/lower-case
        a-pos (filter (complement nil?) (map-indexed #(if (= (lc a) (lc %2)) %1 nil) s))
        b-pos (filter (complement nil?) (map-indexed #(if (= (lc b) (lc %2)) %1 nil) s))
        all (concat a-pos b-pos)]
    (= all (sort all))))

@diavoletto76 your solution is great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment