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.
Okay, this works for me right now...
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))
(ns codewars.sanity.check-t
(:require [codewars.sanity.check :refer :all]
[clojure.test :refer :all]))
(deftest sanity-check
(testing "1 is 1"
(is (= 1 1)))
(testing "24 = 1 * 2 * 3 * 4"
(is (= 24 (multiply 1 2 3 4)))))
;(deftest sad-path
; (testing "Will format an error properly"
; (is (= false (/ 1 0)))))
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 <<-CONFIGdevelopment:sessions:default:database: mongoidhosts:- localhost:27017CONFIGendfork { `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 Userinclude Mongoid::Documentfield :nameend- from pymongo import MongoClient
- with MongoClient() as client:
- table = client["mongoid"]["User"]
- table.insert({'_id': 9999, 'first name': 'William', 'last name': 'the Conqueror'})
test.describe("Checking to see if Mongo contains little Billy...") from pymongo import MongoClient with MongoClient() as client: table = client["mongoid"]["User"] test.assert_equals(table.find_one()['first name'], 'William')
describe User doit 'should save' doTest.expect(User.new.save)Test.assert_equals(User.count, 1)endend- test.describe("Checking to see if Mongo contains little Billy...")
- from pymongo import MongoClient
- with MongoClient() as client:
- table = client["mongoid"]["User"]
- test.assert_equals(table.find_one()['first name'], 'William')
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...");
}
}
(format t "Welcome to Common Lisp, made with secret alien technology.")
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))))
(ns number-to-word-test
(:require [clojure.test :refer :all]
[number-to-words]))
(deftest conversion-test
(is (= "One Million" (number-to-words/convert 1e6)))
(is (= "Nine Hundred Ninety-Nine Thousand" (number-to-words/convert 999e3))
(is (= "Six Hundred Twelve Thousand, Three Hundred Twenty-Three" (number-to-words/convert 612323)))))