Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more

This works now...

The JVM runner doesn't use javac to compile; the runner uses the JVM's builtin compiler that it gets from javax.tools.ToolProvider.getSystemJavaCompiler(), and loads the result into a thread.

If there's no fixture, it infers the class from the code and runs <Class>.main() (all of this is done in clojure, which is what I wrote the JVM runner in).

I didn't originally give it arguments because there's nothing sensible to set them to. After some thought, maybe the right thing to set them to is an array containing "Arguments Disabeled".

Also, clojure really wants the class you export from your solution code to be public ; I think there's a way around this but it'll require some research.

We're also clearly swallowing error messages from exceptions, which is bad.

Code
Diff
  • public class JavaJava {
        public static void main() {
            System.out.println("Hello Java");
        }
    }
  • 1-class Solution {
    2- public static void main(String[] args) {
    3- System.out.println("Hello World :D");
    1+public class JavaJava {
    2+ public static void main() {
    3+ System.out.println("Hello Java");
    44 }
    55 }

Okay, this works for me right now...

Code
Diff
  • public class Java {
      public static int multiply(int x, int y) {
        return y * x;
      }
    }
  • 11 public class Java {
    22 public static int multiply(int x, int y) {
    3- return x * y;
    3+ return y * x;
    44 }
    55 }

Checking to see if the clojure runner has the same problems as the Java runner. Apparently not...

(ns codewars.sanity.check)

(defn multiply [ & args ] (apply * args))
Code
Diff
  • fn main() {
        println!("All we are in rust in the wind...");
    }
  • 1-println "hello, world"
    1+fn main() {
    2+ println!("All we are in rust in the wind...");
    3+}
MongoDB
NoSQL
Databases
Code
Diff
  • import subprocess
    import re
    mongod = subprocess.Popen("mongod", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    # @jhoffner: I listen to my child process until it prints "waiting for connections" instead of sleeping...
    while True:
      l = mongod.stdout.readline()
      m = re.match(r".*waiting for connections on port 27017", l)
      if m:
        print l
        break
    
    from pymongo import MongoClient
    with MongoClient() as client:
      table = client["mongoid"]["User"]
      table.insert({'_id': 9999, 'first name': 'William', 'last name': 'the Conqueror'})
  • 1-require 'mongoid'
    2-File.open('mongoid.yml', 'w') do |file|
    3- file.write <<-CONFIG
    4-development:
    5- sessions:
    6- default:
    7- database: mongoid
    8- hosts:
    9- - localhost:27017
    10-CONFIG
    11-end
    12-fork { `mongod` }
    13-sleep 1 # need to find a better way to wait for mongo to startup
    1+import subprocess
    2+import re
    3+mongod = subprocess.Popen("mongod", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    4+# @jhoffner: I listen to my child process until it prints "waiting for connections" instead of sleeping...
    5+while True:
    6+ l = mongod.stdout.readline()
    7+ m = re.match(r".*waiting for connections on port 27017", l)
    8+ if m:
    9+ print l
    10+ break
    1414
    15-ENV['RACK_ENV'] = 'development'
    16-Mongoid.load!('mongoid.yml')
    17-
    18-class User
    19- include Mongoid::Document
    20- field :name
    21-end
    12+from pymongo import MongoClient
    13+with MongoClient() as client:
    14+ table = client["mongoid"]["User"]
    15+ table.insert({'_id': 9999, 'first name': 'William', 'last name': 'the Conqueror'})
Mathematics
Algorithms
Numbers

Using Liebniz' formula... converges attrociously slowing...

(->> (iterate inc 0) 
     (map #(-> (Math/pow -1 %) (/ (inc (* 2 %))))) 
     (take 1200001) 
     (reduce +) 
     (* 4) 
     println)

When you have a thread ouputting to stdout, and you want to catch its output in a test case...

(defmacro with-out-str-not-thread-safe
  "A version of clojure.core/with-out-str that is not thread-safe"
  [& body]
  `(let [s# (StringWriter.)]
     (with-redefs [*out* s#]
       ~@body
       (str s#))))
class Solution {
    public static void main(String[] args) {
        System.out.println("Hello darkness, my old friend...");
    }
}
Code
Diff
  • import sys
    sys.stdout.write("Hello Python!")
  • 1-from __future__ import print_function
    2-print("Hello Python", end="!\n")
    1+import sys
    2+sys.stdout.write("Hello Python!")
(format t "Welcome to Common Lisp, made with secret alien technology.")
Code
Diff
  • #include <iostream>
    
    int main(){
      
      std::cout << "Hello C++" << std::endl;
      
    }
  • 11 #include <iostream>
    22
    33 int main(){
    44
    5- std::cout << "Hello C++" << endl;
    5+ std::cout << "Hello C++" << std::endl;
    66
    77 }
Code
Diff
  • from __future__ import print_function
    print("Hello Python!")
  • 1-print "Hello Python!"
    1+from __future__ import print_function
    2+print("Hello Python!")
Code
Diff
  • #include "iostream"
    
    int main(){
      namespace std {
        cout << "Hello C++" << endl;
      }
    }
  • 11 #include "iostream"
    22
    3-using namespace std;
    44 int main(){
    5-
    6- cout << "Hello C++" << endl;
    7-
    4+ namespace std {
    5+ cout << "Hello C++" << endl;
    6+ }
    88 }
Numbers
Interview Questions

Converts a number to English. This is a horrendous mess...

(ns number-to-words
  (:require [clojure.string :refer [join]]))

(def digit-to-word
  {1 "One"
   2 "Two"
   3 "Three"
   4 "Four"
   5 "Five"
   6 "Six"
   7 "Seven"
   8 "Eight"
   9 "Nine"})

(def tens-to-word
  {
   20 "Twenty"
   30 "Thirty"
   40 "Fourty"
   50 "Fifty"
   60 "Sixty"
   70 "Seventy"
   80 "Eighty"
   90 "Ninety"})

(def teen-to-word
  {10 "Ten"
   11 "Eleven"
   12 "Twelve"
   13 "Thirteen"
   14 "Fourteen"
   15 "Fifteen"
   16 "Sixteen"
   17 "Seventeen"
   18 "Eighteen"
   19 "Nineteen"})

(defn- small-number-to-words
  [x]
  (cond
   (<= x 9) (digit-to-word x)
   (< x 20) (teen-to-word x)
   (< x 100)
   (let [ones-part (mod x 10)
         tens-part (- x ones-part)]
     (str (tens-to-word tens-part) "-"
          (digit-to-word ones-part)))
   (<= x 999)
   (let [small-part (mod x 100)
         hundreds-digit (-> x (- small-part) (/ 100))]
     (str (digit-to-word hundreds-digit) " Hundred " (small-number-to-words small-part)))))

(defn- digit-shift
  [x shift]
  (-> x (- (mod x shift)) (/ shift) int))

(defn convert
  [x]
  (loop [words [] x x]
    (cond
     
     (-> x (>= 1e6))
     (recur (conj words "One Million")
            (- x 1e6))
     
     (-> x (>= 1e3))
     (recur (conj words
                  (str
                   (-> x (digit-shift 1000) small-number-to-words)
                   " Thousand"))
            (mod x 1000))
     
     (-> x (> 0))
     (recur (conj words (small-number-to-words x)) 0)
     
     :else (join ", " words))))
Loading more items...