Going for style points
Added exhaustive tests; refined solution to the original spec
converter = lambda n: ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"][n]
exec(bytes('潣癮牥整㵲慬扭慤渠嬺稧牥❯✬湯❥✬睴❯✬桴敲❥✬潦牵Ⱗ昧癩❥✬楳❸✬敳敶❮✬楥桧❴✬楮敮Ⱗ琧湥崧湛⁝','u16')[2:])- converter = lambda n: ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"][n]
import codewars_test as test import solution @test.describe("Example") def test_group(): @test.it("test case") def test_case(): w = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] for i in range(0,10): test.assert_equals(solution.converter(i), w[i])
- import codewars_test as test
# TODO Write testsimport solution # or from solution import example# test.assert_equals(actual, expected, [optional] message)- import solution
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
test.assert_equals(solution.converter(0), "zero")test.assert_equals(solution.converter(5), "five")- w = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
- for i in range(0,10):
- test.assert_equals(solution.converter(i), w[i])
Added a test to break the solution one might call 'Divisible by x * y', with the example of 80 being divisible by 40 and 10 (not 400), and a code solution to reflect this distinction
isDivisible = (n, x, y) => !(n % x || n % y)
const isDivisible = (n, x, y) => !(n % (x * y));- isDivisible = (n, x, y) => !(n % x || n % y)
const chai = require("chai"); const assert = chai.assert; chai.config.truncateThreshold=0; describe("Basic tests", () => { it("Fixed Tests", () => { assert.strictEqual(isDivisible(3,3,4),false); assert.strictEqual(isDivisible(12,3,4),true); assert.strictEqual(isDivisible(8,3,4),false); assert.strictEqual(isDivisible(48,3,4),true); assert.strictEqual(isDivisible(80,40,10),true); }); });
- const chai = require("chai");
- const assert = chai.assert;
- chai.config.truncateThreshold=0;
- describe("Basic tests", () => {
- it("Fixed Tests", () => {
- assert.strictEqual(isDivisible(3,3,4),false);
- assert.strictEqual(isDivisible(12,3,4),true);
- assert.strictEqual(isDivisible(8,3,4),false);
- assert.strictEqual(isDivisible(48,3,4),true);
- assert.strictEqual(isDivisible(80,40,10),true);
- });
- });
Design an algorithm that accepts a positive integer and reverses the order of its digits.
def reverse_int(int) int.to_s.reverse.to_i end
public class Algorithms {public static int reverseInt(int n) {int reversed = 0;while(n != 0){reversed = reversed * 10 + (n % 10);n /= 10;}return reversed;}}- def reverse_int(int)
- int.to_s.reverse.to_i
- end
describe "test reverse_int()" do it "should return reversed integers" do for i in 0..4 do int = [698, 132, 86, 2345, 29384765][i] answer = [896, 231, 68, 5432, 56748392][i] expect(reverse_int(int)).to eq(answer) puts "int#{i}: " + int.to_s puts "answer#{i}: " + answer.to_s end end end
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;// TODO: Replace examples and use TDD development by writing your own testspublic class SolutionTest {@Testpublic void reverseIntTest() {assertEquals(54321, Algorithms.reverseInt(12345));}}- describe "test reverse_int()" do
- it "should return reversed integers" do
- for i in 0..4 do
- int = [698, 132, 86, 2345, 29384765][i]
- answer = [896, 231, 68, 5432, 56748392][i]
- expect(reverse_int(int)).to eq(answer)
- puts "int#{i}: " + int.to_s
- puts "answer#{i}: " + answer.to_s
- end
- end
- end
As seraph776 pointed out a couple of days ago:
PEP 8
Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).
func gradeCalc(_ score: Int) -> String { (score < 0 || score > 100) ? "Not a grade" : ["F","F","F","F","F","F","D","C","B","A","A"][score / 10]; }
- func gradeCalc(_ score: Int) -> String {
do {return try letterGrade(for: score)} catch {return "Not a grade"}}func letterGrade(for score: Int) throws -> String {switch score {case 90...100:return "A"case 80..<90:return "B"case 70..<80:return "C"case 60..<70:return "D"case 0..<60:return "F"default:throw ParameterError.outsideOfValidRange}}enum ParameterError: Error {case outsideOfValidRange}- (score < 0 || score > 100) ? "Not a grade" : ["F","F","F","F","F","F","D","C","B","A","A"][score / 10];
- }
bool Or(bool a, bool b) { return a + b; } bool And(bool a, bool b) { return a * b; } bool Xor(bool a, bool b) { return a != b; }
- bool Or(bool a, bool b) {
return a+b;- return a + b;
- }
- bool And(bool a, bool b) {
return a*b;- return a * b;
- }
- bool Xor(bool a, bool b) {
return a!=b;- return a != b;
- }
converter = lambda n: ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"][n]
converter = lambda n: 'zero one two three four five six seven eight nine ten'.split(' ')[n]- converter = lambda n: ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"][n]