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.

Ad
Ad
\pi

$\pi$

def add(a, b):
    return a + b

We know that Finn is friends with Jake, Flame Princess is friends with Finn as is Princess Bubblegum, Jake is friends with Lady Unicorn and Princess Bubblegum.

Write a function friends(X) -> [Y] to find out who are the friends of a person X.

friend('Finn', 'Jake').
friend('Flame Princess', 'Finn').
friend('Princess Bubblegum', 'Finn').
friend('Jake', 'Lady Unicorn').
friend('Jake', 'Princess Bubblegum').

friends(X, R) :- findall(Y, friend(X, Y), R1), findall(Y, friend(Y, X), R2), append(R1, R2, R).

Crie uma classe que receba dois inteiros e retorne a soma desses dois inteiros.

public class Primeira {
    public static int sum (int a, int b) {
        return a + b;
    }
}

Write a metafunction whose result type reverses the template parameters of a std::tuple.

(Maybe, possibly, functional programming.)

// For convenience
template <typename, typename>
struct append;

template <typename ...Ts, typename ...Us>
struct append<std::tuple<Ts...>, std::tuple<Us...>> {
  using type = std::tuple<Ts..., Us...>;
};

template <typename>
struct reverse;

template <typename T, typename ...Ts>
struct reverse<std::tuple<T, Ts...>> {
    using type = typename append<
      typename reverse<std::tuple<Ts...>>::type,
      std::tuple<T>
    >::type;
};

template <>
struct reverse<std::tuple<>> {
  using type = std::tuple<>;
};

static_assert(
  std::is_same_v<
    reverse<std::tuple<int, short, char>>::type,
    std::tuple<char, short, int>
  >
);

static_assert(
  std::is_same_v<
    reverse<std::tuple<int&, short, char, void, int*>>::type,
    std::tuple<int*, void, char, short, int&>
  >
);

A positive integer is an anti-prime when it has more divisors than each positive integer lesser than it.

https://www.youtube.com/watch?v=2JM2oImb9Qg

Write a function that find the largest anti-prime number not exceeding a given number.

1 - as there is no positive numbers below 1 the 1 is a first anti-prime and has one divisor.
2 - has two divisors: 1, 2 so is's anti-prime.
3 - has also two divisors: 1, 3 so it's NOT an anti-prime because 2 already had two divisors.

Other examples of anti-primes are: 1, 2, 4, 6, 12, 24.

export function solution(max: number): number {
  if (max === 0) {
    return 0;
  }
  let maxDiv: number = 0;
  let lastAnti = 1;
  for (let possibleAnti: number = 2; possibleAnti <= max; possibleAnti++) {
    let divCount = 1;
    for (let possibleDiv = 1; possibleDiv <= max; possibleDiv++) {
        if (possibleAnti % possibleDiv === 0) {
           divCount++;
        }
    }
    if (divCount > maxDiv) {
       maxDiv = divCount;
       lastAnti = possibleAnti;
    }
  }
  return lastAnti;
}

A factorial is a number that has been multiplied by each number preceding it. In this exercise, use recursion to get the factorial of any given number. A recursive function is one that calls itself conditionally, for example a function that takes a number, prints it out, then calls itself with func_name(parameter_name - 1) to restart the process using a number one lower. Combine this with an if statement to avoid a recursion error, and you have a basic recursive function. Use this concept to find the factorial of any given number.

def factorial(n):
    # your code goes here
    return

(Moved here per the discussion in https://www.codewars.com/kata/5fbc297af8ed8d0008e971c9/discuss)


Assume you have an array of Person objects that looks like this:

[
  {age: 10, hairColor: "brown"},
  {age: 30, hairColor: "red"},
  {age: 20, hairColor: "brown"},
  {age: 40, hairColor: "black"},
];

Create the following functions:

  • totalAgeYoungerThan - returns the total ages of the people younger than the given age
  • hairColorsYoungerThan - returns an array of the hair colors of the people younger than the given age

In each of these new functions, you may not use for loops, and instead must solve them using any of .map, .filter and/or .reduce.

// this code should fail the tests
// const totalAgeYoungerThan = (people, age) => {
//   let totalAge = 0;
//   for (let i = 0; i < people.length; i++) {
//     if (people[i].age < age) totalAge += people[i].age;
//   }
//   return totalAge;
// }

// this code should fail the tests
// const totalAgeYoungerThan = (people, age) => {
//   let totalAge = 0;
//   for (let person of people) {
//     if (person < age) totalAge += person.age;
//   }
//   return totalAge;
// }

// this code should fail the tests
// const totalAgeYoungerThan = (people, age) => {
//   if (people.length === 0) return 0;
//   const [first, ...rest] = people;
//   return (first.age < age ? first.age : 0) + totalAgeYoungerThan(rest, age);
// }

// this code should theoretically pass the tests
// it includes the word `for` but not in a for loop
// const totalAgeYoungerThan = (people, age) => {
//   return people
//     .filter(person => person.age < age)
//     .reduce((formula, person) => formula + person.age, 0)
// }

const totalAgeYoungerThan = (people, age) => {
  return people
    .filter(person => person.age < age)
    .reduce((sum, person) => sum + person.age, 0)
}

const hairColorsYoungerThan = (people, age) => {
  return people
    .filter(person => person.age < age)
    .map(person => person.hairColor)
}

Given an integer num, return an array consisting of positive consecutive integers that sum to num. There may be multiple sequences that work; return the sequence with the largest number of consecutive integers. Return an empty array if there are no solutions.

Example:

maxConsecutiveSum(45) returns [1,2,3,4,5,6,7,8,9].

Notice that [14,15,16] also add to 45 but there is a solution that has more numbers in it.

public class maxConsecutiveSum{
  public static int[] maxConsecutiveSum(int num){
    //code goes here
    return [];
  }
}

Nous dirons qu'un nombre n est positif n est parent du nombre à 2 chiffres ab
si son chiffre des unités est b et si ses autres chiffres sont différents de 0 et que leur somme est a.
Par exemple, les parents de 31 sont 31, 121, 211 et 1111

We say that a number n is parent of the number ab if it ones digit is equal to b and if he hasn't any digits equal to 0 and finally, if the sum of the digits of n is equal to a+b.

For example

121 is parent of 31 because :
1 = 1, 0 isn't in 1,2 and because 1 + 2 + 1 = 4 = 3 +1.

What you need to do

Complete the parents_of(n:int) function so that she return every parents of n between 1 and 1000.

def parents_of(n:int):
    toTest=[i for i in range(11,1000) if not "0" in str(i)]
    dics={str(i):[] for i in toTest} 
    for nb in toTest:
        for i in range(11,1000):
            if str(i)[-1]==str(nb)[-1]: #Si ils ont le même chiffre des unités
                if not "0" in str(i)[:-1]: #Si ils n'ont pas de 0
                    if sum([int(e) for e in str(i)])==sum([int(e) for e in str(nb)]): #Si ils ont la même some
                        dics[str(nb)]+=[i]
    return dics[str(n)]

Create an infinite list of alternating 1s and 0s, like so:

1:0:1:0:....
module Knot where

import Debug.Trace

onesAndZeroes :: Num a => [a]
onesAndZeroes =
  let x = 1 : y
      y = 0 : x
   in x