Skip to content

Commit

Permalink
Added more files
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurwrls committed Oct 7, 2022
1 parent 4a415ef commit 00d9e75
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
42 changes: 28 additions & 14 deletions Codecademy.playground/Pages/Bottles.xcplaygroundpage/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
//Solution using While
// Solution using While

var numBottles: Int = 99

while numBottles > 0 {

print ("\(numBottles) bottles of milk on the wall, \(numBottles) bottles of milk!")
print ("You take one down and pass it around...")

numBottles -= 1
print ("\(numBottles) bottles of milk on the wall!\n")

print("\(numBottles) bottles of milk on the wall, \(numBottles) bottles of milk!")
print("You take one down and pass it around...")

numBottles -= 1
print("\(numBottles) bottles of milk on the wall!\n")
}

//Solution using For in
// Solution using For in

for numBottles in stride(from: 99, to: 0, by: -1) {

print ("\(numBottles) bottles of milk on the wall, \(numBottles) bottles of milk!")
print ("You take one down and pass it around...")

print ("\(numBottles - 1) bottles of milk on the wall!\n")
print("\(numBottles) bottles of milk on the wall, \(numBottles) bottles of milk!")
print("You take one down and pass it around...")

print("\(numBottles - 1) bottles of milk on the wall!\n")
}

// Another solution
var numMonkeys = 5

while numMonkeys > 1 {
print("\(numMonkeys) little monkeys jumping on the bed.")
print("One fell off and bumped their head!")
print("Mama called the doctor and the doctor said")
print("'No more monkeys jumping on the bed!'\n")

numMonkeys -= 1
}

print("\(numMonkeys) little monkey jumping on the bed.")
print("They fell off and bumped their head!")
print("Mama called the doctor and the doctor said")
print("'Put those monkeys straight to bed!'")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

for num in 1 ... 9 {
for num in 1 ... 10 {
if num % 2 == 1 {
continue
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// With for-in statement

for x in 1 ... 100 {
if (x % 3) == 0, (x % 5) == 0 {
/* can use also such if statement
if i % 3 == 0 {
if i % 5 == 0 {
*/
print("FizzBuzz")
} else if x % 3 == 0 {
print("Fizz")
} else if x % 5 == 0 {
print("Buzz")
} else {
print(x)
}
}

// switch-case usage

for i in 1 ... 100 {
switch (i % 3 == 0, i % 5 == 0) {
case (true, false):
print("Fizz")
case (false, true):
print("Buzz")
case (true, true):
print("FizzBuzz")
default:
print(i)
}
}

//another switch-case (similar as "aliases" usage)

let fizz = true
let buzz = true

for i in 1...100 {
switch (i % 3 == 0, i % 5 == 0) {
case (fizz, buzz):
print("FizzBuzz")
case (fizz, _):
print("Fizz")
case (_, buzz):
print("Buzz")
default:
print(i)
}
}

0 comments on commit 00d9e75

Please sign in to comment.