Angular 2, or just Angular as it’s now called, is new, exciting and constantly evolving. While it’s great to be on the cutting edge of a JS framework, the process can be extremely frustrating at times…
I wanted to take a quick break to test how much of the course has sunk in, so I went back and rebuilt my Random Quote Generator App from the Free Code Camp frontend projects. I picked this because it was fairly simple and I wanted to build an Angular project from creation to deployment without investing too much time.
Building the project was relatively quick and painless, which is a testament to Max’s course mentioned above. You can see my GitHub repo here (and the app finally deployed here). Given that it’s a static app (i.e. no backend), I figured GitHub pages would be a good place for deployment.
This is where the headache started… One of the inherent problems when working something like Angular, that’s constantly changing is that many of the resources become irrelevant and incorrect very quickly (this one, no doubt, included).
I found multiple blogs and Stack Overflow answers, some written only months ago, claiming that you could deploy to GitHub pages with the following one command:
After much frustration, and multiple attempts at rebuilding and redeploying, I found that the deploy command was removed from the angular-cli.
So the command written above certainly wouldn’t work. The functionality, however, is available through the angular-cli-ghpages npm package.
Before we proceed on how to use the angular-cli-ghpages package, there are a couple assumptions being made in this process that will need to be met if this is going to work…
First, I’m assuming that you have Node.js installed on your computer. If not, visit the Node.js site for instructions for download.
I’m also assuming that you have an Angular app, associated with a GitHub repo, written and ready to deploy… If not, go create something and commit it to GitHub!!
With those assumptions out of the way, input the following in a terminal to install the npm package globally:
Once installed, you’ll need to build the app. In the app’s folder, run:
This will deploy a new branch to your GitHub repo called gh-pages branch. Make sure you’re using this branch as the source for your app. You can find/change this option under the GitHub Pages section of the Settings tab of your repo.
It might take a few minutes for the app to show up on your page, be patient!
That’s it for now, I hope this was able to help.
Prefer Heroku? Here’s a good article if you’re having trouble deploying a static angular app to heroku. The title says Vue, but it works great for angular now that they’re using webpack…
As you may have heard, Free Code Camp has announced they are adding a major expansion to their curriculum. A look at the current map of the beta version shows that many of the advanced algorithms will be supplemental to the main program.
Because of this, I’ve decided to wait until the beta is released into production before I continue writing up more algorithm challenges, as I have no way of knowing which challenges will make the final cut.
I am, however, very excited to announce that I’ve decided to add a third major theme here at Crooked Code – Angular 2 (ng2) and the MEAN stack.
I’m doing this for several reasons…
First, about 90% of my focus lately has been on learning Angular 2. It’s the first JS framework I’ve learned, so the process has been a long one, and judging by many of the comments I’ve seen throughout the FCC community, I’m not the only one on this journey. Which brings me to my second reason…
The final release of ng2 is so recent, many of the tutorials and videos out there are a mix of earlier release candidates and the final release all mashed together…
This causes some major confusion in building a working ng2 app, particularly in how to structure the project. So I hope to shed some light on what I’ve found and save others some of the time I’ve spent (and continue to spend) making sense of it all.
Further, I plan to highlight how the ng2 frontend framework ties into the rest of the MEAN stack.
Now that I’ve spent some time digging into Angular 2 on a deeper level, I’m planning on using my previous experience with Node, Express and MongoDB (with Mongoose) to build larger full stack apps. I think this will be a great opportunity to share what I’m learning with the Crooked Code community.
There are a couple algorithms that are consistently asked about in the FCC forums because people are having trouble coming up with a solution. ‘No Repeats Please’ is definitely one of them…
No Repeats Please
In my opinion, the best place to start any of the algorithm scripting challenges is making sure you completely understand the problem.
Here is the challenge as stated by FCC:
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 (abaand aba) don’t have the same letter (in this case a) repeating.
Ok, not exactly the clearest of problems… In my opinion, there are two concepts in this algorithm that add to the difficulty.
First, we’re going to have to understand what permutations are and how to find all of them for a given string. Once we have that, we’ll need to write a regular expression (regex) that looks for repeated letters in each permutation.
Let’s get a better understanding of each of these concepts before we attempt to solve the algorithm challenge.
What are permutations?
The wikipedia page on permutations is a pretty good start if you haven’t been exposed to permutations previously. It states:
In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. These differ from combinations, which are selections of some members of a set where order is disregarded.
The key takeaway from this should be that a permutation involves all the members of the set and the order matters.
Part of the given problem states that we should ‘assume that all characters in the provided string are unique’. This means that for the given example, ‘aab’, the first two letters should be considered unique even though they are both the letter ‘a’. Therefore, the two permutations without repeats, ‘aba’ and ‘aba’, are considered unique and we must count each of them in the solution.
A quick side note… If you’re looking for further clarification as to the difference between a permutation and a combination, this website does a good job comparing the two…
How can we find all permutations of a string?
Permutations are prevalent throughout many areas of mathematics and computer science. It should, therefore, be no surprise that there is a well known algorithm, called Heap’s Algorithm, that will find all the permutations for a set of objects.
Keep in mind, there are n! number of permutations for a set of n objects. So for a string of four letters there are (4 x 3 x 2 x 1) or 24 unique permutations.
Consequently, Heap’s algorithm works on the order of O(n!), the slowest order of functions. Therefore, as the set gets larger, increases of even one number will cause the algorithm to slow drastically. To put that into perspective, a set with 15 elements, will have over 1.3 Trillion permutations.
Ok, enough about the theory of permutations, how do we implement Heap’s algorithm?
Given this isn’t a post specifically about Heap’s Algorithm, I’m not going to go into depth about how it works. Suffice to say that it systematically switches one pair of elements in each step, eventually yielding every possible permutation.
I encourage you to walk through the algorithm until you understand the workings ‘under the hood’. The image to the right shows the order of the elements after each iteration of Heap’s Algorithm for a set of 4 elements.
Here is the psuedocode for the recursive version of Heap’s algorithm. If you prefer to implement non-recursively, there is a version on the algorithm’s Wikipedia page.
Psuedocode for Heap's Algorithm
procedure generate(n:integer,A:arrayof any):
for i :=0;i<n-1;i+=1do
Let’s move on to regular expressions before we put everything together in the final algorithm.
Regular Expressions (RegExp)
w3schools shows the syntax of regular expressions as /pattern/modifiers; . Rather than diving down the rabbit hole of regular expressions, I’ll point out some great resources to learn regex, then write and explain the one we’ll need to solve this challenge.
A Regex for Repeating Letters
Let’s build a regex that will recognize repeating characters. We’ll start with /[a-zA-Z]/, which will match any and every letter in the string.
Patterns in parentheses are remembered and can be referenced in order (i.e. the pattern in the first set of () can later be referenced as \1 , the second as \2 and so on). Therefore, adding parenthesis like so, /([a-zA-Z])/, causes the letter that is matched to be remembered.
Next, adding \1 after ([a-zA-Z]) will cause the regex to look for a repeat of whatever the ([a-zA-Z]) matched (i.e. a pair of the same letter), which is what we need!
Therefore, our regex for finding a repeating letter is going to look like this /([a-zA-Z])\1/ .
Crystal clear right!!
An Additional Note on Regular Expressions
To solve ‘No Repeats Please’, we’ll be using the RegExp.test() method. When the test method is passed a string, it will return a Boolean telling you if that string contained the pattern. So /([a-zA-Z])\1/.test(str) will return true if there are repeating letters in str.
Structuring the Algorithm
Now that we have a better idea of the problem, we can start thinking about how to solve it… There are a couple ways we can structure this algorithm.
One, we could use Heap’s algorithm to iterate through all permutations of the given string, pushing each onto an array. Then, having an array with all permutations, we could check each element of the array for repeats.
Or two, we could use Heap’s algorithm to iterate through all permutations, each time checking for repeats and if not found, increment a counter.
I chose to write it the second way with efficiency in mind. Given the function is on the order of O(n!), we don’t want to iterate through all permutations to create the array, then iterate through again checking for repeats.
This won’t make much of a difference for strings of length 5 or 6, but it will make a huge difference on longer strings. In fact, to demonstrate this, I tested the function written both ways.
When running the function the first way (iterating twice), a string of 10 characters took 3523ms vs 2287ms for a function written the second way. When I increased the string to 11 characters it took the second function 26,802ms while the first function failed due to lack of memory.
Now that we have a structure in mind for the algorithm, let’s finally go ahead and write it!
My Solution to ‘No Repeats Please’
If you want to take a stab a writing the solution on your own, now’s the time… I’m going to post the solution that I came up with below. Again, there are multiple ways to solve this algorithm challenge, this is just the one that I came up with.
My 'No Repeats Please' Solution
//create variable to store number of perms without a repeat
//split string into array
// Call with an array of the original string
// If only 1 element, just output the array
//check for duplicates
// If n is even
//call permAlone() with any string
If you were having trouble with ‘No Repeats Please’, hopefully breaking it down like this helped. This is definitely one of the hardest of the FCC algorithm challenges.
I hope you found this post useful… If so, feel free to leave a comment and let me know. Thanks for reading, that’s all for now…
I recently posted about an overly elaborate solution that I wrote to a CodeWars algorithm. That post has been lurking in the back of my head since I wrote it…
I’ve come to realize that I can’t be too hard on myself for coming up with the solution that I did. After all, the only tool I had available to me was a hammer, so I treated every problem like a nail.
My point is that up until then, any time I had to traverse an array, I had used a for loop rather than any of the methods native to the array prototype. Setting the conditions of the loop to something like (var i = 0; i < arr.length; i++) allows you to easily access and manipulate any element of the array using arr[i]. So that’s what I did…
While there’s (usually) nothing inherently wrong with that (in fact, it’s how I solved many of the algorithm challenges at Free Code Camp), loops aren’t always the most elegant solution to a problem, especially when you start to consider asynchronous options in your code. Besides, the point of this journey is to become the strongest developer I can, so let’s explore other options.
While going through the front end program at Free Code Camp, I was exposed to many of the methods in the array prototype, including forEach(), reduce(), map() and filter(). The problem was that those methods required callback functions, of which my understanding was tenuous, so I found it much easier at the time to use loops.
Working with Node.js these past few months has given me much more exposure using callback functions, so I’d like to revisit these methods and solidify my understanding of them. That way, when I encounter problems in the future, I’ll have more than one tool at my disposal.
Over the next couple weeks, I will write a post that will cover each of the four array methods mentioned above in detail. Hopefully, these posts will serve as good resources for others in my position and, at the same time, give me extra practice using each.
I’ll be sure to come back and provide links to the follow up posts once they’re written.
Since starting FCC in March I’ve made pretty steady progress through the curriculum. Of course I’d like to be moving faster, but considering all the other commitments in my life, I’m happy with my pace thus far…
That is until the past few weeks…
I was moving right along through the back end program. Learning Node and Express was going well and I got through the API projects relatively quickly…
Then I moved on to the voting app, the first of the Dynamic Web Application Projects.
The past few weeks, though, it’s hit me something fierce…
While thinking about how to approach the voting app I considered using a framework, maybe React or Angular 2. So I dug a little deeper…
As someone still relatively new to the JS world, comparing React and A2 isn’t exactly easy. How do I compare JSX vs TypeScript if I haven’t seen either?
Babel and Webpack. Huh?
Redux? Flux? WTF??
I’ve spent the past couple weeks paralyzed with indecision and frustration…
When I was thinking about writing this post, I made a quick list of all the JS libraries and frameworks that I could think of. In about 2 or 3 minutes I came up with 39 of them!!
Off the top of my head!!
I have the scrap of paper to prove it! No wonder JS fatigue is a real thing.
I just wanted to put this out there in case anyone else in the Free Code Camp program was going through the same thing. Plus, I’m hoping this will serve as a bitch session to get it off my chest so I can move forward.
I’ve decided to say F-it and move on without a framework for the time being.
I plan on building 1 or 2 of the full stack apps without a framework, then decide between moving forward with either React or Angular 2.
At that point, I’m hoping FCC has updated their data viz section and the decision will be a little easier.
That’s it for now… Get out there and code something cool, I’m certainly going to try.
I’ve been thinking about writing a post like this for a while but have put it off because, frankly, it’s pretty embarrassing…
Today, I saw a post in the Free Code Camp Forums that made it clear, however, that I’m not the only one that goes through this. So hopefully this post will encourage other beginners to keep going, even if their code looks terrible in their own eyes…
The purpose of this post won’t be to walk through the algorithms and explain everything that’s going on, but rather to emphasize how different two answers to the same problem can be.
Codewars, as I’ve mentioned in previous posts, is a great way to practice solving algorithm challenges. It can be pretty humbling at times, though, because after you solve the problem, you have the opportunity to look at other people’s solutions.
So, when you solve what you thought was a difficult challenge, only to see that others have solved it in one or two lines of code, it makes you wonder if you’re writing in the same language. When you realize you are, you wonder further if this coding thing is really for you…
The upside to this, and, in my opinion, the real strength of Codewars, is that you can learn from these solutions… I’ve only done a handful of algorithms on this site so far, but I’m going to show my first and last solutions as examples of how things can improve.
My First Codewars Solution
The first algorithm I solved at Codewars was called ‘Decode the Morse code’. Try not to laugh….
The site’s description of the algorithm is as follows:
In this kata you have to write a simple Morse code decoder. While the Morse code is now mostly superceded by voice and digital data communication channels, it still has its use in some applications around the world.
The Morse code encodes every character as a sequence of "dots" and "dashes". For example, the letter A is coded as ·−, letter Q is coded as −−·−, and digit 1 is coded as ·−−−. The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words. For example, the message HEY JUDE in Morse code is ···· · −·−− ·−−− ··− −·· ·.
NOTE: Extra spaces before or after the code have no meaning and should be ignored.
In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal SOS (that was first issued by Titanic), that is coded as ···−−−···. These special codes are treated as single special characters, and usually are transmitted as separate words.
Your task is to implement a function decodeMorse(morseCode), that would take the morse code as input and return a decoded human-readable string.
decodeMorse('.... . -.-- .--- ..- -.. .')
//should return "HEY JUDE"
The Morse code table is preloaded for you as MORSE_CODE dictionary, feel free to use it. In Java, the table can be accessed like this: MorseCode.get('.--'). In C#, the preloaded Dictionary can be accessed like this: MorseCode.Get('.--');.
All the test strings would contain valid Morse code, so you may skip checking for errors and exceptions.
In C#, tests will fail if the solution code throws an exception. Please keep that in mind. (This is mostly because else the engine would simply ignore the tests, resulting in a "valid" solution.)
So, in thinking about the solution to this algorithm, I immediately pictured various ‘for’ loops to split the Morse code into words, then letters. Then I could replace the Morse letters for real letters and reassemble the message using more ‘for’ loops.
Here is what I came up with…
//accept morse code string as argument
//trim leading and trailing whitespace
//create 2d array by word then letter
//loop through array exchanging morse code for letters
//compress array into words and return
I was feeling pretty good about myself (it worked after all!) until I saw some of the top rated solutions…
Ok, so was this last attempt better?? Sure… Still room for improvement? You betcha…
Looking through past examples definitely helped me put together a more concise solution for this problem, but I still have a lot to learn…
For instance, looking at the answers to this algorithm reminded me that the array.filter() method doesn’t change the array on which it is called. So, once I filtered the non-zero values into a new array, I still had the original array to work with. I could have used it again to add the zeros to the end of the new array.
Hopefully, next time I come across a problem like this I’ll remember the more elegant solution.
In my experience, learning a new skill, especially one as difficult as programming, is all about repetition and practice. So I encourage you (and myself too) to use sites like Codewars to practice these skills over and over.
I also encourage you to take the time to look through other people’s solutions and learn from them. It’ll probably help with your approach to your next one.
It ain’t necessarily about having the shortest solution…
The second point I want to make is that the shortest solution isn’t necessarily the best solution.
We can argue that ‘for’ loops are actually faster than native functions like array.filter() and array.map(), and they are, but that argument is mostly semantic. Unless you are writing an application that demands extremely high performance, it is unlikely that the user will see a difference between the two.
I believe, therefore, that prioritization should be placed on the readability of the code you write.
Going through the process of teaching myself how to code, I’ve read many examples of code written by others, and can say for certain that all code is not created equal.
Some code is nicely written and readable… Other code is like a plate of spaghetti and impossible to decipher.
Let’s go back to the answers to the first algorithm. My code, while drawn out and overly complicated, is fairly easy to follow. The last two solutions listed, while being the shortest, are not that easy to read (at least to a newbie like me).
I’d argue that there’s gotta be a happy medium, maybe something like the one written by czyzykowski. Their solution is logical, modular and very easy to follow.
Feel free to comment below if you disagree, like I said, I’m still new to this, so maybe those last 2 examples are perfectly readable to someone who’s been at this a while.
I think the takeaway from all this should be – don’t worry if your code looks like a fanewgi wrote it, just keep plugging away…
Read code others have written and keep writing more of your own. Before you know it, you’ll look back at code you wrote a while ago and realize just how far you’ve come!!
That’s it for now, I hope this helped straighten out the code…
It’s been a couple weeks since I’ve had the chance to write a post, as things have been a little crazy around the Crooked Code household…
Football season started for my two boys, they started school this week and my wife went back to work this week!! Hopefully, now things will start to settle down a bit.
Anyway, enough about that, let’s talk about what we’ve accomplished this summer and what we want to accomplish this fall….
What I’ve done this summer…
As I mentioned in a previous post, I finished the front end certificate at Free Code Camp in July!!
I decided to work on the Back End Program at FCC, then jump back and complete Data Visualization. Thus far, the back end has been difficult, but enjoyable.
It’s been difficult in that I’ve really had to change gears in my head. Node.js and working on the server side have been completely different than any other coding I’ve done in the past (granted, it’s a fairly limited past as far as coding is concerned).
I started the back end program by working through the Cloud9 modules for Git, Node.js, Express.js and MongoDB.
If I’m being honest, I found these modules to be pretty useless… I didn’t learn much from them, and half the time I wasn’t even sure what they were asking me to do.
As a beginner, I think it would have been more useful to have a better intro to the languages and better explanations as to what I was trying to do with them.
Instead, I felt like I was trying to satisfy the requirements of some very random modules so I could move on (and getting nothing out of it in return)…
I realize, that’s just my personal opinion and that different people learn by different methods…
Intro to the very helpful Ninja!
Speaking of actually learning, I found a very helpful tutorial series on YouTube called Node JS Tuturial for Beginners by the Net Ninja. I may have mentioned this guy before and I highly recommend these videos if your starting out learning the back end.
There are quite a few videos in the series, but each one is short and to the point and very easy to understand. I watched each of the videos and worked along side them in building a ‘To Do’ app in the last 7 or 8 videos.
In addition to a great intro into Node.js, one of the cool tangential things I learned from this series was working with Atom.
The ‘Ninja’ takes you through the steps of setting up Atom, installing Node, npm, etc. I’ve been working in Atom ever since and find it a much better environment to develop the back end projects than working on Cloud9.
Initial Thoughts on Server Side
At the risk of sounding a little naive and corny, learning how servers work has opened my eyes to the potential you have as a full stack developer.
Let me try to explain what I mean…
I feel like I learned a ton in the Front End program but I always had a nagging in the back of my head like, ‘yeah, these new skills are great, but how do I actually use this to build something bigger than a one page app??’.
Working with Node and Mongo and everything else involved in the back end program (yes, even Git) has calmed that nagging a bit and given me a sense of the bigger picture.
Granted, I’m less than a month into this and have a lot of work ahead of me, but it’s very motivating to start tying things together like this…
You can find the code for all these apps on my GitHub page. Please feel free to comment below with any suggestions or questions about my code.
Where we’re heading this fall…
First and foremost, I’ll be working towards completion of the Back End Certificate. I plan on finishing up the last two API projects then moving right on to the full stack ‘Dynamic Web Application Projects’.
In addition to the Free Code Camp projects, I’ve continued listening to tech related podcasts. I mentioned most of the ones I’m listening to in a previous post, but have picked up a few new ones as well.
The Elections Podcast is by the awesome people at FiveThirtyEight.com where they talk about how they are applying their models in an attempt to predict the outcome of this year’s US presidential elections. It’s very relevant and very cool…
Internet of things
As I’ve been working through the back end program at FCC, I’ve been trying to think of ways to start applying what I’m learning in the real world.
One idea that I’ve been giving a lot of thought to lately is the internet of things (hence the IoT Podcast).
The internet of things, if you don’t already know, is a broad term that describes all the real word items (refrigerators, cars, wearables, thermostats, etc, etc, etc) that are hooked up to the internet and can be sensed and/or controlled remotely, the so called ‘smart’ items.
Just yesterday I ordered a beginners kit that includes a board and a bunch of sensors and motors so I can start playing around with this stuff. I’m really excited about it and one of my sons wants to get involved too, which makes it even better!
A friend of mine, Serg Chernata, just started a blog called ‘Bits & Pieces‘, where he’s going to discuss a bunch of tech related topics, including his experiences with IoT. In fact, his first post was about setting up a Raspberry Pi as a home server to host his blog.
I’ve included a link to his blog on the sidebar of this site under ‘Recommended Blogs’. I’ll keep adding to this list as I come across sites that I find interesting and useful.
Once again I’ve written a much longer post than originally planned, which is not easy for someone who says so little in the real world! Hopefully, if you’re still reading, you got something out of this.