Ad

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");
        }
    }
    • class Solution {
    • public static void main(String[] args) {
    • System.out.println("Hello World :D");
    • public class JavaJava {
    • public static void main() {
    • System.out.println("Hello Java");
    • }
    • }

Okay, this works for me right now...

Code
Diff
  • public class Java {
      public static int multiply(int x, int y) {
        return y * x;
      }
    }
    • public class Java {
    • public static int multiply(int x, int y) {
    • return x * y;
    • return y * x;
    • }
    • }

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...");
    }
    • println "hello, world"
    • fn main() {
    • println!("All we are in rust in the wind...");
    • }
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'})
    • require 'mongoid'
    • File.open('mongoid.yml', 'w') do |file|
    • file.write <<-CONFIG
    • development:
    • sessions:
    • default:
    • database: mongoid
    • hosts:
    • - localhost:27017
    • CONFIG
    • end
    • fork { `mongod` }
    • sleep 1 # need to find a better way to wait for mongo to startup
    • 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
    • ENV['RACK_ENV'] = 'development'
    • Mongoid.load!('mongoid.yml')
    • class User
    • include Mongoid::Document
    • field :name
    • end
    • from pymongo import MongoClient
    • with MongoClient() as client:
    • table = client["mongoid"]["User"]
    • table.insert({'_id': 9999, 'first name': 'William', 'last name': 'the Conqueror'})
Mathematics
Algorithms
Logic
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!")
    • from __future__ import print_function
    • print("Hello Python", end="!\n")
    • import sys
    • 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;
      
    }
    • #include <iostream>
    • int main(){
    • std::cout << "Hello C++" << endl;
    • std::cout << "Hello C++" << std::endl;
    • }
Code
Diff
  • from __future__ import print_function
    print("Hello Python!")
    • print "Hello Python!"
    • from __future__ import print_function
    • print("Hello Python!")
Code
Diff
  • #include "iostream"
    
    int main(){
      namespace std {
        cout << "Hello C++" << endl;
      }
    }
    • #include "iostream"
    • using namespace std;
    • int main(){
    • cout << "Hello C++" << endl;
    • namespace std {
    • cout << "Hello C++" << endl;
    • }
    • }
Numbers
Data Types
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...