Skip to content

Instantly share code, notes, and snippets.

@helloworldsmart
Last active January 20, 2017 09:02
Show Gist options
  • Select an option

  • Save helloworldsmart/b97e01709c206ab838b9c29983f88af6 to your computer and use it in GitHub Desktop.

Select an option

Save helloworldsmart/b97e01709c206ab838b9c29983f88af6 to your computer and use it in GitHub Desktop.
設計一個 func printer(mode: Int, size: Int) mode = 0 時會用 * 印出一個直角三角形 mode = 1 時則會用 * 印出一個等腰三角形 size 為三角形的高度 (共幾層 * 堆疊)
func printer(mode: Int, size: Int) {
guard size >= 2 else { return }
switch mode {
case 0:
for i in 1...size {
for _ in 1...i {
print("*", terminator: "")
}
print("")
}
case 1:
for i in 1...size {
for _ in 0..<(size - i) {
print(" ", terminator: "")
}
for _ in 1...(2 * i - 1) {
print("*", terminator: "")
}
print("")
}
default:
break
}
}
printer(mode: 0, size: 6)
//output:
*
**
***
****
*****
******
printer(mode: 1, size: 5)
//output:
*
***
*****
*******
*********
//13行: 雷區
// fatal error: Can't form Range with upperBound < lowerBound
//Game Points + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment