My Bronze Challenge Solution (Myrhillion)


#1

"“Fix this bug by changing the terrorizeTown() function on the Zombie type to only decrement the town’s population if its population is greater than 0. Also, make sure that the town’s population is set to 0 if the amount to decrement is greater than the current population.”

Excerpt From: Mathias, Matthew. “Swift Programming: The Big Nerd Ranch Guide.” iBooks. "

I took this to mean the >0 check is in Zombie, but that the town’s population check to 0, is mean for the town class itself. Since there’s still the potential for population <10 to lower the population below zero with the zombie’s terrorizeTown function call.

import Foundation

class Zombie: Monster {
var walksWithLimp = true

final override func terrorizeTown() {
    if let enoughPeople = town?.population {
        if enoughPeople > 0 {
            town?.changePopulation(-10)
        }
    }
    super.terrorizeTown()
}

func changeName(name: String, walksWithLimp: Bool){
    self.name = name
    self.walksWithLimp = walksWithLimp
}
}

then Town.swift

import Foundation

struct Town {
var population = 5422
var numberOfStoplights = 4

func printTownDescription() {
    print("Population: \(population); number of stoplights: \(numberOfStoplights)")
}

mutating func changePopulation(amount: Int) {
    if ((population + amount) < 0) {
        population = 0
    } else {
        population += amount
    }
}
}

I originally had (population - amount), but realized with negative amounts, needed to leave it as + to have it work right. Took me about ten minutes to realize that was my bug too. sigh


#2

I like how your solution has the population change logic in the Town class! I chose to avoid the let assignment within the overridden terrorizeTown function, however:

final override func terrorizeTown() {
    if town?.population > 0 {
        town?.changePopulation(-10)
    }

    super.terrorizeTown()
}

There’s no reason to create the additional variable (which, depending on the compiler, might make the code slightly slower since it would create the enoughPeople variable on the stack).


#3

thanks for the feedback, need to get into some of the swiftier habits yet.


#4

But doesn’t this code allow you to set a negative population if the population was only 3 people?


#5

Hi!
I solved the problem with a single if statement that contained an else which would set the population to zero if my criteria wasn’t full filled. I use 9 instead of 0 to avoid printing a negative population.

import Foundation

class Zombie: Monster {
var walksWithLimp = true

final override func terrorizeTown() {
    if town?.population > 9 {
    town?.changePopulation(-10)
    } else {
        town?.population = 0
    }
    super.terrorizeTown()
}
func changeName(name: String, walksWithLimp: Bool) {
    self.name = name
    self.walksWithLimp = walksWithLimp
}

}


#6

Hi, I just updated the changePopulation function

  mutating func changePopulation(amount: Int) {
    population += amount
    if population < 0 {
        population = 0
    }
}

#7

I used a different approach:

final override func terrorizeTown() {
if town?.population > 0 {
(town?.population > 9) ? (town?.changePopulation(-10)) : (town?.population = 0)
}
super.terrorizeTown()
}


#8

I see a lot of you guys used if town?.population > 0 but when I try to use it I get the error:

Binary operator ‘>’ cannot be applied to operands of type ‘Int?’ and ‘Int’

What am I missing here?


#9

The language’s syntax is still evolving.

if let town = town, town.population > 0  {
   // ...
}

if let population = town?.population, population > 0  {
   // ...
}

Have a read of the Optional Chaining section in the manual.


#10

post your code if you didn’t figure it out, but my version at the top using optional binding should still work.

Found this, search on “optional binding” on the page at:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html