*CONSOLIDATION ENTRY* Nick Walter Challenge Revisited!

So, I was happily coasting through the beginner and intermediate aspects of Swift in Nick's course when WHAM! The challenge was just so tough. To be fair, I could have been much more willing to search for how to do the various elements. So I'm going to have another go at it, trying to do what I can from memory. I will then see if I can find out where to get the info from. 

Start Time - 11:29

Right, first off let's see what the challenge was...



So that is the task!

Here is what should be returned as an example



So I am going to, again, try this without any help at all, remembering what I did yesterday. Yesterday was such a rush and I didn't even attempt to look up how to complete the different parts to it.

Right. Various elements to this. As before, I know that I need the following elements -

A function that takes in a string
I am going to print rather than do a return String as that it is too fiddly!
A way of putting the string into separate values in an array
A dictionary that has a key-value pair of the word with the frequency of that word
A rank number that increases by 1
A for loop
Before all this...
Ways to remove all symbols like punctuation and make all lowercase

Right let's make a start....

func wordCounter(text: String) {
    
    let textLowercase = text.lowercased()
    textLowercase.replacingOccurrences(of: ".", with: " ")
    textLowercase.replacingOccurrences(of: ",", with: " ")
    textLowercase.replacingOccurrences(of: "?", with: " ")
    textLowercase.replacingOccurrences(of: "!", with: " ")
    textLowercase.replacingOccurrences(of: "(", with: " ")
    textLowercase.replacingOccurrences(of: ")", with: " ")
    
    

}

So far so good. We have a text that's going in, which will be made lowercased, then all punctuation symbols sorted and replaced with a space!

I now need to see how to put the string into separate array values...

let textArray = textLowercase.components(separatedBy: " ")

AWESOME! And I did that using Google!!

OK, so I've tried searching online to get array to dictionary and can find NOTHING! This is good though, as I spent a while trying! Here is the code so far...

func wordCounter(text: String) {
    
    let textLowercase = text.lowercased()
    textLowercase.replacingOccurrences(of: ".", with: " ")
    textLowercase.replacingOccurrences(of: ",", with: " ")
    textLowercase.replacingOccurrences(of: "?", with: " ")
    textLowercase.replacingOccurrences(of: "!", with: " ")
    textLowercase.replacingOccurrences(of: "(", with: " ")
    textLowercase.replacingOccurrences(of: ")", with: " ")
    textLowercase.replacingOccurrences(of: "'s", with: " ")
    
    let textArray = textLowercase.components(separatedBy: " ")
    //print(textArray)
    
    print("\(textArray.count) Words")
    
    let textDictionary: [String: Int] = [:]
    

}

I've got an empty Dictionary ready but can't find how to get the items from the array to them....So now it's time to see what Nick did!

OK started looking at Nick's video - first I need to check if the word is in the array within the for loop....

Right so here is where I was getting lost before. 

    for word in textArray {
        if textDictionary[word] == nil {
            textDictionary[word]! = 1
        } else {
            textDictionary[word]! = textDictionary[word]! + 1
        }
        
    }

Let's unpick this. The for loop above has the temp value of word. That means it is iterating through every single item (word) in the array. If that is not in the dictionary (i.e. if the word is not already there), then it is given the value of 1. If it is already there, then it is given the value of one more e.g. 2 if there is 1, 3 if there is 2. 

That is a much better way of getting from an array to a dictionary than ANYTHING I have seen online! So no wonder I stood no chance! A cool, useful tool this from Nick. 

So the next bit is try to sort the dictionary by the value...

Again, needed help from Nick but I was quite close with help online!

  var sortedDict = textDictionary.sorted(by: {(word1, word2) -> Bool in
        return word1.value > word2.value
    })
    print(sortedDict)

The issue now is that the words 'key' and 'value' are coming up in the print statement.

I created the rank variable earlier and had it set to 1. 

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

Still have an issue of having 'key' and 'value' still in the text... but am pleased I got that mostly myself! let's see what Nick does...

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

YES! I remembered the .key bit - that seems to work!

Finish Time - 13:08 (1 hour 39 minutes total)

A very worthwhile session! I've got a MUCH better understanding of different concepts through doing this - getting from array to dict, sorting arrays and dicts, removing characters...lots here that I did not know before. Fair play to Nick - the info online was unclear and very wordy in terms of converting from arrays to dictionaries in particular. So good stuff and am glad that I have gone over it. 


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)