patternMinor
Clojure solution to HackerRank Challenge 'Restaurant' under Number Theory
Viewed 0 times
numberclojurerestaurantchallengetheoryunderhackerranksolution
Problem
My main concern is how messy the
This is my first ever Clojure program, so I know this is probably not idiomatic Clojure, nor is it neat, nor probably fast, either. Please give as much feedback as you would like on my code (especially regarding stuff that's bad about it).
The challenge is, given a list of 2D dimensions for pieces of bread, find the minimum number of perfect squares you could cut out of each piece without wasting any bread.
https://www.hackerrank.com/challenges/restaurant
min-number-of-slices function is. I would appreciate help finding good ways to clean that up since it has a lot of nesting/scope.This is my first ever Clojure program, so I know this is probably not idiomatic Clojure, nor is it neat, nor probably fast, either. Please give as much feedback as you would like on my code (especially regarding stuff that's bad about it).
The challenge is, given a list of 2D dimensions for pieces of bread, find the minimum number of perfect squares you could cut out of each piece without wasting any bread.
https://www.hackerrank.com/challenges/restaurant
(ns clojure-solution.core)
(require '[clojure.string :as string])
(defn parse-int [s]
(Integer. s))
(defn parse-line [s]
(map parse-int (string/split s #" ")))
(defn get-loaf-dimensions []
(let [numLoaves (parse-int (read-line))]
(map #(%1) (repeat numLoaves (comp parse-line read-line)))))
(def squares (map #(* % %) (range 1 1001)))
(defn min-number-of-slices [dims]
(let [l (first dims)
b (second dims)
area (* l b)]
(letfn [(perfect-slice-dimension? [slice-area]
(= 0 (+ (mod area slice-area)
(mod b (int (Math/sqrt slice-area)))
(mod l (int (Math/sqrt slice-area))))))]
(let [largest-square (last (filter perfect-slice-dimension? (take (min l b) squares)))]
(int (/ area largest-square))))))
(defn -main [& args] (dorun (map println (map min-number-of-slices (get-loaf-dimensions)))))Solution
You've implemented a brute-force solution, trying every square dimension from 1 up to the minimum of
The optimal square size is simply the greatest common divisor of
Calculating the minimum number of slices follows easily.
You can try calling
l, b, and 1000.The optimal square size is simply the greatest common divisor of
l and b. A very simple algorithm to calculate the GCD is the Euclidean algorithm.(defn gcd [a b]
(if (= b 0)
a
(gcd b (mod a b))))Calculating the minimum number of slices follows easily.
(defn min-number-of-slices [l b]
(let [side (gcd l b)]
(/ (* l b) (* side side))))You can try calling
min-number-of-slices in this ClojureScript demo:.code { font-family: Monaco, monospace; font-size: 10pt; } #log { height: 10em; overflow: auto; white-space: pre-wrap; } #input { width: 100%; height: 12em; }
goog.require('webrepl');
(defn gcd [a b]
(if (= b 0)
a
(gcd b (mod a b))))
(defn min-number-of-slices [l b]
(let [side (gcd l b)]
(/ ( l b) ( side side))))
(min-number-of-slices 6 9)
Code Snippets
(defn gcd [a b]
(if (= b 0)
a
(gcd b (mod a b))))(defn min-number-of-slices [l b]
(let [side (gcd l b)]
(/ (* l b) (* side side))))Context
StackExchange Code Review Q#71843, answer score: 3
Revisions (0)
No revisions yet.