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 waysToScore(n: Int) -> Int { | |
var arr: [Int] = [Int](repeating: 0, count: n + 1) | |
arr[0] = 1 | |
for index in 1...n { | |
// print("index: \(index)") | |
if index - 3 >= 0 { | |
// print("index - 3 > 0 ----Start--------") | |
// print("\(arr[index])") | |
// print("\(arr[index - 3])") |
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 input = [ | |
[1,3,5,8], | |
[4,2,1,7], | |
[4,3,2,3] | |
] | |
/* | |
// Recursion | |
func maxPathCost(input: [[Int]], m: Int, n: Int) -> Int { | |
if m == 0 && n == 0 { |
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 input = [ | |
[1,3,5,8], | |
[4,2,1,7], | |
[4,3,2,3] | |
] | |
func minPathCost(input: [[Int]], m: Int, n: Int) -> Int { | |
if m == 0 && n == 0 { | |
return input[0][0] | |
} |
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 getCelebrity(input: [[Int]]) -> Int { | |
// Add person to stack | |
var stack: [Int] = [] | |
for i in 0..<input.count { | |
stack.append(i) | |
} | |
// pick a person and check with others | |
while stack.count > 1 { |
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
infix operator !! | |
func !!<T>(lhs: T?, rhs: T) -> T { | |
guard let lhs = lhs else { | |
return rhs | |
} | |
return lhs | |
} | |
var someVar: String? = "has value" | |
print(someVar !! "no value") |
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
struct BiometricIDHandler { | |
static func authenticateWithBiometrics() { | |
let laContext = LAContext() | |
var error: NSError? | |
if laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, | |
error: &error) { |
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
protocol Weakifiable where Self: class {} | |
extension NSObject: Weakifiable {} | |
extension Weakifiable { | |
func weakify<T>(_ code: @escaping (Self, T) -> Void) -> (T) -> Void { | |
{ | |
[weak self] (data) in | |
guard let self = self else { return } |
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 literalExpressionExample() { | |
/// #file : String, it represents the path to the file | |
/// #function : String, name (with signature) of the function | |
/// #line : Int, line number on which it appears | |
print(#file + " : " #function + " : " + " : " + \(#line)) | |
} |
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
protocol A { | |
var someVar: String { get set } | |
func someFunc() | |
} | |
protocol B { | |
var someVar: String { get set } | |
func someFunc() | |
} | |
struct C: A, B { | |
@_implements(A, someVar) |
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 Sequence { | |
func myFilter(_ operation: (_ val: Element) -> Bool) -> [Element] { | |
var output: [Element] = [] | |
for item in self { | |
if operation(item) { | |
output.append(item) | |
} | |
} | |
return output |
NewerOlder