I don't know why I've done this, or whether I'm proud of it, but just... here. Take it.
from math import floor, sin, cos, sqrt, factorial, radians, pow, tau, pi, asin, gamma, atan, log2, tan, e, ceil, acos, lgamma, log, exp from cmath import exp, phase import numpy as np from scipy import linalg, integrate #I'm sorry def add(x, y): return ceil(pow(sqrt(pow(factorial(x)/gamma(x), pow(tau/pi, tau/pi)) + \ phase(-1+0j)/asin(pi/tau)*pow(x, pi/atan(pow(tan(pi/6), tan(pi*-1/4))))*abs(exp(tau*1j))*y + \ pow(factorial(floor(e))*y,gamma(floor(pi)))*linalg.det(np.array([[x,0],[0,x]])) + \ ceil(acos(cos(pi/gamma(floor(pi))) - ceil(np.euler_gamma)))*x*pow(y,integrate.quad(lambda x:x+x,1,2)[0]) + \ linalg.det(np.eye(10))*pow(y,ceil(pi))) + linalg.det(np.full((5,5),1)) + \ lgamma(sin(acos(tan(log(abs(exp(floor(pi) % ceil(e)))))))), sin(radians(factorial(4) + gamma(4))))) if x>0 and y>0 else None #I'm so sorry
# Only works for x, y > 0 for all x, y in Zfrom math import floor, log10, sqrt- from math import floor, sin, cos, sqrt, factorial, radians, pow, tau, pi, asin, gamma, atan, log2, tan, e, ceil, acos, lgamma, log, exp
- from cmath import exp, phase
- import numpy as np
- from scipy import linalg, integrate
- #I'm sorry
- def add(x, y):
return floor(sqrt(10**(2*log10(x)) + 2 * 10**(log10(x) + log10(y)) + 10**(2*log10(y))))- return ceil(pow(sqrt(pow(factorial(x)/gamma(x), pow(tau/pi, tau/pi)) + \
- phase(-1+0j)/asin(pi/tau)*pow(x, pi/atan(pow(tan(pi/6), tan(pi*-1/4))))*abs(exp(tau*1j))*y + \
- pow(factorial(floor(e))*y,gamma(floor(pi)))*linalg.det(np.array([[x,0],[0,x]])) + \
- ceil(acos(cos(pi/gamma(floor(pi))) - ceil(np.euler_gamma)))*x*pow(y,integrate.quad(lambda x:x+x,1,2)[0]) + \
- linalg.det(np.eye(10))*pow(y,ceil(pi))) + linalg.det(np.full((5,5),1)) + \
- lgamma(sin(acos(tan(log(abs(exp(floor(pi) % ceil(e)))))))), sin(radians(factorial(4) + gamma(4))))) if x>0 and y>0 else None
- #I'm so sorry
import unittest class MyTest(unittest.TestCase): def test0(self): self.assertEqual(add(3,4), 7) def test1(self): self.assertEqual(add(1,5), 6) def test2(self): self.assertEqual(add(78,81), 159) def test3(self): self.assertEqual(add(9,21), 30) def test4(self): self.assertEqual(add(5,7), 12) def test5(self): self.assertEqual(add(50, 50), 100) if __name__ == '__main__': unittest.main()
- import unittest
- class MyTest(unittest.TestCase):
- def test0(self):
- self.assertEqual(add(3,4), 7)
- def test1(self):
- self.assertEqual(add(1,5), 6)
- def test2(self):
- self.assertEqual(add(78,81), 159)
- def test3(self):
- self.assertEqual(add(9,21), 30)
- def test4(self):
- self.assertEqual(add(5,7), 12)
- def test5(self):
- self.assertEqual(add(50, 50), 100)
- if __name__ == '__main__':
- unittest.main()
Fixed version of original, taking divisibility into account
(even though rowcased's solution is probably better)
(I'll take a victory on readability though!)
def validateKey(key): segments = key.split('-') if len(segments) != 2: return False end_ints = list(map(lambda x: int(x), segments[1])) illegalSites = ["333", "444", "555", "666", "777", "888", "999"] illegalEnds = ["0", "8", "9"] return segments[0].isnumeric() and segments[1].isnumeric() and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds and sum(end_ints) % 7 == 0
- def validateKey(key):
- segments = key.split('-')
- if len(segments) != 2: return False
- end_ints = list(map(lambda x: int(x), segments[1]))
- illegalSites = ["333", "444", "555", "666", "777", "888", "999"]
- illegalEnds = ["0", "8", "9"]
return len(segments) == 2 and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds- return segments[0].isnumeric() and segments[1].isnumeric() and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds and sum(end_ints) % 7 == 0
# TODO: Add your tests here # Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework. # [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework) # are still available for now. # # For new tests, using [Chai](https://chaijs.com/) is recommended. # You can use it by requiring: # const assert = require("chai").assert; # If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted. import unittest class MyTest(unittest.TestCase): def test0(self): self.assertEqual(validateKey('879-5676524'), True); def test1(self): self.assertEqual(validateKey('879-5676529'), False); def test2(self): self.assertEqual(validateKey('535-5676524'), True); def test3(self): self.assertEqual(validateKey('000-5676524'), True); def test4(self): self.assertEqual(validateKey('999-5676524'), False); def test5(self): self.assertEqual(validateKey('764-2365839'), False); def test6(self): self.assertEqual(validateKey('3-5676524'), False); def test7(self): self.assertEqual(validateKey('364-17688'), False); def test8(self): self.assertEqual(validateKey('5676524'), False); def test9(self): self.assertEqual(validateKey('3-'), False); def test10(self): self.assertEquals(validateKey("111-1111112"), False); if __name__ == '__main__': unittest.main()
- # TODO: Add your tests here
- # Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
- # [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
- # are still available for now.
- #
- # For new tests, using [Chai](https://chaijs.com/) is recommended.
- # You can use it by requiring:
- # const assert = require("chai").assert;
- # If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
- import unittest
- class MyTest(unittest.TestCase):
- def test0(self):
- self.assertEqual(validateKey('879-5676524'), True);
- def test1(self):
- self.assertEqual(validateKey('879-5676529'), False);
- def test2(self):
- self.assertEqual(validateKey('535-5676524'), True);
- def test3(self):
- self.assertEqual(validateKey('000-5676524'), True);
- def test4(self):
- self.assertEqual(validateKey('999-5676524'), False);
- def test5(self):
- self.assertEqual(validateKey('764-2365839'), False);
- def test6(self):
- self.assertEqual(validateKey('3-5676524'), False);
- def test7(self):
- self.assertEqual(validateKey('364-17688'), False);
- def test8(self):
- self.assertEqual(validateKey('5676524'), False);
- def test9(self):
- self.assertEqual(validateKey('3-'), False);
- def test10(self):
- self.assertEquals(validateKey("111-1111112"), False);
- if __name__ == '__main__':
- unittest.main()
It's still pretty trivial in Java 8
I use StringBuilder instead of + for concatenation because I recall being taught that StringBuilder is more efficient
public class Kata { public static String multiply(String input,int times){ StringBuilder str = new StringBuilder(); for(int i = 0; i < times; i++) { str.append(input); } return str.toString(); } }
- public class Kata {
- public static String multiply(String input,int times){
return input.repeat(times);- StringBuilder str = new StringBuilder();
- for(int i = 0; i < times; i++) {
- str.append(input);
- }
- return str.toString();
- }
- }
I'm not quite certain I get what's up with this one, but here's your original code with a few veeery minor improvements
public class Loan { private int amount; private int termYears; private int termMonths; private double annualInterest; public Loan(int amount, int termYears, int termMonths, double annualInterest){ this.amount = amount; this.termYears = termYears; this.termMonths = termMonths; this.annualInterest = annualInterest; } public double getMonthlyPayment(){ return amount*this.getMonthlyRate()/ (1-Math.pow(1+getMonthlyRate(), -getTermInMonths())); } public double getMonthlyRate(){ return this.annualInterest/1200.0; } public int getTermInMonths(){ return termYears*12+termMonths; } public double riskFactor(int anualIncome){ return 0; } }
- public class Loan {
- private int amount;
- private int termYears;
- private int termMonths;
private double anualInterest;- private double annualInterest;
public Loan(int amount, int termYears, int termMonths, double anualInterest){- public Loan(int amount, int termYears, int termMonths, double annualInterest){
- this.amount = amount;
- this.termYears = termYears;
- this.termMonths = termMonths;
this.anualInterest = anualInterest;- this.annualInterest = annualInterest;
- }
- public double getMonthlyPayment(){
return this.amount*this.getMonthlyRate()/(1-Math.pow(1+this.getMonthlyRate(), -this.getTermInMonths()));- return amount*this.getMonthlyRate()/
- (1-Math.pow(1+getMonthlyRate(), -getTermInMonths()));
- }
- public double getMonthlyRate(){
return this.anualInterest/100/12;- return this.annualInterest/1200.0;
- }
- public int getTermInMonths(){
return this.termYears*12+this.termMonths;- return termYears*12+termMonths;
- }
- public double riskFactor(int anualIncome){
- return 0;
- }
- }
Faster version using the closed form of the Fibonacci sequence given by Binet's formula
This version is prone to round-off error for large enough n, but it gets the right answer up to the 32-bit signed integer cut-off.
public class NthFib { public static final double phi = (1 + Math.sqrt(5))/2.0; public static final double psi = (1 - Math.sqrt(5))/2.0; public static int fib(int n) { return (int)((Math.pow(phi, n) - Math.pow(psi, n))/Math.sqrt(5)); } }
class NthFib {static int fib(int n) {return n > 1 ? fib(--n) + fib(--n) : n;- public class NthFib {
- public static final double phi = (1 + Math.sqrt(5))/2.0;
- public static final double psi = (1 - Math.sqrt(5))/2.0;
- public static int fib(int n) {
- return (int)((Math.pow(phi, n) - Math.pow(psi, n))/Math.sqrt(5));
- }
- }
Although just using the variance function works, I feel like this is more in the spirit of what this was supposed to be.
x <- c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94) #x = x is true for everything except NA edited <- x[which(x == x)] mean <- sum(edited)/length(edited) normal <- edited - mean squares <- normal * normal sum_of_squares = sum(squares) var <- sum_of_squares/(length(edited) - 1) print(var)
c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94)- x <- c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94)
- #x = x is true for everything except NA
- edited <- x[which(x == x)]
- mean <- sum(edited)/length(edited)
- normal <- edited - mean
- squares <- normal * normal
- sum_of_squares = sum(squares)
- var <- sum_of_squares/(length(edited) - 1)
- print(var)
A similar solution in Python.
Sorry about changing the format of the test cases, I copied the format from another Kumite because I don't know how to do it otherwise.
def validateKey(key): segments = key.split('-') illegalSites = ["333", "444", "555", "666", "777", "888", "999"] illegalEnds = ["0", "8", "9"] return len(segments) == 2 and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds
function validateKey(key) {var segments = key.split('-');- def validateKey(key):
- segments = key.split('-')
var illegalSites = [333, 444, 555, 666, 777, 888, 999];var illegalEnds = [0, 8, 9];- illegalSites = ["333", "444", "555", "666", "777", "888", "999"]
- illegalEnds = ["0", "8", "9"]
if (segments.length != 2|| illegalSites.includes(+(segments[0])) || +(segments[0]) > 999 || +(segments[0]) < 0 || segments[0].length != 3|| +(segments[1]) % 7 == true || illegalEnds.includes(+(segments[1].slice(-1))) || segments[1].length != 7) {return false}return true;}- return len(segments) == 2 and len(segments[0]) == 3 and len(segments[1]) == 7 and not segments[0] in illegalSites and not segments[1][-1:] in illegalEnds
# TODO: Add your tests here # Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework. # [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework) # are still available for now. # # For new tests, using [Chai](https://chaijs.com/) is recommended. # You can use it by requiring: # const assert = require("chai").assert; # If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted. import unittest class MyTest(unittest.TestCase): def test0(self): self.assertEqual(validateKey('879-5676524'), True); def test1(self): self.assertEqual(validateKey('879-5676529'), False); def test2(self): self.assertEqual(validateKey('535-5676524'), True); def test3(self): self.assertEqual(validateKey('000-5676524'), True); def test4(self): self.assertEqual(validateKey('999-5676524'), False); def test5(self): self.assertEqual(validateKey('764-2365839'), False); def test6(self): self.assertEqual(validateKey('3-5676524'), False); def test7(self): self.assertEqual(validateKey('364-17688'), False); def test8(self): self.assertEqual(validateKey('5676524'), False); def test9(self): self.assertEqual(validateKey('3-'), False); if __name__ == '__main__': unittest.main()
// TODO: Add your tests here// Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.// [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)// are still available for now.//// For new tests, using [Chai](https://chaijs.com/) is recommended.// You can use it by requiring:// const assert = require("chai").assert;// If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.- # TODO: Add your tests here
- # Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
- # [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
- # are still available for now.
- #
- # For new tests, using [Chai](https://chaijs.com/) is recommended.
- # You can use it by requiring:
- # const assert = require("chai").assert;
- # If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
- import unittest
describe("Solution", function() {it("validate keys", function() {Test.assertEquals(validateKey('879-5676524'), true);Test.assertEquals(validateKey('879-5676529'), false);Test.assertEquals(validateKey('535-5676524'), true);Test.assertEquals(validateKey('000-5676524'), true);Test.assertEquals(validateKey('999-5676524'), false);Test.assertEquals(validateKey('764-2365839'), false);Test.assertEquals(validateKey('3-5676524'), false);Test.assertEquals(validateKey('364-17688'), false);Test.assertEquals(validateKey('5676524'), false);Test.assertEquals(validateKey('3-'), false);});});- class MyTest(unittest.TestCase):
- def test0(self):
- self.assertEqual(validateKey('879-5676524'), True);
- def test1(self):
- self.assertEqual(validateKey('879-5676529'), False);
- def test2(self):
- self.assertEqual(validateKey('535-5676524'), True);
- def test3(self):
- self.assertEqual(validateKey('000-5676524'), True);
- def test4(self):
- self.assertEqual(validateKey('999-5676524'), False);
- def test5(self):
- self.assertEqual(validateKey('764-2365839'), False);
- def test6(self):
- self.assertEqual(validateKey('3-5676524'), False);
- def test7(self):
- self.assertEqual(validateKey('364-17688'), False);
- def test8(self):
- self.assertEqual(validateKey('5676524'), False);
- def test9(self):
- self.assertEqual(validateKey('3-'), False);
- if __name__ == '__main__':
- unittest.main()
Recursion makes everything more elegant, right?
...right?
import numpy def add(x, y): if x == 0 and y == 0: return 0 elif y == 0: return add(x - numpy.sign(x), 0) + numpy.sign(x) else: return add(x, y - numpy.sign(y)) + numpy.sign(y)
from math import ceil, log10, sqrt- import numpy
- def add(x, y):
return ceil(10 ** log10(ceil(sqrt(x**2 + 2*x*y + y**2))))- if x == 0 and y == 0:
- return 0
- elif y == 0:
- return add(x - numpy.sign(x), 0) + numpy.sign(x)
- else:
- return add(x, y - numpy.sign(y)) + numpy.sign(y)
import unittest class MyTest(unittest.TestCase): def test0(self): self.assertEqual(add(3,4), 7) def test1(self): self.assertEqual(add(5,5), 10) def test2(self): self.assertEqual(add(9,9), 18) def test3(self): self.assertEqual(add(-9,-9), -18) def test4(self): self.assertEqual(add(-9,9), 0) def test5(self): self.assertEqual(add(9,-9), 0) if __name__ == '__main__': unittest.main()
- import unittest
- class MyTest(unittest.TestCase):
- def test0(self):
- self.assertEqual(add(3,4), 7)
- def test1(self):
- self.assertEqual(add(5,5), 10)
- def test2(self):
- self.assertEqual(add(9,9), 18)
- def test3(self):
- self.assertEqual(add(-9,-9), -18)
- def test4(self):
- self.assertEqual(add(-9,9), 0)
- def test5(self):
- self.assertEqual(add(9,-9), 0)
- if __name__ == '__main__':
- unittest.main()