Draco Torre

David G Shrock

Swift Code Exercises

I’ve updated some of my programming tutorial exercises with Swift code samples. See the original exercise for complete problem description. Swift 2.1 code tested in Xcode 7.2 Playground.

The anagram problem

A string is an anagram of another string if re-arranging the letters results in the other string. “Astronomer” and “moon-starer!” are anagrams.

In the original problem we stripped all punctuation since symbols and spaces may be ignored. Let’s say we wanted to count “Ray.adverb” as a single word (for whatever reason) including the period, but “ray-adverb” should still ignore the hyphen as normal. In the code below we strip by word boundaries.

Swift 2.1: are two strings anagrams
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Cocoa

var str1 = "Astronomer!"
var str2 = " Moon starer?"
var str3 = "ray-adverb"
var str4 = "barry-dave."
var str5 = "Ray.Adverb"
var str6 = "Tom Marvolo-Riddle"
var str7 = "I am Lord Voldemort!"

func areAnagrams(phrase1: String, phrase2: String) -> Bool
{
    if phrase1.isEmpty { return false }
    if phrase2.isEmpty { return false }

    //var trimmedPhrase1 = phrase1.stringByReplacingOccurrencesOfString(" ", withString: "")
    //var trimmedPrhase2 = phrase2.stringByReplacingOccurrencesOfString(" ", withString: "")


    var trimmedPhrase1 = stripWordBoundaries(phrase1)
    var trimmedPrhase2 = stripWordBoundaries(phrase2)

    trimmedPhrase1 = trimmedPhrase1.lowercaseString
    trimmedPrhase2 = trimmedPrhase2.lowercaseString

    if trimmedPrhase2 == trimmedPhrase1 {
        return false
    }
    if trimmedPrhase2.characters.count != trimmedPhrase1.characters.count {
        return false
    }
    let chars1 = trimmedPhrase1.characters.sort()
    let chars2 = trimmedPrhase2.characters.sort()

    if chars1 == chars2 {
        return true
    }
    return false
}

// let's treat "Ray.adverb" as a word so period would not be stripped, but "Adverb-Ray?" would become AdverbRay
func stripWordBoundaries(string: String) -> String
{
    var words : [String] = []
    string.enumerateSubstringsInRange(string.characters.indices,
        options: .ByWords) {
            (substring, _, _, _) -> () in
            words.append(substring!)
    }
    return words.joinWithSeparator("")
}

func anagramTest(phrase1 ph1: String, phrase2 ph2: String)
{
    if areAnagrams(ph1, phrase2: ph2) {
        print("'\(ph1)' and '\(ph2)' are anagrams")
    }
    else {
        print("'\(ph1)' and '\(ph2)' are NOT anagrams")
    }
}

anagramTest(phrase1: str1, phrase2: str2)
anagramTest(phrase1: str3, phrase2: str4)
anagramTest(phrase1: str5, phrase2: str4)
anagramTest(phrase1: str6, phrase2: str7)

Columbia Gorge Photos

For Winter Solstice, I took a drive up Columbia River Gorge on the Oregon side. The first photo is the twisty turns on old Hwy 30 seen from Rowena Crest.

Rowenaturn

Starvation Creek Falls has its own exit off I-84 eastbound only. A very short walk to see the falls.

Starvfalls

Multnomah Falls is Oregon’s tallest, and almost always a busy attraction. The hike to the top is only a mile long with 11 switchbacks.

Multnomahbr

Multnomahlow

MultnomahBW

Multnomahover

Skyrim Gear 4: Heavy Armor

SkyrimBladesDragon3

For Skyrim, I previously shared some of my favorite modded clothing for mages, Akiviri-related gear, and light armor mods. This post covers some nice heavy-armor mods.

difficulty levels

I usually play mages and stealthy rogues leaving heavy armor choices for followers. Recently, I gave the paladin style a try with a warrior, Regulus, adventuring as an agent for Mara. The experience has been a reminder on how unbalanced the difficulty settings are in TES games. Choices in difficulty only adjust damage given by the player character (PC) and damage taken by the PC.

modifier for damage given by PC – received by PC (eldescrolls.wikia):

  • adept: 1 – 1
  • expert: 3/4x – 1.5x
  • master: 1/2x – 2x
  • legendary: 1/4x – 3x

Stealth and magic (mostly) remain the same. Followers always fight 1-to-1 with NPCs, so giving the follower the best armor and weapon is a good strategy.

Some frustrating moments, but for the most part, Regulus manages okay on master difficulty. He must remember to fight smarter: take advantage of terrain and traps; hit hard with ranged weapons; push his follower in front and keep her healed. What hurts most, though, he sometimes feels humiliated watching his follower shrug off multiple hits from large two-handed weapons, but he bleeds profusely when rapidly stabbed by butter knives. Two-handed weapons are very deadly with 2x or 3x damage! On master, might as well ditch the armor and run faster.

I enjoy the game best on expert, and add challenge by limiting schools of magic or making different choices based on role-playing. Regulus fights fair by announcing his intentions and respecting occupied forts. He admonishes necromancy and hunts vampires. He chooses not to use dark soul gems believing the use restricts souls from reaching their destination. Summoning daedra is for the weak.

SkyrimRegulus1

Regulus’s face uses “Better Males” “YoungerFacesMergedWithMenByGeonix”, “Beards” by Hvergelmir texture update, “The Eyes of Beauty” by Gabriel Mailhot, and Superior Lore-Freindly Hair by skyrimmaguas. His armor of choice: “Dragon Carved Armor Set” by hideto84.

Skyrim Gear 3: Light Armor

SkyrimEzekielBSA2

Previously, I shared my favorite Skyrim clothing mods for mages and Akaviri gear. Below are nice mods fit for rangers, rogues, spellswords, and some suited for those playing dark characters. Armor that improves by feeding on blood sounds cool, doesn’t it?

on “lore-friendly” and my preference

When “realistic” armor is mentioned in regard to Skyrim, I assume this refers to protection in combat and from the elements within context of The Elder Scrolls. Removing dragons and magic would leave Skryim as a civil war game with bandit and item-collecting quests. TES is a fantasy series which includes unrealistic fantasy gear such as Golden Saint, Dark Seducer, and some unwieldy oversized weapons. Daedric armors are imbued with dark powers, and are highly decorative to cause fear. Our history is full of armor decorations to confuse the enemy or inspire confidence among the citizens. Practical is nice, but looking cool has its place.

No matter the armor, direct hits hurt. Armor may be simple padded hides or layered with chain and plates. Real chainmale would hurt flesh without padding, but decorative chain can make an ordinary garment look nice. Coverage is more about flak and incidental contact. The warrior’s skill at wearing a particular armor matters, and more important is the skillset at avoiding combat. Speech, stealth, distraction, and fleeing help one stay alive better than any particular outfit. And there are enchantments to consider in this fantasy world. Coverage may be more important for heavy armors, and light armors need to allow for more movement or stealth.

How decorative, revealing, or practical outfit choice is comes down to my character’s taste. My retired mage, Reisha, always prefered normal clothing, the mourner’s dress and “Blades Hakama” her favorites. She blended in better than wearing mage robes or Dark Brotherhood outfits around town. Another character likes intimidating (decorative) armors.

I give preference to original work. Styles should be similar to or inspired by outfits found throughout Tamriel, which is very diverse in style and cultural background. Historical recreation or realistic gear is great, but I prefer a touch of fantasy suitable for Skyrim.

SkyrimFlameDecap

Skyrim Gear 2: Akavir

SkyrimBladesDragon1

The Akavir (elderscolls.wikia) are a lost people of Tamriel. How much of their culture, skills, and weapons might remain is up to the player. There’s a number of Akaviri-related mods to choose from. Previous TES chapters included limited, and generally more powerful, swords such as katanas and dai-katanas. Considering The Blades have discovered ancient artifacts, and Sky Haven Temple, former crafting techniques have been uncovered allowing Esbern and Delphine to maintain gear for new recruits. Why not add more Akaviri style in Skyrim?

SkyrimHakamaSoulC