Skip to content

Instantly share code, notes, and snippets.

@amfischer
Created September 28, 2016 21:48
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 amfischer/80830fff074a1587a3a89405be2fd4da to your computer and use it in GitHub Desktop.
Save amfischer/80830fff074a1587a3a89405be2fd4da to your computer and use it in GitHub Desktop.
FCC Algorithm Challenges
/*
Convert a date range consisting of two dates formatted as YYYY-MM-DD into a more readable format.
The friendly display should use month names instead of numbers and ordinal dates instead of cardinal (1st instead of 1).
Do not display information that is redundant or that can be inferred by the user: if the date range ends in less than a year from when it begins, do not display the ending year.
Additionally, if the date range begins in the current year (i.e. it is currently the year 2016) and ends within one year,
the year should not be displayed at the beginning of the friendly range.
If the range ends in the same month that it begins, do not display the ending year or month.
Examples:
makeFriendlyDates(["2016-07-01", "2016-07-04"]) should return ["July 1st","4th"]
makeFriendlyDates(["2016-07-01", "2018-07-04"]) should return ["July 1st, 2016", "July 4th, 2018"].
*/
function makeFriendlyDates(arr) {
var firstDate = arr[0].split('-').map(function(current) {
return parseInt(current);
});
var secondDate = arr[1].split('-').map(function(current) {
return parseInt(current);
});
var firstDateObject = new Date(firstDate[0], firstDate[1] - 1, firstDate[2]);
var secondDateObject = new Date(secondDate[0], secondDate[1] - 1, secondDate[2]);
var firstDateStamp = Date.parse(firstDateObject);
var secondDateStamp = Date.parse(secondDateObject);
var answerArr = [];
var today = new Date();
if (firstDateStamp === secondDateStamp) {
answerArr[0] = convertMonth(firstDateObject.getMonth()) + " " + convertDay(firstDateObject.getDate()) + ", " + firstDateObject.getFullYear().toString();
} else if (today.getFullYear() === firstDate[0] && (secondDateStamp <= firstDateStamp + 31536000000)) {
if (firstDateObject.getMonth() === secondDateObject.getMonth()) {
answerArr[0] = convertMonth(firstDateObject.getMonth()) + " " + convertDay(firstDateObject.getDate());
answerArr[1] = convertDay(secondDateObject.getDate());
} else {
answerArr[0] = convertMonth(firstDateObject.getMonth()) + " " + convertDay(firstDateObject.getDate());
answerArr[1] = convertMonth(secondDateObject.getMonth()) + " " + convertDay(secondDateObject.getDate());
}
} else if (secondDateStamp < firstDateStamp + 31536000000) {
answerArr[0] = convertMonth(firstDateObject.getMonth()) + " " + convertDay(firstDateObject.getDate()) + ", " + firstDateObject.getFullYear().toString();
answerArr[1] = convertMonth(secondDateObject.getMonth()) + " " + convertDay(secondDateObject.getDate());
} else {
answerArr[0] = convertMonth(firstDateObject.getMonth()) + " " + convertDay(firstDateObject.getDate()) + ", " + firstDateObject.getFullYear().toString();
answerArr[1] = convertMonth(secondDateObject.getMonth()) + " " + convertDay(secondDateObject.getDate()) + ", " + secondDateObject.getFullYear().toString();
}
return answerArr;
}
function convertMonth(num) {
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
return months[num];
}
function convertDay(num) {
var day = num.toString();
switch(day[day.length - 1]) {
case '1':
day += day.length === 2 && day[0] === '1' ? 'th' : 'st';
break;
case '2':
day += day.length === 2 && day[0] === '1' ? 'th' : 'nd';
break;
case '3':
day += day.length === 2 && day[0] === '1' ? 'th' : 'rd';
break;
default:
day += 'th';
}
return day;
}
/* Return the number of total permutations of the provided string that don't have repeated consecutive letters.
Assume that all characters in the provided string are each unique.
For example, aab should return 2 because it has 6 total permutations (aab, aab, aba, aba, baa, baa),
but only 2 of them (aba and aba) don't have the same letter (in this case a) repeating.
*/
function swap(str, index1, index2) {
var char1 = str[index1];
var char2 = str[index2];
if (index1 === index2) {
return str.substring(0, index1) + char1 + str.substring(index2 + 1, str.length);
} else {
return str.substring(0, index1) + char2 + str.substring(index1 + 1, index2) + char1 + str.substring(index2 + 1, str.length);
}
}
function permute(str, start, end, arr) {
if (start === end) {
arr.push(str);
} else {
for (var i = start; i <= end; i++) {
var newString = swap(str, start, i);
permute(newString, start + 1, end, arr);
//swap(newString, start, i);
}
}
return arr;
}
function noDouble(arr) {
var newArr = arr.filter(function(element, index) {
var subArr = element.split("");
var newSubArr = subArr.filter(function(e, i) {
if (e === subArr[i + 1]) {
return false;
} else {
return true;
}
});
return element.length === newSubArr.length ? true : false;
});
return newArr;
}
function permAlone(str) {
var perm = [];
permute(str, 0, str.length -1, perm);
return noDouble(perm).length;
}
//A much more efficient solution to the FCC smallest common multiple challenge
//I had to lookup the function for finding the prime numbers of any given number
//Also a very hard challenge for me, completed 7/8/16
function prime(n) {
var arr = [];
var num = 2;
while (n > 1) {
while (n%num === 0) {
arr.push(num);
n = n/num;
}
num ++;
}
return arr;
}
function filterPrimes(arr) {
var num = 2,
i,
counter = false,
primeArr = [];
while (arr.length > 0) {
for(i = 0; i < arr.length; i++) {
if (arr[i].indexOf(num) !== -1) {
arr[i].shift();
counter = true;
}
}
if (arr[0].length === 0) {
arr.shift();
}
if (counter) {
primeArr.push(num);
counter = false;
} else {
num ++;
}
}
return primeArr;
}
function smallestCommons(arr) {
var start = Math.min(arr[0], arr[1]);
var end = Math.max(arr[0], arr[1]);
arr.pop();
arr.pop();
for (var x = start; x <= end; x++) {
arr.push(x);
}
var testArray = [];
for (var i = 0; i < arr.length; i++) {
testArray.push(prime(arr[i]));
}
var primesArray = filterPrimes(testArray);
var answer = 1;
for (var j = 0; j < primesArray.length; j++) {
answer *= primesArray[j];
}
return answer;
}
smallestCommons([1,13]);
//Spinal Tap Case challenge for FCC. The hardest one I've had to do yet. Completed 6/23/16
function spinalCase(str) {
var all = /[A-Za-z]/g,
regex = str.match(all),
lowerThenUpper = /[a-z](?=[A-Z])/g,
ltu = str.match(lowerThenUpper),
nonLetter = /[^a-zA-Z]/g,
non = str.match(nonLetter),
i;
if (non !== null) {
str = str.replace(nonLetter, "-");
}
if (ltu != null) {
for (i = 1; i < regex.length; i++) {
if (regex[i] === regex[i].toUpperCase()) {
str = str.replace(/[a-z](?=[A-Z])/, regex[i-1] + "-");
console.log(str);
}
}
}
str = str.toLowerCase();
console.log(str);
}
//spinalCase("thisIsSpinalTap");
//spinalCase("AllThe-small Things");
spinalCase("This Is Spinal Tap");
//spinalCase("The_Andy_Griffith_Show");
//spinalCase("Teletubbies say Eh-oh");
function steamrollArray(arr) {
// I'm a steamroller, baby
var newArr = [];
function rolling(arr) {
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
rolling(arr[i]);
} else {
newArr.push(arr[i]);
}
}
}
rolling(arr);
return newArr;
}
//steamrollArray([1, [2], [3, [[4]]]]);
steamrollArray([[["a"]], [["b"]]]);
function roll(element) {
return Array.isArray(element) ?
element.reduce(function(a,b) {
return a.concat(b);
}, [])
: element;
}
function steamrollArray(arr) {
// I'm a steamroller, baby
var x = arr.map(function(e) {
return Array.isArray(e) ?
e.reduce(function(a,b) {
return a.concat(b);
}, [])
: e;
});
return arr.reduce(function(a,e) {
return Array.isArray(e) ?
a.concat(e)
: a.concat(e);
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment