Skip to content

Instantly share code, notes, and snippets.

@sarkarchandan
Last active July 16, 2017 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarkarchandan/5ea3fbb03e0d7187f1c83488390af2d2 to your computer and use it in GitHub Desktop.
Save sarkarchandan/5ea3fbb03e0d7187f1c83488390af2d2 to your computer and use it in GitHub Desktop.
Find patching pairs of objects in an array that add up to a certain value -- Shortened
// Defining an extension for implementing Binary Search in the input array where
// array elements conform to Comparable protocol
extension Array where Element: Comparable{
func binarySearch(_ value: Element) -> Int{
return binSearch(0, self.count - 1, value)
}
func binSearch(_ start: Int, _ end: Int, _ value: Element) -> Int {
if start > end {
return -1
}else{
let mid = start + (end - start)/2
if value > self[mid] {
return binSearch(mid + 1, end, value)
}else if value < self[mid] {
return binSearch(start, mid - 1, value)
}else if value == self[mid]{
return mid
}else{
return -1
}
}
}
}
// Declaring protocol Type to overload the "-" operator.
protocol Type: Comparable {
static func -(leftHandSide: Self, rightHandSide: Self) -> Self
}
// Defining the behavior of the "-" operator for Integer type.
extension Int: Type{
static func -<T: Type> (_ leftHandSide: T, _ rightHandSide: Int) -> T {
return leftHandSide - rightHandSide
}
}
// Defining the behavior of the "-" operator for String type.
extension String: Type{
static func -<T: Type> (_ leftHandSide: String, _ rightHandSide: String) -> T {
if leftHandSide.contains(rightHandSide) {
let difference = leftHandSide.components(separatedBy: rightHandSide)
return difference[1] as! T
}else{
return "default_value" as! T
}
}
}
// Defining an extension for determining the pairs of positions of components as per
// passed in value
extension Array where Element: Type {
func findPairFor(_ value: Element) -> [(Int,Int)] {
var resultPairs = [(Int,Int)]()
for index in 0..<self.count {
let compliment = value - self[index]
let complimentIndex = self.binarySearch(compliment)
if complimentIndex != -1 {
resultPairs.append(index,complimentIndex)
}
}
return resultPairs
}
}
//Sample Executions
// Integer Array Implementation
let integerArray = [15,9,4,7,20,2,8,26,3,17]
//Sorting with the help of a Closure
let sortedIntegerArray = integerArray.sorted(by: {
return $0 < $1
})
print("Sorted Integer Array: \(sortedIntegerArray)")
// Fining out the pairs of positions for value 10
for pair in sortedIntegerArray.findPairFor(10) {
print(pair)
}
// String Array Implementation
let stringArray = ["John","Ned","Arya","Sansa","Robert","Tyrion","Stark","Lanister","Baratheon","Snow"]
//Sortng with the help of a closure
let sortedStringArray = stringArray.sorted(by: {
return $0 < $1
})
print("Sorted String Array: \(sortedStringArray)")
// Finding out the pairs of positions for input value NedStark
for pair in sortedStringArray.findPairFor("NedStark") {
print(pair)
}
Sorted Integer Array: [2, 3, 4, 7, 8, 9, 15, 17, 20, 26]
// Looking for the value 10
(0, 4)
(1, 3)
(3, 1)
(4, 0)
Sorted String Array: ["Arya", "Baratheon", "John", "Lanister", "Ned", "Robert", "Sansa", "Snow", "Stark", "Tyrion"]
// Looking for the value "NedStark"
(4, 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment