Bob Lee Course Part 7 (lecture 11)

So a long old time! Unavoidable unfortunately - parents' evenings, car renewal (complicated in Dubai!), my girlfriend's friend staying, going into work over Half Term to get things sorted before an upcoming inspection...yep - busy! Sadly, coding takes the worst of this as I simply haven't had the time nor brain capacity to even consider it. Yet I'm making some time for it! Time in between getting back from work - during Half Term and all - and heading out to see people this evening. Once Cath's friend has gone tomorrow, I'll hopefully be able to better balance things. 

So....last time with Bob it was about classes and structs, and when to choose between using these. A lot of that was familiar but it was good to confirm that classes, being a reference type, are tricky to use as the original values change. Anyway, moving on today with tuples!

*By the way, I've gone back to naming this by lecture rather than lesson, as what Bob calls 'lessons' do not match up to the dashboard list. So it is lecture 11 today (which he calls lesson 9!). 

Start time - 17:20

Set - eliminating duplicated items
Tuples - combining types

What are Sets....?

I haven't had much experience with these before. They store values of the same type with no defined ordering and no duplicated elements. 

Well with arrays, there IS a set order so that is a key difference there. Also, you can repeat values in an array e.g. in a Fibonacci sequence at the start. 

So these are all ways of creating arrays first of all. 

var arrayOne: [Double] = []
var arrayTwo = [Double]()

var arrayThree: Array<Double> = Array()
var arrayFour: Array<Double> = []

Interesting to see the syntax for the < > used. 

So creating sets - 

var setOne: Set = [1, 2, 3]

var setTwo: Set<String> = ["Josh", "Cath", "Alex"]

var setThree = Set<String>()

var setFour = Set(["Josh", "Cath", "Alex"])

setOne.insert(4)
setTwo.contains("Josh")
setFour.remove("Alex")

Good for arrays - example

var oddNumbers: [Int] = []
var evenNumbers: [Int] = []

for number in 1...50 {
    if number % 2 == 0 {
        evenNumbers.append(number)
    } else {
        oddNumbers.append(number)
    }
}


OK some other features - 

Union

var oddNumberSet = Set(oddNumbers)
let evenNumberSet = Set(evenNumbers)

print(oddNumberSet.union(evenNumberSet))

This will mean all values that are for odd or even will be printed. But not in order - SETS are NOT in order!

Here are other ones - 

oddNumberSet.symmetricDifference(evenNumberSet)

oddNumberSet.intersection(evenNumberSet)

oddNumberSet.subtract(evenNumberSet)

Union and symmetricDiff - exactly the same. Intersection and subtract - nothing gets printed. 

OK I'm doing my own example now as I'm bored with Bob's numbers...

var rumbleWinnersSet = Set(["Brock Lesnar", "John Cena", "Bret Hart", "Hulk Hogan"])

var headlinedWrestelmaniaSet = Set(["Brock Lesnar", "John Cena", "Chris Jericho", "Triple H"])

print(rumbleWinnersSet.intersection(headlinedWrestelmaniaSet))

Cool! In the print statement, I have Brock and Cena - as they appear in BOTH lists. Cool!

Right so Bob has touched on converting from Arrays to Sets. Now the other way round....

Bob uses a closure to arrange the order, but I've just found 'sorted' - 

let rumbleArray = rumbleWinnersSet.sorted()

print(rumbleArray)

Now the names are in order!







Here is a practical use of TUPLES.  Bob mentions that he won't be using these too much on the course but I can see how it's useful to store multiple values, especially when they are of different types.

Finish Time - 17:57 (37 minutes) 

Well there we go. A modest, gently 37 minutes to ease me back in! That's good - no point trying to do too much in one go. I will need to, pretty soon, do my 'consolidation' entry as that's where I can really delve in at a deeper level and link some of the knowledge together. Still, I'm enjoying going through Bob's course gradually. Today, it was great to find out about sets - never used those before, how they are different to arrays and what you can do with them. I will get in some more coding tomorrow. Until then!

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)