Skip to content

Instantly share code, notes, and snippets.

@Sohail05
Created April 30, 2018 01:12
Show Gist options
  • Select an option

  • Save Sohail05/bd601b49b0a1f8e06c1469bb2d86057f to your computer and use it in GitHub Desktop.

Select an option

Save Sohail05/bd601b49b0a1f8e06c1469bb2d86057f to your computer and use it in GitHub Desktop.
Codewars
Array.prototype.all = function (p) {
return this.every( value => p(value) );
};
Array.prototype.none = function (p) {
return this.every( value => !p(value) );
};
Array.prototype.any = function (p) {
return this.some( value => p(value) );
};
function isGreaterThanZero (num) {
return num > 0;
}
function isLessThanZero (num) {
return num < 0;
}
function isGreaterThanZero (num) {
return num > 0;
}
function isLessThanZero (num) {
return num < 0;
}
console.log([1, 2, 3].all(isGreaterThanZero), 'All are greater than zero');
console.log(![-1, 0, 2].all(isGreaterThanZero), 'One is less than zero');
console.log(![-1, 2, 3].none(isLessThanZero), 'One is less than zero');
console.log([-1, -2, -3].none(isGreaterThanZero), 'None are greater than zero');
console.log([-1, 2, 3].any(isGreaterThanZero), 'Two are greater than zero');
console.log(![-1, -2, -3].any(isGreaterThanZero), 'None are greater than zero');
"use strict";
console.log(list([{name: 'Bart'},{name: 'Lisa'},{name: 'Maggie'},{name: 'Homer'},{name: 'Marge'}]));
function list(names){
let string, nameArray;
nameArray = names.map( name => name.name );
string = nameArray.length > 1 ? " & " + nameArray.splice(nameArray.length-1) : "";
string = nameArray.join(", ") + string;
return string;
}
/*
best found solution
var list = (names) => names.map(x => x.name).join(', ').replace(/(.*),(.*)$/, "$1 &$2")
*/
function breakPieces (shape){
var rows = shape.split("\n");
var squares = [];
var i = -1;
var re = /\+/g;
rows.forEach( (row) => {
if(re.test(row)){
squares.push([]);
i++;
}
if(i){
squares[i-1].push(row);
}else{
squares[i].push(row);
}
} );
return squares;
}
var shape =
["+------------+",
"| |",
"| |",
"| |",
"+------+-----+",
"| | |",
"| | |",
"+------+-----+"].join("\n");
var solution = [
["+------------+",
"| |",
"| |",
"| |",
"+------------+"].join("\n"),
["+------+",
"| |",
"| |",
"+------+"].join("\n"),
["+-----+",
"| |",
"| |",
"+-----+"].join("\n")
];
console.log(breakPieces(shape));
//Test.assertSimilar(solution.sort(), breakPieces(shape).sort());
"use strict";
function countBy(x, n) {
let z = [], i = -1;
while( ++i < n ){ z.push( x * i ) }
return z;
}
console.log(countBy(3,10));
/*
omits i
function countBy(x, n) {
var z = [];
while (z.length < n) z.push(x * (z.length + 1));
return z;
}
const countBy = (x, n) => Array.from({length: n}, (v, k) => (k + 1) * x)
*/
function getRealFloor(n) {
if(n < 14){
return n > 1 ? n - 1 : 0;
}else{
return n > 1 ? n - 2 : 0;
}
}
console.log(getRealFloor(10));
/*
The illuminati is a secret society that operates in the dark.
Most of their staff are old dinosaures, this is where you come in!
You are the new initiate that has just been recuited by The illuminati.
Looking for new ways to expand they're presence secretly, they put you in charge of inserting their
"secret" symbol into multimedia.
What you have to do is no mere easy task.
Create a function that will draw a triangle.
### Task
* the function takes in a `integer` that will define the height of the triangle's output.
* That `return` the triangle as a `string`.
* The triangle has to be constituted of small `o`.
* If the passed paramater is not a positive integer, return a string `"illuminati is fake"`.
* The returned string must contain spaces `" "` in hollow around the `o` except for the base.
* Each line must contain a new line `\n` except for the base.
(so that anyone else trying to you use it can't trace it back to the secret society)
`TriangleGen(3)` will return a string containing the following:
```
o
ooo
ooooo
```
*/
function TriangleGen(height){
var pieces, i;
if( !Number.isInteger(height) || height < 1 ){
return 'illuminati is fake';
}
for( pieces=[], i = 0 ; i < height ; i++){
var padding = Array(i+1).join(" ");
pieces.push(padding + Array( 2*(height-i) ).join("o") + padding);
}
return pieces.reverse().join('\n');
}
var smallTriangle =
[" o \n",
" ooo \n",
"ooooo"].join('');
var midTriangle =
[" o \n",
" ooo \n",
" ooooo \n",
" ooooooo \n",
" ooooooooo \n",
"ooooooooooo"].join('');
var bigTriangle =
[" o \n",
" ooo \n",
" ooooo \n",
" ooooooo \n",
" ooooooooo \n",
" ooooooooooo \n",
" ooooooooooooo \n",
" ooooooooooooooo \n",
" ooooooooooooooooo \n",
" ooooooooooooooooooo \n",
" ooooooooooooooooooooo \n",
" ooooooooooooooooooooooo \n",
"ooooooooooooooooooooooooo",].join('');
var TheBigOne =
[" o \n",
" ooo \n",
" ooooo \n",
" ooooooo \n",
" ooooooooo \n",
" ooooooooooo \n",
" ooooooooooooo \n",
" ooooooooooooooo \n",
" ooooooooooooooooo \n",
" ooooooooooooooooooo \n",
" ooooooooooooooooooooo \n",
" ooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
" ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo \n",
"ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"].join('');
Test.expect(TriangleGen(3) == smallTriangle, "Test Passed");
Test.expect(TriangleGen(6) == midTriangle, "Test Passed");
Test.expect(TriangleGen(13) == bigTriangle, "Test Passed");
Test.expect(TriangleGen(0) == "illuminati is fake", "Test Passed");
Test.expect(TriangleGen(-1) == "illuminati is fake", "Test Passed");
Test.expect(TriangleGen(0.5) == "illuminati is fake", "Test Passed");
Test.expect(TriangleGen("triangle") == "illuminati is fake", "Test Passed");
Test.expect(TriangleGen(50) == TheBigOne, "Illuminati Confirmed!");
String.prototype.toJadenCase = function () {
return this.split(' ')
.map(word => word.substr(0,1).toUpperCase() + word.substr(1))
.join(' ');
};
var text = "How can mirrors be real if our eyes aren't real";
console.log(text.toJadenCase());
"use strict";
class Tree {
constructor(keys) {
this.root = new Node(keys);
}
build(keys = []){
this.root.addChild(keys);
}
}
class Node{
constructor(keys = []) {
this.key = Array.isArray(keys) ? keys.shift() : keys ;
this.children = [];
this.addChild(keys);
}
addChild(keys = [] ){
if(!Array.isArray(keys)){
return;
}
keys.forEach( keys => {
this.children.push(new Node(keys));
});
}
}
class SQL {
constructor(debug = false) {
this.query = new Object();
this.duplicate = {};
this.query.where = {and:[] , or:[]};
this.query.property = [];
this.query.orderBy = [];
this.query.data = [];
this.query.groupBy = [];
this.errors = [];
this.debug = debug;
}
select(...property) {
this.query.select = this.query.select ? this.logError("SELECT") : "set";
this.query.property = property;
return this;
}
from(data = []) {
this.query.from = this.query.from ? this.logError("FROM") : "set";
this.query.data = data;
return this;
}
where(...fns) {
fns.length > 1 ? this.query.where.or.push(fns) : this.query.where.and.push(fns[0]);
return this;
}
orderBy(fn) {
this.duplicate.orderBy = this.duplicate.orderBy ? this.logError("ORDERBY") : "set";
this.query.property.concat(fn);
this.query.orderBy.push(fn);
return this;
}
groupBy(...fns) {
this.duplicate.groupBy = this.duplicate.groupBy ? this.logError("GROUPBY") : "set";
this.query.groupBy = this.query.groupBy.concat(fns);
return this;
}
having() {
return this;
}
executeWhere(){
let validData = [];
this.query.data.forEach((row) => {
//Validate where conditions
let whereAndCondition = this.query.where.and.every( (fn) => fn(row) );
let whereORCondition = this.query.where.or.every( (arr) => arr.some( (fn) => fn(row) ) );
if( whereAndCondition & whereORCondition){ validData.push(row); }
});
return validData;
}
executeSelect(rows = []){
let selectRows = [];
if (!this.query.property.length) { return rows; }
rows.forEach((row) => {
let rowData;
this.query.property.forEach(fn => { rowData = fn(row) });
selectRows.push(rowData);
});
return selectRows;
}
executeOrderBy(rows = []) {
let orderByRows = [];
if (!this.query.groupBy.length) {return rows;}
let keySets = this.findKeys(rows,this.query.groupBy);
let tree = new Tree();
tree.build(keySets);
let keys = [];
rows.forEach((row) =>{
this.query.groupBy.forEach( (orderBykey) => {
keys.push(orderBykey(row));
});
});
return orderByRows;
}
logError(msg) {
this.errors.push(`Duplicate ${msg}`);
}
checkError(){
this.errors.forEach( (er) => {throw new Error(er)} );
return this.errors.length > 0;
}
findKeys(rows = [], keyGens = []){
let keySet = [];
keyGens.forEach( (keyGen, i) => {
keySet.push([]);
rows.forEach((row) => {
keySet[i].push(keyGen(row));
});
});
keySet.forEach( (key,i) => {
keySet[i] = [...new Set(key)];
});
return keySet;
}
execute() {
if(this.checkError()){ return; }
let whereResult = this.executeWhere();
let selectResult = this.executeOrderBy(whereResult);
let result = this.executeSelect(selectResult);
return result;
}
}
var query = () => new SQL();
var persons = [
{name: 'Peter', profession: 'teacher', age: 20, maritalStatus: 'married'},
{name: 'Michael', profession: 'teacher', age: 50, maritalStatus: 'single'},
{name: 'Peter', profession: 'teacher', age: 20, maritalStatus: 'married'},
{name: 'Anna', profession: 'scientific', age: 20, maritalStatus: 'married'},
{name: 'Rose', profession: 'scientific', age: 50, maritalStatus: 'married'},
{name: 'Anna', profession: 'scientific', age: 20, maritalStatus: 'single'},
{name: 'Anna', profession: 'politician', age: 50, maritalStatus: 'married'}
];
function profession(person) {
return person.profession;
}
function name(person) {
return person.name;
}
//SELECT * FROM persons WHERE profession='teacher' GROUPBY profession, name
console.log(query().select().select().from(persons).groupBy(profession, name).execute() ) ;
function XO(str) {
let O = str.match(/[(oO)]/g).length || 0;
let X = str.match(/[(xX)]/g).length || 0;
return O == X;
}
console.log(XO(""));
/*
best solutions
1.
const XO = str => (str.match(/x/gi) || []).length == (str.match(/o/gi) || []).length;
2.
XO = (str) => !(str.split(/[oO]/).length - str.split(/[xX]/).length);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment