Giter Site home page Giter Site logo

maximshushakov / code-challenges Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 1.0 30 KB

Some solutions for coding challenges (from Codewars)

Home Page: https://www.codewars.com/users/mshushakov

JavaScript 100.00%
coding-challenges challenge codewars-solutions codewars codewars-javascript

code-challenges's Introduction

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))

code-challenges's People

Contributors

maximshushakov avatar mshushakov avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

delta94

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.