Skip to content

Instantly share code, notes, and snippets.

@yokolet
Created August 29, 2019 04:03
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 yokolet/efad0031c483bcc77ea9ada37e708db2 to your computer and use it in GitHub Desktop.
Save yokolet/efad0031c483bcc77ea9ada37e708db2 to your computer and use it in GitHub Desktop.
;; A solution and test code for https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem
;; test/thirty-days-code.core_test.clj
(ns thirty-days-code.core-test
(:require [clojure.java.io :as io]
[clojure.test :refer :all]))
(defn wrap-test [n f]
(with-open [rdr (io/reader (io/resource n))]
(binding [*in* rdr]
(f))))
;; test/thirty_days_code.phone_number_test.clj
(ns thirty-days-code.phone-number-test
(:require [clojure.test :refer :all]
[thirty-days-code.core-test :as core-test]
[thirty-days-code.phone-number :refer :all]))
(defn phone-number-test-fixture [f]
(core-test/wrap-test "test-data/phone-number.txt" f))
(use-fixtures :once phone-number-test-fixture)
(def expected "sam=99912222\nNot found\nharry=12299933\n")
(deftest phone-number-test
(testing "given a name should find a phone number"
(let [result (with-out-str (phone-number))]
(is (= expected result)))))
;; solution
;; src/thirty_days_code.phone_number.clj
(ns thirty-days-code.phone-number)
(defn phone-number []
(let [n (Integer/parseInt (clojure.string/trim (read-line)))
book (reduce (fn [acc line]
(let [l (clojure.string/trim line)
[k v] (clojure.string/split l #"\s+")]
(assoc acc k (str k "=" v))))
{}
(repeatedly n #(read-line)))]
(doseq [q (line-seq (java.io.BufferedReader. *in*))]
(println (get book q "Not found")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment