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 / Categorize New Member
Created September 19, 2020 06:46
The Western Suburbs Croquet Club has two categories of membership, Senior and Open. They would like your help with an application form that will tell prospective members which category they will be placed. To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +…
function openOrSenior(data){
// ...
return data.map(array => array[0] >= 55 && array[1] > 7 ? 'Senior' : 'Open');
}
@hassan-maavan
hassan-maavan / Does my number look big in this?
Created September 19, 2020 06:45
A Narcissistic Number is a positive number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this Kata, we will restrict ourselves to decimal (base 10). https://www.codewars.com/kata/5287e858c6b5a9678200083c
function narcissistic(value) {
let array = value.toString().split('');
let count = array.length;
let result = array.map((num) => Math.pow(num, count));
if(eval(result.join(',').replace(/,/g, '+')) == value) {
return true;
} else {
return false;
}
}
@hassan-maavan
hassan-maavan / The Supermarket Queue
Created September 19, 2020 06:44
There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out! input customers: an array of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out. n: a posi…
function queueTime(customers, n) {
let i = 0,
tills = [];
for(i; i < n; i++){
tills[i] = 0;
}
customers.forEach((min) => {
tills.fill((getMin(tills)['min'] + min), getMin(tills)['index'], getMin(tills)['index'] + 1);
});
@hassan-maavan
hassan-maavan / Multiples of 3 or 5
Created September 19, 2020 06:44
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. Note: If the number is a multiple of both 3 and 5, only count it once. Also, if a number is negative, return 0(…
function solution(number){
var sum = 0;
for(var i = 1;i< number; i++){
if(i % 3 == 0 || i % 5 == 0){
sum += i
}
}
return sum;
}
@hassan-maavan
hassan-maavan / Split Strings
Created September 19, 2020 06:42
Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').
function solution(str){
var str_array = str.split('');
var result = [];
str_array.forEach((value, key) => {
if(key % 2 == 0) {
let next = (str_array[key + 1] ? str_array[key + 1] : '_')
result.push(value + next);
}
});
return result;
@hassan-maavan
hassan-maavan / Watermelon
Created September 19, 2020 06:42
It's too hot, and they can't even… Given an integral number of watermelons w (1 ≤ w ≤ 100; 1 ≤ w in Haskell), check whether Pete and Billy can divide the melons so that each of them gets an even amount.https://www.codewars.com/kata/55192f4ecd82ff826900089e
function divide(weight){
//your code here
return weight > 2 && weight % 2 == 0 ? true : false;
}
@hassan-maavan
hassan-maavan / Simple Pig Latin
Created September 19, 2020 06:39
Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched. https://www.codewars.com/kata/520b9d2ad5c005041100000f
function pigIt(str){
var format = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
return str.split(' ').map((word) => {
if(format.test(word))
return word;
let array = word.split('');
let first = array.shift();
return (array.join('') + first + 'ay');
}).join().replace(/,/g, ' ');
}
@hassan-maavan
hassan-maavan / Human readable duration format
Created September 19, 2020 06:37
Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way. The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds. https://www.codew…
function formatDuration (sec) {
if(sec == 0)
return 'now';
let s = sec % 60;
let m = Math.floor((sec % 3600) / 60);
let h = Math.floor((sec % 86400) / 3600);
let d = Math.floor((sec % 31536000) / 86400);
let Y = Math.floor(sec / 31536000);
let string = '';
let flag = false;
@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);
}
@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++)
{