Skip to content

Commit 988c0f1

Browse files
committed
Add playground for linear search
Change Comparable to Equatable
1 parent f91c252 commit 988c0f1

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//: Playground - noun: a place where people can play
2+
3+
func linearSearch<T: Equatable>(array: [T], _ object: T) -> Int? {
4+
for (index, obj) in array.enumerate() where obj == object {
5+
return index
6+
}
7+
return nil
8+
}
9+
10+
let array = [5, 2, 4, 7]
11+
linearSearch(array, 2) // returns 1
12+
linearSearch(array, 3) // returns nil
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

Linear Search/LinearSearch.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func linearSearch<T: Comparable>(array: [T], _ object: T) -> Int? {
1+
func linearSearch<T: Equatable>(array: [T], _ object: T) -> Int? {
22
for (index, obj) in array.enumerate() where obj == object {
33
return index
44
}

Linear Search/README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ We start by comparing the first number in the array, `5` with the number we're l
1515
Here is a simple implementation of insertion sort in Swift:
1616

1717
```swift
18-
func linearSearch<T: Comparable>(array: [T], _ object: T) -> Int? {
18+
func linearSearch<T: Equatable>(array: [T], _ object: T) -> Int? {
1919
for (index, obj) in array.enumerate() where obj == object {
2020
return index
2121
}

0 commit comments

Comments
 (0)