Created
April 12, 2018 11:04
-
-
Save aniltv06/1482427646ca62992efed06fc4dcb868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fetchCount(array: [Int], length: Int, symmetric: Bool) -> Int { | |
var count = 0 | |
for index in array.indices where index + length < array.count { | |
let subArray = array[index..<(index + length)] | |
print("Sub-array \"\(subArray)\" is \(subArray.allEqual() == true ? "Symmetrical" : "Asymmetrical")") | |
if symmetric == subArray.allEqual() { | |
count = count + 1 | |
} | |
} | |
return count | |
} | |
extension ArraySlice where Element : Equatable { | |
func allEqual() -> Bool { | |
if let firstElem = first { | |
return !dropFirst().contains { $0 != firstElem } | |
} | |
return true | |
} | |
} | |
let array = [1,1,2,2,3,3,3,4,5,6,7] | |
let length = 2 | |
let symmetric = true | |
print("Total \(symmetric == true ? "Symmetrical" : "Asymmetrical") count is \(fetchCount(array: array, length: length, symmetric: symmetric))") | |
//Output | |
//Sub-array "[1, 1]" is Symmetrical | |
//Sub-array "[1, 2]" is Asymmetrical | |
//Sub-array "[2, 2]" is Symmetrical | |
//Sub-array "[2, 3]" is Asymmetrical | |
//Sub-array "[3, 3]" is Symmetrical | |
//Sub-array "[3, 3]" is Symmetrical | |
//Sub-array "[3, 4]" is Asymmetrical | |
//Sub-array "[4, 5]" is Asymmetrical | |
//Sub-array "[5, 6]" is Asymmetrical | |
//Total Symmetrical count is 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment