Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
460 PurelyFunctional.tv Newsletter

NOTE: If you're looking for issue 461 (Primes in a number), please go here: https://gist.github.com/ericnormand/d52d1fe0e80f06e4b517d45a082765a0

Rearrange sentence

Here's a neat, yet contrived, text processing problem. The words in your sentence have been mixed up. Luckily, there's a number embedded in each word that says its position in the sentence. Write a function that puts the words in the right order and removes the position digits.

Examples

(rearrange "World2! He1llo,") ;=> "Hello, World!"
(rearrange "fo3r 5more Elegan1t 2weapons age.7 civil6ized a4") ;=> "Elegant weapons for a more civilized age."
(rearrange "") ;=> ""

Thanks to this site for the problem idea, where it is rated Very Hard in Java. The problem has been modified.

Please submit your solutions as comments on this gist.

To subscribe: https://purelyfunctional.tv/newsletter/

@safehammad
Copy link

safehammad commented Feb 2, 2022

Part of me thinks the final str/replace could be more elegant, but it's a start:

(require '[clojure.string :as str])

(defn rearrange [s]
  (->> (str/split s #" ")
       (sort-by (partial re-find #"\d+"))
       (str/join " ")
       (#(str/replace % #"\d+" ""))))

@jonasseglare
Copy link

jonasseglare commented Feb 2, 2022

(defn rearrange [words]
  (->> (clojure.string/split words #"\s+")
       (map #(mapv (group-by (fn [c] (Character/isDigit c)) %) [true false]))
       (sort-by (comp read-string #(apply str %) first))
       (map #(apply str (second %)))
       (clojure.string/join " ")))

@nbardiuk
Copy link

nbardiuk commented Feb 2, 2022

(require '[clojure.string :as string])

(defn rearrange [sentence]
  (letfn [(over-words [s f] (->> (string/split s #" ") f (string/join " ")))
          (first-long [s] (some->> s (re-find #"\d+") (Long/parseLong)))
          (remove-numbers [s] (string/replace s #"\d+" ""))]
    (-> sentence
        (over-words #(sort-by first-long %))
        remove-numbers)))

@mchampine
Copy link

mchampine commented Feb 2, 2022

(defn rearrange [words]
  (let [digs #{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9}]
    (->> (sort-by #(some digs %) (s/split words #" "))
         (map #(apply str (remove digs %)))
         (s/join " "))))

@filippocostalli
Copy link

filippocostalli commented Feb 3, 2022

(require '[clojure.string :as str])

(defn rearrange [s]
  (->> (str/split s #" ")
       (map (fn [x] {:position (str/replace x #"[^\d]" "") :word (str/replace x #"[\d]" "")}))
       (sort-by :position)
       (map :word)
       (str/join " ")))

@miner
Copy link

miner commented Feb 5, 2022

;; assuming single digit per word
(defn rearrange [sentence]
  (->> (str/split sentence #" ")
       (reduce (fn [sm word] (assoc sm (re-find  #"\d" word) (str/replace word #"\d" "")))
               (sorted-map))
       vals
       (str/join " ")))

;; allow multiple consecutive digits
(defn rearrange2 [sentence]
  (->> (str/split sentence #" ")
       (reduce (fn [sm word]
                 (assoc sm
                        (Long/parseLong (or (re-find  #"\d+" word) "0"))
                        (str/replace word #"\d+" "")))
               (sorted-map))
       vals
       (str/join " ")))

@KingCode
Copy link

KingCode commented May 27, 2022

(def re #"([^\d]*)(\d+)([^\d]*)")

(defn n+word [word]
  (let [[pfx number sfx] (->> word (re-matches re) rest)]
    [(Integer/parseInt number) (str pfx sfx)]))

(defn rearrange [sentence]
  (let [order->w  (->> (clojure.string/split sentence #"\s+")
                       (into (sorted-map) 
                             (comp
                              (filter seq)
                              (map #(n+word %)))))]
    (->> order->w vals (interpose " ") (apply str))))

(rearrange "World2! He1llo,") ;=> "Hello, World!"
(rearrange "fo3r 5more Elegan1t 2weapons age.7 civil6ized a4")
;=> "Elegant weapons for a more civilized age."
(rearrange "") ;=> ""

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