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
import Foundation | |
public struct Queue<T> { | |
private var array: Array<T?> | |
private var availableSpaceCount: Int | |
private var readIndex: Int |
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 maxProduct(_ nums: [Int]) -> Int { | |
guard nums.count > 0 else { | |
return 0 | |
} | |
var maximum = nums.first! | |
var maxPositivEnding = nums.first! | |
var minNegativeEnding = nums.first! | |
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 maxProduct(_ nums: [Int]) -> Int { | |
guard nums.count > 0 else { | |
return 0 | |
} | |
guard let first = nums.first, first != 0 else { | |
return max(0,maxProduct(Array(nums.dropFirst()))) | |
} | |
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 maxSubArray(_ nums: [Int]) -> Int { | |
guard nums.count > 0 else { | |
return Int.min | |
} | |
var bestEndingSum = nums.first! | |
var maxSum = nums.first! | |
for num in nums[1..<nums.endIndex] { | |
bestEndingSum = max(bestEndingSum + num, num) | |
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
let str = " Taylor Swift is £!?" | |
let trimmed = str.trimmingCharacters(in: .whitespacesAndNewlines) |
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
if let range3 = string.range(of: "rain", options: .caseInsensitive) { | |
// match | |
} else { | |
// no match | |
} |
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
let str = "Hello, world!" | |
let reversed = String(str.reversed()) | |
print(reversed) |
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
extension String { | |
func deletingPrefix(_ prefix: String) -> String { | |
guard self.hasPrefix(prefix) else { return self } | |
return String(self.dropFirst(prefix.count)) | |
} | |
} |
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
extension String { | |
subscript(i: Int) -> String { | |
return String(self[index(startIndex, offsetBy: i)]) | |
} | |
} |
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
if let filepath = Bundle.main.path(forResource: "example", ofType: "txt") { | |
do { | |
let contents = try String(contentsOfFile: filepath) | |
print(contents) | |
} catch { | |
// contents could not be loaded | |
} | |
} else { | |
// example.txt not found! | |
} |
NewerOlder