Swift Coding Puzzles #2 – Balanced Brackets

This is part of a series of coding puzzles inspired by the #CodingPuzzle tag/puzzles originally curated and shared by Ali Spittel. This is my approach to solving those puzzles in Swift. If you have any comments or answers you want to share, post them below! I’d love to read them.


Puzzle #2

Given strings of brackets, determine whether each sequence of brackets is balanced.

When would you come across a situation like this? Well, if you ever write a source editor, you might come across something like this. As most programming languages use brackets to denote context, functions, and other divisions, we’ve probably all come across a situation where we forgot to close something off.

For the sake of this exercise, let’s define what brackets mean. In this case, it’ll be three sets of characters: ( ) , [ ] , and { } . The first character we’ll call the “open bracket” and the latter the “close bracket”.

So, what makes a balanced sequence?

  1. A matched pair of brackets.
  2. Any nested brackets must also be matched pairs.

An example of some balanced sequences include: () , [(){}] , {([()])}

An example of some imbalanced sequences include: (() , [([)]] , {([)]}

If any of those become unpaired, then the entire sequence is considered imbalanced. If you’ve ever created a class in Swift and forgot to close a brace when defining functions, you’ll know what that is.

By the above definitions, we can thus determine that in a balanced sequence:

  1. It has an even number of brackets
  2. An open bracket will never meet a close bracket of a different type
  3. A closed bracket will never appear before its corresponding open bracket

That said, here’s how I approached the problem.

First, I defined what each of the brackets were.

Using a dictionary, I’ll be able to easily look up the close bracket for a given open bracket.

Next, let’s look at my function to solve this.

First, if the string has an odd number of characters, we automatically fail it. This is in line with point number 1 above.

I then declare a variable to temporarily hold brackets. This stack will be used in the following loop.

Next, we go through each character of the string. Our first check is whether the character is a close bracket or not. If it is, and our stack is empty, it means we’re encountering a close bracket before its open bracket counterpart. We thus return false. If the stack isn’t empty, we check if the last character on the stack is an open counterpart of this close bracket. If it is, we remove the open bracket from the stack. Otherwise, we return false.

If it’s not a close bracket, we’re likely looking at an open bracket. Those automatically get added to the stack.

This loop functions for all characters in the string. Afterwards, our return is whether or not the stack is empty. If it’s not empty, it meant we had more open brackets than closed brackets, and while the earlier catches may not have produced a return, it technically is an unbalanced bracket set.

Here are a few sample calls to this function and their outputs.

I had initially hoped to tackle this with recursion, but I found the above solution to be easier to follow and much easier to manage.

Did you have a different solution? Is there something I could improve with my Swift code? I’d love to hear from you!

Paper by FiftyThree

Lauren Goode at Wired: FiftyThree, Maker of Popular Paper and Paste Apps, Gets Acquired:

BACK IN 2012, a Seattle-based startup named FiftyThree launched a drawing app designed exclusively for iPad, with a name that sounded like it was designed specifically for an Apple crowd: Paper. Despite its simplicity and also because of it, Apple crowned it the iPad App of the Year. Tech writers described it as “the next great iPad app”, “a superbly designed sketching app,” and “a fresh canvas ready and waiting for your ideas, inspiration, and art.” FiftyThree later expanded to include an iPhone app, an optional subscription called Paper Pro, and Paste, a collaboration app.

Today FiftyThree announced its apps and team have been acquired by WeTransfer, a cloud-based file transfer company with headquarters in Amsterdam and Los Angeles.

I’ve been a Paper user off and on ever since they launched in 2012. It’s been my go-to app for a lot of drawing projects, is one of our son’s favorite apps on his iPad, and is popular with my wife, who uses it for a lot of wonderful sketches. Like with any acquisition, there’s a little hesitation as to whether or not it will be a good thing for an existing company and its products. But I’m hoping we’ll see Paper stick around for years to come.

Swift Coding Puzzles #1 – Simple Pig Latin

I’ve been following Ali Spittel‘s #CodingPuzzle challenges. While I’ve read most of them, I haven’t actually done them.  But each of them would be a fun little exercise. So, I figured I’d go back and tackle each of them in Swift.

Before I start, a note: There are always multiple ways to tackle coding challenges. This is just my approach to it. If there are better/more optimal ways of doing it, please comment and share your solutions! I’d love to see them. This is as much a way for me to share what I’ve done as it is for me to learn from how others see problems and want to solve them.

That said, here is the first puzzle:

Puzzle #1 – Simple Pig Latin

“Move the first letter of each word to the end of it, then add “ay” to the end of the word. Leave punctuation marks untouched.”

If this was just taking a sentence without punctuation, this would be a lot easier. You could split the array, modify the words, and rebuild the sentence. But keeping punctuation untouched adds a bit of a challenge.

Being me, I also wanted to make sure that this would handle if someone inadvertently misses a space after some punctuation. (No, this likely doesn’t handle apostrophes properly, so contractions will probably not be correct; but I’m no native Pig Latin speaker to know.) So I may have put a little too much thought into this.

First, I wanted to make a function that modifies a word into its proper Pig Latin form.

With this function, I can now pass in parts of a sentence as needed. With that done, now I just need to get the words to pass in.

I could have split the array based on space, but that wouldn’t necessarily preserve punctuation. So I instead opted to go through the string character-by-character and parsing words that are found.

I go through each character and, if it’s a letter, I add it to a temporary word. If I hit a non-letter (punctuation or whitespace) or the end of the string, I take that temporary word, modify it for authentic Pig Latin, and append it to the return string.

With all of that, here are some sample outputs:

So that’s how you can do Pig Latin in Swift. No, it’s not perfect. There are other cases that aren’t dealt with, such as how to handle words starting with vowels or ‘qu’. But I’m no linguist.

Managing Time for Side Projects

I’m quickly coming up on my two year anniversary at my day job. I’ve learned quite a lot in my professional experience as an iOS developer. But one unfortunate side effect of doing something that I love during the day: it’s hard to find the time and motivation needed to work on my app projects, even when the desire is there.

Max’s tweet was originally about trying to work too much. But I felt inspired to address some of the issues as to why I feel I don’t work enough, whether or not that’s true.

The Problem(s)

Here are a few of the issues I’ve found myself facing:

  • Too Many Ideas – My mind seems to come up with new side projects at least once a month. Of course, there’s no way for me to complete (or even start) many of these. But this has been happening for a while. I still have a file in Evernote with about 6 years of app and service ideas. I want to work on one, but I sometimes get distracted with my list and lose focus on what is achievable.
  • Wrong Project Scope – For the ideas that I am able to actually work on, I find myself dreaming up massive plans. ‘Maybe I’ll completely rewrite X!‘ ‘I could do Y like this, but if I do it like this, it’ll be future proof and everything!‘ What was going to be a simple project that I could enjoy working on becomes a large project that might as well take my full time.
  • Too Little Time – I know what you’re going to say. “Isn’t that the whole point of this post?” Well, yes. But it’s worth mentioning. It’s hard to work on side projects when you spend time playing games or watching TV.  If you’ve set aside time for yourself, you are in control of what happens with that time. And that doesn’t always mean those desired projects get a share of it.Nor does that take into account responsibilities you might have if you’re a husband or father and the time you need to spend with your family.

The Solutions(?)

Before I had my current job, I did a lot of my development work on the side. Sometimes I even found some time during the lulls of the day to get a little bit of work done. But now, I have to try other things.

A few things I’ve ultimately realized:

  • I have to make up my mind. Some work I’ve done on my projects lately has stalled because I can’t make up my mind on what to do. In one example, I’m considering using Realm as a data store. I use Realm at work. It’s something I’m fairly comfortable with. But then I think that I could use this time to try working with Core Data again. Or maybe I’ll just stick to using SQLite directly but use a new library. Soon, I’m stuck in this cycle of each direction being one to consider, but I end up doing nothing but considering choices and don’t end up going any one way. So I’ve started telling myself, as my own boss on these projects, that I have to make a choice, stick with it, and move on.
  • I don’t have to be perfect. Similar to the last point, these projects exist for me and me alone. While I am particular about the quality of my own work, I am not going to be pushing these through any kind of code review. So I can let some things slide. If anything, it gives me things I can address with future releases. But ultimately, I don’t need to perfect something before I release it. So long as it works, I can be content.
  • I have to know what I’m doing. I’ve been a fan of Asana for several years. While it was great for my day jobs, I started using it over a year ago for my side projects.  It doesn’t need to be as complicated as my day job (with Epics, Work Requests, and Iteration planning), but I do need to have some idea for what I want my deliverables to be. Setting that for my various projects, including reasonable feature sets for various stages of release, is helping me to make progress.
  • I need a long term plan. I decided earlier this year to go overboard and plan out my 2018 project goals. I’ve got quarterly release goals (mostly Alpha and Beta releases), planned App Store releases, and a general idea of what I would like to accomplish this year. All of it is set up to be achievable but not overwhelming. So no, I’m not launching 6 new apps this year. Yes, I’m hoping to have at least 1 app update and 1 new app on the App Store by December.1

Overall, I’m not getting as much done with my side app projects as I’d like. But as I am a developer during the day, that takes a lot of my developer energy. And lastly, I have to balance my interest in side projects with my family, which has my ultimate priority. Thankfully, it’s kept me from being a workaholic.2

In my case, I just have to accept the reality: I can’t do what I once could. But that’s okay.3


  1. I said it was achievable. I didn’t say it was impressive. 

  2. As much as I love to work. 

  3. I started this blog post shortly after Max’s initial tweet, and that was 4 days ago. Shows you how my time is allocated. 

Clips, iOS 10.3, and the future of 32-bit apps

In March, alongside a few hardware announcements, Apple also unveiled a new media sharing app called Clips. Clips was released today. Reviews seem mixed. But I’m not here to talk to you about Clips.

iOS 10.3 was released to a vast majority of iOS devices on March 27. It was a huge behind-the-scenes update that migrated iOS devices to Apple’s new file system.  But I’m not here to talk to you about iOS 10.3.

What I am here to talk to you about is 32-bit app support. It’s definitely coming to an end. And this might affect some apps you like using. For me, some of my favorite games fall into this category. Want to see which of your apps won’t work on future iOS versions? Head to Settings > General > About > Applications to see the list.

You might’ve gotten a glimpse of this even without going to the Settings app. One other change in iOS 10.3 noted by people before and after release: Apps not compiled for 64-bit present an alert to the user upon launch. This alert says that the app in question will not work with future versions of iOS.

Now, why did I mention Clips earlier? Because Clips has some notable requirements. Sure, it requires iOS 10.3. That’s the latest OS release, and its no surprise that Apple will target the latest release with its latest apps. But the hardware requirements don’t include all devices running iOS 10.3. Specifically, Clips only works on 64-bit devices. Don’t believe me? Check the bottom of the Clips site and try to find a 32-bit device in the list of supported devices. You won’t find any.

Another big tip: iOS 10.3.2 is available for developers to test. It is only available for 64-bit devices.

The writing is clearly on the wall: 32-bit apps will soon be unsupported in iOS. But while some think iOS 11 will be the big cutoff, it may be sooner than most people realize. If you see your favorite apps in that list in your Settings app, consider contacting the developers of those applications. Urge them to update their apps.

I know I don’t want to lose my favorite games.

 

On Apple’s new new iPad

This week, Apple announced a few new or updated products via press release. One of those was a brand new iPad model. Known only as iPad, it’s an update to the 9.7-inch iPad model but staying below the new Pro line in terms of functionality.

There’s been some confusion as to the iPad’s name, as well as Apple’s approach to its design and price. But if we take a moment to analyze its name, I think we can put the rest of the details into place.

Apple is calling this simply iPad. Specifically, it shows up as iPad (5th Generation)1 on Apple’s site, which is leading to issues on Wikipedia, as the iPad Air was unofficially known as the 5th generation iPad. Why is Apple calling it the 5th generation iPad?

Ultimately, I think we can consider the iPad itself having ended as a product line with the 4th generation iPad. When a new 9.7-inch iPad model debuted in 2013, it came with a new name: the iPad Air. This began a new line of Air model iPads. These models kept pushing the envelope forward in terms of weight, thinness, and power. I still think the iPad Air 2 is a very capable iPad, even if Apple no longer advertises it on their website.

Apple's two iPad Pro models

Apple’s two iPad Pro models

That said, the iPad Pro line is yet another new line of iPads. The fact that the iPad Pro 9.7-inch model happens to have identical dimensions to the iPad Air 2 just shows the evolution of that sleek, powerful design.

With the Pro moniker, Apple is clearly targeting the iPad Pro towards those wanting to do more with their iPad. Looking to do serious work? Here’s an iPad that also supports a convenient Smart Keyboard and state-of-the-art Apple Pencil.

The iPad (4th Generation), the predecessor to the iPad (5th Generation)

The iPad (4th Generation), the predecessor to the iPad (5th Generation)

What about the consumer? That’s where the iPad (5th Generation) comes in. This iPad isn’t a new Air model. This isn’t related to the Pro models at all. Instead, this is the follow-up to the iPad 4. As such, it’s able to get away with a few things.

First, we can revert to the design of the iPad Air. Sure, it means the iPad is thicker than the Air 2. But it still is a great size and, more importantly, is still close to 1 pound in weight. That makes this iPad one you can hold for a longer period of time than the iPad 4 (or older).

This older design also shows up when we look at the various technologies used in the iPad (5th Generation). The display is still of Retina quality, but it lacks the anti-reflective material that’s built into the latest iPads. We also see Apple resorting to the older antenna bands for the LTE models.

However, despite having some older technologies in this new model, they can also throw in some very capable internals. The A9 chip, while technically over a year old, is still more powerful than what’s in any other consumer-targeted iPad model. This puts it on par with the iPhone SE, another iOS device considered to be for those looking at a more price-friendly option.

This leads me to the second point: price. Because Apple is using a mix of older technologies and designs along with semi-newer chipsets (which, as a volume purchase, likely cost less the more Apple integrates them into products), they can also release this iPad at an amazing price: $329 for 32GB. That’s the lowest any new 9.7-inch iPad model has ever sold for.

The lower price leads me to two conclusions:

  1. Apple is going to create two tiers of iPads like they’ve done for years with the MacBook line. The average consumer will go for a powerful-but-not-too-powerful MacBook, while professionals will grab the MacBook Pro. Similarly, the iPad line will be what most consumers will need. And at a new lower price point, there’s very little reason for someone to get one of these iPads, especially as an upgrade from an older model.
  2. The iPad Mini will never again have the top-of-the-line specs. Right now, the only iPad Mini left in Apple’s lineup is the iPad Mini 4. The only configuration is a 128GB model for $399. I still think the iPad Mini line has a place with certain audiences, but at best it will share the same specs as the new consumer iPad line. At worst, it will disappear completely.

Overall, this new iPad will be a very positive thing for Apple. The lower price should bring in new iPad owners, as well as move those with anything older than an iPad Air 2 to upgrade.


  1. For instance, in the list of supported devices for the new Clips app

[Updated] Watch faces

One thing I do like about the Apple Watch is the ability to easily change your watch face. Very quickly, the Watch can go from being a simple time piece to something very personal. As I’ve now spent 48 hours with a Watch, I wanted to share how I’ve set up my Watch faces. I’d also love to hear comments on how you set up your faces.

Complications

Before I begin, note that the complications are likely to change as I try more apps for Apple Watch. I know I’ll drop some of these eventually. But for now, these are the ones I’m using:

  • Battery – Temporary while I learn how long my Watch will last during the day
  • Activity – The main purpose for buying the Watch was to help me be more active. So having this visible at all times is a must.
  • Date
  • Weather – Makes it real quick to see how things are outside
  • Calendar – Being able to see when my next meeting or event occurs at a glance is great.

Now that I’ve listed all of the complications, here are my Watch faces.

Utility

Apple Watch Utility face

This Watch face is my main one. I like the overall appearance of it, as it feels like the kind of watch face I’d want even on a mechanical watch.  I have this one when I’m not working (and sometimes when I am). I have the Battery complication in the top left, Activity in the top right, Date with the day of the week inside, and the weather along the bottom. When I drop the Battery one, I’ll move the weather to that corner (even though it’ll only show the temperature) and replace it with the calendar at the bottom.

Modular

Apple Watch modular face

This is my current Watch face during the work day. The center complication is the calendar because of the amount of data it can show. It’s a bit odd to see the time off-center, but I will let that slide given the amount of data all of the complications can show.

Color

Apple Watch color face

I thought I would use this one more, but I don’t use it as much anymore. I do like how it’ll show color more than the Utility face. When I go out and want a little fun with the Watch, I’ll use this and tweak the color.

Astronomy

I do miss complications when using this Watch face, but this is a fun one to sometimes switch to at night.

Timelapse / Photo Album

Apple Watch photo album face

These are my evening Watch faces for when I don’t need to see data but want to see something personal, especially with the Photo Album face. I look forward to creating some Live Photos next week and making Watch faces out of them.

There are other faces I don’t use. I don’t have anything to say on them really. The ones I just listed are my favorites thus far. But I’ve not had my Watch for months like some people, so I’m sure this will change the more I use it.

Update (Dec 1)

Having had some more time with my Watch, I’ve cut down on some of the faces that I was using before and added a few new ones.

Utility, as mentioned above, is still the same, but it is now my main Watch face.

Modular, while still the same, is hardly used anymore. It’s nice, but I find it doesn’t provide me as much useful information while I’m working at my desk. If I was working elsewhere and not always near my computer’s calendar, then maybe I would find it more useful.

I’ve made some new additions to my faces.

Simple

Apple Watch simple face

As the name implies, this is a simple face. I use this when I’m out or at meetings. I think it removes enough distractions to allow me to focus on where I’m at without feeling the need to always glance at my wrist.

Modular (2)

Apple Watch sleep modular face sleep++

This is another Modular set up. However, I use this one specifically for when I sleep. The center complication is for the app Sleep++ by David Smith. When I’m ready for bed, I’ll switch to this complication, set my alarm, go into Airplane mode, and then tap on the center complication to take me to the Sleep++ app to start tracking my sleep. The other complications, including the weather and sunrise/sunset, seem appropriate for this watch face.

Limiting Distractions: Notifications

Sometimes, especially if you’re like me, you’ll be on a roll when it comes to work, perhaps figuring out a program you’re working on, when you suddenly get interrupted. And just like that, your mental focus is gone. What were you doing? You’ll figure that out after a few minutes.

Notifications can sometimes be that interruption. Sure, it may be cool to have your iPhone docked next to your computer. But if the constant comments on Facebook are distracting you, is that a good thing to keep in sight?

The better question: are you in control of your device, or is it in control of you?

While I’m not one to disable all of my notifications1, as some have done, earlier this week I started disabling notifications that I had set up for some time. In particular, anything with a social element has been muted. My iPhone and iPad no longer have permission to display notifications, play sounds, or show a badge icon. In short, if I want to know what’s going on there, I’ll find out when I manually check those platforms.

Even if you’re a heavy Twitter user, is there any reason why you need to be disturbed with every mention? Is something so urgent that a social media message should disrupt your life? I’m doing my best to say no to those questions. My iOS devices are useful. To me. They respond to what I want to use them for. And that’s how this relationship will stay.


  1. Yet 

iOS today view widgets and margins

One of the toughest things I’ve had to deal with while working on an app has been the today view widget. The widget I’ve been working on is very simple: 2 buttons and a label. Yet, there were a couple of issues I kept running into.

  1. When loading the widget, my buttons will appear and layout. With a button tap, the two buttons change size to what is constrained in the Storyboard. This only happens when the widget is loaded and with the first tap. Subsequent taps don’t affect it again.
  2. The widget, as much as I tried, would not appear as it did in the storyboard. Things weren’t centered properly.

The first item I still haven’t solved. The issue that ended up solving the second item is what I thought was affecting the button layout. Essentially, here is what I had: left button is x points from the left edge of the view, is y points from the right button, and has a width equal to the right button. The right button is the same except it has a constraint to the right edge of the view instead of the left. I had hoped that this would lead to a dynamic button size that would change on various devices and widths. Unfortunately, the small issue with the resizing buttons kept bothering me. If someone can figure this out, let me know.

The second issue was actually fairly easy to figure out. If you look at any of your current widgets, you probably notice two different layouts: ones that are full width, and ones that start indented.

I thought that Apple wanted all widgets indented, but there are even some of Apple’s own widgets that are full width. While it wouldn’t solve the first issue I was having (and gave up on), it did impact why the storyboard, set to the width of the device, wasn’t looking like what my widget was showing.

To solve this, make use of the NCWidgetProviding protocol. There is a method called widgetMarginInsetsForProposedMarginInsets  that you can use to either accept the default margin or set it to a custom value.

By removing the insets, I suddenly had a widget that not only was centered but also was taking up the space in the view as it was in the Storyboard.

If you want to see what I was working on, check out Countr on the App Store.