Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Created March 22, 2021 14:58
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/f0df145ef62eacb0d7becd1b6d5e3d28 to your computer and use it in GitHub Desktop.
Save ericnormand/f0df145ef62eacb0d7becd1b6d5e3d28 to your computer and use it in GitHub Desktop.
419 PurleyFunctional.tv Newsletter

String difference

You are given two strings, a and b. We consider each letter to be unique, meaning duplicates are significant. Write a function that returns the count of letters in b which do not occur in a.

Examples

(strdiff "abc" "") ;=> {} ;; no characters in b don't occur in a
(strdiff "abc" "abc") ;=> {} ;; ditto
(strdiff "" "abc") ;=> {\a 1 \b 1 \c 1}
(strdiff "axx" "abcc") ;=> {\b 1 \c 2}
(strdiff "xxxx" "xxxxxx") ;=> {\a 2} ;; two x's in b that don't occur in a

Thanks to this site for the challenge idea where it is considered Hard in Python. The problem has been modified from the original.

Please submit your solutions as comments on this gist.

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

@vmpj
Copy link

vmpj commented Mar 26, 2021

(defn strdiff [a b]
  (let [fa (frequencies a) 
        fb (frequencies b)]
    (->> (reduce-kv #(update %1 %2 (fnil - 0) %3) fb fa)
         (filter #(> (second %) 0)) 
         (into {}))))

@prairie-guy
Copy link

prairie-guy commented Apr 2, 2021

(use '[clojure.set :only (difference intersection)])

(defn strdiff [a b]                                                                                                                                   
  (let [fa (frequencies a)                                                                                                                            
        fb (frequencies b)                                                                                                                            
        ka (set (keys fa))                                                                                                                            
        kb (set (keys fb))]                                                                                                                           
    (apply array-map 
     (flatten                                                                                                                         
      (append(for [i (intersection ka kb)                                                                                                             
                   :let [n (- (fb i) (fa i))]                                                                                                         
                   :when (> n  0)]                                                                                                                    
               [i n])                                                                                                                                 
             (for [d (difference kb ka)] [d (fb d)]))))))

@prairie-guy
Copy link

Basic question: How do I get highlighted code? First time posting . . .

@steffan-westcott
Copy link

@prairie-guy To get syntax highlighting, wrap your code between ```clojure and ``` on their own lines. See the Github docs on syntax highlighting for more examples.

@prairie-guy
Copy link

prairie-guy commented Apr 2, 2021

Thanks.

@sim-f
Copy link

sim-f commented Apr 5, 2021

(defn update-freq-map [m k v]
  (if (get m k)
    (update m k #(- % v))
    m))

(defn dissoc-less-one [m]
  (let [less-one-key (->> m
                          (filter (fn [[_ v]] (< v 1)))
                          (map first))]
    (reduce
     (fn [m k] (dissoc m k))
     m
     less-one-key)))

(defn strdiff [a b]
  (let [freq-a (frequencies a)
        freq-b (frequencies b)]
    (->> (reduce-kv
          update-freq-map
          freq-b
          freq-a)
         dissoc-less-one)))

@s3dse
Copy link

s3dse commented Apr 9, 2021

(defn strdiff [a b]
  (loop [freq-b (frequencies b)
         acc {}]
    (cond
      (or (= a b) (empty? freq-b)) acc
      (empty? a) freq-b
      :else
      (let [[k vb] (first freq-b)
            va ((frequencies a) k 0)
            diff (- vb va)]
        (if (pos-int? diff)
          (recur (rest freq-b) (assoc acc k diff))
          (recur (rest freq-b) acc))))))

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