-
Notifications
You must be signed in to change notification settings - Fork 2
/
Staircase.swift
86 lines (64 loc) · 1.18 KB
/
Staircase.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Approach 1
func staircase1(n: Int) {
var counter = n-1
for _ in 0..<n {
for j in 0..<n {
if (j >= counter) {
print("#", terminator: "")
}
else {
print(" ", terminator: "")
}
if (j == n-1) { print("") }
}
counter -= 1
}
}
//Approach 2
func staircase2(n: Int) {
for i in 1...n {
var row = ""
let space = n-i
let hash = n-space
row += String(repeating: Character(" "), count: space)
row += String(repeating: Character("#"), count: hash)
print(row)
}
}
print(staircase1(n: 4))
/*
Output:
#
##
###
####
*/
print(staircase2(n: 6))
/*
Output:
#
##
###
####
#####
######
*/
/*
Flow for understanding:
i = 0
j = 0,1,2,3
p = 0,0,0,1
i = 2
j = 0,1,2,3
p = 0,0,1,1
i = 3
j = 0,1,2,3
p = 0,1,1,1
i = 4
j = 0,1,2,3
p = 1,1,1,1
where,
- p denotes printed output
- 0 in p denotes spaces
- 1 in p denotes hashes
*/