Angela Yu Course Part 5 (up to lesson 72)
Yes - managed to find some time late into the evening again - my last one at Basingstoke, for the time being. Tomorrow I'll be back to 'normal' if you will and really want to crack on with this course from Angela as I'm learning a lot from it. The last few and next few bits look like more recap/going over of concepts, so it'll be partly useful, partly confirming that I do know a lot about the actual syntax and language! Here we go!
BMI Calculator Challenge!

So this is what I am working on in a playground!
BMI Calculator Challenge!
So this is what I am working on in a playground!
func checkBMI(weightInKg weight: Double, heightInMetres height: Double) -> String {
let bmi = weight / (height * height)
if bmi > 25 {
return "Your BMI is \(bmi). You are overweight."
} else if bmi >= 18.5 {
return "Your BMI is \(bmi). You are of normal weight."
} else {
return "Your BMI is \(bmi). You are underweight."
}
}
let personCheck = checkBMI(weightInKg: 78, heightInMetres: 1.75)
Angela has made the point of using 'power' for squared. So I could use pow(height, 2) instead of height * height. Fair enough! Makes sense for doing power of 3 or more.
Also there is a way of rounding to two decimal places - will come in handy!
let shortenedBMI = String(format: "%.2f", bmi)
Loops - the For-In Loop
let arrayOfNumbers = [1, 5, 3, 6, 7, 8, 17, 15]
var sum = 0
for number in arrayOfNumbers {
sum += number
}
for number in 1...10 {
print(number)
}
All recap to be honest. But good to go over!
for number in 1...10 where number % 2 == 0 {
print(number)
}
Now THAT is a new - using where! Don't think I've seen that but good to know - again gives lots of options!
Bottles of Beer challenge
I found this all too conceptually challenging - it's my flaw - the application side of things. I just have to admit defeat here and say that I wasn't up to it. Doesn't mean I've failed at coding! It just means that this challenge was all a bit beyond me - at this moment in time. There are - as always - some key takeaways:
You can use the .reversed key word in the for-in loop - this is perfect for this particular challenge.
func beerSong(withThisManyBottles bottles: Int) -> String {
Well I may have failed at the main concept of this one but I've aced the parameter bit - used good logic for this! I've put in external and internal parameters and used the internal one within the loop! Perfect and relieved that applied something and didn't wait for the solution! Now I could have used an invisible parameter (_) but it would need a more detailed func name. I'll pass!
Same with the fibonacci one - these challenges are more mathematical rather than logic - perhaps both. I am good with both, in theory, but not to the point that I have a secure conceptual understanding. Therefore, when attempting these sort of problems I do genuinely struggle. Something to work on for sure - loops within problems!
There's a website that Angela recommends:
https://projecteuler.net/archives
This is a really good idea - it's all about coding in practice and trying out some ideas. I will try to tackle these here and there - not too many in one go!
It looks like I'm doing less each time but actually these lessons were each quite a bit longer than before! I'm really glad that I've recapped the 'basics' because they were absolutely worthwhile. In some ways they make me feel a little disenchanted that I'm not able to conceptualise what to do and how to tackle the problem in a logical way - at least not for bottles of beer and fibonacci! However, that's something I need more experience with - using loops in context. It's not a big deal as they are more mathematical challenges than actual programming tasks. Anyway, a lot of useful stuff again today. More tomorrow!
Comments
Post a Comment