Skip to content

Instantly share code, notes, and snippets.

View amfathi's full-sized avatar
🎧

Ahmed Fathi amfathi

🎧
View GitHub Profile
@amfathi
amfathi / Colors.swift
Created February 23, 2020 13:22
Colors Extensions Sample
import UIKit
extension UIColor {
// MARK: - Backgrounds
static var mainBackground = UIColor(named: "mainBackground")!
static var secondaryBackground = UIColor(named: "secondaryBackground")!
static var elevatedBackground = UIColor(named: "elevatedBackground")!
static var separator = UIColor(named: "seperator")!
static var navigationBarTint = UIColor(named: "navigationBarTint")!
@amfathi
amfathi / Formatters.swift
Created February 28, 2020 13:51
Example of how your app should create formatters (Date, number, etc..)
enum Formatters {
static var apiDate: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return formatter
}()
static var uiDisplay: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .long
import Foundation
let mod = 10000000 // 10e7
func fastPow(base: Int, power: Int, mod: Int = mod) -> Int {
// Base cases
if base == 0 {
return 0
} else if power == 0 {
return 1
import Foundation
// MARK: - Interval
struct Interval {
var low: Int
var high: Int
func overlaps(_ otherInterval: Interval) -> Bool {
return contains(otherInterval.low) || contains(otherInterval.high)
}
@amfathi
amfathi / LinkedList.swift
Last active April 21, 2020 08:52
Naive implementation for a linked list.
import UIKit
// MARK: - Node
class Node<T: Equatable> {
var value: T?
var next: Node<T>?
var previous: Node<T>?
init() {}
}
@amfathi
amfathi / Reverse.swift
Last active April 21, 2020 08:08
Basic reversing algorithms for arrays and strings.
import Foundation
// MARK: - Reverse Array
func reverse<T>(array: [T]) -> [T] {
var reversedArray = array
var index = 0
while index < array.count {
reversedArray[index] = array[array.count - index - 1]
index += 1
}
import Foundation
extension Array where Element == Int {
/// Returns a decoded version of a delta encoded array
public func deltaDecoded() -> [Element] {
let filteredArray = filterArray(self)
var cummulativeSum = 0
var decodedValues = [Element]()
extension String {
/// Returns whether the string is a pangram or not
public func isPangram() -> Bool {
if count < 26 { return false }
var alphabetOccurances = Array(repeating: false, count: 26)
var totalCount = 0
for char in lowercased() {
if let ascii = char.asciiValue {
let index = Int(ascii) - 97
@amfathi
amfathi / Sieve.swift
Last active April 21, 2020 08:58
Sieve of Eratosthenes to get primes below a specific positive integer.
/// All the primes number below & including 10e7 using Sieve algorithm O(n log log n).
/// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
func sieve() -> [Int] {
let limit = 10000000 // 10e7
// Create an array assumping all numbers are primes
var isPrime = Array(repeating: true, count: limit)
// Mark trivial cases as not primes
isPrime[0] = false
@amfathi
amfathi / throttle.swift
Last active April 15, 2021 14:20
[Tutorial] Naive throttling for a block from rapid execution for a specific period of time. Inspired by https://rxmarbles.com/#throttleTime
import Foundation
private var timer: Timer?
func throttle(_ interval: TimeInterval, block: @escaping (() -> Void)) {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false, block: { _ in
block()
})
}