Skip to content

Instantly share code, notes, and snippets.

@NeoSPU
Last active December 23, 2017 19:20
Show Gist options
  • Save NeoSPU/66922c84ff881ae4c71636b176dcbaad to your computer and use it in GitHub Desktop.
Save NeoSPU/66922c84ff881ae4c71636b176dcbaad to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
// =============== 1 =====================
func changeItem<T>(_ array: [T]) -> [T] {
guard !array.isEmpty else {
print("\(array) -> Array is Empty!")
return array
}
var changeArray: [T] = array
let firstItem = changeArray[0]
guard let lastItem = changeArray.last else { return array }
changeArray[0] = lastItem
changeArray[(changeArray.count - 1)] = firstItem
print("\(array) --> \(changeArray)\n")
return changeArray
}
let oldArray = [Any]()
let oldIntArray = [1, 2, 3, 4, 5]
print("\n=============== 1 =====================\n")
changeItem(oldArray)
changeItem(oldIntArray)
// =============== 2 =====================
func isSymmetric<T: Equatable>(_ array: [T]) -> Bool {
let count = array.count
// if count % 2 == 0 {
let arrayLeft = array.dropLast(count / 2)
let arrayRight = array.dropFirst(count / 2)
let arrayTest = arrayRight.reversed()
let answer = arrayLeft.elementsEqual(arrayTest)
print("\(array) symmetric -is-> \(answer)\n")
return answer
// }
// print("\(array) symmetric -is-> false")
// return false
}
print("\n=============== 2 =====================\n")
isSymmetric(["a", "b", "4", "b", "a"])
//Дааа, Макс! заставил же ты меня .... ))))) а я протокол делаю, структуру....Equatable))))
// =============== 3 =====================
print("\n=============== 3 =====================\n")
class SortedArray<T: Comparable> {
private var arraySorted: [T] = []
init(array: [T]) {
arraySorted = array.sorted()
}
func append(_ item: T) {
arraySorted.append(item)
arraySorted.sort()
print(arraySorted)
}
func removeLast(){
arraySorted.removeLast()
print(arraySorted)
}
func removeAtIndex(_ atIndex: Int){
arraySorted.remove(at: atIndex)
print(arraySorted)
}
}
let newSortedArrayType = SortedArray(array: ["1", "b", "c", "2"])
newSortedArrayType.append("7")
newSortedArrayType.removeAtIndex(2)
newSortedArrayType.removeLast()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment