Categories
Programming

Learn mobile development at RayWenderlich.com for 50% off — but only until Monday!

Native mobile development skills are rare. Being able to point to apps in the store and say “I made those” can really help you stand out. And you can learn how — at half price, if you hurry!

raywenderlich.com is the premier mobile development tutorial subscription site, and right now, everything in their store is 50% off!

They’ve got a number of subscription plans that are a great deal, especially when you factor in over 3000 videos, dozens of books, and access to the forums:

  • Ultimate Beginner plan ($12.42/month on sale, normally 29.99/month):
    • Access beginner courses
    • Access beginner books
    • iOS & Android learning paths
    • Stream via iOS/Android app
    • Hands-on challenges
    • Download source code
    • Forum membership
  • Ultimate Professional plan ($24.92/month on sale, normally 49.99/month):
    • Access every book in our library
    • Access advanced ‘Pro’ courses
    • Download & watch courses offline
    • Access beginner courses
    • iOS & Android learning paths
    • Stream via iOS/Android app
    • Hands-on challenges
    • Download source code
    • Forum membership

These deals last only until Monday, November 30th, so if you want to save some money and pick up or sharpen your mobile dev skills, you should act quickly!

I learned iOS development through RayWenderlich.com’s book, iOS Apprentice, back when Objective-C was the only option, and the iPhone 4S was the hot new device. I became a regular reader of the site, and over time, got to know the team and even joined, going on to write articles, a video, and even co-author a book! I can’t think of a better place or a better group of people to learn from.

I’m not the only one who thinks so…

If you want to get into mobile development, this deal’s for you. Sign up before Monday, November 30th, and get mobile coding!

Categories
Programming

Once more, in Swift (or: A solution to day one of Advent of Code 2019)

As I wrote in the previous post, the Advent of Code is happening soon — it start on Tuesday, December 1st and runs all the way to December 25th. If you want to give your programming skills a good workout or test, you’ll want to try out Advent of Code’s challenges!

The previous post featured Python solutions to the day one challenges from the 2019 edition of Advent of Code. In this post, I’ll present solutions written in Swift.

Day one challenge, part one

Here’s the first part of day one’s challenge:

The Elves quickly load you into a spacecraft and prepare to launch.

At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven’t determined the amount of fuel required yet.

Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.

For example:

  • For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
  • For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
  • For a mass of 1969, the fuel required is 654.
  • For a mass of 100756, the fuel required is 33583.

The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.

What is the sum of the fuel requirements for all of the modules on your spacecraft?

While the problems in the Advent of Code are the same for every participant, the data for each participant is different (there’s a sign-up process, which gives you an account, your own progress tracker, and your own data). This prevents participants from simply sharing the solution.

Here are the module masses that were provided for my account:

I used the Python REPL for my Python-based solution. For my Swift-based solution, I used the closest analogue: an Xcode Playground.

Swift, like Python uses the three quotes to denote multiline strings. I used them to define a multiline string constant, rawInput, into which I pasted the data:

With rawInput defined, it’s time to convert it from a multiline string into an array of strings, with each line getting turned into its own array element. The String class’ split method does this quite easily, and the result was the splitInput string array:

The next step was to convert splitInput’s numbers-in-string-form into actual numbers. This process would involve applying the same function — the Int struct’s “init from string” method — to all the elements in an array, which is exactly what the map method is for:

Swift’s map method takes a closure containing a function as its argument and applies that function to every item in the given array, creating a new array as its result.

In this case, the function in question is:

Parameters passed into the closure begin with the $ character, which is then followed by a number specifying which parameter it is. The first parameter is $0, followed by the second parameter, $1, followed by the third parameter, $2, and so on.

Only one parameter is passed to the closure: $0, which represents the current element of the splitInput array. It’s fed into the init method of Int that takes a string and attempt to produce an integer. Since it’s possible that this method will be given a string that can’t be converted into an integer, the method’s return type is the optional type Int?.

Since I’m quite certain that all the strings in the splitInput array convert to integers, I used the ! operator to force unwrap the resulting Int? values.

The end result is masses, an array of integers. Each element in the array represents the mass of a component in the ship, and we need to calculate the fuel necessary to propel each component to the final destination.

This calculation involves applying a function to every element in masses, and that function is:

  • Divide the mass by 3, rounding down.
  • Subtracting 2 from the result above.

Once again, I used map:

In the function above, mass and 3 are both integers, so mass / 3 is an integer division, which automatically rounds down.

The result of this mapping is fuelRequirements, an array of integers containing the fuel requirements for each module.

The result is the sum of all the values in fuelRequirements. Unfortunately, Swift doesn’t have a built in method for getting the sum of an array, so we’ll need to roll our own:

For my data, the result was 3454942. This turned out to be correct, so it was time to tackle part two.

Day one challenge, part two

Part two involved recalculating the fuel requirements when also taking into account the mass of the added fuel:

During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.

Fuel itself requires fuel just like a module – take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.

So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:

  • A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
  • At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
  • The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.

What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)

This called for a recursive function, the Swift code for which is below:

I used this function to map the values in the masses array from part one onto a new array, updatedFuelRequirements

…and the sum of its the elements was the answer for part two:

For my data, the answer was 5179544.

Categories
Programming

The Advent of Code is coming in a few days!

We’re only a few days from December, which means it will soon be time for the great programming exercise known as the Advent of Code!

Think of it as an Advent calendar, but chocolates (or cheese, or wine), you’re presented with a new programming puzzle every day between the start of December and Christmas Day, in which you try to save Santa’s mission. You can use whatever programming language you want, and you don’t need to be an expert — as the site says, “just a little programming knowledge and some problem solving skills will get you pretty far.”

Advent of Code started in 2015, and has been taking place every year ever since. The 2020 edition begins on Tuesday, December 1st at 12:00 midnight Eastern time (UTC-5).

Not only do I plan on participating in this year’s Advent of Code, but I might even use a couple of the challenges in the Python class I’m currently teaching on behalf of Computer Coach.

Solving Advent of Code 2019’s day one challenge

Here’s the premise of the 2019 Advent of Code’s challenges: Santa is stuck at the edge of the solar system, and you have to rescue him. Each day’s challenge, which has two parts, gets you closer to bringing him home and saving Christmas.

Day one challenge, part one

Here’s the first part of day one’s challenge:

The Elves quickly load you into a spacecraft and prepare to launch.

At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven’t determined the amount of fuel required yet.

Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.

For example:

  • For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
  • For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
  • For a mass of 1969, the fuel required is 654.
  • For a mass of 100756, the fuel required is 33583.

The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.

What is the sum of the fuel requirements for all of the modules on your spacecraft?

While the problems in the Advent of Code are the same for every participant, the data for each participant is different (there’s a sign-up process, which gives you an account, your own progress tracker, and your own data). This prevents participants from simply sharing the solution.

Here are the module masses that were provided for my account:

I decided to use the Python REPL to tackle this problem.

My first step was to copy the numbers above, paste them into a triple-quoted string, and assign that string to the variable raw_input:

Now that I had the data in a string, I could split the string into an array, using the newline character as the delimiter. I named the array split_input:

split_input is an array of strings which needed to be converted into integer values.

In many other languages, I’d do this by using the map function to apply a “convert a string to its integer value” function to every item in the array, creating a resulting array called masses. Here’s the Python version of that approach:

It works, but from a Python programming point of view, it just doesn’t feel right.

The Pythonic approach would involve using a list comprehension instead of map (and then using the resulting iterator into a list). It just seems more readable:

Once I had the masses, I could then calculate the fuel requirements for each mass. This may be the only time I’ve ever made use of Python’s floor division operator, which performs an integer division, rounding down:

The map/list/lambda approach works just fine, but once again, a list comprehension just seems more elegant to my eye:

And now that I had the fuel requirements for each module, all I had to do was get their sum:

I entered the value for total_fuel into the solution text field, and Advent of Code immediately told me that I had solved part one of the day one challenge! So far, so good.

Day one challenge, part two

Christine Darden, one of the “Hidden Figures” mathematicians who helped land the astronauts on the Moon.

Part two of the challenge was a refinement of part one:

During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.

Fuel itself requires fuel just like a module – take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.

So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:

  • A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
  • At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
  • The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.

What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)

Upon reading this, my first thought was:

The trick to writing recursive functions is to solve the “escape” case first — that is, the case where you stop the recursion and just return a value.

For this problem, the “escape” case is when the repeated fuel calculation gives a result of 0 or less:

Otherwise, take the result, and apply the fuel calculation to it again. That’s what gives us the recursive part. Here’s the resulting if statement:

And finally, we have to handle the initial calculation. The end result is the fuel_required function:

Now that we have the fuel_required function, we can apply it to every item in the masses array from part one:

This yields the updated fuel requirements for each module. The final step was to get their sum:

Entering the value of updated_total_fuel into the solution text field completed the day one challenge.

Categories
Programming

Find out more about .NET 5 on the Auth0 Developers Blog

Tap to view at full size.

Graphic: dotNET robot.NET 5 is the 5th major version of the .NET framework, the bedrock of modern Microsoft platform development, and it’s out today!

(For the official details, check out the announcement on the .NET Blog: Announcing .NET 5.0.)

In addition to being the replacement for .NET Framework 4.8 as well as .NET Core 3.1, .NET 5 unifies the various flavors of .NET — “.NET Framework”, .Net Core, Mono, the various Xamarins, Unity, and Universal Windows Platform — into a more cohesive whole.

Piano keyboard showing C# and F# keys

With this new version of .NET come new versions of its programming languages: C# 9 and F#5, as well as ASP.NET Core and EF Core.

Want to find out more about .NET 5? A good starting point is the article Five Things You Should Know About .NET 5, written by my teammate Andrea Chiarelli on the Auth0 Developers Blog.

Categories
Conferences Current Events Programming Tampa Bay

Suncoast Developers Guild’s Fall 2020 Conference is THIS SATURDAY!

Suncoast Developers Conference this Saturday!

Hey, Tampa Bay! We’ve got another Suncoast Developers Conference happening this Saturday, and you can attend for FREE, from the comfort of your own computer or phone.

Once again, it’s this Saturday, it’s online, and it’s free-as-in-beer to attend. REGISTER HERE.

The conference will be made of bite-size (15 minutes or shorter!) presentations by Tampa Bay techies and demos of capstone projects by Suncoast Developers Guild alums.

I’ll be presenting on PyGame!

I’ll be doing a presentation in the afternoon, on the topic of…

PyGame! I try to keep my topics both technical and fun, and what’s more technical and fun than Python and game development?

There will be coding, and I’ll also provide source code and a free book.

What was the last Suncoast Developers Conference like?

The last conference took place in the summer, and was also free and online. You can watch the entire thing below…

…and if you want to jump to a specific presentation, use these links:

  1. 00:00:00 Reticulating Splines…
  2. 00:00:58 Welcome & Introductions
  3. 00:08:15 Jason L Perry: Badges? We don’t need no stinkin’ badges!
  4. 00:22:11 Robert Bieber: Will it Scale?
  5. 00:58:13 Kento Kawakami – Demo: Smash Bros Combo
  6. 01:28:30 Cody Banks & Abtahee Ali – Demo: Evolution X
  7. 01:54:54 Daniel Demerin – The Rubber Duck Pal Program
  8. 02:11:12 Colter Lena – Demo: Furry Friends
  9. 02:24:50 Trent Costa – Demo: Tailgate
  10. 02:41:54 Dylan Attal – Don’t Crash! CSS-Modules in React
  11. 02:59:42 Vincent Tang – How to start your own Coding Podcast 101
  12. 03:30:31 Michele Cynowicz – Pull Requests and the Developers Who Love Them
  13. 03:52:11 Abe Eveland – Demo: Rollerblade Buyers Guide
  14. 04:04:45 Liz Tiller – Post-Bootcamp Reflections
  15. 04:23:20 Joey deVilla – Ren’py
  16. 04:41:20 Michael Traverso – You Do Belong Here
  17. 05:01:46 Rob Mack – Demo: What’s for Dinner?
  18. 05:15:38 Kat Batuigas – A Taste Of Docs As Code

How do you register for the conference?

REGISTER HERE. Once again, it’s this Saturday, it’s online, and it’s free-as-in-beer to attend.

I’ll see you on Saturday!

One more thing: What is Suncoast Developers Guild?

Since opening their doors in the summer of 2018, Suncoast Developers Guild’s coding school has graduated over 100 students, and before that, they taught people to code in their previous incarnation as the Tampa Bay branch of The Iron Yard.

In another life, I was a developer evangelist who travelled across North America and I saw tech scenes from Palo Alto to Peoria. I can tell you that one of the signs of a healthy tech community in a small- to medium-sized city is a coding school that acts as a social/technical/gathering place. If your city had one, things were looking up for local techies. If not, it was a safe bet that the place was experiencing a brain drain.

Here in Tampa Bay, Suncoast Developers Guild fills that vital role, and it does so spectacularly. They’re a key part of the heart and soul of tech in the area, and it shows in their efforts, such as events like this.

Categories
Programming

Yup.

Categories
Humor Programming

Developer meme of the day

Photo: Chef (labeled “developers”) pouring olive oil from a comically oversized bottle (labeled “JavaScript”) onto a salad (labeled “website”).
Tap to view at full size.

Not pictured: A oil drum-sized barrel of balsamic vinegar labeled “React”, “Vue”, or “Angular”, and a forgotten can of anchovies labeled “jQuery”.

Thanks to Cameron Barrett for the find!