I’m trying out doing individual posts for each day. I think it may be a better way for me to document what I’ve been learning vs doing small summaries all on one page. I’m very much unhappy with how the code looks (visually in wordpress) currently. Hopefully I’ll figure something out soon.
Tonight I did 5.5h of studying. I started off with 4 algorithm questions from freeCodeCamp. Here they are:
sorted union – the battleground!
This was a struggle. It’s still not bug free, either, I’m having trouble flattening arrays recursively, and then I discovered that one of the tests relies on preserving and adding a single element array to the output. That means that flattening is not the right choice for this. I should be recursively crawling the array, as opposed to flattening it.
/*
ideas:
make a new outArray
spread the first array to the outArray
iterate through each of the remaining arrays (and sub-arrays), checking if each item is in the initial array
if it is not in the first array, push it to outArray
eventually return outArray
*/
function uniteUnique(arr, ...args) { let outArr = [...arr]; let searchResult; args.forEach(function(each) { each.forEach(function(subArrItem) { console.log(`subArrItem is ${subArrItem}`) searchResult = outArr.indexOf(subArrItem) console.log(`looking for ${subArrItem} in ${outArr} and finding ${searchResult}`) console.log(Array.isArray(subArrItem)) if (searchResult == -1){ console.log(`pushing ${subArrItem} to list`) outArr.push(subArrItem) } }); }) console.log(`outArr is ${outArr}`) return outArr } // console.log(arrFlattener([1, 3, 2], [1, [5,7,[9],2]], [2, [4]])) uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]) // uniteUnique([1, 3, 2], [1, [5]], [2, [4]]) /* what I learned: don't treat the input as one array if it isnt use ...args to capture any number of inputs into an array my forEach loop is handling sub arrays without me thinking about it?! THIS is an example of it passing the tests, but only because there are untested edge cases. it currently adds any sub arrays in their entirety to the outArr. the examples don't have any repeated chars in the subarrays, so it looks right. */
Convert HTML Entries
function convertHTML(str) {
// :)
/*
game plan: do multiple replaces?
or switch statement? this seems like it would be less code.
*/
var temp = str.split(”);
let outStr=””
for (let i=0;i<temp.length;i++){
switch (temp[i]){
case ‘&’:
temp[i] =”&”;
break;
case ‘<‘:
temp[i] =”<”;
break;
case ‘>’:
temp[i] =”>”;
break;
case “‘”:
temp[i] = “'”;
break;
case ‘”‘:
temp[i] = ‘"’;
break;
}
}
temp=temp.join(”)
console.log(temp)
return temp;
}
convertHTML(‘Stuff in “quotation marks”‘)
convertHTML(“Schindler’s List”)
/*
what I learned
splitting on “” will split every char, same as iterating through, except the array is mutable, so it’s a little easier.
I ran into a really weird bug where my code looked exactly like their code, but didn’t work the same. It could be a curly quote issue. it was only on the last 2 rules, based on quotation marks and apostrophes.
*/
It can also be stripped down to about 4 lines by using .replace()
function convertHTML(str) {
str=str.replace(/&/g, “&”).replace(/</g, “<”).replace(/>/g, “>”).replace(/’/g, “'”).replace(/\”/g, “"”)
return str
}
sum of all odd fibonacci numbers
function sumFibs(num) {
/*
strategy
generate fibs up to num inclusive
filter fibs with modulo to remove evens
sum fibs
return sum
*/
return makeFibs(num)
function makeFibs(max){
let fibs=2;
let curFib=1;
let lastFib=1;
while (curFib<=max){
[lastFib, curFib]=[curFib,curFib+lastFib]
if((curFib<=max)&&(curFib%2!=0)){
fibs += curFib
}
}
return fibs
}
}
sumFibs(1000);
/*
I optimized a bit while making this. Instead of having a function to return all fibs I changed it to only write the odd ones, and then I changed it to keep the sum as opposed to the array. I think both of those choices should reduce memory size while slightly increasing computational cost.
I got lost by not realizing that a return in the sub function is not the same as a return in the main function.
I had to look up how to swap 2 values simultaneously [a,b]=[b,a]
I also looked up while loops
*/
sum all primes
function sumPrimes(num) {
/*
strategy
make a function to find primes up to a certain number and then return the sum of them
this should be similar to the previous example with fib numbers
*/
let numSum=2;
let curNum=2;
return sortPrimes()
function sortPrimes(){
while (curNum<=num) {
// console.log(curNum<=num)
// console.log(curNum+”/”+num)
let filterFail=false
if ((curNum%2==0)){
filterFail=true
// console.log(‘divisible by 2’)
// console.log(‘even ‘ + curNum) // check for even
} else {
for (let i=3;i<curNum;i+=2){ // start at 3, only do odds, stop halfway
if (curNum%i==0){
filterFail=true
// console.log(‘divisible by ‘+i)
}
}
}
if (filterFail==false){
// console.log(“adding “+curNum+” to “+numSum)
numSum += curNum
} else {
// console.log(curNum +” is not prime”)
}
// console.log(“sum=” + numSum)
// console.log(“curNum=” + curNum)
// console.log()
curNum++
}
return numSum
}
// console.log(numSum)
return numSum;
}
sumPrimes(977);
What I learned. In this I ended up getting my logic tangled up in knots. It took a lot to get it unsnarled. I also had to comment out all of my logging in order to get it to pass the tests on inputting 73156. I guess it timed out when it is doing that much writing to the console. I believe this would be O(N^2) The main loop and the for loop would dominate the other linear things
I also did these two videos on big O notation, and that is sinking in fairly well. Then I moved on to some great videos by Gayle Laakmann McDowell on bubble sort, quicksort, and binary trees. It’s pretty cool to be learning from a woman for once. She definitely knows her stuff!