code-reviews

zendevil 2021-05-17T08:39:18.045700Z

https://prit.substack.com/p/hackerrank-flipping-bits

eckardjf 2021-05-17T17:17:08.045900Z

(defn flippingBits [n]
  (let [bs (doto (java.util.BitSet/valueOf (long-array 1 n))
             (.flip 0 32))]
    (if (.isEmpty bs) 0 (aget (.toLongArray bs) 0))))

2021-05-17T17:21:24.046500Z

we don't really use pascalCase but other than having too many expressions on one line at the end that looks fine

2021-05-17T17:21:36.046800Z

I always put the condition of an if and each of the branches on their own line, so minimum three lines if the else arm is used

eckardjf 2021-05-17T17:25:58.048700Z

Thanks, I agree but the hackerrank stubs supply the odd camelCase names 🙂

2021-05-17T17:28:01.049Z

oops right, that's camelCase not PascalCase

2021-05-17T17:29:13.050200Z

also, style wise I'd use (:import (java.util BitSet)) in the ns decl, instead of using the full class name in the code (both do work)

seancorfield 2021-05-17T17:29:34.051100Z

Strictly headlessCamelCase. Otherwise it’s CamelCase.

2021-05-17T17:29:36.051200Z

or if you can't have a custom ns decl, just (import (java.util BitSet)) at the top level before the function definition

2021-05-17T17:31:01.051600Z

@seancorfield that's a new one to me, not mentioned in the wiki article https://en.wikipedia.org/wiki/Camel_case

seancorfield 2021-05-17T17:43:08.052400Z

Interesting that it’s mentioned here https://simple.wikipedia.org/wiki/CamelCase but not on the main wiki.

2021-05-17T17:48:45.052800Z

(defn generate-pin [length] 
  "generate number string, with leading zeros, of specified length"
  (let [r (rand-int (Math/pow 10 length))
        z (- length (count (str r)))]
	(str (clojure.string/join (repeat z "0")) r)))

2021-05-17T17:49:15.052900Z

is there a better way to do this? cl-format maybe?

2021-05-17T19:41:41.053100Z

user=> (format "%010d" (rand-int 10000))
"0000007057"

2021-05-17T19:41:56.053300Z

regular format handles it fine

2021-05-17T19:47:18.053500Z

more complete example

user=> (defn generate-pin
  [length]
  (let [r (rand-int (Math/pow 10 length))
        fmt (str "%0" length "d")]
    (format fmt r)))
#'user/generate-pin
user=> (generate-pin 8)
"00321275"

đź‘Ť 1
2021-05-17T19:47:54.053700Z

also be careful providing arbitrary args to rand-int, you can easily generate an arg that is too large for Integer and error

2021-05-17T19:48:15.053900Z

user=> (rand-int (Math/pow 10 10))
Execution error (IllegalArgumentException) at user/eval186 (REPL:1).
Value out of range for int: 4440081733

2021-05-17T20:01:43.054100Z

possible implementation for rand-long

(cmd)user=> (defn rand-long
  [n]
  (let [bytes-0-4 (rand-int (bit-and (long n) Integer/MAX_VALUE))
        bytes-5-8 (if (< n Integer/MAX_VALUE)
                      0
                      (rand-int (bit-shift-right (long n) 32)))]
    (+ bytes-0-4
       (bit-shift-left bytes-5-8 32))))
#'user/rand-long
(cmd)user=> (rand-long 12)
3
(ins)user=> (rand-long (Math/pow 10 10))
277282388

2021-05-17T20:02:25.054300Z

of course that + at the end could be bit-or

2021-05-17T20:36:51.054600Z

cool, thanks @noisesmith. yeah i don’t need pins longer than 7, and i was thinking bigint might be the way to go. but i like your approach to format..