Skip to content

Instantly share code, notes, and snippets.

View hassan-maavan's full-sized avatar
😏
what else?

Hassan Raza hassan-maavan

😏
what else?
View GitHub Profile
@hassan-maavan
hassan-maavan / secret_string.txt
Last active September 17, 2020 06:56
Recover a secret string from random triplets
var recoverSecret = function(triplets) {
let duplets = [];
let res = '';
triplets.forEach(trip => {
duplets.push(trip.slice(0,2).join(''));
duplets.push(trip.slice(1, 3).join(''));
})
duplets = duplets.filter((value, index, array) => array.indexOf(value) === index)
let word = getWord(duplets);
while(word) {
@hassan-maavan
hassan-maavan / Remove K
Created September 17, 2020 07:20
Given a number k, find the smallest integer n such that if any k elements are removed from the set {1, 2, ..., n}, one can still find k distinct numbers among the remaining elements with sum of n.
function removeK(k){
return k * (3*k + 1) / 2;
}
@hassan-maavan
hassan-maavan / String array duplicates
Created September 17, 2020 08:05
String array duplicates In this Kata, you will be given an array of strings and your task is to remove all consecutive duplicate letters from each string in the array. For example: dup(["abracadabra","allottee","assessee"]) = ["abracadabra","alote","asese"]. dup(["kelless","keenness"]) = ["keles","kenes"]. Strings will be lowercase only, no spac…
function dup(s) {
return s.map((str) => Array.from(str).filter((v, i, arr) => arr[i - 1] !== v).join(''))
};
@hassan-maavan
hassan-maavan / gist:c69ed63796b37aad65f9bfcdcc35a847
Created September 18, 2020 19:11
16+18=214 For this Kata you will have to forget how to add two numbers together. In simple terms, our method does not like the principle of carrying over numbers and just writes down every number it calculates :-) You may assume both integers are positive integers
num1 = ('' + num1).split('').reverse()
num2 = ('' + num2).split('').reverse()
let max = Math.max(num1.length, num2.length)
let str = [];
for(let i = 0; i < max; i++)
str.push((parseInt(num1[i]) || 0) + (parseInt(num2[i]) || 0))
num1 = str.reverse().join('');
return parseInt(num1);
@hassan-maavan
hassan-maavan / 16+18=214
Created September 18, 2020 19:11
16+18=214 For this Kata you will have to forget how to add two numbers together. In simple terms, our method does not like the principle of carrying over numbers and just writes down every number it calculates :-) You may assume both integers are positive integers https://www.codewars.com/kata/5effa412233ac3002a9e471d/train/javascript
num1 = ('' + num1).split('').reverse()
num2 = ('' + num2).split('').reverse()
let max = Math.max(num1.length, num2.length)
let str = [];
for(let i = 0; i < max; i++)
str.push((parseInt(num1[i]) || 0) + (parseInt(num2[i]) || 0))
num1 = str.reverse().join('');
return parseInt(num1);
@hassan-maavan
hassan-maavan / gist:318b3c3d48c1607345c3ee45d93db3a4
Created September 19, 2020 06:18
Create a RomanNumerals class that can convert a roman numeral to and from an integer value. It should follow the API demonstrated in the examples below. Multiple roman numeral values will be tested for each helper method. Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digi…
var RomanNumerals = {
toRoman : function (number) {
var ret = '';
while(number > 0) {
if(number/1000 >= 1) {
ret += 'M';
number -=1000;
} else if(number/900 >= 1) {
ret += 'CM';
number -=900;
@hassan-maavan
hassan-maavan / Snail Sort
Created September 19, 2020 06:24
Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise. array = [[1,2,3], [4,5,6], [7,8,9]] snail(array) #=> [1,2,3,6,9,8,7,4,5]
snail = function(array) {
if(array.length === 0) return [];
if(array.length === 1) return array[0];
let top = array[0].slice(0,-1);
let right = array.map(a => a[a.length - 1]);
let bottom = array[array.length - 1].slice(0, -1).reverse();
let left = array.slice(1, -1).map(b => b[0]).reverse();
let inner = array.slice(1, -1).map(c => c.slice(1, -1));
return [].concat(top, right, bottom, left, snail(inner));
}
@hassan-maavan
hassan-maavan / Diagonals sum
Created September 19, 2020 06:28
Create a function that receives a (square) matrix and calculates the sum of both diagonals (main and secondary) Matrix = array of n length whose elements are n length arrays of integers. 3x3 example: https://www.codewars.com/kata/5592fc599a7f40adac0000a8/javascript
function sum(matrix) {
let i = 0;
let sum = 0;
matrix.forEach(array => {
sum += array[i];
sum += array[array.length - i -1]
i++;
if(i >= array.lenght) {
i = 0;
}
@hassan-maavan
hassan-maavan / Nesting Structure Comparison
Created September 19, 2020 06:33
Complete the function/method (depending on the language) to return true/True when its argument is an array that has the same nesting structures and same corresponding length of nested arrays as the first array. https://www.codewars.com/kata/520446778469526ec0000001
Array.prototype.sameStructureAs = function (other) {
Array.prototype.status = Array.prototype.status || true;
if(this.length != other.length){
Array.prototype.status = false;
return false;
}
for(var index = 0; index < this.length; index++)
{
@hassan-maavan
hassan-maavan / Permutations
Created September 19, 2020 06:36
In this kata you have to create all permutations of an input string and remove duplicates, if present. This means, you have to shuffle all letters from the input in all possible orders. https://www.codewars.com/kata/5254ca2719453dcc0b00027d
function permutations(string) {
var arr = string.split(''), tmp = arr.slice(), heads = [], out = [];
if(string.length == 1) return [string];
arr.forEach(function(v, i, arr) {
if(heads.indexOf(v) == -1) {
heads.push(v);
tmp.splice(tmp.indexOf(v), 1);
permutations(tmp.join('')).forEach(function(w) {out.push(v + w);});
tmp.push(v);
}