Ad

I don't know why I've done this, or whether I'm proud of it, but just... here. Take it.

Code
Diff
  • 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 Z
    • from 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
Algorithms
Logic
Fundamentals

Fixed version of original, taking divisibility into account

(even though rowcased's solution is probably better)

(I'll take a victory on readability though!)

Code
Diff
  • 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

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

Code
Diff
  • 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

Code
Diff
  • 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.

Code
Diff
  • 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.

Code
Diff
  • 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)
Algorithms
Logic
Fundamentals

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.

Code
Diff
  • 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

Recursion makes everything more elegant, right?

...right?

Code
Diff
  • 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)