Tuesday, October 11, 2016

Python and simultaneous assignment

I'm taking a little break from Javascript at the moment to pick up Python. While I know some C and JS and have seen that they share a lot of similarities in syntax structure Python is a completely new animal to me (pun totally not intended).

While I could list off the syntactical differences there are some things that Python can do that make life just a little bit easier for the average programmer. I plan to bring those up as I continue to make my way through learning the language and in that spirit I present the first "huh, that's cool" difference: Simultaneous assignment.

For today's moment let's compare Python to C. Let's say the first thing we want to do is create two integer variables and then assign the first variable (num1 in our example) the value of 4, and the second (num2) we want to assign the value 0.

In C we would go about doing this with statement something like this:

C program to for this example

Pretty simple right? We declare the types and then through separate statements assign them each their respective values.

So what can you do with Python?

Python program to accomplish the same task
Pretty cool, right? Python was able to create the variables and assign them the correct values in 1 line of code while it took C to do so. It's not earth shattering or anything but more compact (though still readable!) code is nearly always better. But that's not the only thing that is super cool about this feature:

Let's say we're faced with a classic problem in programming, needing to switch the values in two variables. So in our example we would like num1 to equal 0 and num2 to equal 4 after initially being assigned the values of 4 and 0 respectively.

In C we'd have to do this tried and true solution:

In which we create a third helper variable to hold the value from one of the other variables, change the value of the variable that we just copied to the helper variable to the other "main" variable, and then finally assign the helper variable's value to the original value. That's a decent amount of code just to switch the values of two variables!

So how does Python handle this?:

That's it, just like the assignment of the initial values you can simultaneously switch the values in one statement. No helper variables, no multiple lines of code to juggle the values around, just beautiful simultaneous switching. It won't change the world but it's a nice feature all the same!

Tuesday, October 4, 2016

Javascript's fromCharCode() method

So as I mentioned in my previous post I've been making my way through freecodecamp.com's curriculum during my downtime. While working through the intro bonfires I stumbled upon something mildly interesting:

Caesar's Cipher solution

The above is my solution for the "Caesar's Cipher" bonfire which asks you to translate a string by substituting each letter for the one 13 places in the alphabet from the input string (e.g. B = O). Since I've played around in C I'm familiar with ASCII character codes so the idea behind this particular algorithm wasn't that challenging. What was challenging was getting it to work correctly.

The first part of this code converts each letter of the input string into it's corresponding ASCII code character and then pushes it to an array after modifying it to fit the requirements of the algorithm; values A-M were char value + 13, while values A-Z were char value - 13, and any other character was left unchanged. Nothing earth shattering at this point.

From here the only trick was to convert these character codes back to their corresponding values. Javascript has a built in method, String.fromCharCode(), that does just this, though it's a bit trickier then I realized...

Javascript, just like every other programming language I've encountered, allows you to reference a variable when assigning a new value to that same variable (e.g. x = x + 5;), in fact, like most languages it actually has a host of built in shorthand operators to make it easier to do so: +=, -=, *=, /=, etc...

That's all well and good. The strange this was that this wasn't an option when using the String.fromCharCode() method. This method was new to me as I'm not used to dealing with many of the static methods in Javascript (It's the first time I've actually used the "String" object (prototype?)) in JS so maybe that explains my difficulties in getting this to work...

I spent ten minutes trying to get my second for loop to run with the code

arr[j] = String.fromCharCode(arr[j]);

and the code not working. Finally I decided to push the converted values to a new array (storArr) in the code and it worked. The thing is, I don't know why this worked for the new array and not for the previous one :(.


Thursday, September 29, 2016

A massive case of the gits...

Just a quick update with a link to my github profile. I've been told by those in the software industry that version control is somewhat important.

In other news I'm making my way through FreeCodeCamp.com's bonfires and I imagine that those will be the first things I actually upload to my repository... or far more likely the entire contents of my desktop when I manage to bungle the path :).

Tuesday, September 20, 2016

Introductions

Hi and welcome to the blog that I'm using to track my progress learning how to code and work with the big wide world of electronics! I'm a rank amateur but it's all about improvement, right?


Instead of saying where I'm at and how I'm progressing please read this as if I was a rank amateur (which, spoiler, I am!)

Thanks for your interest and hopefully I can get something interesting than this intro post to get you to stick around!