Skip to content

Instantly share code, notes, and snippets.

@5SMNOONMS5
Last active January 23, 2017 15:52
Show Gist options
  • Save 5SMNOONMS5/9d562c93a48b4b1c2e753c3967ee4761 to your computer and use it in GitHub Desktop.
Save 5SMNOONMS5/9d562c93a48b4b1c2e753c3967ee4761 to your computer and use it in GitHub Desktop.
Print right-angled-triangle or isosceles-triangle
// mode: 0 是 直角三角形, 1 是 等腰三角形
// size: 邊長
func printer(mode: Int, size: Int) {
// Print a right angled triangle
func rightAngledTriangle() {
var i = 1
while i <= size {
var string = ""
for _ in 0..<i { string += "*" }
print(string + "\n")
i += 1
}
}
// Print a isosceles triangle
func isoscelesTriangle() {
var i = size - 1
while i >= 0 {
var string = ""
for _ in 0..<i { string += " " }
let count = (size - i - 1) * 2 + 1
for _ in 0..<count { string += "*" }
print(string + "\n")
i -= 1
}
}
(mode == 0) ? rightAngledTriangle() : isoscelesTriangle()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment