edX, CodeWars, and CriticalRoleRenamer

So, I was supposed to be starting a Python course through edX, but apparently it’s starting late (was supposed to start about 12 hours ago, so I was going to give my first thoughts after reading the syllabus and such, but alas)… so that sounds like something I’ll have to do in the future…

Yesterday I started with CodeWars and completed 11 exercises (codewars calls them Katas), although 2 of them were simple parts of the sign up process. I’m going to try to do at least 2-3 a week, when not focusing on my own projects.

Speaking of my own projects, over the weekend, I wrote my first Python program for personal use. As I’ve stated before, I’m a gamer, and I’ve been trying to watch this show called Critical Role, which involves a group of voice actors playing Dungeons & Dragons. It’s actually a good show to see the game in action… But I don’t have 3 hours an episode to sit their and watch… but what I do have is that time when I’m walking, and driving to work, etc…

So I used a program called ClipGrab and downloaded the episodes from YouTube, and converted them to mp3 files, and then put them on my phone, so that my podcast player (Pocket Casts) would see them. Now I can listen (as admittedly, there isn’t a great need to ‘watch’ the program) while I’m exercising, or driving, etc. Plus Pocket Cast lets me increase the speed and jumps sound pauses (silence in the conversation), so that 3 hour episode only takes me about 2 hours…

But I digress…

When being downloaded and converted, the youtube naming convention (which admittedly is probably just the name the uploader used) isn’t the most consistent, or friendly, especially when you have things like Plex or Kodi for media serving content… so I wrote a python program that grabbed the episode number and title from the file name, and renamed the file… Taking it from, for example, “Enter Vasselheim – Critical Role RPG Show: Episode 16.mp3” to “Critical Role – S01E16 – Enter Vasselheim.mp3”, which is a common naming convention. The neat thing was, by using regex (again, another thing I’m learning), I was able to grab the title, and then episode number, but with code, helped normalize them… because sometimes they used a pipe (|) instead of a hyphen to separate, sometimes they had the colon after show, and sometimes not, and sometimes they had the show title uppercased… But I was able to rename the whole thing (which literally took less than a second of time to run) so that the episode title was standard Capital Case, and based on thetvdb.com, had the season numbers properly meshed out…

While its a simple program, all in all, i’m rather proud of it… My next step will be using it for other shows I listen to! You can see it in its current form on my GitHub page!

Rocky Mountain Way – Joe Walsh
Never B Flat, Sometimes B Sharp, Always B Natural

Project Euler #1

So I’ve been threatening to do this basically since day 1…

To aid in my programming learning, I’m doing various programming puzzles… via Project Euler, Exercism.io, Coding Bat and others…

But I think another part of the learning process is explaining things, or sometimes at least why I did what I did…

So I want to discuss my solutions… Instead of boring you here with code, I’ll link my GitHub Gist, as well as my JSFiddle solutions.

First of all, the problem:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

Now, first, the key word here is under. But we’ll get to that in a second.

At first, this looks like it can be solved with a simple loop, and really, for this exact problem, it can be, with little concern about cost. However, what if this was changed to 1 million, or 10 million, or 100 million…

Well, on most modern-day computers, looping through 1 to 1,000,000 takes about 2 seconds… 1 to 10,000,000 takes about 20 seconds… while 1 to 100,000,000 takes over 3 minutes… while 2 seconds is not horrendous, 20 seconds is bad, and 200 seconds is down right insufferable – at least from today’s web standards…

The solution for this is simple, and usually overlooked… Its taking a simple equation 3 times and your done.

Wait, what?!

All this is looking for is the sum of all multiples of 3 under 1000 (so 3+6+9+12…+996+999) and the sum of all multiples of 5 under 1000 (important to note since 1000 is divisible by 5, if we use it, we’ll get the wrong result) (so 5+10+15+20…+990+995). Now, if you remember your high school math, there was this equation that allows you to get the sum off all the consecutive integers that made up a number… so, for example, the sum of the consecutive integers for 10 is 1+2+3+4+5+6+7+8+9+10=55. The formula is n(n+1)/2… or 10(11)/2 >> 110/2 = 55. So having that, all we have to do is realize that the sum of 3+6+9+12 (quick example) = 30 is the same as (1+2+3+4)*3 >> 10 * 3…

Now, if that’s the case, then the sum of consecutive multiples of three would be 3 * ((n/3)(n/3+1) / 2), or in this case, we know that 999 is the last, and 333 is 1/3 of that, so 3 * 333 * 334 / 2 = 166833, and the same would be true of for 5 (5 * 199 * 200 / 2 = 99500). So we would think, at first glance, that our answer is 266333. However, we’d be wrong!

Wait, what?!

Well, we forgot one thing. In using this equation, every multiple of 15 (3×5) is being added in twice… once for the 3 side of it, and once for the five side of it… so all we have to do is subtract out one of them… so 15 * 66 * 67 / 2 = 33165… Now, if we take 166833 + 99500 - 33165 = 233168, which would be the exact same value our loop gives us…

So if we did this to get the results for 100,000,000 instead of 1000, we’d get our solution in under 1/100th of a second, vs 3 1/3 minutes, and at way fewer CPU cycles, which could be very important for some algorithms you write!

So that is my first foray into Project Euler, and the programming puzzles!

Young and Innocent -Elefante
Never B Flat, Sometimes B Sharp, Always B Natural