Skip to content

Adjusting Whitespace in Swift Code #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contents/bogo_sort/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ In code, it looks something like this:
{% sample lang="rs" %}
[import:16-20, lang:"rust"](code/rust/bogosort.rs)
{% sample lang="swift" %}
[import:32-39, lang:"swift"](code/swift/bogosort.swift)
[import:25-31, lang:"swift"](code/swift/bogosort.swift)
{% endmethod %}

That's it.
Expand Down
14 changes: 3 additions & 11 deletions contents/bogo_sort/code/swift/bogosort.swift
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
import Foundation


func isSorted(inputArray: [Int]) -> Bool {

for i in 0..<inputArray.count-1 {
if inputArray[i] > inputArray[i+1] {
return false
}
}

return true
}



func shuffle(inputArray: inout [Int]) -> [Int] {

var shuffledArray = [Int]()

for _ in 0..<inputArray.count {
let rand = Int(arc4random_uniform(UInt32(inputArray.count)))
shuffledArray.append(inputArray[rand])
inputArray.remove(at: rand)
}

return shuffledArray
}



func bogoSort(sortArray: inout [Int]) -> [Int] {

while(!isSorted(inputArray: sortArray)) {
sortArray = shuffle(inputArray: &sortArray)
}

return sortArray
}

Expand Down
2 changes: 1 addition & 1 deletion contents/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
{% sample lang="racket" %}
[import:6-19, lang:"racket"](code/racket/bubbleSort.rkt)
{% sample lang="swift" %}
[import:1-12, lang:"swift"](code/swift/bubblesort.swift)
[import:1-13, lang:"swift"](code/swift/bubblesort.swift)
{% sample lang="ti83b" %}
[import:2-13, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
{% endmethod %}
Expand Down
1 change: 0 additions & 1 deletion contents/bubble_sort/code/swift/bubblesort.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
func bubbleSort(sortArray: inout [Int]) -> [Int] {

for i in (1..<sortArray.count).reversed() {
for j in 0..<i {
if sortArray[j] > sortArray[j+1] {
Expand Down
2 changes: 2 additions & 0 deletions contents/forward_euler_method/code/swift/euler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ func solveEuler(timeStep: Double, n: Int) -> [Double] {
for i in 1...n {
result.append(result[i - 1] - 3 * result[i - 1] * timeStep)
}

return result
}

Expand All @@ -19,6 +20,7 @@ func checkResult(result: [Double], threshold: Double, timeStep: Double) -> Bool
isApprox = false
}
}

return isApprox
}

Expand Down
11 changes: 0 additions & 11 deletions contents/monte_carlo_integration/code/swift/monte_carlo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,21 @@

import Foundation



public extension Double {

public static var random: Double {

return Double(arc4random()) / 0xFFFFFFFF // Returns a random double between 0.0 and 1.0, inclusive.
}

public static func random(min: Double, max: Double) -> Double {

return Double.random * (max - min) + min
}
}


func isInCircle(x: Double, y: Double, radius: Double) -> Bool {

return (x*x) + (y*y) < radius*radius
}


func monteCarlo(n: Int) -> Double {

let radius: Double = 1
var piCount = 0
var randX: Double
Expand All @@ -41,11 +32,9 @@ func monteCarlo(n: Int) -> Double {
}

let piEstimate = Double(4 * piCount)/(Double(n))

return piEstimate
}


func main() {
let piEstimate = monteCarlo(n: 10000)
print("Pi estimate is: ", piEstimate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ each point is tested to see whether it's in the circle or not:
{% sample lang="java" %}
[import:13-15, lang:"java"](code/java/MonteCarlo.java)
{% sample lang="swift" %}
[import:21-25, lang:"swift"](code/swift/monte_carlo.swift)
[import:15-17 lang:"swift"](code/swift/monte_carlo.swift)
{% endmethod %}

If it's in the circle, we increase an internal count by one, and in the end,
Expand Down