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
Algorithms
Logic
Search
Strings
Data Types

Write a function called findFirstSubString that accept two not null string(string and substring) and returns the index of the first occurrence of the substring in the string.

Examples

findFirstSubString("1234","12") => returns 0
findFirstSubString("012","12") => returns 1
findFirstSubString("4321","21") => returns 2
findFirstSubString("ABCDE","21") => returns null
findFirstSubString("","21") => returns null

Notes

  1. if no substring is found the function returns NULL
  2. the function returns only the index of the first occurrence of the string
fun findFirstSubString(string: String, subString: String): Int? {
    val stringArray = string.toList()
    val substringArray = subString.toList()

    if (stringArray.size < substringArray.size || stringArray.isEmpty() || substringArray.isEmpty()) {
        return null
    }

    var counter = 0
    var startIndex = 0

    var i = 0

    while (i < stringArray.size) {
        if (stringArray[i] == substringArray[counter]) {
            if(counter == 0) {
                startIndex = i
            }

            if ((counter + 1) == substringArray.size) {
                return startIndex
            }
            counter++
            i++
        }
        else {
            if (counter > 0) {
                i = startIndex + 1
            }
            else {
                i++
            }
            counter = 0
            startIndex = 0
        }

        println(i)
    }

    return null
}
Algorithms
Logic
Algebra
Mathematics

Summary

Given an integer number - let's call it N -, calculate the sum of the first positive integers up to N (inclusive) that are divisible by 3 or 7.

Example

Let's take N = 30:

Sum of integers divisible by 3 : 3 + 6 + 9 + 12 + 15 + 18 + 21 + 24 + 27 + 30 + 33 = 165 (#1)

Sum of integers divisible by 7 : 7 + 14 + 21 + 28 + 35 = 70 (#2)

Sum of integers divisible by 3 and 7 : 21 + 42 = 21 (#3)

Total : #1 + #2 - #3 = 165 + 70 - 21 = 214

Attention

Please be wary that N may be a large number (N larger than 1,000,000). This requires brushing up on optimization.

Caveat

There is one way of resolving this problem in O(1) complexity. For further details, please refer to this wiki.

using System;

namespace Kumite
{
  public class Problem
  {
    public static long Sum(int N)
    {
      // Add your code here.
      
      return 0;
    }
  }
}

Write a function that adds the digits of an integer. For example, the input receives a number: 10023. The result should be - 6 (1 + 0 + 0 + 2 + 3).

def digit_sum(number: int) -> int:
    return(sum([int(num) for num in str(abs(number))]))
function move(event)
    {
var k=event.keyCode; 

var chr = {

    updown : function (){
            var y=0;
            if (k==38) 
                {--y;
            }else if (k==40)
                 {++y;}
            return y; 
        },

    leftright : function (){
        var x=0;
        if (k==37) 
            {--x;
        }else if (k==39) 
            {++x;}
        return x; 
            }


    };

    chrId.style.top = (chr.updown())+"px";
    chrId.style.left = (chr.leftright())+"px";

}
#import <Foundation/Foundation.h>

@interface SampleClass:NSObject
/* method declaration */
- (int)odd_count:(int)n;
@end

@implementation SampleClass
- (int) odd_count:(int) n{
  //your code here
  int odd_counter = 0;
  
  for (int i = 1; i < n; i=i+2)  { 
        // Numbers that are divisible by 2 
        if (i % 2 != 0) 
            odd_counter++;
    } 
  return odd_counter;
}

@end
\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;
}