Skip to content

Commit d629c7e

Browse files
author
Chris Pilcher
committed
Indentation changes for combinatorics
1 parent b68e464 commit d629c7e

File tree

3 files changed

+136
-136
lines changed

3 files changed

+136
-136
lines changed

Diff for: Combinatorics/Combinatorics.playground/Contents.swift

+78-78
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
/* Calculates n! */
44
func factorial(n: Int) -> Int {
5-
var n = n
6-
var result = 1
7-
while n > 1 {
8-
result *= n
9-
n -= 1
10-
}
11-
return result
5+
var n = n
6+
var result = 1
7+
while n > 1 {
8+
result *= n
9+
n -= 1
10+
}
11+
return result
1212
}
1313

1414
factorial(5)
@@ -21,13 +21,13 @@ factorial(20)
2121
in groups of size k.
2222
*/
2323
func permutations(n: Int, _ k: Int) -> Int {
24-
var n = n
25-
var answer = n
26-
for _ in 1..<k {
27-
n -= 1
28-
answer *= n
29-
}
30-
return answer
24+
var n = n
25+
var answer = n
26+
for _ in 1..<k {
27+
n -= 1
28+
answer *= n
29+
}
30+
return answer
3131
}
3232

3333
permutations(5, 3)
@@ -42,17 +42,17 @@ permutations(9, 4)
4242
See also Dr.Dobb's Magazine June 1993, Algorithm Alley
4343
*/
4444
func permuteWirth<T>(a: [T], _ n: Int) {
45-
if n == 0 {
46-
print(a) // display the current permutation
47-
} else {
48-
var a = a
49-
permuteWirth(a, n - 1)
50-
for i in 0..<n {
51-
swap(&a[i], &a[n])
52-
permuteWirth(a, n - 1)
53-
swap(&a[i], &a[n])
54-
}
45+
if n == 0 {
46+
print(a) // display the current permutation
47+
} else {
48+
var a = a
49+
permuteWirth(a, n - 1)
50+
for i in 0..<n {
51+
swap(&a[i], &a[n])
52+
permuteWirth(a, n - 1)
53+
swap(&a[i], &a[n])
5554
}
55+
}
5656
}
5757

5858
let letters = ["a", "b", "c", "d", "e"]
@@ -67,29 +67,29 @@ permuteWirth(xyz, 2)
6767

6868
/*
6969
Prints out all the permutations of an n-element collection.
70-
70+
7171
The initial array must be initialized with all zeros. The algorithm
7272
uses 0 as a flag that indicates more work to be done on each level
7373
of the recursion.
74-
74+
7575
Original algorithm by Robert Sedgewick.
7676
See also Dr.Dobb's Magazine June 1993, Algorithm Alley
7777
*/
7878
func permuteSedgewick(a: [Int], _ n: Int, inout _ pos: Int) {
79-
var a = a
80-
pos += 1
81-
a[n] = pos
82-
if pos == a.count - 1 {
83-
print(a) // display the current permutation
84-
} else {
85-
for i in 0..<a.count {
86-
if a[i] == 0 {
87-
permuteSedgewick(a, i, &pos)
88-
}
89-
}
79+
var a = a
80+
pos += 1
81+
a[n] = pos
82+
if pos == a.count - 1 {
83+
print(a) // display the current permutation
84+
} else {
85+
for i in 0..<a.count {
86+
if a[i] == 0 {
87+
permuteSedgewick(a, i, &pos)
88+
}
9089
}
91-
pos -= 1
92-
a[n] = 0
90+
}
91+
pos -= 1
92+
a[n] = 0
9393
}
9494

9595
print("\nSedgewick permutations:")
@@ -104,15 +104,15 @@ permuteSedgewick(numbers, 0, &pos)
104104
of size k out of a total number of distinct elements (n) you can make.
105105
*/
106106
func combinations(n: Int, _ k: Int) -> Int {
107-
return permutations(n, k) / factorial(k)
107+
return permutations(n, k) / factorial(k)
108108
}
109109

110110
combinations(3, 2)
111111
combinations(28, 5)
112112

113113
print("\nCombinations:")
114114
for i in 1...20 {
115-
print("\(20)-choose-\(i) = \(combinations(20, i))")
115+
print("\(20)-choose-\(i) = \(combinations(20, i))")
116116
}
117117

118118

@@ -122,13 +122,13 @@ for i in 1...20 {
122122
k things out of n possibilities.
123123
*/
124124
func quickBinomialCoefficient(n: Int, _ k: Int) -> Int {
125-
var result = 1
126-
127-
for i in 0..<k {
128-
result *= (n - i)
129-
result /= (i + 1)
130-
}
131-
return result
125+
var result = 1
126+
127+
for i in 0..<k {
128+
result *= (n - i)
129+
result /= (i + 1)
130+
}
131+
return result
132132
}
133133

134134
quickBinomialCoefficient(8, 2)
@@ -138,48 +138,48 @@ quickBinomialCoefficient(30, 15)
138138

139139
/* Supporting code because Swift doesn't have a built-in 2D array. */
140140
struct Array2D<T> {
141-
let columns: Int
142-
let rows: Int
143-
private var array: [T]
144-
145-
init(columns: Int, rows: Int, initialValue: T) {
146-
self.columns = columns
147-
self.rows = rows
148-
array = .init(count: rows*columns, repeatedValue: initialValue)
149-
}
150-
151-
subscript(column: Int, row: Int) -> T {
152-
get { return array[row*columns + column] }
153-
set { array[row*columns + column] = newValue }
154-
}
141+
let columns: Int
142+
let rows: Int
143+
private var array: [T]
144+
145+
init(columns: Int, rows: Int, initialValue: T) {
146+
self.columns = columns
147+
self.rows = rows
148+
array = .init(count: rows*columns, repeatedValue: initialValue)
149+
}
150+
151+
subscript(column: Int, row: Int) -> T {
152+
get { return array[row*columns + column] }
153+
set { array[row*columns + column] = newValue }
154+
}
155155
}
156156

157157
/*
158158
Calculates C(n, k), or "n-choose-k", i.e. the number of ways to choose
159159
k things out of n possibilities.
160-
160+
161161
Thanks to the dynamic programming, this algorithm from Skiena allows for
162162
the calculation of much larger numbers, at the cost of temporary storage
163163
space for the cached values.
164164
*/
165165

166166
func binomialCoefficient(n: Int, _ k: Int) -> Int {
167-
var bc = Array(count: n + 1, repeatedValue: Array(count: n + 1, repeatedValue: 0))
168-
169-
for i in 0...n {
170-
bc[i][0] = 1
171-
bc[i][i] = 1
172-
}
173-
174-
if n > 0 {
175-
for i in 1...n {
176-
for j in 1..<i {
177-
bc[i][j] = bc[i - 1][j - 1] + bc[i - 1][j]
178-
}
179-
}
167+
var bc = Array(count: n + 1, repeatedValue: Array(count: n + 1, repeatedValue: 0))
168+
169+
for i in 0...n {
170+
bc[i][0] = 1
171+
bc[i][i] = 1
172+
}
173+
174+
if n > 0 {
175+
for i in 1...n {
176+
for j in 1..<i {
177+
bc[i][j] = bc[i - 1][j - 1] + bc[i - 1][j]
178+
}
180179
}
181-
182-
return bc[n][k]
180+
}
181+
182+
return bc[n][k]
183183
}
184184

185185
binomialCoefficient(30, 15)

Diff for: Combinatorics/Combinatorics.swift

+43-43
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ func factorial(n: Int) -> Int {
1010
}
1111

1212
/*
13-
Calculates P(n, k), the number of permutations of n distinct symbols
14-
in groups of size k.
15-
*/
13+
Calculates P(n, k), the number of permutations of n distinct symbols
14+
in groups of size k.
15+
*/
1616
func permutations(n: Int, _ k: Int) -> Int {
1717
var n = n
1818
var answer = n
@@ -24,10 +24,10 @@ func permutations(n: Int, _ k: Int) -> Int {
2424
}
2525

2626
/*
27-
Prints out all the permutations of the given array.
28-
Original algorithm by Niklaus Wirth.
29-
See also Dr.Dobb's Magazine June 1993, Algorithm Alley
30-
*/
27+
Prints out all the permutations of the given array.
28+
Original algorithm by Niklaus Wirth.
29+
See also Dr.Dobb's Magazine June 1993, Algorithm Alley
30+
*/
3131
func permuteWirth<T>(a: [T], _ n: Int) {
3232
if n == 0 {
3333
print(a) // display the current permutation
@@ -43,15 +43,15 @@ func permuteWirth<T>(a: [T], _ n: Int) {
4343
}
4444

4545
/*
46-
Prints out all the permutations of an n-element collection.
46+
Prints out all the permutations of an n-element collection.
4747

48-
The initial array must be initialized with all zeros. The algorithm
49-
uses 0 as a flag that indicates more work to be done on each level
50-
of the recursion.
48+
The initial array must be initialized with all zeros. The algorithm
49+
uses 0 as a flag that indicates more work to be done on each level
50+
of the recursion.
5151

52-
Original algorithm by Robert Sedgewick.
53-
See also Dr.Dobb's Magazine June 1993, Algorithm Alley
54-
*/
52+
Original algorithm by Robert Sedgewick.
53+
See also Dr.Dobb's Magazine June 1993, Algorithm Alley
54+
*/
5555
func permuteSedgewick(a: [Int], _ n: Int, inout _ pos: Int) {
5656
var a = a
5757
pos += 1
@@ -70,21 +70,21 @@ func permuteSedgewick(a: [Int], _ n: Int, inout _ pos: Int) {
7070
}
7171

7272
/*
73-
Calculates C(n, k), or "n-choose-k", i.e. how many different selections
74-
of size k out of a total number of distinct elements (n) you can make.
75-
Doesn't work very well for large numbers.
76-
*/
73+
Calculates C(n, k), or "n-choose-k", i.e. how many different selections
74+
of size k out of a total number of distinct elements (n) you can make.
75+
Doesn't work very well for large numbers.
76+
*/
7777
func combinations(n: Int, _ k: Int) -> Int {
7878
return permutations(n, k) / factorial(k)
7979
}
8080

8181
/*
82-
Calculates C(n, k), or "n-choose-k", i.e. the number of ways to choose
83-
k things out of n possibilities.
84-
*/
82+
Calculates C(n, k), or "n-choose-k", i.e. the number of ways to choose
83+
k things out of n possibilities.
84+
*/
8585
func quickBinomialCoefficient(n: Int, _ k: Int) -> Int {
8686
var result = 1
87-
87+
8888
for i in 0..<k {
8989
result *= (n - i)
9090
result /= (i + 1)
@@ -93,29 +93,29 @@ func quickBinomialCoefficient(n: Int, _ k: Int) -> Int {
9393
}
9494

9595
/*
96-
Calculates C(n, k), or "n-choose-k", i.e. the number of ways to choose
97-
k things out of n possibilities.
96+
Calculates C(n, k), or "n-choose-k", i.e. the number of ways to choose
97+
k things out of n possibilities.
9898

99-
Thanks to the dynamic programming, this algorithm from Skiena allows for
100-
the calculation of much larger numbers, at the cost of temporary storage
101-
space for the cached values.
102-
*/
99+
Thanks to the dynamic programming, this algorithm from Skiena allows for
100+
the calculation of much larger numbers, at the cost of temporary storage
101+
space for the cached values.
102+
*/
103103

104104
func binomialCoefficient(n: Int, _ k: Int) -> Int {
105-
var bc = Array(count: n + 1, repeatedValue: Array(count: n + 1, repeatedValue: 0))
106-
107-
for i in 0...n {
108-
bc[i][0] = 1
109-
bc[i][i] = 1
110-
}
111-
112-
if n > 0 {
113-
for i in 1...n {
114-
for j in 1..<i {
115-
bc[i][j] = bc[i - 1][j - 1] + bc[i - 1][j]
116-
}
117-
}
105+
var bc = Array(count: n + 1, repeatedValue: Array(count: n + 1, repeatedValue: 0))
106+
107+
for i in 0...n {
108+
bc[i][0] = 1
109+
bc[i][i] = 1
110+
}
111+
112+
if n > 0 {
113+
for i in 1...n {
114+
for j in 1..<i {
115+
bc[i][j] = bc[i - 1][j - 1] + bc[i - 1][j]
116+
}
118117
}
119-
120-
return bc[n][k]
118+
}
119+
120+
return bc[n][k]
121121
}

0 commit comments

Comments
 (0)