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

Write a function, that returns a strings of Fizz, Buzz or FizzBuzz. Signature on python:

def fizz_buzz(fizz: int, buzz: int, seq: Iterable) -> str:
...

Where

fizz, buzz - any natural number
seq - any sequence of natural numbers

Example:

fizz_buzz(fizz=3, buzz=5, seq=range(1, 16))
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

The input data will always be correct.

def fizz_buzz(fizz, buzz, seq):
    ... # your code

Given a positive integer n, write all the unique ways to sum to it using only positive integers sorted in descending order to the given ostringstream. Each partition should be written as a line of the integers in weakly descending order delimited by spaces with a trailing space on each line. A trailing newline should be written.

I ended up feeling like implementing integer partitions. The way I did it here is a huge mess, but quick-bench.com says it is faster than the C++ solution from https://origin.geeksforgeeks.org/generate-unique-partitions-of-an-integer/, written by Hariprasad NG. This is likely due to the fact that I wrote my solution specifically for the output to be written to a stream immediately, but you can make your own kumite if you have a problem with that. Also, I think my solution is unique enough that it is worth publishing.

#include <string>
#include <deque>
#include <sstream>

void print_all_unique_parts(int n, std::ostringstream &oss)
{
    oss << n << ' ' << '\n';
    std::deque<std::deque<std::string>> dp[n];
    for (int i0 = 1; i0 < n - 1; ++i0)
    {
        std::string oss_prefix = std::to_string(n - i0) + ' ';
        dp[i0].emplace_front();
        if (n - i0 >= i0)
        {
            dp[i0][0].push_front(std::to_string(i0) + ' ');
            oss << oss_prefix << dp[i0].front().front() << '\n';
        }
        for (int i1 = i0 - 1 <= n - i0 ? i0 - 1 : n - i0; i1; --i1)
        {
            std::cout << i0 << ' ' << i1 << '\n';
            std::string dp_prefix = std::to_string(i1) + ' ';
            dp[i0].emplace_front();
            std::deque<std::string> &out_deque = dp[i0].front();
            std::deque<std::deque<std::string>> &in_deque = dp[i0 - i1];
            for (int i2 = i1 - 1; i2 >= 0; --i2)
            {
                for (std::string body : in_deque[i2]) 
                {
                    out_deque.push_back(dp_prefix + body);
                    oss << oss_prefix << out_deque.back() << '\n';
                }
            }
        }
    }
    for (int i = 0; i < n; ++i)
    {
        oss << "1 ";
    }
    oss << '\n';
}
Arithmetic
Mathematics
Algorithms
Logic
Numbers

Input:
A list of length 1 to 10

Output:
The sum of the last two items of the list

Examples:

  • [1,2] -> 3
  • [-1,2] -> 1
  • [2,2,-4] -> -2
  • [4] -> 4
def sum_last_two_items(ls):
    return ls[-1]+ls[-2] if len(ls)>1 else ls[0]

It is a 1 in 846288600223 chance to work (unless my math was wrong) even using only the characters in the desired string.

import string
import random

def greet():
    hope = ''.join(random.choice("helowrd! ") for _ in range(12))
    return hope

I don't know why I like impossible (well rather, theoretically possible but so unlikely that it will never happen) stuff so much.

Since there are 26 letters in the alphabet, 100 in a string, and 5 tests, it will be (26^100)^5.

The total odds are 1 in... the calculator on Google will not say.

Each test has a 1 in 3.142931e+141 chance.

using System;
namespace Impossible
{
  public static class IM
  {
    public static string HundredCharacterString()
    {
      string result = "";
      Random rand = new Random();
      for(int i = 0; i < 100; i ++)
      {
        result += ((char)rand.Next(65, 91)).ToString();
      }
      return result;
    }
  }
}

Examples
"cac" => returns "cc"
"aabcc" => returns "aacc"
"aaabbbg" => returns "aaabbb"
"pppenm" => returns "ppp"
"ekekua" => returns "ekek"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* string_editor(char* str)
{
	char temp = 0;
	char size = (char)strlen(str);
	char *new = (char*)calloc(size,sizeof(char));
	for (char i = 0; i < size; i++)
		for (char j = 0; j < size; j++)
			if ((str[i] == str[j]) && (i != j))
				new[temp++] = str[i];
	new[temp] ='\0';
	return new;
}

In this challenge, first we want to find out the maximum value between number of occurrences for each characters, then only return characters which have the same number of occurrences as maximum value.

For example, "abcde" is a valid string, because max count of characters is 1, and other all character counts are one, so we can return "abcde". On the other hand, "abcdeee" is not valid, because count of 'e' is 3, not 1 as the others. So we need to get rid of "a","b","c" and "d", so we can return "eee".

Specification

string_editor(str)
Remove all characters from string according to maximum count of a character in the input String

Parameters
str: char* - input to work on

Return Value
char* - final decision about that string

Constraints
Input string consists of English alphabet characters
All of the characters are lowercase

Examples
"cac" => returns "cc"
"aabcc" => returns "aacc"
"aaabbbg" => returns "aaabbb"
"pppenm" => returns "ppp"
"ekekua" => returns "ekek"

#include <stdlib.h>
#include <string.h>

char* string_editor(const char* str)
{
	size_t s = 0;
	size_t count[256] = { 0 };
	size_t size = strlen(str);
	size_t max = 0;
	char *new = (char*)calloc(size + 1, sizeof(char));
	for (size_t i = 0; i < size; ++i)
		++count[str[i]];
	for (size_t i = 0; i < size; ++i)
		if (count[str[i]] > max)
			max = count[str[i]];
	for (size_t i = 0; i < size; ++i)
		if (count[str[i]] == max)
			new[s++] = str[i];
	new = (char *)realloc(new, (s + 1) * sizeof(char));
	new[s] = '\0';
	return new;
}
def multiplica(a, b):
    return a * b
resultado = multiplica(4, 5)
print(resultado)
Mathematics
Algorithms
Logic
Numbers
Data Types

The task: make an array of arrays which store n rows of pascal's triangle, where each entry is each successive row starting from the 0th row. For all n < 0, you can return an empty array.

Examples (with whitespace added for clarity):

pascal-triangle(-1) == []

pascal-triangle(0) == [
  [1]
]

pascal-triangle(1) == [
  [1],
  [1, 1]
]

...

pascal-triangle(3) == [
  [1],
  [1, 1],
  [1, 2, 1],
  [1, 3, 3, 1]
]

... etc
USING: arrays combinators kernel locals math math.ranges sequences ;
IN: pascal

: pascal-index ( n seq -- elt/0 )
  ?nth dup not [ drop 0 ] when ;
  
:: pascal-add ( seq n -- m )
  n 1 - seq pascal-index
  n seq pascal-index
  + ;

:: pascal-next ( arr -- arr' )
  arr empty? [
    { { 1 } }
  ] [
    arr ?last :> lastarr
    lastarr length 1 + :> newlen
    { } :> curr!
    
    newlen [0,b) [| n |
      curr lastarr n pascal-add suffix curr!
    ] each
    
    arr curr suffix
  ] if ;

:: pascal-triangle ( n -- arr ) 
  { } n 1 + [ pascal-next ] times ;

A friend had showed this too me a couple of days ago on TeleHack.com (which you all should check out). It uses lambda to print the numbers in the fibonacci sequence.

#To print a specific term in the sequence
f = lambda a, b: (a lambda: f(b, a + b))
g = f(0, 1)

n = int(input('What term?: '))

for i in range(1, n):
    g = g[1]()
    print(g[0])

#To print the sequence upto this term
f = lambda a, b: (a lambda: f(b, a + b))
g = f(0, 1)

n = int(input('What term?: '))

for i in range(1, n):
    g = g[1]()
    print(g[0]) #Simply just tabbing this into the for loop