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 SwiftUI | |
extension Color { | |
init?(hex: String) { | |
var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines) | |
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "") | |
var rgb: UInt64 = 0 | |
var red: CGFloat = 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
import UIKit | |
class ImageSaver: NSObject { | |
var succesHandler: (() -> Void)? | |
var errorHandler: ((Error) -> Void)? | |
func writeToPhotoAlbum(image: UIImage) { | |
UIImageWriteToSavedPhotosAlbum(image, self, #selector(saveCompleted), nil) | |
} | |
@objc func saveCompleted(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) { |
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 | |
enum NetworkingError: Int, Error, LocalizedError { | |
// 100 Informational | |
case `continue` = 100 | |
case switchingProtocols = 101 | |
case processing = 102 | |
case earlyHints = 103 | |
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 | |
/* | |
Array's `Element` type is defined as a generic type parameter `T` that conforms to `Comparable` | |
*/ | |
func mergesort<T: Comparable>(_ array: [T]) -> [T] { | |
// If the list has zero or one values, there's nothing to sort, so we return it as-is | |
if array.count <= 1 { return array } | |
// At this point the algorithm is in the recursive portion |
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 | |
func binarySearch(searchArray: [Int],target: Int, lower: Int, upper: Int) -> Int? { | |
if lower > upper { | |
return nil | |
} else { | |
var middle = (lower + upper) / 2 | |
print(middle) | |
if searchArray[middle] == target { |
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
//Given an array, rotate the array to the right by k steps, where k is non-negative. | |
// Input: nums = [1,2,3,4,5,6,7], k = 3 | |
// Output: [5,6,7,1,2,3,4] | |
// Explanation: | |
// rotate 1 steps to the right: [7,1,2,3,4,5,6] | |
// rotate 2 steps to the right: [6,7,1,2,3,4,5] | |
// rotate 3 steps to the right: [5,6,7,1,2,3,4] | |
//SOLUTION: |
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
disabled_rules: | |
- trailing_whitespace | |
- colon | |
- comma | |
- control_statement | |
- vertical_whitespace | |
- switch_case_alignment | |
- opening_brace | |
- comment_spacing | |
- trailing_newline |
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 | |
extension Bundle { | |
func decode<T: Codable> (_ file: String) -> T { | |
guard let url = self.url(forResource: file, withExtension: nil) else { | |
fatalError("Failed to locate \(file) in bundle.") | |
} | |
guard let data = try? Data(contentsOf: url) else { | |
fatalError("Failed to load \(file) from bundle.") |
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 recursiveSum(_ nums: [Int]) -> Int { | |
if nums.count == 0 { | |
return 0 | |
} else { | |
return nums[0] + recursiveSum(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 quickSort(nums: [Int]) -> [Int] { | |
if nums.count < 2 { | |
return nums | |
} else { | |
let pivot = nums[0] | |
var lessPart: [Int] = [] | |
var greaterPart: [Int] = [] | |
for i in 1..<nums.count { |
OlderNewer