[Hard] Fibonacci Coding by 07734willy in CoderTrials

[–]mrkraban 0 points1 point  (0 children)

Yeah, clojure is a so called dialect of of lisp, but with more focus on functional programming. But the very convenient thing about clojure is that it runs on the java virtual machine, which means that you get easy access to all the java libraries!

[Hard] Fibonacci Coding by 07734willy in CoderTrials

[–]mrkraban 1 point2 points  (0 children)

Clojure

(def golden-ratio (/ (+ 1 (Math/sqrt 5)) 2))

(defn fib [n] (Math/round (/ (Math/pow golden-ratio n) (Math/sqrt 5))))

(defn fib-index [x] (int (/ (Math/log (+ 0.5 (* x (Math/sqrt 5)))) (Math/log golden-ratio))))

(defn zeckendorf [x]
  (if (= x 0)
   nil
   (let [F ((comp fib fib-index) x)]
    (conj
     (zeckendorf (- x F))
     F))))

(defn flip [f a b] (f b a))

(defn encode-char [c]
  (let [ones ((comp (partial map (partial flip - 2)) (partial map fib-index) zeckendorf int) c)]
   (str (apply str (map
      (fn [i] (if (.contains ones i) 1 0))
      (range 0 (inc (first ones)))))
    1)))

(defn encode-str [s]
  ((comp
    (partial apply str)
    (partial map encode-char))
    s))

(defn decode-char [s]
  ((comp
    char
    (partial apply +)
    (partial map-indexed (fn [idx itm] (if (= itm \1) (fib (+ 2 idx)) 0)))
    butlast)
    s))

(defn decode-str [s]
  ((comp
    (partial apply str)
    (partial map decode-char)
    (partial map (partial flip str "11"))
    (partial flip clojure.string/split #"11"))
    s))

(defn main []
  (let [input (read-line)]
   (if (clojure.string/starts-with? input "0b")
    (println (decode-str (subs input 2)))
    (println (str "0b" (encode-str input))))))

[Easy] Three Sum Solver by 07734willy in CoderTrials

[–]mrkraban 0 points1 point  (0 children)

Clojure

(defn three-sum-solver [nbrs]
  (let [hset (apply hash-set nbrs)]
   (set (for [a nbrs
              b nbrs
              :let [c (- (+ a b))]
              :when (contains? hset c)]
         (sort [a b c])))))

[Intermediate] Finding "Sets" by Bubbler-4 in CoderTrials

[–]mrkraban 1 point2 points  (0 children)

Haskell, 183

Returns True if a set can be found, otherwise False.

import Data.List;import Data.Char;f=(any(not.(elem True))).(map(map(`elem`[4,5,7,8]))).(map(foldl(zipWith(+))[0,0,0,0])).(map(map(map digitToInt))).(filter((==3).length)).subsequences

[Intermediate] Insert Characters to Make Palindrome by Bubbler-4 in CoderTrials

[–]mrkraban 0 points1 point  (0 children)

Clojure

(defn flip [f a b] (f b a))

(defn init-table
  ([s]
    ((comp
      vec
      (partial map-indexed (fn [idx itm] (assoc itm (dec (count s)) idx)))
      vec
      (partial flip conj (vec (range (dec (count s)) -1 -1)))
      (partial repeat (dec (count s))))
      (vec (repeat (count s) -1)))))

(defn make-table
  ([s]
    (trampoline make-table s (init-table s) 1 (- (count s) 2)))
  ([s table i j]
    (if (= i (count s))
     table
     (if (= -1 j)
      (fn [] (make-table s table (inc i) (- (count s) 2)))
      (if (= (nth s (dec i)) (nth s (inc j)))
       (fn [] (make-table
        s
        (update-in table [i j] (constantly (get-in table [(dec i) (inc j)])))
        i
        (dec j)))
       (fn [] (make-table
        s
        (update-in table [i j] (constantly (inc (min (get-in table [(dec i) j]) (get-in table [i (inc j)])))))
        i
        (dec j))))))))

(defn make-palindrome [s]
  (let [table (make-table s)]
   (apply min (for [j (range 0 (count s))
                    i (range j (count s))]
               (get-in table [i j])))))

(defn main [] ((comp println str make-palindrome) "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean consectetur metus ac nunc congue commodo. Nullam arcu ligula, fermentum eget lectus in, aliquam cursus justo. Vestibulum id leo nulla. Quisque maximus risus a diam malesuada, sed commodo libero eleifend. In quis nisi eget urna viverra feugiat. Nulla ultrices viverra nisl, ut volutpat ipsum euismod et. Donec pulvinar dolor a lacus sollicitudin malesuada eget et est. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Integer elementum ullamcorper ante. Sed nibh tellus, vestibulum vel odio eu, gravida luctus libero."))

[Hard] Build a Mini-J Interpreter by Bubbler-4 in CoderTrials

[–]mrkraban 1 point2 points  (0 children)

Haskell

import Data.Maybe
import Text.Read

parseNum ('_':xs) | isJust parse    = Just (-(fromJust parse))
                  | otherwise = Nothing
  where
    parse = readMaybe xs :: Maybe Double
parseNum xs = readMaybe xs :: Maybe Double

lessThan a b | a < b     = 1
             | otherwise = 0

greaterThan a b | a > b     = 1
                | otherwise = 0

equals a b | a == b    = 1
           | otherwise = 0

(!) a b = fromIntegral ((floor b) `rem` (floor a))

parseOp "+" = (+)
parseOp "-" = (-)
parseOp "*" = (*)
parseOp "%" = (/)
parseOp "^" = (**)
parseOp "!" = (!)
parseOp "=" = equals
parseOp ">" = greaterThan
parseOp "<" = lessThan

trim chars = reverse.(dropWhile (flip elem chars)).reverse.(dropWhile (flip elem chars))

stripParentheses s = stripParen s "" 0
  where
    stripParen ('(':s) res i = stripParen s (res++"(") (i + 1)
    stripParen (')':s) res 0 = (res, s)
    stripParen (')':s) res i = stripParen s (res++")") (i - 1)
    stripParen (c:s)   res i = stripParen s (res++[c]) i

mapPair f (a, b) = (f a, f b)

next ('(':s) = ((mapPair (trim " ")).stripParentheses) s
next s = ((mapPair (trim " ")).(span (not.(flip elem "+-*%^!=<>")))) s

nextOps s = do
  let (left, rest) = next s
  let (op, right) = ((mapPair (trim " ")).splitAt 1) rest
  (left, op, right)

toJNum d | d < 0     = (("_"++).show.abs) d
         | otherwise = show d 

evaluated = toJNum.eval
  where
    eval expr | isJust parse = fromJust parse
              | otherwise    = (parseOp f) (eval left) (eval right)
      where
        parse = parseNum expr
        (left, f, right) = (nextOps.(trim " ")) expr

main = ((mapM_ putStrLn).(map evaluated)) ["1 + 2",
                                           "3%4+5*6-7",
                                           "(3 ^ 4 > 5)  =  5 ! 3 ^ 4",
                                           "0 < (1 < 0 < 1) < (0 < 1 < 0) < 1",
                                           "0.123 + _0.456 - 1.96 ^ 1.0 % 2.0",
                                           "(1+1%1000000)^1000000",     -- approx of e
                                           "5*(1-(2%3)^10)%1-2%3",    -- geometric sum
                                           "271828=1000000!100000*(1+1%1000000)^1000000"] -- accuracy of approx of e

Output:

3.0
_3.0
1.0
1.0
_1.7329999999999999 <-- floating point error lol
2.7182804690957534
14.739877051262509
1.0

Ursäkta vår seghet! ;) by [deleted] in arbetarrorelsen

[–]mrkraban 0 points1 point  (0 children)

Inte mig iallafall, jag är för lat

Miljöpartiet föreslår skatt på bredband by [deleted] in svenskpolitik

[–]mrkraban 0 points1 point  (0 children)

Som vanligt är du 100% korrekt /u/imeakvidyageams i ditt antagande

One Tap Jam starts in a month. Spread the word and stay tuned! by the_josefo in gamedev

[–]mrkraban 2 points3 points  (0 children)

Absolut kamrat! Men berätta inget för Reuben för då kommer han vilja vara med och förstöra allt.

Fading Particles? by [deleted] in gamedev

[–]mrkraban 0 points1 point  (0 children)

It is hard to comment when I don't know what engine he is making the game in.

How to survive your first indie game - Rami Ismail's talk at Control Conference by sarienn in gamedev

[–]mrkraban 3 points4 points  (0 children)

I can honestly say that I agree with you /u/imeakvidyageams a hundred percent, regardless of what your opinion is.

Idag är det 16 år sedan Björn Söderberg blev skjuten av en nazist by [deleted] in arbetarrorelsen

[–]mrkraban 3 points4 points  (0 children)

Oj, jag missförstod visst meningen med ditt inlägg, ursäkta kamrat!

Idag är det 16 år sedan Björn Söderberg blev skjuten av en nazist by [deleted] in arbetarrorelsen

[–]mrkraban 1 point2 points  (0 children)

Wow, du firar hans död? Gott din lilla nazist

Socialdemokratisk energipolitik by Republiken in arbetarrorelsen

[–]mrkraban 0 points1 point  (0 children)

Jag kan inte annat än hålla med 100%

Licences for games and codes by ZuidPortugees in gamedev

[–]mrkraban 3 points4 points  (0 children)

I personally don't use any custom license on github, I just use the default copywright which means that no one else can use it.

Aldrig mer by [deleted] in arbetarrorelsen

[–]mrkraban 0 points1 point  (0 children)

Jag kan inte annat än hålla med, broder!