Skip to content

Instantly share code, notes, and snippets.

@izzydoesit
Created May 27, 2018 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izzydoesit/3ff215e84165ad26cd14fff91c056cb5 to your computer and use it in GitHub Desktop.
Save izzydoesit/3ff215e84165ad26cd14fff91c056cb5 to your computer and use it in GitHub Desktop.
DimwittedLovelyAdware created by anonymous - https://repl.it/repls/DimwittedLovelyAdware
assertEquality = (actual, expected, description) => {
if (actual === expected) {
console.log("It should..." + description + '...PASS!')
} else {
console.log("It should..." + description + "...FAIL\n...expected: '" + expected + "' but got: '" + actual + "' instead.")
}
}
assertEquality(true, true, "assert equality")
// 1
// 11
// 21
// 1211
// 111221
lookAndSay = (num) => {
// initialize value as 1
let value = 1;
// loop through from 1 up until the input number
for (let i = 1; i <= num; i++) {
// each time print out value
// reassign value to the output value of reading the last value
console.log(value);
value = newOutputValue(value);
}
}
newOutputValue = (input) => {
input = input.toString();
if (input.length === 1) {
return `1${input}`;
}
// two pointers
// initialize count at 1
let returnString = "";
let current = input[0];
let count = 1;
for (let j = 1; j <= input.length; j++) {
// if the next number doesn't equal the current one
// or its the last character in the string
if (current !== input[j] || j === input.length) {
// put the count and the number in the return string
// reset count and increment the current pointer
returnString += `${count}${current}`;
count = 1;
current = input[j];
} else {
// otherwise they are the same, just count it
count++;
}
}
return returnString;
}
// console.log(lookAndSay(5))
// console.log(lookAndSay(10))
function oneEditApart(str1, str2) {
if (str1.length !== str2.length) {
let idx1 = 0, idx2 = 0;
let diff = 0;
while (idx1 <= str1.length && idx2 <= str2.length) {
if (str1[idx1] !== str2.length) {
diff++;
}
idx1++;
idx2++;
}
if (Math.abs(idx1 - idx2) > 1) {
return false
}
} else {
let diff = 0;
for (let i = 0; i < str1.length; i++) {
if (str1[i] !== str2[i]) {
diff++;
}
if (diff >= 2) {
return false
}
}
}
return true;
}
assertEquality(oneEditApart("cat", "dog"), false, 'tell if "cat" and "dog" are one edit apart');
assertEquality(oneEditApart("cat", "cats"), true, 'tell if "cat" and "cats" are one edit apart');
assertEquality(oneEditApart("cat", "cut"), true, 'tell if "cat" and "cut" are one edit apart');
assertEquality(oneEditApart("cat", "cast"), true, 'tell if "cat" and "cast" are one edit apart');
assertEquality(oneEditApart("cat", "at"), true, 'tell if "cat" and "at" are one edit apart');
assertEquality(oneEditApart("cat", "act"), false, 'tell if "cat" and "act" are one edit apart');
/*
Spiral:
Find the pattern and complete the function:
int[][] spiral(int n);
where n is the size of the 2D array.
input = 3
123
894
765
input = 4
01 02 03 04
12 13 14 05
11 16 15 06
10 09 08 07
input = 8
1 2 3 4 5 6 7 8
28 29 30 31 32 33 34 9
27 48 49 50 51 52 35 10
26 47 60 61 62 53 36 11
25 46 59 64 63 54 37 12
24 45 58 57 56 55 38 13
23 44 43 42 41 40 39 14
22 21 20 19 18 17 16 15
*/
function getSpiral(n) {
if (n <= 0) {
throw new Error("N must be greater than 0");
}
let dc = [ 1, 0, -1, 0 ];
let dr = [ 0, 1, 0, -1 ];
let dir = 0, value = 0, row = 0, column = 0, limit = n*n;
let matrix = [...Array(n)].map(x => new Array(n).fill(0));
while (value++ < limit) {
matrix[row][column] = value;
console.log('added: ', value, 'to row:', row, 'and column', column )
row += dr[dir];
column += dc[dir];
if (isInvalid(matrix, row, column)) {
console.log('is invalid', row, column)
row -= dr[dir];
column -= dc[dir];
console.log('readjust:', row, column)
dir = (dir+1) % 4;
console.log('dir', dir)
row += dr[dir];
column += dc[dir];
console.log('new coord: ', row, column)
}
}
return matrix;
}
function isInvalid(matrix, row, column) {
return row < 0 || column < 0 || row >= matrix.length || column >= matrix.length || matrix[row][column] !== 0;
}
console.log(getSpiral(4));
/*
transpose matrix:
input: 2d Array matrix
output: transposed 2d Array matrix
- iterate through 1/2 of rows
- for each row, iterate through columns
- 4 way swap with same position 90 degrees clockwise
- use temp var
- i, j => j, n-i-1 => n-i-1, n-j-1 => n-j-1, i
- return matrix
*/
transposeMatrix = (matrix) => {
let n = matrix.length;
for (let i = 0; i < n/2; i++) {
for (let j = i; j < n-i-1; j++) {
let temp = matrix[i][j];
matrix[i][j] = matrix[n-j-1][i];
matrix[n-j-1][i] = matrix[n-i-1][n-j-1];
matrix[n-i-1][n-j-1] = matrix[j][n-i-1];
matrix[j][n-i-1] = temp;
}
}
return matrix;
}
const matrix1 = [ [ 1, 2, 3, 4, 5 ],
[ 6, 7, 8, 9, 10],
[ 11, 12, 13, 14, 15],
[ 16, 17, 18, 19, 20],
[ 21, 22, 23, 24, 25]]
console.log(transposeMatrix(matrix1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment