From 34c250bacdfc6630633488cdd0fa25132cbe03ab Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Fri, 21 Aug 2020 15:51:04 -0700 Subject: rename source files in c74 Clojure filenames must use underscores; dashes are not allowed. Add a symlink so that the expected convention (using dashes) leads people to the right place. --- .../tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj | 21 +------------ .../tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj | 34 +--------------------- .../tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj | 20 +++++++++++++ .../tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj | 33 +++++++++++++++++++++ 4 files changed, 55 insertions(+), 53 deletions(-) mode change 100644 => 120000 challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj mode change 100644 => 120000 challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj create mode 100644 challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj create mode 100644 challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj deleted file mode 100644 index 416d2a1d85..0000000000 --- a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj +++ /dev/null @@ -1,20 +0,0 @@ -(ns tw.weekly.ch-1) - -(defn majority - "Calculate the majority element, which is the element in a list that appears more than floor(size_of_list/2), prefering the least significant digit that matches that criteria. Returns -1 if no such element is found." - [coll] - (let [minimum (Math/floor (/ (count coll) 2)) - maj (->> coll - frequencies - (filter (comp #(> % minimum) second)) - (sort second) - ffirst)] - (or maj -1))) - -(defn -main - "Run Task 1 with a list of integers, defaulting to the sample given in the task description." - [& args] - (let [N (if (not-empty args) - (map clojure.edn/read-string args) - [2, 2, 2, 3, 2, 4, 2])] - (println (majority N)))) diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj new file mode 120000 index 0000000000..924a7a086e --- /dev/null +++ b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj @@ -0,0 +1 @@ +ch_1.clj \ No newline at end of file diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj deleted file mode 100644 index 299ab63690..0000000000 --- a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj +++ /dev/null @@ -1,33 +0,0 @@ -(ns tw.weekly.ch-2 - (:require [clojure.string :as str]) - (:require [flatland.ordered.map :as fo])) - -(defn ordered-frequencies - "A modification of clojure.core/frequencies that guarantees the map returns keys in the order seen in the collection. Thus it returns an ordered map from distinct items in coll to the number of times they appear." - [coll] - (persistent! - (reduce (fn [counts x] - (assoc! counts x (inc (get counts x 0)))) - (transient (fo/ordered-map)) coll))) - -(defn find-fnr [string] - "Find the first non-repeating character (FNR) for a string." - (let [counts (ordered-frequencies string) - non-repeaters (filter (comp #(= 1 %) second) counts)] - (-> non-repeaters last first))) - -(defn find-fnr-over-string - "Generate a string comprised of the first non-repeating character (FNR) for successive substrings of a given string." - [string] - (->> string - (reductions str "") - (drop 1) - (map find-fnr) - (map #(or % \#)) - str/join)) - -(defn -main - "Run Task 2 with a string, defaulting to the sample given in the task description." - [& args] - (let [S (or (-> args first) "ababc")] - (println (find-fnr-over-string S)))) diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj new file mode 120000 index 0000000000..5a32e17ef9 --- /dev/null +++ b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj @@ -0,0 +1 @@ +ch_2.clj \ No newline at end of file diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj new file mode 100644 index 0000000000..416d2a1d85 --- /dev/null +++ b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj @@ -0,0 +1,20 @@ +(ns tw.weekly.ch-1) + +(defn majority + "Calculate the majority element, which is the element in a list that appears more than floor(size_of_list/2), prefering the least significant digit that matches that criteria. Returns -1 if no such element is found." + [coll] + (let [minimum (Math/floor (/ (count coll) 2)) + maj (->> coll + frequencies + (filter (comp #(> % minimum) second)) + (sort second) + ffirst)] + (or maj -1))) + +(defn -main + "Run Task 1 with a list of integers, defaulting to the sample given in the task description." + [& args] + (let [N (if (not-empty args) + (map clojure.edn/read-string args) + [2, 2, 2, 3, 2, 4, 2])] + (println (majority N)))) diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj new file mode 100644 index 0000000000..299ab63690 --- /dev/null +++ b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj @@ -0,0 +1,33 @@ +(ns tw.weekly.ch-2 + (:require [clojure.string :as str]) + (:require [flatland.ordered.map :as fo])) + +(defn ordered-frequencies + "A modification of clojure.core/frequencies that guarantees the map returns keys in the order seen in the collection. Thus it returns an ordered map from distinct items in coll to the number of times they appear." + [coll] + (persistent! + (reduce (fn [counts x] + (assoc! counts x (inc (get counts x 0)))) + (transient (fo/ordered-map)) coll))) + +(defn find-fnr [string] + "Find the first non-repeating character (FNR) for a string." + (let [counts (ordered-frequencies string) + non-repeaters (filter (comp #(= 1 %) second) counts)] + (-> non-repeaters last first))) + +(defn find-fnr-over-string + "Generate a string comprised of the first non-repeating character (FNR) for successive substrings of a given string." + [string] + (->> string + (reductions str "") + (drop 1) + (map find-fnr) + (map #(or % \#)) + str/join)) + +(defn -main + "Run Task 2 with a string, defaulting to the sample given in the task description." + [& args] + (let [S (or (-> args first) "ababc")] + (println (find-fnr-over-string S)))) -- cgit