-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
arthurwrls
committed
Oct 7, 2022
1 parent
4a415ef
commit 00d9e75
Showing
3 changed files
with
79 additions
and
15 deletions.
There are no files selected for viewing
42 changes: 28 additions & 14 deletions
42
Codecademy.playground/Pages/Bottles.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'") |
2 changes: 1 addition & 1 deletion
2
Codecademy.playground/Pages/Exclude odd numbers.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
50 changes: 50 additions & 0 deletions
50
Codecademy.playground/Pages/FizzBuzz.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |