/*
Alphabet Rangoli Generator in Swift
This program constructs a geometric alphabet pattern of size N.
It uses helper functions, clear logic, and Swift's built‑in
collection operations to keep the implementation concise.
*/
import Foundation
// Entry point
func main() {
let n = 5
print(makeRangoli(n))
}
/*
Builds the entire rangoli as a multi-line string.
The pattern is symmetric vertically, so we iterate from -(n-1) to +(n-1).
*/
func makeRangoli(_ n: Int) -> String {
guard n > 0 else { return "" }
let totalWidth = 4 * n - 3 // Width formula identical to the reference implementation
// Build each row and join them with newline characters
let rows = (-((n - 1))...(n - 1)).map { offset in
buildRow(n, abs(offset), totalWidth)
}
return rows.joined(separator: "\n")
}
/*
Builds a single row of the rangoli.
currentDist = distance from the center row.
totalWidth = final width after padding with hyphens.
*/
func buildRow(_ n: Int, _ currentDist: Int, _ totalWidth: Int) -> String {
// Number of descending characters (e.g., e d c ...)
let count = n - currentDist
// Descending characters: e d c b a (for n=5)
let descending: [Character] = (0..<count).map { j in
Character(UnicodeScalar(UInt8(ascii: "a") + UInt8(n - 1 - j)))
}
// Ascending characters: b c d e (excluding the center)
let ascending: [Character] = (0..<(count - 1)).reversed().map { j in
Character(UnicodeScalar(UInt8(ascii: "a") + UInt8(n - 1 - j)))
}
// Combine both sides and insert hyphens
let row = (descending + ascending).map { String($0) }.joined(separator: "-")
// Compute padding to center the row
let padding = (totalWidth - row.count) / 2
let hyphens = String(repeating: "-", count: padding)
return hyphens + row + hyphens
}
// Run program
main()
/*
run:
--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------
*/