Posts

How-To: Substrings in Swift

Jun 28, 2016

Extracting a character or sub-string in the Swift programming language may seem less intuitive for programmers familiar with Java or C. One interesting difference is the CharacterView to access the length (count) of a string such as aString.characters.count. Swift String includes subscript notation by String.Index instead of an Integer, and the substring takes a Range or ClosedRange as argument. Let’s go over some code to see it in action.

updated for Swift 5

Swift 5: sub-string
import UIKit
let aString = "A classic string, \"Hello World!\""
// need to use String.Index
let fifthChar = aString[aString.index(aString.startIndex, offsetBy: 5)] // s
// substring deprecated
//let suffix = aString.substring(from: aString.index(aString.startIndex, offsetBy: 18))
let suffix = aString.firstIndex(of: "\"")!
let start = aString.index(aString.startIndex, offsetBy: 19)
var end = aString.index(aString.startIndex, offsetBy: 23)
// get a sub-string with a ClosedRange
let range = start...end
let substring = aString[range] // Hello
// replace by text matching is easy enough
let byeStr = aString.replacingOccurrences(of: substring, with: "Goodbye,").replacingOccurrences(of: "classic", with: "dire")
// replace by Range
end = aString.index(aString.startIndex, offsetBy: 24)
let bString = aString.replacingCharacters(in: Range(uncheckedBounds: (lower: start, upper: end)), with: "Hi,")
Continue reading...

Skyrim Special Edition Announced

Jun 13, 2016

Bethesda Softworks announced a remastered “Skyrim Special Edition” coming 2016 October 28th for PC, Xbox One, and PlayStation 4. The edition includes updated art, better snow and water shaders, improved reflections, and dynamic depth of field along with support for mods on the console game systems. Check out this YouTube video at 3:17. This will be a free update for PC players with “Legendary Edition” (or combined DLC packs). A 64-bit upgrade with more memory availability would be welcome on the PC, but would mean an update to SKSE for many mods.

Besides taking advantage of the popularity of TES V: Skyrim, this indicates to me that the next chapter in The Elder Scrolls may still be several years away. In the mean time, it will be interesting to see how the updated visuals compare with the work the mod community has accomplished. For Xbox One and PS4 players, it should be nice being able to install new quests, armors, or homes built by the mod community.

For TES VI, I’d be happy to visit Summerset Isles or Valenwood, but I’d like to see Elsweyr the most.

Update 12 August: As mentioned on Gamespot, Pete Hines has stated (on Twitter) about TES VI the studio “would make it eventually.” MMORPG interview with Pete Hines ends with a statement about Bethesda Game Studios’s interest in pursuing other projects. These follow up on comments made earlier this year by Todd Howard (IGN) and Pete Hines, the studio is pursuing at least two other projects before returning to TES. For now, fans will have to be content with modding Skyrim including the Special Edition.

Update 4 October: An announcement post on Bethesda.net confirms that Sony will allow mods on PS4, but only with existing assets found within the game. PS4 Pro will “render in native 4k.”


Draco Calculation Update: No Ads

May 26, 2016

Update to Draco Calculation v1.05 removes ads and improves hint suggestion quality. Still free.

DracoCalcIcon 200

Since iAd will be closing down soon, and I have no intentions on using an alternative ad provider, I removed ads. The primary reason I implemented iAd was for the experience and secondarily to help pay license fees. The AI improvements for suggestions address several cases playing to the tableau that might block future play. My goal for the AI is to play better than a beginner, and not nearly as good as an expert. Suggestions should be learning opportunities for beginners without becoming a crutch or giving away advanced strategy. There’s room for more improvement, but I believe the AI has reached my goal.

Thanks for playing.

Continue reading...

Prime Factors Problem 2: Largest Prime Factor

May 25, 2016

Like the previous problem, this challenge comes from ProjectEuler.net, “Problem 3”.

Find the largest prime factor of 600,851,475,143.

Now that’s a big number. Test your code with smaller values that you can calculate or look up. “Problem 3” states the largest prime factor of 13,195 is 29, a good test case. Finding primes can take time, so we’ll need to consider our method before tackling 600,851,475,143. There’s some fancy algorithms you could find online, but try it on your own. This makes for a decent software engineer interview question, or just for fun.

Give it a try before looking at my process or solution. Write a function to calculate the largest prime factor of a natural number.

Continue reading...

Prime Factors Problem 1: LCM

May 24, 2016

I’ve been looking over ProjectEuler.net and found a few fun problems involving factoring and prime numbers. This first one is easy for computer and math geeks, but should be do-able by anyone with a calculator and a recollection of factoring. I’ll go through it step-by-step.

Find the smallest number divisible by all integers between 1 and n, where n > 2.

This is based on “Problem 5” of Project Euler, but before you look at the page, first try attacking an easy case. When n is 3, it’s painfully easy. 1 x 2 x 3 = 6, so 6 is is it. Try an interesting value. Let n = 10.

What is the smallest number divisible by all integers between 1 and 10?

Go ahead and think about how you might solve this. (Hint: title of this post.)

Continue reading...