Skip to content

Commit

Permalink
Refactor is-pangram function for improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-rurik committed Jul 21, 2024
1 parent 81b7f8d commit 8281564
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions otus-02/src/otus_02/homework/pangram.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
(ns otus-02.homework.pangram
(:require [clojure.string :as string]
[clojure.set :as set]))

(def alphabet-set (set "abcdefghijklmnopqrstuvwxyz"))
(ns otus-02.homework.pangram)

(defn is-pangram [^String input]
(let [normalized-chars (-> input
string/lower-case
(string/replace #"[^a-z]" "")
set)]
(set/subset? alphabet-set normalized-chars)))

(loop [chars input
found-letters #{}]
(if (or (empty? chars) (= 26 (count found-letters)))

This comment has been minimized.

Copy link
@Ivana-

Ivana- Jul 23, 2024

(or (= 26 (count found-letters)) ..... without duplicates :)

(= 26 (count found-letters))
(let [current-char (Character/toLowerCase (first chars))]
(if (and (>= (int current-char) (int \a))
(<= (int current-char) (int \z)))

This comment has been minimized.

Copy link
@Ivana-

Ivana- Jul 23, 2024

Clojure is lisp, so (<= (int \a) (int current-char) (int \z)) or maybe even without casts to int, if <= works on charachters

(recur (rest chars) (conj found-letters current-char))
(recur (rest chars) found-letters))))))

This comment has been minimized.

Copy link
@Ivana-

Ivana- Jul 23, 2024

Yep, that was the goal!

0 comments on commit 8281564

Please sign in to comment.