Skip to content

maximshushakov/code-challenges

Repository files navigation

Code challenges

Solutions for some coding challenges on Codewars

8. Sudoku Solver

Write a function that will solve a 9x9 Sudoku puzzle. The function will take one argument consisting of the 2D puzzle array, with the value 0 representing an unknown square. Codewars Kata

Solution in sudoku-solver.js

const puzzle = [
  [5,3,0,0,7,0,0,0,0],
  [6,0,0,1,9,5,0,0,0],
  [0,9,8,0,0,0,0,6,0],
  [8,0,0,0,6,0,0,0,3],
  [4,0,0,8,0,3,0,0,1],
  [7,0,0,0,2,0,0,0,6],
  [0,6,0,0,0,0,2,8,0],
  [0,0,0,4,1,9,0,0,5],
  [0,0,0,0,8,0,0,7,9]
];

// should solve sudoku puzzle
Sudoku.solve(puzzle)

7. Binary Genetic Algorithms

Create GeneticAlgorithm class for a finding random binary string of 35 (or more) digits with a preloaded fitness function.
Codewars Kata

Solution in genetic-algorithm.js

// should return "101010101010101"
new GeneticAlgorithm().run(fitness('101010101010101'), 15, .6, .002)

6. My smallest code interpreter (aka Brainf**k)

Create an interpreter of esoteric programming language (Brainf**k). [It has eight simple commands and an instruction pointer]. Codewars Kata

Solution in brainf**k.js

// should return "Hello World!"
brainfk('++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.');

// should multiply 8 by 9 and return char code for the result
brainfk(',>,<[>[->+>+<<]>>[-<<+>>]<<<-]>>.', String.fromCharCode(8,9));

5. K-means

Implement k-means clustering algorithm.
This implementation was used for solving Decode the Morse code Kata

kmeans(data, centroids, iterations, filter) where
data - an array of non-clustered values
centroids - centroids for the first iteration
iterations - max number of iterations
filter - a function what invokes each iteration for more specific clastering

Solution in k-means.js

// should return 3 clusters: [[0,1,2,3,4,5],[10,11,12,13],[20,21,22,23]]
kmeans([0,1,2,3,4,5,10,11,12,13,20,21,22,23], [1,2,3]).clusters

4. Base64 Encoding

Extend the String object with methods that convert the value of the String to and from Base64 using the ASCII character set. Codewars Kata

Solution in base64-encoding.js

// should return 'dGhpcyBpcyBhIHN0cmluZw=='
'this is a string'.toBase64(); 

// should return 'this is a string'
'dGhpcyBpcyBhIHN0cmluZw=='.fromBase64();

3. Look And Say

Implement a function that reads off the digits of the input:

lookAndSay(1) = 11 because it reads off 'one 1'

lookAndSay(11) = 21 because it reads off 'two 1'

lookAndSay(21) = 1211 because it reads off 'one 2, one 1'

lookAndSay(digits, n) n times recursion of lookAndSay

Solution in look-and-say.js

// should return 111221
lookAndSay(1211)

// should return 312211
lookAndSay(111221)

// should return 1211 because 21 => 1211
lookAndSay(11, 2)

// should return 22 because 22 => always 22
lookAndSay(22, 100)

// should return 22, performance test
lookAndSay(22, 10000000)

2. Base Atlassian

Implement a function that converts an integer to base 7 and returns encoded string based on next codes:
['0', 'a', 't', 'l', 's', 'i', 'n']

Solution in base-atlassian.js

// should return a0
convert(7)

// should return atlassian
convert(7792875)

// should return sansalita
convert(24659559)

1. Find in a list

Implement basic List data structure and an indexOf like function for searching index of

Solution in find-in-list.js

// should return 1
find(List(1,2,3), List(2,3))

// should return -1
find(List(1,2,3), List(3,2))

// should return 4
find(List(1,2,3,1,3,2), List(3,2))