Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.

You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.

A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.

### steffan153's Kumite #1928

``even(N) :- 0 is mod(N, 2).``

### Prime Numbers - Inefficient!

This is a super-ineffective prime number finder. Can you make it better?

``````def prime(x):
counter = 0
for i in range(1,x):
if x % i is 0:
counter += 1
if counter is 0:
return True
else:
return False``````

### TimeFormatter Kumite

Algorithms
Logic
Formats
Strings
Data Types
Formatting

I just did the 4 kyu Human readable duration format Kata, and Although I managed to answer correctly, I feel like my code is unpolished.

I want to see the best approaches to this, and if possible, the explanation of what I did wrong.

Thanks.

RULES (taken straight out of the page):
Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.

For the purpose of this Kata, a year is 365 days and a day is 24 hours.

Note that spaces are important.

Detailed rules
The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space. The unit of time is used in plural if the integer is greater than 1.

The components are separated by a comma and a space (", "). Except the last component, which is separated by " and ", just like it would be written in English.

A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.

Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.

A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.

A unit of time must be used "as much as possible". It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a component must not be greater than any valid more significant unit of time.

``````public class TimeFormatter {

private final static int YEAROFSECONDS = 31536000;
private final static int DAYOFSECONDS = 86400;
private final static int HOUROFSECONDS = 3600;
private final static int MINUTEOFSECONDS = 60;

public static String formatDuration(int seconds) {
int[] counter = new int[5];
int componentCounter = 0;
StringBuilder sb = new StringBuilder();
while(seconds / YEAROFSECONDS >= 1) {
counter[0]++;
seconds = seconds - YEAROFSECONDS;
}
while(seconds / DAYOFSECONDS >= 1) {
counter[1]++;
seconds = seconds - DAYOFSECONDS;
}
while(seconds / HOUROFSECONDS >= 1) {
counter[2]++;
seconds = seconds - HOUROFSECONDS;
}
while(seconds / MINUTEOFSECONDS >= 1) {
counter[3]++;
seconds = seconds - MINUTEOFSECONDS;
}
counter[4] = seconds;
if(counter[0] == 0 && counter[1] == 0 && counter[2] == 0 && counter[3] == 0 && counter[4] == 0) {
return "now";
} else {
for(int i:counter) {
if(i != 0) {
componentCounter++;
}
}
int index = 0;
for(int i:counter) {
if(i != 0) {
switch(index) {
case 0:
if(i == 1) {
}  else {
}
index++;
componentCounter--;
break;
case 1:
if(i == 1) {
}  else {
}
index++;
componentCounter--;
break;
case 2:
if(i == 1) {
}  else {
}
index++;
componentCounter--;
break;
case 3:
if(i == 1) {
}  else {
}
index++;
componentCounter--;
break;
case 4:
if(i == 1) {
}  else {
}
index++;
componentCounter--;
break;
}
if(componentCounter == 1) {
sb.append(i);
sb.append(" ");
sb.append(" and ");
} else {
sb.append(i);
sb.append(" ");
if(componentCounter !=0){
sb.append(", ");
}

}

} else {
index++;
}
}
return sb.toString();
}
}

}``````

### tenooki's Kumite #3

You are developing a microservice that processes loan requests and performs a variety of calculations. In this particular scenario, you will receive a batch of loan requests which must be processed to obtain the monthly interest rate, monthly payment amount and the risk factor. These 3 values must be stored in a read efficient in-memory structure in order to serve as a cache for other systems to query.

The formula to calculate the monthly payment amount is as follows:

(LOAN_AMOUNT * MONTHLY_RATE) / (1 - (1 + MONTHLY_RATE ^ ((-1) * TERM_MONTHS))

The risk factor is a value from 0 to 1, where 0 means no risk and 1 is the highest possible risk. The bank follows these guidelines to estimate a borrower's credit quality (higher percentages mean better credit capabilities):

• No delinquent debt (+35%)
• Debt to Income ratio (Monthly Debt / Monthly Income) < 43% (+30%)
• Credit history (Good +25%)
• Type of credit used (Mortgage +10%)

Input:

JSON Array of loan submissions

``````[
{
loanId: 1,
amount: 100000,
termYears: 10,
termMonths: 0,
anualInterest: 6,
type: "mortgage",
borrower: {
name: "Jhon Smith",
anualIncome: "150000",
delinquentDebt: false,
amountOwed: 0
}
},
{
loanId: 2,
amount: 200000,
termYears: 15,
termMonths: 0,
anualInterest: 6.5,
type: "student",
borrower: {
name: "James Gosling",
anualIncome: "150000",
delinquentDebt: false,
amountOwed: 0
}
}
]
``````

Output:

In-memory collections of instances of LoanAnalysis POJO containing:

• loanId,
• monthlyInterestRate
• monthlyPayment,
• riskFactor
``````public class Loan {
private int amount;
private int termYears;
private int termMonths;
private double anualInterest;

public Loan(int amount, int termYears, int termMonths, double anualInterest){
this.amount = amount;
this.termYears = termYears;
this.termMonths = termMonths;
this.anualInterest = anualInterest;
}

public double getMonthlyPayment(){
return this.amount*this.getMonthlyRate()/
(1-Math.pow(1+this.getMonthlyRate(), -this.getTermInMonths()));
}

public double getMonthlyRate(){
return this.anualInterest/100/12;
}

public int getTermInMonths(){
return this.termYears*12+this.termMonths;
}

public double riskFactor(int anualIncome){
return 0;
}
}``````

### Suma de números

Dados dos enteros a y b, que pueden ser positivos o negativos, encuentre la suma de todos los números entre incluirlos también y devolverlos.
Si los dos números son iguales, devuelva a o b.

Nota: a y b no están ordenados!

ObtenerSuma(1, 0) == 1 // 1 + 0 = 1
ObtenerSuma(1, 2) == 3 // 1 + 2 = 3
ObtenerSuma(0, 1) == 1 // 0 + 1 = 1
ObtenerSuma(1, 1) == 1 // 1 Since both are same
ObtenerSuma(-1, 0) == -1 // -1 + 0 = -1
ObtenerSuma(-1, 2) == 2 // -1 + 0 + 1 + 2 = 2

``````public class Suma
{
public int ObtenerSuma(int a, int b)
{
return (a + b) * (Math.abs(a - b) + 1) / 2;
}
}``````

### Next Fibonacci Number.

Getting an integer `n`, return the next digit after the nth digit of the fibonacci sequence.

For example:

n == 3, return 2.

n == 2, return 1.

n == 12, return 144.

Fib sequence. 0,1,1,2,3,5,8,13,21,34,55,89,144. . .

``````public class nthFib{

public static int fib(int n){
return n == 1 ? 1 : n == 0 ? 0 : (fib(n - 1) + fib(n - 2));
}
}``````

### Shorter Code - If Statement

Make this if statement shorter!

``````def larger_than_5(a):
return True if a > 5 else False``````

### Invert Pitch Set

Fundamentals

Write a function that takes a list of 12 UNIQUE numbers, ranging from 0 to 11. Find the interval difference of each step, and reverse the interval in the new set.

If the number is less than 0, it should wrap around. For example, if -1, then 11. If -2, then 10.

Also if the number is greater than 11, the number should be subtracted by 12.

The first number in the new set should be the same as the original set.

For example:

[7, 6, 4, 3, 1, 11, 10, 0, 9, 8, 2, 5] should return [7, 8, 10, 11, 1, 3, 4, 2, 5, 6, 0, 9]

(I'm having a hard time putting the description into words that make this seem completely independent to music. Knowing and understand music theory would make this a lot easier to understand)

``````def invert(testset):

base12 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

invset = [(testset[0])]
for s in range(len(testset)-1):
interval = testset[s] - testset[s+1]
if s == 0:
inv_val = testset[s] + interval
if inv_val > 11:
inv_val = inv_val - 12
else:
inv_val += interval
if inv_val > 11:
inv_val = inv_val - 12
if inv_val < 0:
inv_abs = base12[inv_val]
else:
inv_abs = inv_val
invset.append(inv_abs)
return(invset)``````

### Find Array indices from center to border

The idea is given an array size, find all indices in the correct order.

The order start at the center, then move up from the center for 1, then move down from the center for 1, and it continues.

If the size is odd : we take the actual center
(size is 5, the indice of the center will be 2)
| | | | | | -> center is | | |X| | | -> indice of X is 2

If the size is even : we take the indice below the center
(size if 6, the indice of the center is 2)
| | | | | | | -> center is | | |X| | | |-> indice before the center (X) is 2

For the first 10 numbers, solution is
#Number 0 - []
#Number 1 - 0
#Number 2 - 0, 1
#Number 3 - 1, 2, 0
#Number 4 - 1, 2, 0, 3
#Number 5 - 2, 3, 1, 4, 0
#Number 6 - 2, 3, 1, 4, 0, 5
#Number 7 - 3, 4, 2, 5, 1, 6, 0
#Number 8 - 3, 4, 2, 5, 1, 6, 0, 7
#Number 9 - 4, 5, 3, 6, 2, 7, 1, 8, 0
#Number 10 - 4, 5, 3, 6, 2, 7, 1, 8, 0, 9

``````def generateOrder(n):
if n == 0:
return []

r = []

if n % 2 == 0 :
r = generateOrder(n-1)
r.append(n-1)
else:
middle = round((n / 2) - 0.01)
r = [middle]
for i in range(middle):
r.append(middle + i + 1)
r.append(middle - i - 1)

return r``````

### NSObjectProtocol+configure

Allows for things like:

``````import UIKit

let label = UILabel().configure {
\$0.text = "Hello World"
\$0.numberOfLines = 4
\$0.font = UIFont.systemFont(ofSize: 16)
}
``````
``````import Foundation

public extension NSObjectProtocol {