Coding Dojo

Edit on Gitlab

Diamond

Python

alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R","S", "T", "U", "V", "W", "X", "Y", "Z"];
input = "E"

index = alphabet.index(input)


arr = alphabet[1:index] + alphabet[index:0:-1]

total = index*2 + 1
edge = index - 1

print(" "*index + "A" + " "*index)
for i in arr:
    print(" " *abs(edge) + i + " " * (total - 2 * abs(edge) - 2) + i + " " * abs(edge))
    edge = edge - 1
print(" " * index + "A" + " " * index)

Rust

This is the more accurate solution for the moment for the Diamond kata. Feel free to create a merge request on the codingdojo repository to improve it.

FizzBuzz

This is the list of solutions to the KataFizzBuzz.

Java

This is a solution with the twist of supplying the filtering from the outside of the ClassUnderTest .

FizzBuzzTest.java:

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;



public class FizzBuzzTest {

    private static final Object[] ARRAY_OF_2_FIZZ = {2, "Fizz"};
    private static final Object[] ARRAY_OF_1_2 = {1, 2};
    private static final Object[] ARRAY_OF_FIZZ_7_8_FIZZ = {"Fizz",
                                                            7, 8,
                                                            "Fizz"};
    private static final Object[] ARRAY_OF_BUZZ = {"Buzz"};
    private static final Object[] ARRAY_OF_FIZZBUZZ = {"FizzBuzz"};
    private static final Object[] ARRAY_OF_FIZZBUZZ_TO_FIZZ = { "FizzBuzz",
                                                                16, 17,
                                                                "Fizz",
                                                                19,
                                                                "Buzz", "Fizz",
                                                                22, 23,
                                                                "Fizz", "Buzz",
                                                                26,
                                                                "Fizz",
                                                                28, 29,
                                                                "FizzBuzz"};

    FizzBuzz fizzBuzz = new FizzBuzz();

    @Before
    public void addFilters() {
        fizzBuzz.addFilter(new FizzBuzzFilter() {
            public String filter(int integer) {
                return integer % 3 == 0? "Fizz" : "";
            }
        });
        fizzBuzz.addFilter(new FizzBuzzFilter() {
            public String filter(int integer) {
                return integer % 5 == 0? "Buzz" : "";
            }
        });
    }
    
    @Test
    public void shouldReturn1And2AsIs() throws Exception {
        assertArrayEquals(ARRAY_OF_1_2, fizzBuzz.filter(range(1, 2)));
    }
    

    @Test
    public void shouldConvertThreeToFizz() throws Exception {
        assertArrayEquals(ARRAY_OF_2_FIZZ, fizzBuzz.filter(range(2, 3)));
    }

    @Test
    public void shouldConvert6And9ToFizz() throws Exception {
        assertArrayEquals(ARRAY_OF_FIZZ_7_8_FIZZ,
                          fizzBuzz.filter(range(6, 9)));
    }
    
    @Test
    public void shouldConvert5ToBuzz() throws Exception {
        assertArrayEquals(ARRAY_OF_BUZZ, fizzBuzz.filter(range(5, 5)));
    }
    
    @Test
    public void shouldConvert15ToFizzBuzz() throws Exception {
        assertArrayEquals(ARRAY_OF_FIZZBUZZ,
                          fizzBuzz.filter(range(15, 15)));
    }
    
    @Test
    public void shouldConvert15To30Correctly() throws Exception {
        assertArrayEquals(ARRAY_OF_FIZZBUZZ_TO_FIZZ,
                          fizzBuzz.filter(range(15, 30)));
    }

    private int[] range(int start, int end) {
        int[] range = new int[end-start+1];
        for (int count = 0; count < range.length; count++)
          range[count] = start+count;
        return range;
    }
}
FizzBuzz.java:

import java.util.ArrayList;


public class FizzBuzz {

    private ArrayList<FizzBuzzFilter> filters = new ArrayList<FizzBuzzFilter>();

    public Object[] filter(int[] integers) {
        Object[] result = new Object[integers.length];
        for (int i = 0; i < integers.length; i++) {
            result[i] = convert(integers[i]);
        }
        return result;
    }

    private Object convert(int integer) {
        String converted = applyFilters(integer);
        return "".equals(converted) ? integer : converted;
    }

    private String applyFilters(int integer) {
        String converted = "";
        for (FizzBuzzFilter filter : filters) {
            converted += filter.filter(integer);
        }
        return converted;
    }

    public void addFilter(FizzBuzzFilter fizzBuzzFilter) {
        filters.add(fizzBuzzFilter);
    }
}

Smalltalk

    fb := [:counter| |rules|
       rules := {15->'FizzBuzz'. 5->'Buzz'. 3->'Fizz'. 1->counter}.
       rightRule := rules detect: [:aRule| counter \\ aRule key == 0].
       rightRule value].  

self assert: (fb value: 7) == 7.
self assert: (fb value: 3) == 'Fizz'.
self assert: (fb value: 5) == 'Buzz'.
self assert: (fb value: 15) == 'FizzBuzz'.

1 to: 100 do: [:counter | 
               Transcript 
                 show: (fb value: counter) asString;
                 cr]

C++

This solution was made on Linux with the unit test framework Catch2 placed at the same level as our “main”, and compiled with g++:

FizzBuzz Java

/*

  • @ProjectName:mianshi

  • @Package:PACKAGE_NAME

  • @ClassName:MianShi

  • @Author:Rain

  • @Date:2022-01-05 10:17

  • @Description: */ public class MianShi { public static void main(String[] args) {

     m1();
     System.out.println("********************");
     m2();
    

    }

    public static void m1() { for (int i = 1; i <= 100; i++) { if (i % 3 == 0 && i % 5 == 0) { System.out.println(“FizzBuzz”); } else if (i % 3 == 0) { System.out.println(“Fizz”); } else if (i % 5 == 0) { System.out.println(“Buzz”); } else { System.out.println(i); } } }

Kata Roman Numerals

Feel free to create a merge request on the codingdojo repository to suggest your own solution.

Python

This is the most readable solution of the Roman Numerals Kata downloaded on this website.

#!/usr/bin/env python

def number_to_numeral(number):
    """
    >>> number_to_numeral(1)
    'I'
    >>> number_to_numeral(2)
    'II'
    >>> number_to_numeral(12)
    'XII'
    >>> number_to_numeral(10)
    'X'
    >>> number_to_numeral(20)
    'XX'
    >>> number_to_numeral(99)
    'XCIX'
    >>> number_to_numeral(100)
    'C'
    >>> number_to_numeral(199)
    'CXCIX'
    >>> number_to_numeral(2399)
    'MMCCCXCIX'
    >>> number_to_numeral(4000)
    Traceback (most recent call last):
    ...
    IndexError: list index out of range

    """

    numerals_dict = {
        'ones':     ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
        'tens':     ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],
        'hundreds': ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'],
        'thousands': ['', 'M', 'MM', 'MMM'],
    }

    s = []
    for index in ['ones', 'tens', 'hundreds', 'thousands']:
        number, remainder = divmod(number, 10)
        s.insert(0, numerals_dict[index][remainder])
    return ''.join(s)

KataBankInGroovyByNiravAssar

This was a solution I did in groovy. I have posted the entire package on

google docs. The notable points to the solution are as follows:

  • done in groovy

  • done with object oriented principles and design

  • uses recursion

  • contains some high level documentation

https://docs.google.com/leaf?id=0B62m4BA4zKvjMTdlZTNkNDktN2IyMS00YjcxLWJkN2ItZDc5MjNhMjYwOWE4&sort=name&layout=list&num=50

You can contact me with questions at

KataBowlingByAndreasLarsson

In CodersDojoSweden we have done the KataBowling quite a lot of times.

Our test suite usually ends up like:

  • Gutter game = all zeroes, (score = 0)
  • One pin down in each roll, (score = 20)
  • Spare in first roll, one pin down in each other roll, (score = 10 + 1 + 18 = 29)
  • Spare in last roll, one pin down in each other roll, (score = 18 + 10 + 1 = 29)
  • Strike in first roll, one pin down in each other roll, (score = 10 + 1 + 1 + 18 = 30)
  • Strike in last roll, one pin down in each other roll, (score = 18 + 10 + 1 + 1 = 30)
  • Golden game = all strikes (score = 300)

The highligts of the BowlingGameKata is:

KataFizzBuzzSmalltalkSolution

    fb := [:counter| |rules|
       rules := {15->'FizzBuzz'. 5->'Buzz'. 3->'Fizz'. 1->counter}.
       rightRule := rules detect: [:aRule| counter \\ aRule key == 0].
       rightRule value].  

self assert: (fb value: 7) == 7.
self assert: (fb value: 3) == 'Fizz'.
self assert: (fb value: 5) == 'Buzz'.
self assert: (fb value: 15) == 'FizzBuzz'.

1 to: 100 do: [:counter | 
               Transcript 
                 show: (fb value: counter) asString;
                 cr]

KataFizzBuzzSolution

This my solution to the [KataFizzBuzz](/kata/FizzBuzz) , with the added

twist of supplying the filtering from the outside of the ClassUnderTest .

FizzBuzzTest.java:

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;



public class FizzBuzzTest {

    private static final Object[] ARRAY_OF_2_FIZZ = {2, "Fizz"};
    private static final Object[] ARRAY_OF_1_2 = {1, 2};
    private static final Object[] ARRAY_OF_FIZZ_7_8_FIZZ = {"Fizz", 7,
                                                            8, "Fizz"};
    private static final Object[] ARRAY_OF_BUZZ = {"Buzz"};
    private static final Object[] ARRAY_OF_FIZZBUZZ = {"FizzBuzz"};
    private static final Object[] ARRAY_OF_FIZZBUZZ_TO_FIZZ = { "FizzBuzz", 16,
                                                                17, "Fizz", 19,
                                                                "Buzz", "Fizz",
                                                                22, 23, "Fizz",
                                                                "Buzz", 26,
                                                                "Fizz", 28, 29,
                                                                "FizzBuzz"};

    FizzBuzz fizzBuzz = new FizzBuzz();

    @Before
    public void addFilters() {
        fizzBuzz.addFilter(new FizzBuzzFilter() {
            public String filter(int integer) {
                return integer % 3 == 0? "Fizz" : "";
            }
        });
        fizzBuzz.addFilter(new FizzBuzzFilter() {
            public String filter(int integer) {
                return integer % 5 == 0? "Buzz" : "";
            }
        });
    }
    
    @Test
    public void shouldReturn1And2AsIs() throws Exception {
        assertArrayEquals(ARRAY_OF_1_2, fizzBuzz.filter(range(1, 2)));
    }
    

    @Test
    public void shouldConvertThreeToFizz() throws Exception {
        assertArrayEquals(ARRAY_OF_2_FIZZ, fizzBuzz.filter(range(2, 3)));
    }

    @Test
    public void shouldConvert6And9ToFizz() throws Exception {
        assertArrayEquals(ARRAY_OF_FIZZ_7_8_FIZZ, fizzBuzz.filter(range(6, 9)));
    }
    
    @Test
    public void shouldConvert5ToBuzz() throws Exception {
        assertArrayEquals(ARRAY_OF_BUZZ, fizzBuzz.filter(range(5, 5)));
    }
    
    @Test
    public void shouldConvert15ToFizzBuzz() throws Exception {
        assertArrayEquals(ARRAY_OF_FIZZBUZZ, fizzBuzz.filter(range(15, 15)));
    }
    
    @Test
    public void shouldConvert15To30Correctly() throws Exception {
        assertArrayEquals(ARRAY_OF_FIZZBUZZ_TO_FIZZ,
                          fizzBuzz.filter(range(15, 30)));
    }

    private int[] range(int start, int end) {
        int[] range = new int[end-start+1];
        for (int count = 0; count < range.length; count++)
            range[count] = start+count;
        return range;
    }
}
FizzBuzz.java:

import java.util.ArrayList;


public class FizzBuzz {

    private ArrayList<FizzBuzzFilter> filters = new ArrayList<FizzBuzzFilter>();

    public Object[] filter(int[] integers) {
        Object[] result = new Object[integers.length];
        for (int i = 0; i < integers.length; i++) {
            result[i] = convert(integers[i]);
        }
        return result;
    }

    private Object convert(int integer) {
        String converted = applyFilters(integer);
        return "".equals(converted) ? integer : converted;
    }

    private String applyFilters(int integer) {
        String converted = "";
        for (FizzBuzzFilter filter : filters) {
            converted += filter.filter(integer);
        }
        return converted;
    }

    public void addFilter(FizzBuzzFilter fizzBuzzFilter) {
        filters.add(fizzBuzzFilter);
    }
}

KataRomanNumeralsAsReadableAsWeCouldMakeIt

#!/usr/bin/env python
#
## Roman Numerals Kata
##

def number_to_numeral(number):
    """
    >>> number_to_numeral(1)
    'I'
    >>> number_to_numeral(2)
    'II'
    >>> number_to_numeral(12)
    'XII'
    >>> number_to_numeral(10)
    'X'
    >>> number_to_numeral(20)
    'XX'
    >>> number_to_numeral(99)
    'XCIX'
    >>> number_to_numeral(100)
    'C'
    >>> number_to_numeral(199)
    'CXCIX'
    >>> number_to_numeral(2399)
    'MMCCCXCIX'
    >>> number_to_numeral(4000)
    Traceback (most recent call last):
    ...
    IndexError: list index out of range

    """

    numerals_dict = {
        'ones':     ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
        'tens':     ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],
        'hundreds': ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'],
        'thousands': ['', 'M', 'MM', 'MMM'],
    }

    s = []
    for index in ['ones', 'tens', 'hundreds', 'thousands']:
        number, remainder = divmod(number, 10)
        s.insert(0, numerals_dict[index][remainder])
    return ''.join(s)

KataRomanNumeralsYetAnotherOne

import doctest,pdb

def roman\_help(value, low, medium, high):

    """
    >>> roman_help(1, "I", "V", "X")
    'I'
    >>> roman_help(2, "I", "V", "X")
    'II'
    >>> roman_help(3, "I", "V", "X")
    'III'
    >>> roman_help(4, "I", "V", "X")
    'IV'
    >>> roman_help(5, "I", "V", "X")
    'V'
    >>> roman_help(6, "I", "V", "X")
    'VI'
    >>> roman_help(7, "I", "V", "X")
    'VII'
    >>> roman_help(8, "I", "V", "X")
    'VIII'
    >>> roman_help(9, "I", "V", "X")
    'IX'
    >>> roman_help(10, "I", "V", "X")
    'X'
    >>> roman_help(11, "I", "V", "X")
    'XI'
    >>> roman_help(12, "I", "V", "X")
    'XII'
    >>> roman_help(13, "I", "V", "X")
    'XIII'
    >>> roman_help(14, "I", "V", "X")
    'XIV'
    >>> roman_help(15, "I", "V", "X")
    'XV'
    >>> roman_help(16, "I", "V", "X")
    'XVI'
    >>> roman_help(17, "I", "V", "X")
    'XVII'
    >>> roman_help(18, "I", "V", "X")
    'XVIII'
    >>> roman_help(19, "I", "V", "X")
    'XIX'
    >>> roman_help(20, "I", "V", "X")
    'XX'

    """

    cases={1:low, 2:low*2, 3:low*3, 4:low+medium, 5: medium,
     6:medium+low, 7:medium+low*2, 8: medium+low*3, 9: low+high,0:''
     }
    return high*(value/10 )+cases[value%10]

def roman(value):

    """
    >>> roman(1)
    'I'
    >>> roman(2)
    'II'
    >>> roman(3)
    'III'
    >>> roman(4)
    'IV'
    >>> roman(5)
    'V'
    >>> roman(6)
    'VI'
    >>> roman(7)
    'VII'
    >>> roman(8)
    'VIII'
    >>> roman(9)
    'IX'
    >>> roman(10)
    'X'
    >>> roman(19)
    'XIX'
    >>> roman(49)
    'XLIX'
    >>> roman(99)
    'XCIX'
    >>> roman(309)
    'CCCIX'
    >>> roman(527)
    'DXXVII'
    >>> roman(789)
    'DCCLXXXIX'
    >>> roman(1888)
    'MDCCCLXXXVIII'
    >>> roman(1999)
    'MCMXCIX'
    >>> roman(2001)
    'MMI'

    """
    out=""

    if value/100:
       out+=roman_help(value/100, "C", "D", "M")
       value=value%100
    if value/10:
       out+=roman_help(value/10, "X", "L", "C")
       value=value%10
    if value:
       out+=roman_help(value, "I", "V", "X")
    return out

if \_\_name\_\_=='\_\_main\_\_':

    print doctest.testmod()