From 44bde9772980b169e017f7f8b8719647d864c757 Mon Sep 17 00:00:00 2001 From: tmaskall Date: Sat, 22 May 2021 09:02:00 +0100 Subject: [PATCH 1/2] Lesson one labs mostly completed --- .../Contents.swift | 9 +++-- .../Contents.swift | 27 ++++++++++++-- .../Contents.swift | 25 +++++++++++-- .../Contents.swift | 12 +++++- .../Contents.swift | 9 ++++- .../Contents.swift | 16 +++++++- .../Contents.swift | 18 ++++++++- .../Contents.swift | 7 +--- .../Contents.swift | 3 ++ .../Contents.swift | 8 +++- .../Contents.swift | 37 +++++++++++++++---- 11 files changed, 141 insertions(+), 30 deletions(-) diff --git a/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift b/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift index 8721318..f58b60b 100644 --- a/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift +++ b/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift @@ -3,16 +3,19 @@ Declare a constant called `friends` to represent the number of friends you have on social media. Give it a value between 50 and 1000. Print out the value by referencing your constant. */ +let friends = 55 //number of facebook friends +print("friends") // output to the terminal /*: Now assume you go through and remove friends that aren't active on social media. Attempt to update your `friends` constant to a lower number than it currently is. Observe what happens and then move to the next step. */ - +// friends = friends -1 +// cant do the above its a constant /*: Does the above code compile? Why not? Print your explanation to the console using the `print` function. Go back and delete your line of code that updates the `friends` constant to a lower number so that the playground will compile properly. */ - - +print("you cannot change the value of a constant\nInstead use var to declare it if you want it to be variable") +//note the use of the escape \n to add a new line within the string //: page 1 of 10 | [Next: App Exercise - Step Goal](@next) diff --git a/Lab - Control Flow.playground/Pages/1. Exercise - Logical Operators.xcplaygroundpage/Contents.swift b/Lab - Control Flow.playground/Pages/1. Exercise - Logical Operators.xcplaygroundpage/Contents.swift index e3db378..366474d 100644 --- a/Lab - Control Flow.playground/Pages/1. Exercise - Logical Operators.xcplaygroundpage/Contents.swift +++ b/Lab - Control Flow.playground/Pages/1. Exercise - Logical Operators.xcplaygroundpage/Contents.swift @@ -10,46 +10,67 @@ 1. `9 == 9` */ +print("true") +print (9==9) /*: 2. `9 != 9` */ - +print("false") +print (9 != 9) /*: 3. `47 > 90` */ +print("false") +print (47 > 90) /*: 4. `47 < 90` */ - +print("true") +print (47 < 90) /*: 5. `4 <= 4` */ +print("true") +print (4 <= 4) // less than or equal to /*: 6. `4 >= 5` */ +print("false") +print (4 >= 5) /*: 7. `(47 > 90) && (47 < 90)` */ +print("false as both conditions cannot be true") +let output = (47 > 90) && (47 < 90) // could not simply print it out as type bool could not be automatically set +print (output) + + + + /*: 8. `(47 > 90) || (47 < 90)` */ - +let orResult = (47 > 90) || (47 < 90) // should be true +print (orResult) /*: 9. `!true` */ +print ("false") +print (!true) + //: page 1 of 9 | [Next: Exercise - If and If-Else Statements](@next) diff --git a/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift b/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift index 5bb5325..e2d22bf 100644 --- a/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift +++ b/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift @@ -4,18 +4,35 @@ Imagine you're creating a machine that will count your money for you and tell you how wealthy you are based on how much money you have. A variable `dollars` has been given to you with a value of 0. Write an if statement that prints "Sorry, kid. You're broke!" if `dollars` has a value of 0. Observe what is printed to the console. */ var dollars = 0 - +if dollars == 0 +{ + print ("Sorry, kid you're broke!") +} /*: `dollars` has been updated below to have a value of 10. Write an an if-else statement that prints "Sorry, kid. You're broke!" if `dollars` has a value of 0, but prints "You've got some spending money!" otherwise. Observe what is printed to the console. */ dollars = 10 - +if dollars == 0 +{ + print ("Sorry, kid you're broke!") +} +else +{ + print ("You have some spending money!") +} /*: `dollars` has been updated below to have a value of 105. Write an an if-else-if statement that prints "Sorry, kid. You're broke!" if `dollars` has a value of 0, prints "You've got some spending money!" if `dollars` is less than 100, and prints "Looks to me like you're rich!" otherwise. Observe what is printed to the console. */ dollars = 105 - - +if dollars == 0{ + print ("Sorry, kid you're broke!") +} +else if dollars < 100{ + print ("You have some spending money!") +} +else{ + print ("You're rich!") +} //: [Previous](@previous) | page 2 of 9 | [Next: App Exercise - Fitness Decisions](@next) diff --git a/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift b/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift index d0d23b3..5b14469 100644 --- a/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift +++ b/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift @@ -10,6 +10,16 @@ /*: Now create a new, but similar, if-else-if statement that prints "Way to get a good start today!" if `steps` is less than a tenth of `stepGoal`, prints "You're almost halfway there!" if `steps` is less than half of `stepGoal`, and prints "You're over halfway there!" if `steps` is greater than half of `stepGoal`. */ - +var stepGoal = 10000 +var steps = 8000 // test values used were 20, 5000 and 8000 +if steps < stepGoal / 10{ + print ("you made a start") +} +else if steps <= stepGoal/2{ + print ("you are almost halfway") +} +else if steps > stepGoal/2{ + print ("over half way") +} //: [Previous](@previous) | page 3 of 9 | [Next: Exercise - Boolean Practice](@next) diff --git a/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift b/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift index 5db9740..1e0a119 100644 --- a/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift +++ b/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift @@ -11,8 +11,15 @@ let hasFish = true let hasPizza = false -let hasVegan = true +let hasVegan = true // test with false here to check logic +if (hasFish || hasPizza) && hasVegan +{ + print ("Lets go, I am starving!") +} +else { + print ("No good try again") +} /*: Imagine you're trying to decide whether or not to go on a walk. You decide that you'll go on a walk if it's not raining or if it's 82 degress or warmer and sunny out. Create a constant `isNiceWeather` that is equal to an expression that evaluates to a boolean indicating whether or not the weather is nice enough for you to go for a walk. Write an if statement that will print "I'm going for a walk!" if the weather is nice. diff --git a/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift b/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift index 8740e75..327635a 100644 --- a/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift +++ b/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift @@ -9,7 +9,19 @@ */ let targetLowerBound = 120 let targetUpperBound = 150 -let currentHR = 147 - +let currentHR = 147 // test values 100 147 and 200 +let isInTarget = (currentHR <= targetUpperBound) && (currentHR > targetLowerBound) +let isBelowTarget = currentHR < targetLowerBound +let isAboveTarget = currentHR > targetUpperBound +if isInTarget +{ + print("you are on track") +} +else if isBelowTarget{ + print ("Go faster slowcoach") +} +else if isAboveTarget{ + print ("Slow down you are on fire") +} //: [Previous](@previous) | page 5 of 9 | [Next: Exercise - Switch Statements](@next) diff --git a/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift b/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift index ed3e397..2446ff3 100644 --- a/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift +++ b/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift @@ -3,11 +3,27 @@ Imagine you're on a baseball team nearing the end of the season. Create a `leaguePosition` constant with a value of 1. Using a `switch` statement, print "Champions!" if the `leaguePosition` is 1, "Runners up" if the value is 2, "Third place" if the value is 3, and "Bad season!" in all other cases. */ - +let leaguePosition = 3 // test with 1 2 3 & 4+ +switch leaguePosition { +case 1: + print ("Champions") +case 2: + print ("runners up") +case 3: + print ("third place") +default: + print ("give up and go home, your useless") +} /*: Write a new `switch` statement that prints "Medal winner" if `leaguePosition` is within the range of 1-3. Otherwise, print "No medal awarded". */ +switch leaguePosition { +case 1 , 2, 3: + print ("In the medals") +default: + print ("give up and go home, your useless") +} //: [Previous](@previous) | page 6 of 9 | [Next: App Exercise - Heart Rate Zones](@next) diff --git a/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift b/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift index 24cbb56..c2647cc 100644 --- a/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift +++ b/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift @@ -7,10 +7,7 @@ let number1 = 14 let number2 = 25 var largest: Int -if number1 > number2 { - largest = number1 -} else { - largest = number2 -} +largest = number1 > number2 ? number1 : number2 +print (largest) //: [Previous](@previous) | page 8 of 9 | [Next: App Exercise - Ternary Messages](@next) diff --git a/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift b/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift index 5c86dde..efe143b 100644 --- a/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift +++ b/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift @@ -8,11 +8,14 @@ let stepGoal = 10000 let steps = 3948 +/* if steps < stepGoal / 2 { print("Almost halfway!") } else { print("Over halfway!") } + */ +print (steps < stepGoal ? ("Almost halfway") : ("Over halfway")) /*: diff --git a/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift b/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift index a019d00..2082a7c 100644 --- a/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift +++ b/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift @@ -3,13 +3,17 @@ Create a for-in loop that loops through values 1 to 100, and prints each of the values. */ - +for counter in 1...100{ + print (counter) +} /*: Create a for-in loop that loops through each of the characters in the `alphabet` string below, and prints each of the values alongside the index. */ let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - +for (index, letter) in alphabet.enumerated(){ + print ("\(index) : \(letter)") +} /*: Create a `[String: String]` dictionary, where the keys are names of states and the values are their capitals. Include at least three key/value pairs in your collection, then use a for-in loop to iterate over the pairs and print out the keys and values in a sentence. diff --git a/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift b/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift index 8c68750..c254e84 100644 --- a/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift +++ b/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift @@ -3,28 +3,39 @@ You decide to build a shed and want to know beforehand the area of your yard that it will take up. Create two constants, `width` and `height`, with values of 10 and 20, respectively. Create an `area` constant that is the result of multiplying the two previous constants together, and print out the result. */ - +let width = 10 +let length = 20 +let area = width * length +print ("the area needed for the shed is \(area) square feet") // ok I added a bit + + + /*: You decide that you'll divide your shed into two rooms. You want to know if dividing it equally will leave enough room for some of your larger storage items. Create a `roomArea` constant that is the result of dividing `area` in half. Print out the result. */ - +let roomArea = area/2 +print("each half of the room is \(roomArea) square feet") /*: Create a `perimeter` constant whose value equals `width` plus `width` plus `height` plus `height`, then print out the result. */ - +let perimeter = width * 2 + length * 2 +print("The perimeter of the room is \(perimeter) feet") /*: Print what you would expect the result of integer division of 10 divided by 3 to be. Create a constant, `integerDivisionResult` that is the result of 10 divided by 3, and print the value. */ - +let integerDivisionResult = 10/3 +print (integerDivisionResult)// does not show the decimal part as 10 and 3 are considered to be integers /*: Now create two constants, `double10` and `double3`, set to 10 and 3, and declare their types as `Double` values. Declare a final constant `divisionResult` equal to the result of `double10` divided by `double3`. Print the value of `divisionResult`. How does this differ from the value when using integer division? */ - - +let double10 : Double = 10 +let double3: Double = 3 +let divisionResult = double10 / double3 +print (divisionResult) // this time the decimal is included /*: Given the value pi (3.1415927), create a `radius` constant with a value of 5.0, then calculate the diameter and circumference of the circle using the following equations, and print the results: @@ -32,17 +43,27 @@ *circumference = 2 * pi * radius.* */ -let pi = 3.1415927 - +let pi = 3.1415927 +let radius = 5.0 +let diameter = radius * 2 +let circumference = 2 * pi * radius +print ("The diameter is \(diameter) the circumference is \(circumference) ") /*: Create an integer constant. Using the remainder operator, set its value to the remainder of 12 divided by 5. */ +let intConstant: Int = 12 % 5 // % returns the remainder +print (intConstant) /*: Create two integer constants, `even` and `odd` and set them to any even integer and any odd integer, respectively. For each, print the remainder of dividing the value by 2. Looking at the results, how do you think you could use the remainder operator to determine if an integer is even or odd? */ +let even = 10 +let odd = 5 +print (even / 2) +print (odd / 2) +// using the remainder operator and if zero the value is even, when divided by 2 //: page 1 of 8 | [Next: App Exercise - Fitness Calculations](@next) From cafd4f66178891e5ad29fc9807ec2901611cdd75 Mon Sep 17 00:00:00 2001 From: tmaskall Date: Sat, 22 May 2021 09:19:32 +0100 Subject: [PATCH 2/2] Update Contents.swift --- .../1. Exercise - Constants.xcplaygroundpage/Contents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift b/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift index f58b60b..25bd6e3 100644 --- a/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift +++ b/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift @@ -12,7 +12,7 @@ print("friends") // output to the terminal */ // friends = friends -1 // cant do the above its a constant - +// added comment to test git /*: Does the above code compile? Why not? Print your explanation to the console using the `print` function. Go back and delete your line of code that updates the `friends` constant to a lower number so that the playground will compile properly. */