Chris Ching Course Part 4 (Challenges 1 and 2)

Right, here we go - time for the challenges! I'm going to take my time with these, look up the syntax in any previous blog entries - where necessary- and hope that they're not too hard! Right let's go!

Start time - 19:10

Challenge 1

Right I've listened to the first challenge instructions. As I mentioned before, there are a few ways of doing this - what I did last time worked well. I have to admit that I'm cheating a little, as I can pretty much see how this code plays out. But that's good - it means I'm remembering things and applying them!

// Challenge #1: The Lost Animal Challenge
//
// Instructions: 
// Given the two arrays below, write a function that takes a String as an input parameter and returns a Boolean value. The function should return true if the String input is in either array and it should return false if the String input is in neither array.
//
// Examples:
// Call your function and pass in the String "cat" as the input. Your function should return true
// Call your function and pass in the String "cow" as the input. Your function should return false

let array1 = ["dog", "cat", "bird", "pig"]
let array2 = ["turtle", "snake", "lizard", "shark"]


// Write your function below:

So the first thing to do before launching into the code is interpreting what the question is asking and what I must do.

Here's what I know for sure:


  • I must have a function created
  • It will take a string and return a bool
  • The string input in either array returns true


So that is all fairly straight forward.

func animalFinder(animalName: String) -> Bool {
    
    let combinedArray = array1 + array2
    
    for animal in combinedArray {
        if animalName == animal {
            return true
        }
    }
    return false

}

I made an error here - I put in an else clause and had return false in a bracket before. Not totally sure why it needs to go in the next one down but I had to check with my previous attempt at this for the syntax and accidentally saw the answer! Last time I did this:

func stringFinder(_ animal: String) -> Bool {
    
    if array1.contains(animal) {
        return true
    } else if array2.contains(animal) {
        return true
    } else {
        return false
    }
    
}

Now I do like this original solution - I learned the 'contains' method by trying it out and it is pretty elegant. Still the original one cuts out a lot by combining the arrays. I could have done this...

func animalFinder2(animalName: String) -> Bool {
    
    if combinedArray.contains(animalName) {
    return true
    } else {
    return false
    }


}

So this works when I've moved 'combinedArray' outside of the scope, but I could have repeated the concatenating. Not sure if I actually need the else clause either!

So some good stuff here. Next!

Challenge 2

Challenge #2: The Caterpillar Challenge
//
// Instructions:
// Complete the class definition so that you get the expected output in the console (specified at the bottom of this playground).
//
// Each time the "add" function is called, keep track of the String input that is passed in.
//
// When the "go" function is called, use the print command to output all of the String data (added through calling the "add" function) on a single line.
//
// Example:
// See under the class definition for an example of expected output for the given commands.

// --- Your code goes below this line ---
class StringCaterpillar {
    
    var body = [String]()
    
    func add(_ text:String) {
        // Note:
        // You must use the body array declared above to store the pieces.
        // It may be unnecessary to use an array for this but my intention is for you to practice using arrays.
    }
    
    func go() {
        
    }
    
}
// --- Your code goes above this line ---

// --- Don't edit or add anything below this line ---
let myCaterpillar = StringCaterpillar()

myCaterpillar.add("h")
myCaterpillar.add("e")
myCaterpillar.add("l")
myCaterpillar.add("l")
myCaterpillar.add("o")

myCaterpillar.go()

// Expected Output:
// Expected output in the console after the above statements: "hello"

// For Bonus Credit: 
// Create a custom initializer so that you can declare a caterpillar object like this
//
// let myCaterpillar = StringCaterpillar("hi")
// 
// The input for this init should be stored.
//
// After changing the "myCaterpillar" constant above to use your new custom initializer, the new expected output should be: "hihello"

So there is a LOT to this one! First let's make sense of it...I was tempted to head straight in but that would have been silly!

Here is what we know...

  • I need to add something to the add function
  • I will be adding/creating/doing something with an array!
  • When the add function is called, the character that is added is stored inside the body
  • When go is called,  whatever string is now stored will be the output

Right so first of all let's focus on the add function. 

OK I think we could be done...

class StringCaterpillar {
    
    var body = [String]()
    
    func add(_ text:String) {
        body += [text]
      
    
    func go() {
        print(body)
        
    }
    
}

Xcode not running properly - AGAIN.... Is it that easy? Before I check, let's try the custom init...

convenience init(_: String) {
        self.init()
    }

Yes that seems to work!

OK, well I didn't quite get it but a good try! In the solution, Chris used the 'joined' method to put together the individual letters. 

Now here is what we have:

class StringCaterpillar {
    
    var body = [String]()
    
    init(_ startString: String) {
        body += [startString]
    }
    
    func add(_ text:String) {
        body += [text]
        
    }
    
    func go() {
        print(body.joined())
        
    }
     
}

A couple of points - I didn't need convenience init as there is only one! I've made it so you don't need to have a parameter to add in what you want to start with ("hi" from the example). I've learned the 'joined' method - that could be useful. So yes, although I didn't get it all, some useful stuff!

I also used += between the collection name and the item adding to it, rather than append. Absolutely fine!

I'm going to stop there!

Time - 19:59 (49 minutes total)

That's plenty for now. Some good stuff to have a look at in practice - lots of useful opportunities to apply some skills. This task was A LOT easier than when I looked it at it just a few days ago. So definitely worth doing! Next time it will be challenge 3, which looks like a real bugger!

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)