100 Days of Coding — Day 0

So I’ve sort of let life and work take me by storm, and haven’t written anything on here in months… which is a shame, as I’ve bought a new computer, installed Ubuntu-Mate as well as a new python install, all of which I should be posting about…

But I recently read an article here about coding for 100 days in a row, and I really wanted to make an effort to do this – to not only improve myself, but also make a stronger effort to learn various programming algorithms and languages.

So – for day 0, I spent the time learning git – and made a couple of commits to a project I was using, and had made changes to on my local system – but hadn’t been using git for… so I changed that… forked the repo, cloned it to my PC, made the modifications in 2 separate branches, and then commited said changes and made pull requests…

All that was a bit of a process as I had to set up SSH key for GitHub and what not – but it worked out at the end of the day…

I have several projects in mind, as well as working on freeCodeCamp as well…

Here is hoping the next 100 days are eventful! (At least for programming!)
Never B Flat, Sometimes B Sharp, Always B Natural

Regular Expressions, Part III (Phone Number – basic)

As I promised, here is another discussion about regular expressions, and more specific, phone numbers. The first part of this, I’m going to discuss the HackerRank Python Regex problem on validating phone numbers with regular expressions, but as its a very simplistic case, I’m going to expand on it.

Because, it’s pretty simplistic, I’m simply going to show you the code for the Hackerrank problem.

^[789]\d{9}$

Since it’s so simplistic, I figure I’ll just go over the whole thing. First, notice the ^ and $ anchors. Again, these refer to the fact that everything is occurring on the evaluation. I’ll explain this better last…

The next piece is [789]. This simply tells us that we are looking for a 7, 8, or 9 as the first digit.

Next is \d{9} which is 2 parts, the first \d meaning we are looking for any digit, and the second {9} meaning we are looking for exactly 9 of them.

All that combined, along with the anchors I mentioned before, means that we are searching the input we are using regex on for 10 numbers, starting with a 7, 8, or 9. Nothing else will generate a match.

Now, suppose we didn’t care about starting with a 7, 8, or 9. The first thing we would do is drop the [789] and change {9} to {10}. Simple enough!

However, lets make this a bit more realistic. This makes no allusion to formatting… it doesn’t quantify the real formatting for a North American phone number. So let’s sat that formatting (which I’m simply going to refer to North American Phone Numbers as phone numbers).

A little education for us here. Phone numbers are divided into 3 sections. The first 3 digits are referred to as the area code. The next 3 digits are referred to as the Central Office code, frequently called the exchange, and then the last 4 digits are the subscriber number. Now there are some rules for the area code and the exchange.

For both the area code and exchange, the first digit must be between 2 and 9 (thus no 0’s or 1’s). The next 2 digits can be anything, however, for the exchange, they can’t be 1 and 1 (as those are for special services such as 911 or 411, etc). For simplicity, I’m going to leave it at that, and not worry about the extra formatting or extensions and such… this is more for a basic understanding…

We know we will use our anchors still, so ^$ go in immediately. Also, the last 4 digits are simple as well. I’m going to use parenthesis in the regex to simply group everything, so we know which sections are for which. We know that \d{4} will work for our last for digits, so we’ll add those.

So we currently have ^(\d{4})$. Now, the area code is pretty simple. It’s 3 digits, but can’t be 0 or 1, so that means [2-9]\d{2}, so we’ll add that in the front, giving us ^([2-9]\d{2})(\d{4})$. Now comes the hard part.

The exchange is a bit more difficult, because it can’t be #11. It can be #1#, or ##1, so it has to be broken down into multiple pieces.
The first piece is if the 2nd digit is a 1. That would be [2-9]1[02-9]. Remember the first digit can’t be 0 or 1 (always, from the above rule). We already stated that in this piece, the second digit is 1, which means the third piece can’t be 1, thus the [02-9].
Now the next piece will be the 3rd digit being 1. The nice thing is all the other rules apply, so all we need to do is switch the 2nd and 3rd numbers, giving us [2-9][02-9]1.
Now the last piece is the one that makes logical sense. If we’ve covered the case where the 2nd digit is 1, and the case where the 3rd digit is 1, then all we need to do is cover the case where neither digit is 1. We already have that rule [02-9], so all we do is put that in both spots, giving us [2-9][02-9][02-9]. There are all 3 pieces. Now we just put them together.

If you remember back in the Roman Numerals post, we talked about alternation, or the pipe |. That is basically a way to say this or that, and would look like this|that. All we have to do is combine the 3 pieces, putting that alternation between each piece, giving us [2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9][02-9].

Which gives us our final result of ^([2-9]\d{2})([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9][02-9])(\d{4})$. All done.

For the record, I tested the above on regexr.com, adding the following phone numbers: 5123456789 and 5123114567. It only matched the first one! Yay!

Until next time!

Never B Flat, Sometimes B Sharp, Always B Natural

Regular Expressions, Part II (Roman Numerals)

So, lately with HackerRank, I’ve been going over their regex section in python. I thought I’d review some of what I’ve done, for my own sake to review later on down the road!

Here are some of the regex problems I’ve solved… For the record, I’ve modified the original post of this to what it is now – before, I just listed the regex expression, and what the whole expression did… Now, however, I’m explaining what each bit does for my own understanding! I’m also reducing how many I do per post so there will be more posts about it as I go into a deeper detail for them…

The one that I will be discussing today is a regular expression to validate roman numerals.

For roman numerals, the expression is:
^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$

We start of with ^ which is called an anchor and it is used to say we are starting from the beginning of the string we are comparing…

Next is M{0,3} which tells us we are expecting the letter M between 0 and 3 times. Which covers any thousands digit.

The next piece is (CM|CD|D?C{0,3}) and it involves our first grouping. The parenthesis defines our grouping which can be used to later to pull out this specific information. We next have letters between pipes (|), which is called alternation. It’s saying we are next looking for CM or CD or D?C, which the question mark here means that the D is optional – meaning we are looking for C alone, or DC together, and that is followed by {0,3} which, like before, means we are looking for 0-3 of… so that whole grouping says we are for CM (900), CD (400), or D (500) with up to 3 C’s… which covers hundreds portion of any number over 99.

The piece after this is (XC|XL|L?X{0,3}) and it is also a grouping (which again, we can pull from later). This section covers our 10’s digit, and just like the last grouping, is looking for XC (90) or XL (40) or L (50) and/or up to 3 X’s (10 each).

The last piece is (IX|IV|V?I{0,3})$ and covers our 1’s digit. It’s also grouped so we can pull out this information later. Just like the prior 2 groups, it’s looking for IX (9) or IV (4) or V (5) and/or up to 3 I’s (1 each). The last piece, the $, is referring to the end of the line.

So if we put this all together, we are saying in the line we pass in, if there is a character that isn’t an M, D, C, L, X or I, this will fail. Also, if proper characters aren’t in the proper order, this will fail, and if there is anything after the lowest number, it will fail.

All in all, a pretty neat expression!

Next time, I’ll go over phone numbers.

Never B Flat, Sometimes B Sharp, Always B Natural

JACU II

Once again, it’s another installment of JACU (or Just Another Computing Update)!

HackerRank: I currently have 1577.91 points, and am ranked 793. That means I’ve earned 570 points, and risen 698 ranks! Not bad. I’m almost through regex in python (of which there will be a couple of posts for)

CodeWars: I’m currently at 5kyu with 154 honor points, and am ranked # 10923 [93%]. I’ve increased 1 kyu, and 63 honor, as well as risen 8374 ranks (and 10%). I’m going to work on these a bit more as well.

CheckIO: I’m currently level 9, with 543/750. I completed the last 2 puzzles in Home, as well as finished Elementary, and have started on O’Reilly. There are some puzzles that are locked, since I’m not a paying member… I’ll think about remedying that…

CodeEval: I’m currently at 70 points – but haven’t completed the challenge, as they don’t tell you what is wrong, so I’m not sure where the bug is in my submission (I get the correct answer on my system), so I’ve sort of stalled on this one… I’ll be coming back to it soon.

Project Euler: Using HackerRank’s Project Euler contest, I’m ranked 1307th/48187, so that’s a good increase of 1608 ranks, and about 5000 more contestants joined, so I’m going strong here. I’m currently working on Project #23 (which has been solved on the PE site, just having an issue solving 1 test case, and not getting any help on the details…

So that is where I’m at! I will be blogging about my daily programmer exercises, once they are closed!

The More I Drink – Blake Shelton
Never B Flat, Sometimes B Sharp, Always B Natural

How To: Use Let’s Encrypt with a (my) hosted VPS!

Please Note: The following tutorial is how I used my VPS and installed Let’s Encrypt on all of my sites. This is also for apache. nginx is not too different, but you’ll have to deviate from this tutorial slightly.

First thing to note is there are lot’s of less manual ways to handle this… Sites with newer versions of CPanel have a Let’s Encrypt feature, so you should use that. Also, if you are hosting yourself, or using a box ‘you own’ as in have full admin privileges (such as Digital Ocean), then there are much better ways to do this… However, I purchased a 2 year plan with my VPS, and wanted to do it this way to see how difficult it really is (it really isn’t that bad – lots of copying/pasting).

As I’m doing this for my 4 sites (my ME site, my upcoming business site, my gaming page, and of course this site here! The steps are the same for each, you just have to do it for each site!

So here you go…

  1. You will need to have the following 3 windows open (however you choose)…
    • an SSH session into your hosted space (I just used Terminal and ssh’d into myusername@myarvixeip.tld and entered my password. I know I should set up an SSH Key so I don’t worry about the password, and I’ll probably do that once I reformat my computer). I’ll refer to this as ssh when I’m talking about this window below.
    • a web browser open to your cpanel, which I’ll refer to as cpanel
    • and a web browser open to Let’s Encrypt’s free HTTP Site, which I’ll refer to as encrypt
  2. In cpanel, go to your SSL/TLS Manager and select Private Keys. In there, you will generate 2 private keys (I used 4096 bit). I labeled 1 Account for musicalcoder and the other was domain for musicalcoder. Will make it simpler later on
  3. In ssh, you should see where your keys were created (~/ssl/keys). You’ll then create a symbolic link to the account key (named account.key). This will be useful in future steps.
  4. In ssh, Enter openssl rsa -in account.key -pubout which generates your public key for your private account key. You’ll then copy that into the clipboard.
  5. In encrypt, Enter your account admin email address (for me it was admin@musicalcoder.com), and then paste that public key into the box. You’ll then click the Validate Account Info button.
  6. In cpanel, go to your SSL Manager, and then go to CSRs. Here you will want to generate a CSR with your DOMAIN key for your site (which we generated earlier, remember?) Fill out all the pertinent information it wants, and then click generate. (NOTE:If you want multiple entries, such as yoursite.com and www.yoursite.com, you must enter them both in the domain window, on separate lines.) Once that’s done, you’ll see your ‘Encoded CSR’. You will copy this to your clipboard.
  7. In encrypt, paste your CSR information in step 2, then click Validate CSR
  8. At this point, you should be at Step 3 in encrypt! Here there are 4 boxes that you will want to copy, one at a time. You’ll notice that it references account.key – because we made a symbolic link before, this will be as simple as copy and paste. Without it, you’ll have to edit the command to reference your account key.
  9. Paste the command into your ssh session, and you’ll get a a huge set of hexadecimal values. You’ll copy the entire output (including the (stdin) bit) into your clipboard, and the paste it in the corresponding text box in encrypt. Repeat this for each of the 4 entries.
  10. Once you’ve done all 4, click the Validate Signatures button. This should take you to step 4.
  11. In step 4, you’ll copy and paste another entry similar to step 3. Once you’ve pasted the result, you’ll then click on Option 2 – file based and then how do I do this?. In the section that appears, copy step 4 (the echo command) and paste it in ssh. It will error (as expected) because its using a /path/to/www reference, which you will have to change to your actual www reference. Once you’ve fixed that, run the command.
  12. Click the I’m now serving this file on [yourdomain.tld]. If you entered multiple domains in step 6, you’ll have multiple files to create. They are just repeating the last 2 steps for encrypt step 4.
  13. Once that is done, you’ll be on encrypt step 5, and will generate your certificate. Copy the certificate code.
  14. In cpanel, go to SSL Manager, and then Certificates, and paste that code into the textbox provided. Then click Generate
  15. When that’s finished, you should then have an entry at the top for your domain. Click Install
  16. When that’s finished, go back to encrypt, and test your installation.
  17. Before closing ssh, you can delete the directory .well-known and its subdirectories and files.

All finished!!!

I did that for each of my domains… it took about a half hour, although the last site to only a minute or two as I was used to it.

I hope this proves useful to you!

Never B Flat, Sometimes B Sharp, Always B Natural

September 2016 Infodump (and October as well…)

As I stated, I’m doing monthly updates (at least when I post during the month – which is why this is October as well, since there were NO October posts – shame on me…). Again, as I have had no visitors (that I can tell) and I’m not promoting or selling anything, so there is no income – this will just be a goals update!

My September 2016 Goals were:

  1. 10 blog posts
  2. 8 comments on other blogs

So, let’s see how I did…

So only roughly 60% of my goals were met…

I will work harder on making those goals…

So, for November, I’m not changing anything… My goals are:

  1. 10 blog posts
  2. 8 comments on other blogs

Until next time!
Never B Flat, Sometimes B Sharp, Always B Natural

Where, oh where, has MusicalCoder gone???

The month of October has slipped on by, and me without a single blog post… whatsupwiththat?!?!

I’m going to make the effort (attempt at least) to blog every day this month… we’ll see how that works!

More soon!

Call Me… I mean… Collatz…

So – I was working on a Project Euler problem (#14 specifically), that deals with Collatz numbers. For those that don’t know, a Collatz number is a sequence by which you get a number to 1. If the number is even, you divide by 2, if its odd, you multiply by 3, and add 1, and the number is how many steps it takes to get to 1. So for example, 13, has a Collatz number of 10, as it takes 10 steps to get to 1 ( 13 > (3*13 +1) 40 > 20 > 10 > 5 > (3*5+1) 16 > 8 > 4 > 2 > 1). Naturally, as you get bigger numbers, it takes more steps to calculate the number of steps…

Now, not only do I do this on Project Euler, I also do this on the HackerRank site… Because while the problem is basically the same, they always throw a twist in… Using this example, Project Euler wants to know what number below 1 million has the most steps to get to 1… HackerRank on the other hand will have anywhere from 3 – 15 ‘test’ cases you need to solve, that follow certain rules, and your program only has so much time to run to pass. So for this problem, a single test case will contain anywhere from 1 to 10000 integers, that range anywhere from 1 to 5 million, and the program has 10 seconds to figure out what the maximum collatz sequence is for any given number. As an example, if the number give was 10, you would have to respond with 9 (since the number 9 has the most collatz sequences of any number between 1 and 10), and then if you are given 15, you would have to respond with 9 (since again, it has the most sequences to get to 1 between 1 and 15) and then you are given 20, and have to respond with 19 (technically 18 and 19 both have the same number of steps (22) to get to 1 – however the constraint dictates that if there are multiple ‘max’ paths, take the higher number – thus 19)… So this is what a simple test case of 3 numbers is… now handle 15 test cases, each with a different set of up to 10000 integers, ranging from 1 to 5 million… and do it in 10 seconds…

So that was the task…

So first – we realize we want to use memoization… What’s that you ask? In simple words, its caching calculations so you don’t have to do them again… Using our 13 example from above, what if, when we calculated 13, we stored each step… so that when 13 was done, we knew that 13 takes 10 steps, but we also know that 40 takes 9 steps, and 20 takes 8 steps, and 10 takes 7 steps, and 5 takes 6 steps, and 16 takes 5, 8 takes 4, 4 takes 3, 2 takes 2, and 1 is 1 step. By keeping this cached, we then are able to save lots of steps. For example, if we get to the number 80, we start our function, and step 1 takes us to 40… now instead of doing the other 9 steps, we look in our cache and see that 40 takes 9 steps, so then we can say in 2 steps, that 80 takes 10 steps… So by doing that and storing the values, we can then simply look up a given number and see how many steps it takes once they are all calculated…

So I created a recursive function that checks our cache, and if our number exists, it returned the # of steps, otherwise, it return the number of steps of either the even calculation or the odd calculation, depend on whether the number was even or odd… so I create that, and add the function for reading the input sequence, and let her rip… My little program goes through the first 2 test cases… and then starts to time out… because she is taking longer than 10 seconds to go through all of the thousands of possible numbers… hmm….

So then I think, well since I’m already using memoization, what if I prebuild all of the values from 1 to 5 million, so then all I need to do is simply look up the max value for a given number…

Except building the initial list from 1 to 5 million takes 13 seconds (on my computer)… hmm…

So now its time to start thinking outside the box…

So first, I think, well, if I start at 5 million and work my way down, lots of things will have been created by the time I got to them (as an example, when 5 million process, it process 2.5 million, which in turn processes 1.25 million, which in turn processes 625000, which processes 312500, etc, etc… So I change my prebuild to go from 5 million to 1 down… This actually knocks 2 seconds of my prebuild…

So… more efficiency… I think and realize that at some point building downward, I’ll reach a number that afterwards, every number below it has already been processed… So I add some variables to my Collatz function, and after processing, it tells me that moving downwards, 125001 is the last uncached number… so now, I only have to build down to 125001, thus saving me 125000 function calls, plus all of those recursive function calls…

However, this only knocked about 2 seconds off as well… so I’m sitting here at 8+ seconds… failing my test cases because it still takes, even with the cache build of 8 seconds, 14 seconds to process all 5 million numbers… So now what…

I start timing individual numbers… how long does it take to calc the 3 million? So I run that in quarter million increments to see how much these higher numbers take, and then noticed something… 3.75, 4, 4.25, 4.5, 4.75, and 5 million all return the same maximum… 3732423 at 579 steps. And then the light bulb goes off… If I ‘hardcode’ a list with that value, I don’t need to test any value above 3732423, because that is the max… that will save 1.27 million function calls + all of the additional recursive calls… So I write a function that sees if the value is greater than or equal to my list value, and if so, report the list value, otherwise, do the cacluations…

That one step shaved literally a second of my test time…

So, to make a long store short (too late), I ended up hand calculating (using my function), the top 50 #s… to understand how fast that made the program, when you see that #50 is the number 97, you realize that for all 4999903 numbers above 97 that could be in the list of integers, there are only 50 possible answers for the maximum collatz… which of course, means my program ran in under 1 second for all 10000 entries, no matter what they were…

So there, that is the story of how I didn’t use a function in the normal way, to gain some major efficiency in a program! If you want to see my program, you can see it on my github page here

Call Me – Blondie
My Way – Frank Sinatra
Never B Flat, Sometimes B Sharp, Always B Natural

J.A.C.U.

So – I’m going to name these Program Testing updates (or coding updates) JACU – for Just Another Coding Update… in honor of Blue Thunder’s JAFO… {google it}

As I start participating in any other sites, I’ll be logging information here… I’ve done reddit’s daily programming once or twice, and I might be signing up for stack exchange’s Code Golf soon… but I’ll have to review it as it seems like something I might not be interested in…

But for now… I digress…

So, we’ll just go down the list… Since my last update, here is where I stand…

    HackerRank:

  • I’m in the 4th Percentile in Algorithm’s – Haven’t done any of these since I completed the initial trek – focusing on…
  • Python: 1007.91 points (boosted by doing 1 partial contest), with a rank of 1491… I’ve completed Introduction, Basic Data Types, Strings, Sets, Math, Itertools, & Collections
  • Week of Code #23: I took part, and did some, but it was a bit beyond me… but still placed 2642nd/10494 [or top 25%]… I’ll probably try a few of their simpler contests to get a better feel, then do more of these!
    CodeWars:

  • 6 kyu (still) – although I’m about 90% finished with level 6
  • 91 Honor points
  • Honor Position # 19297 [83%]
    CheckIO.org:

  • Level 6: 177/235
  • Home: 110 pts – 53% completed… The last 2 are moderate and challenging rated, and moderate is re-writing min/max functions… for me, that is a couple of hours spent on this 1 test… so I’ll wait until I feel more comfortable with args. However, I’ve unlocked O’Reilly, Scientific, Elementary, & Electronic Station.
  • Elementary: 60 pts – 50% completed
    CodeEval:

  • I literally just confirmed my account – so I don’t have anything here yet… will start either later today or tomorrow!
    Project Euler:

  • I’ve finished the first 13 puzzles
  • using HackerRank’s ProjectEuler contest, I’m ranked 2925th/43394

So there is my progress since I last checked in with my coding challenges!

Until next time!

Eye of the Tiger – Survivor
Never B Flat, Sometimes B Sharp, Always B Natural

But I digress…

First – much respect to bloggers who blog on a regular basis… its very easy to let life get in the way of your ‘scheduled’ blogging time!

Now, on with the show!

I figured this would be a short topic on ‘What I learned this week’ – or for me, this month!

I seem to be the type of person that needs lists to get things done… so I’ve been researching Bullet Journal as well as following Kara Benz on BohoBerry regarding her Bullet Journal 101.

I ordered my notebook (which should be here tomorrow) and I’ll review the system, once I get it into place! (Which will probably be the rest of the week).

I’m also writing an update post on the various programming exercise sites I’ve been using, and what my status is on each site – just to track and inform!

This feels like a short one today – but I’ll be posting that programming update, so I’m okay with it!

The Show Must Go On – Queen
Never B Flat, Sometimes B Sharp, Always B Natural