Nick Walter Swift 5 Course - Part 3 (lectures 23 to 24)

Getting onto a good roll now! This is what I need to maintain - not every day, but finding and making time. Of course it's easier during the Eid break - the real challenge will be next week, getting back to 'normal' hours and being at work etc. So a bit of time now and possibly some more later on!

Start Time - 11:40



This is the last part of the Intermediate section before a challenge! Then it will be the 'Advanced' part.
Switches I am all too familiar with - similar to if statements with a few differences in syntax. Also, it's good to use switches for various numerical options.

Here is Nick's example.



It's good for numerical values because the ... is an easier range to use than the greater than/less than symbols.

The default keyword must be used if the conditions are NOT exhaustive. This is going to be the case for numbers!

It works really well with enums - I think I made this point last time when Nick was using a function to find out the type of enum.

func directionFinder(whichWay: Compass) {
    
    switch whichWay {
    case .east:
        print("Turn right!")
    case .north:
        print("Turn up!")
    case .west:
        print("Turn left!")
    case .south:
        print("Turn down!")
    }
    

}

Challenge - create an enum then use a switch statement - 

enum F1Team {
    case ferrari, mercedes, mclaren, redBull, williams
}

func teamFinder(teamName: F1Team) -> String {
    
    var returnString = ""
    
    switch teamName {
    case .ferrari:
        returnString = "Ferrari"
    case .mercedes:
        returnString = "Mercedes"
    case .mclaren:
        returnString = "McLaren"
    case .redBull:
        returnString = "Red Bull"
    case .williams:
        returnString = "Williams"
    }
    
    return "Team name is \(returnString)"
}

teamFinder(teamName: .ferrari)

OK, so actually it needed to be within an already created Class! So I've done so with the dog fur. Easy peasy. 

class Dog {
    
    var name = ""
    var age = 0
    var colour = ""
    var furColour: FurColour = .black
    
    func bark() {
        print("Woof!")
    }
    
    func barkDetails() {
        print("My name is \(name) and I am \(age) years old")
    }
    
    func furChecker() {
        let furString = "The dog has"
        
        switch furColour {
            
        case .black:
            print("\(furString) black fur!")
        case .brown:
            print("\(furString) brown fur!")
        case .golden:
            print("\(furString) golden fur!")
        case .white:
            print("\(furString) white fur!")
        }
    }
}

One alteration - I did not need any parameters in the function - this would already be there within the class! Doh! And switch for furColour - not the parameter as none needed. Cool. 


Coding Project 2!

This is going to be more in depth. Last time I got it but needed a little help. Let's see how it goes!


This does look like fun!

So, let's break down what we know...

A function must be used
It will take in a String
It needs to return a String, which has the count of words, the count for each word, and each word sorted from most to least frequent. Wow, tricky!

func wordCounter(text: String) -> String {
    
    let totalWords = text.count
    
    return "\(totalWords) Words"
}

wordCounter(text: "Well this is just an example. It will be interesting to see if there are many words counted. I will then use this to see if there are repeated words. It probably would have been easier to have copied and pasted words from an online source! Well it shouldn't matter anyway. So let us see!")

First part done! I've got it all set up and have a word counter. The hard bit now is to break down each word and then list them from most to least frequent. Let's try something out before looking on Google! Either way, I am not going to look at Nick's solution until I've tried both!

OK nothing online and I've tried a for loop! So I need a HINT from Nick...

First hint from Nick is to create an array of Strings...

Well this is proving MUCH harder than I thought! MY initial idea wasn't even right - that counted the characters! So. Here's what I've done from Nick - 

func wordCounter(words: String) {
    
    
    let text = words.lowercased()
    
    let words = text.components(separatedBy: " ")
    
    var wordDictionary : [String: Int] = [:]
    


}

wordCounter(words: "Well this is just an example. It will be interesting to see if there are many words counted. I will then use this to see if there are repeated words. It probably would have been easier to have copied and pasted words from an online source! Well it shouldn't matter anyway. So let us see!")

I've learned about the lowercased function (haven't needed to use that for a while!), the components method too is how you actually separate those out. The word dictionary will have each word added to it, then the number. This is hard!

func wordCounter(words: String) {
    
    words.replacingOccurrences(of: ".", with: "")
    words.replacingOccurrences(of: ",", with: "")
    words.replacingOccurrences(of: "?", with: "")
    words.replacingOccurrences(of: "!", with: "")
    
    let text = words.lowercased()
    
    let words = text.components(separatedBy: " ")
    
    var wordDictionary : [String: Int] = [:]
    
    for word in words {
        
        if wordDictionary[word] == nil {
            
            wordDictionary[word] = 1
            
        } else {
            
            wordDictionary[word] = wordDictionary[word]! + 1
        }
        
    }
    
    print("\(wordDictionary.count) Words")
    
    let sortedWords = wordDictionary.sorted { (word1, word2) -> Bool in return
        word1.value > word2.value
    

    }
    var rank = 1
    
    for word in sortedWords {
        print(" \(rank). \(word.key) - \(word.value)")
        rank += 1
    }


}

OK that was so hard! I had to give up and follow along with Nick. It shows to me that I never had a chance with this particular challenge. I understood most of the concepts going in, but combining it and knowing what to do was so hard! So here's what I need to do - apply the concepts from Nick's challenge to something else! Later or tomorrow, I'm going to have a go at making a similar sort of function!

Finish Time - 12:55 (Total time 1 hour 15 minutes)

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)