当前位置:网站首页>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))
边栏推荐
- 雲計算未來 — 雲原生
- 【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
- 232. Implement queue with stack
- Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
- Idea packages the web project into a war package and deploys it to the server to run
- 社交社区论坛APP超高颜值UI界面
- Apache Mina Development Manual
- Flutter Widget : Flow
- Use bloc to build a page instance of shutter
- 2.9 overview of databinding knowledge points
猜你喜欢

Shutter: add gradient stroke to font

ES6新特性

Implement verification code verification

记录自己vulnhub闯关记录

Cloud Computing future - native Cloud

Record your vulnhub breakthrough record

使用BLoC 构建 Flutter的页面实例

Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.

Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?

T430 toss and install OS majave 10.14
随机推荐
Shutter: about inheritedwidget
Sword finger offer06 Print linked list from end to end
最新版盲盒商城thinkphp+uniapp
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
剑指Offer06. 从尾到头打印链表
Sqoop1.4.4原生增量导入特性探秘
CNN MNIST handwriting recognition
Alibaba is bigger than sending SMS (user microservice - message microservice)
The difference between lambda and anonymous inner class
temp
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
Integer string int mutual conversion
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
Jsup crawls Baidu Encyclopedia
剑指Offer10- I. 斐波那契数列
Use of atomicinteger
Unicode查询的官方网站
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
2.9 overview of databinding knowledge points