Sunday, June 23, 2019

Day 077: Finally Out of the Slump? Coding/Algorithm Challenges

As I eluded to in my tweet, I was still feeling some sort of way after the theft and invasion of privacy so much so that it affected me a lot more than I care to admit. It still really bothers me even now.


Like I really couldn't bring myself to do anything productive and only squeaked by with "coding" by watching (and sometimes coding along) with videos CSS, NodeJS and such. But thankfully a member from my Daily Accountability group suggested doing Codewars problems together which really helped get me out of the slump.

I really enjoyed it when I did the Lighthouse Labs 30 day challenge. But now that I am kind of having a go at it myself, I am finding the challenges on these types of sites really addicting. And I get to learn more about JS and practice to boot!

So far, I've signed up for:

I think I will stick with those 3 for now. 3 is even probably a bit much right now.

I like the Codewars set up the most, though it can get really slow at times. Edabit is a close second and is much faster. Hackerrank can be confusing at times with their instructions and set up (or maybe it's the issues with the particular set of challenges I've encountered), but so far so good. What is great about all of them is that they display other user solutions after you complete your own which also has been a great learning experience to see how more experienced folks do it. This sometimes leads me down a rabbit hole of learning about different methods and getting more exposure to them.

FreeCodeCamp Challenges

Even better was finding out that FCC had challenges in their JS Algorithms & Data Structures section. I mean, I probably peeked ahead at them when I started FCC way back when and thought to myself that it all looks like gibberish and that it will make sense to me in time... well, now's the time! I guess I want to save some of my answers that I was particularly pleased with.

Basic Algorithm Scripting: Return Largest Numbers in Arrays
Probably not the most efficient code in the world, but I was pretty happy with solving this one. It is close to the basic solution they presented.
function largestOfFour(arr) {
let answer = [];
  for (let i = 0 ; i < 4 ; i++) {
    let maxNum = arr[i][0];
    for (let j = 0 ; j < 4; j++){
      if (arr[i][j] > maxNum) {
        maxNum = arr[i][j];
      }
    }
    answer.push(maxNum);
  }
  console.log(answer);
return answer;
}


Basic Algorithm Scripting: Title Case a Sentence 
I solved a similar one to this particular challenge in Codewars (or was it Edabit?), but it was done with the basics length and iterating through the array with a for loop. I needed some practice with using map() and came up with the below. It's a bit of "code golf" (for me at least). I also got some use out of substring() as well!
function titleCase(str) {
  let arr = [];
  str.toLowerCase().split(" ").map(x => arr.push(x[0].toUpperCase() + x.substring(1)));
  return arr.join(" ");
}

titleCase("I'm a little tea pot");


Basic Algorithm Scripting: Chunky Monkey
Only because I wanted to get more practice out of a while loop, I came up with the following answer which is similar-ish to one of their advanced solutions. I also ended up using slice() and originally attempted to push it into an array assuming that it removes the items from the array, but slice doesn't work that way. After peeking at the rest of the solutions, what I wanted to use was splice() which does actually remove the elements from the array.
function chunkArrayInGroups(arr, size) {
  let answer = [];
  let add = 0;
 while (answer.length < arr.length/size) { 
     answer.push(arr.slice(0+add,size+add))
     add = add + size;
    }
  console.log(answer)
  return answer;
}


Basic Algorithm Scripting: Mutations
A tiny bit more code golfing.
function mutation(arr) {
  for (let i = 0 ; i < arr[1].length ; i++){
    if (arr[0].toLowerCase().indexOf(arr[1].toLowerCase()[i]) < 0) {
      return false;
    }
  }
  return true;
}


JavaScript Algorithms and Data Structures Projects: Telephone Number Validator
This one was a doozy. I was initially very scared of regular expressions, but after going through the RegEx section in the FCC, it's not so bad! Especially when you have a handy tool like Regex101 with you.

FCC's regex /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/ 
MY regex /^1?\-?\s?(\(\d{3}\)|\d{3})\-?\s?\d{3}\s?\-?\d{4}$/ 

I was surprised at how close I came to their basic solutions. My answers aren't a perfect match but it still works. That's what I love about programming.. there's more than one right answer!


No comments:

Post a Comment