Skip to content

Instantly share code, notes, and snippets.

View knowsudhanshu's full-sized avatar

Sudhanshu knowsudhanshu

  • New Delhi
View GitHub Profile
@knowsudhanshu
knowsudhanshu / The Celebrity Problem
Created April 4, 2024 14:56
In a party of N people, only one person known to everyone but doesn't know anyone. Such a person is called celebrity.
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 {
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")
@knowsudhanshu
knowsudhanshu / BiometricIDHandler.swift
Last active December 1, 2021 17:54
TouchID and FaceID integration
struct BiometricIDHandler {
static func authenticateWithBiometrics() {
let laContext = LAContext()
var error: NSError?
if laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,
error: &error) {
@knowsudhanshu
knowsudhanshu / Wikifiable.swift
Last active September 27, 2021 03:27
Wikifiable: enables getting rid of the boilerplate code in an escaping closure where there is a repeated code [weak self] ( Credit: Vincent Pradeilles)
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 }
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))
}
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)
extension Sequence {
func myFilter(_ operation: (_ val: Element) -> Bool) -> [Element] {
var output: [Element] = []
for item in self {
if operation(item) {
output.append(item)
}
}
return output
extension Sequence {
func myCompactMap<T>(_ operation: (Element) -> T?) -> [T] {
var output: [T] = []
for item in self {
if let value = operation(item) {
output.append(value)
}
}
extension Sequence {
/// Reduce: applies a given operation on elements of a given sequence and returns a single value (of same type as of the elements in given sequence) at the end
/// - Parameters:
/// - initialValue: Initial value for the operation to start
/// - operation: function that'll be applied on elements of the given sequence
/// - Returns: final value after applying operation on the elements of given sequence
func myReduce(_ initialValue: Element,
_ operation: (_ value1: Element, _ value2: Element) -> Element) -> Element {
var output: Element = initialValue
@knowsudhanshu
knowsudhanshu / myMap.swift
Last active June 19, 2021 11:25
myMap Example
extension Sequence {
func myMap<T>(_ operation: (Element) -> T) -> [T] {
var arrayAfterOperatedValues: [T] = []
for item in self {
/// Apply operation on each element (as map(_:) does)
let operatedValue = operation(item)
arrayAfterOperatedValues.append(operatedValue)
}