Răsfoiți Sursa

Adding solution for 908

Vinicius Teshima 1 lună în urmă
părinte
comite
0ed625decb
2 a modificat fișierele cu 37 adăugiri și 0 ștergeri
  1. 33 0
      Racket/908.rkt
  2. 4 0
      Racket/build.sh

+ 33 - 0
Racket/908.rkt

@@ -0,0 +1,33 @@
+#lang racket
+
+(define/contract (smallest-range-i nums k)
+  (-> (listof exact-integer?) exact-integer? exact-integer?)
+  (if (<= (length nums) 1) 0
+    (let ([mi (apply min nums)]
+          [ma (apply max nums)])
+      (for*/fold
+        ([c ma])
+        ([i (in-range (+ k 1))]
+         [j (in-range (+ k 1))])
+        (min c (abs (- (- ma i) (+ mi j))))
+        )
+      )
+    )
+  )
+
+(define (main)
+  (define (r l k e)
+    (display
+      (format
+        "(smallest-range-i ~a ~a) = ~a | Exp: ~a\n"
+        l k (smallest-range-i l k) e
+        )
+      )
+    )
+  (r '(1) 0 0)
+  (r '(0 10) 2 6)
+  (r '(1 3 6) 3 0)
+  (r '(506 4763 8681 4243 4040 8587 9235 442 1865 2820) 5899 0)
+  )
+
+(main)

+ 4 - 0
Racket/build.sh

@@ -0,0 +1,4 @@
+newest_file="$(find ./ -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1)"
+
+set -x
+racket $newest_file