Created
June 24, 2021 14:42
-
-
Save seltzer1717/f4e230feab0c1837e1d688a3d74543d6 to your computer and use it in GitHub Desktop.
Coding Interview - https://www.youtube.com/watch?v=rw4s4M3hFfs&t=932s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns cloud.seltzer1717.s3.apartment-test | |
(:require [clojure.test :as test] | |
[cloud.seltzer1717.s3.apartment :as apt])) | |
(test/with-test | |
(def sample-blocks | |
[{:gym false :school true :store false} | |
{:gym true :school false :store false} | |
{:gym true :school true :store false} | |
{:gym false :school true :store false} | |
{:gym false :school true :store true}]) | |
(def sample-requirements | |
[:gym :school :store]) | |
(test/is (= 3 (apt/distance 0 3))) | |
(test/is (= 3 (apt/distance 3 0))) | |
(test/is (= [1 2] (apt/indexes-by-type sample-blocks :gym))) | |
(test/is (= [0 2 3 4] (apt/indexes-by-type sample-blocks :school))) | |
(test/is (= 1 (apt/min-distance-by-app-ind-type sample-blocks 0 :gym))) | |
(test/is (= 1 (apt/min-distance-by-app-ind-type sample-blocks 1 :school))) | |
(test/is (= 4 (apt/min-distance-by-app-ind-type sample-blocks 0 :store))) | |
(test/is (= 4 (apt/max-distance-by-app-ind sample-blocks 0 sample-requirements))) | |
(test/is (= 3 (apt/max-distance-by-app-ind sample-blocks 1 sample-requirements))) | |
(test/is (= 1 (apt/max-distance-by-app-ind sample-blocks 3 sample-requirements))) | |
(test/is (= 2 (apt/max-distance-by-app-ind sample-blocks 4 sample-requirements))) | |
(test/is (= [3 1] (apt/best-apartment sample-blocks sample-requirements))) | |
(test/is (= [{:gym false :school true :store false :winner false} | |
{:gym true :school false :store false :winner false} | |
{:gym true :school true :store false :winner false} | |
{:gym false :school true :store false :winner true} | |
{:gym false :school true :store true :winner false}] | |
(apt/show-best sample-blocks sample-requirements))) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns cloud.seltzer1717.apartment) | |
(defn distance | |
[apt-ind blk-ind] | |
(Math/abs (- blk-ind apt-ind))) | |
(defn indexes-by-type | |
[blocks type] | |
(->> blocks | |
(map-indexed (fn [ind block] (if (type block) ind nil))) | |
(filter #(not (nil? %))) | |
vec)) | |
(defn min-distance-by-app-ind-type | |
[blocks apt-ind type] | |
(->> type | |
(indexes-by-type blocks) | |
(map (partial distance apt-ind)) | |
(apply min))) | |
(defn max-distance-by-app-ind | |
[blocks apt-ind requirements] | |
(apply max | |
(map (partial min-distance-by-app-ind-type blocks apt-ind) | |
requirements))) | |
(defn best-apartment | |
[blocks requirements] | |
(reduce (fn [[bind bmaxmin :as best] [ind maxmin :as current]] (if (> bmaxmin maxmin) current best)) | |
(map-indexed (fn [ind block] | |
[ind (max-distance-by-app-ind blocks ind requirements)]) | |
blocks))) | |
(defn show-best | |
[blocks requirements] | |
(let [[bind bmaxmin] (best-apartment blocks requirements) | |
blk-cnt (count blocks)] | |
(loop [output blocks | |
ind 0] | |
(if (<= 0 ind (dec blk-cnt)) | |
(let [winner (= ind bind)] | |
(recur (update-in output [ind :winner] (constantly winner)) | |
(inc ind))) | |
output)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment