Created
October 15, 2018 03:51
-
-
Save singhamit089/0d7143627d6c9605781c896bf2ebde8e to your computer and use it in GitHub Desktop.
Print a pattern
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
class PrintPattern { | |
var number:Int | |
var left = 0 | |
var right = 0 | |
var count = 1 | |
var center:Int | |
var ifStarFirst = true | |
var ifCountDecrementStarted = false | |
init(number:Int) { | |
self.number = number | |
self.center = ((number-1)/2)+1 | |
self.run() | |
} | |
func run() { | |
for i in 1...number { | |
if ifStarFirst{ | |
left = count | |
right = number - count | |
ifStarFirst = false | |
printStar(count: left) | |
printHash(count: right) | |
}else { | |
right = count | |
left = number - count | |
printHash(count: left) | |
printStar(count: right) | |
ifStarFirst = true | |
} | |
updateCount() | |
print("") | |
} | |
} | |
func updateCount() { | |
if (count < center) && !ifCountDecrementStarted { | |
count = count + 1 | |
}else if (count > 1 ){ | |
ifCountDecrementStarted = true | |
count = count - 1 | |
} | |
} | |
func printStar(count:Int) { | |
for i in 1...count { | |
print("*", separator: " ", terminator: "") | |
} | |
} | |
func printHash(count:Int) { | |
for i in 1...count { | |
print("#", separator: " ", terminator: "") | |
} | |
} | |
} | |
var somePattern = PrintPattern(number: 8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment