Created
February 7, 2021 14:23
-
-
Save tedbow/4f3dab1448436bc747a13ecdfeef744f to your computer and use it in GitHub Desktop.
swift printer
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
// Test your 🔁 skills! | |
var printer :String | |
var current: Int = 1 | |
var upperLimit: Int = 40 | |
var goUp: Bool = false | |
for _ in 1...100 { | |
printer = "" | |
let change: Int = Int.random(in: 2...7) | |
switch current { | |
case 1: | |
goUp = true | |
case upperLimit: | |
goUp = false | |
default: | |
goUp = Int.random(in:1...2) == 2 | |
} | |
goUp = Int.random(in:1...5) == 2 ? !goUp : goUp | |
current = goUp ? current + change : current - change | |
switch current { | |
case let x where x < 1: | |
current = 1 | |
case let x where x > upperLimit: | |
current = upperLimit | |
default: | |
break | |
} | |
for _ in 1...current { | |
printer += "*" | |
} | |
print(printer) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment