当前位置:网站首页>alright alright alright
alright alright alright
2022-07-03 12:35:00 【iWillook】
//
// main.swift
// Ultimate
//
// Created by Mewlan Musajan on 4/26/21.
//
//Excerpt From: Apple Inc. “The Swift Programming Language (Swift 5.3).” Apple Books. https://books.apple.com/us/book/the-swift-programming-language-swift-5-3/id881256329
func findIndex(ofString valueToFind: String, in array: [String]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
let someArrayToFindIndex = ["apple", "banana", "cush", "donat"]
if let someIndex = findIndex(ofString: "alma", in: someArrayToFindIndex) {
print(someIndex)
} else {
print("404 Not Found")
}
func findAnyTypeOfIndex<T: Equatable>(of valueToFind: T, in array:[T]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
let someIntValues = [1, 2, 3, 4, 5]
if let intValue = findAnyTypeOfIndex(of: 2, in: someIntValues) {
print(intValue)
}
protocol Container {
associatedtype Item
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
struct IntStack: Container {
var items = [Int]()
mutating func push(_ item: Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
typealias Item = Int
mutating func append(_ item: Int) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Int {
return items[i]
}
}
struct Stack<Element>: Container {
var items = [Element]()
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
mutating func append(_ item: Element) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Element {
return items[i]
}
}
extension Array: Container{}
extension Container {
func average() -> Double where Item == Int {
var sum = 0.0
for index in 0..<count {
sum += Double(self[index])
}
return sum / Double(count)
}
func endsWith(_ item: Item) -> Bool where Item: Equatable {
return count >= 1 && self[count-1] == item
}
}
let numbers = [1260, 1200, 98, 37]
print(numbers.average())
print(numbers.endsWith(37))
边栏推荐
- How to deploy web pages to Alibaba cloud
- 02_ Lock the code, and don't let the "lock" become a worry
- Slf4j log facade
- 雲計算未來 — 雲原生
- 023 ([template] minimum spanning tree) (minimum spanning tree)
- Unicode查询的官方网站
- Solve the problem of VI opening files with ^m at the end
- Tensorflow binary installation & Failure
- Lambda expression
- [combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
猜你喜欢
Pki/ca and digital certificate
Display time with message interval of more than 1 minute in wechat applet discussion area
TOGAF认证自学宝典V2.0
Record your vulnhub breakthrough record
Fluent: Engine Architecture
Develop plug-ins for idea
2021 autumn Information Security Experiment 1 (password and hiding technology)
Wechat applet - basic content
Sword finger offer07 Rebuild binary tree
Day 1 of kotlin learning: simple built-in types of kotlin
随机推荐
Swagger
init. RC service failed to start
Sword finger offer05 Replace spaces
TOGAF认证自学宝典V2.0
Introduction to concurrent programming (II)
【嵌入式】---- 内存四区介绍
[download attached] password acquisition tool lazagne installation and use
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
Airflow installation jump pit
Sword finger offer07 Rebuild binary tree
Dart: about grpc (I)
Applet wxss introduction
LeetCode 0556.下一个更大元素 III - 4步讲完
Shell: basic learning
Dart: view the dill compiled code file
Flutter: about monitoring on flutter applications
ES6 standard
Develop plug-ins for idea
DEJA_ Vu3d - cesium feature set 053 underground mode effect
New features of ES6