Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

words with duplicate letters

Some words have duplicate letters ("book"—two o's—or "that"—two t's). Many don't.

Your task is to write a predicate that returns true if the string contains any words with duplicate letters.

Examples:

(duplicate-letter? "Hello, my friend!") => true (because "hello" has two l's)

(duplicate-letter? "Hey friend!") => false (because no words have duplicates)

Have fun with this one! Try out different ways to implement it.

Thanks to to this site for the challenge idea!

(defn duplicate-letter? [s]
(true?
(some (comp (partial < 1)
last
sort
vals
frequencies)
(str/split s #" "))))
(defn duplicate-letter? [s]
(let [words (clojure.string/split s #" ")
letter-freq (map #(vals (frequencies %)) words)
duplicates? (map (fn [xs]
(some #(> % 1) xs))
letter-freq)]
(some true? duplicates?)))
(defn duplicate-letter? [s]
(if (re-find #"\W" s)
(boolean (some duplicate-letter? (clojure.string/split s #"\W+")))
(not= (count s) (count (set (clojure.string/lower-case s))))))
(defn duplicate-letter? [s]
(not-every? #(= (count (set %)) (count %)) (clojure.string/split s #" ")))
;; case insensitive. treats all characters as 'letters'
(defn wordhasdup [s]
(->> s
clojure.string/lower-case
frequencies
vals
(apply max)
(< 1)))
(defn duplicate-letter? [s]
(->> (clojure.string/split s #" ")
(map wordhasdup)
(some true?)
true?))
(duplicate-letter? "Fortune favours the bold.") ;; false
(duplicate-letter? "You can lead a horse to water, but you can't make him drink.") ;; false
(duplicate-letter? "Look before you leap.") ;; true
(duplicate-letter? "An apple a day keeps the doctor away.") ;; true
;; clojure.string required as :str
(defn duplicate-letter?-1 [s]
(->> (str/split s #" ")
(map #(not (apply distinct? %))) ;=> true for all words with duplicates
(some true?)))
(defn duplicate-letter?-2 [s]
(->> (str/split s #" ")
(map #(not= (count %) (count (set %))))
(some true?)))
(defn duplicate-letter?-3 [s]
(->> (clojure.string/split s #" ")
(mapcat (comp vals frequencies))
(some #(< 1 %))))
(defn duplicate-letter?
[s]
(->> (re-seq #"\w+" s)
(map frequencies)
(map vals)
(apply concat)
(some (partial not= 1))
boolean))
;;;
;;; Some words have duplicate letters ("book"—two o's—or "that"—two t's)
;;;
;;; Write a predicate that returns true if the string contains any words
;;; with duplicate letters.
;;;
;;; ??? Should upper and lower case of same letter count? (I'll say yes)
;;; ??? Should repeated-digits like "abc111" count? (I'll say no)
;;; ??? Should repeated-punctuation, etc. count? (I'll say no)
;;;
(defn reused-letter? [wd]
(let [letters-in-wd (re-seq #"[a-z]" (clojure.string/lower-case wd))]
(not= (count letters-in-wd)
(count (set letters-in-wd)))))
(defn duplicate-letter? [s]
(let [words #(str/split % #"\s+")]
(some reused-letter? (words s))))
(duplicate-letter? "Hello, my friend!") => true
(duplicate-letter? "Hey friend!") => nil
(duplicate-letter? "Huh?") => true
(duplicate-letter? "yow bar baz abc111") => nil
(duplicate-letter? "bar ba%%z") => nil
(defn duplicate-letter?
[s]
(boolean
(some->> (partition 2 1 s)
(filter (partial apply =))
not-empty)))
(defn duplicate-letter?
[s]
(boolean
(->> (map = s (rest s))
(drop-while false?)
first)))
(defn duplicate-letter? [s]
(let [alph? #(or (and (> % 64) (< % 95)) (and (> % 96) (< % 123)))]
(>=
(first (sort > (vals (filter #(alph? (-> % key int)) (frequencies s)))))
2)))
(ns duplicate-letters-challenge)
(defn- has-duplicate-letter? [word]
(< (count (distinct word)) (count word)))
(defn duplicate-letter? [s]
(let [words (clojure.string/split s #"\W")]
(boolean (some has-duplicate-letter? words))))
(defn duplicate-letter? [s]
(not (reduce
(fn [acc c] (if (acc c)
(reduced false)
(conj acc c)))
#{}
s)))
(ns eu.teod.scratch2.newsletter.pftv-359
(:require [clojure.string :as string]))
(let [a (int \a)
z (int \z)
A (int \A)
Z (int \Z)]
(defn ascii-lowercase? [ch]
(let [c (int ch)]
(or (<= A c Z)
(<= a c z)))))
(defn words [sentence]
(for [word-with-punctuation (string/split sentence #"\s")]
(apply str (filter ascii-lowercase? word-with-punctuation))))
(defn word-duplicate-letter? [word]
(not= (count word)
(count (set word))))
(defn duplicate-letter? [sentence]
(reduce #(or %1 %2)
false
(map word-duplicate-letter? (words sentence))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(for [case ["Hello, my friend!" "Hey, friend!"]]
[case (duplicate-letter? case)])
;; => (["Hello, my friend!" true] ["Hey, friend!" false])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.