Skip to content

Instantly share code, notes, and snippets.

View MickhailP's full-sized avatar
🎯
Focusing on a issue

Mikhail Perevozchikov MickhailP

🎯
Focusing on a issue
View GitHub Profile
@MickhailP
MickhailP / HexadecimalColor.swift
Created November 4, 2022 17:49
HexadecimalColor
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
@MickhailP
MickhailP / ImageSaver.swift
Created November 8, 2022 16:54
ImageSaver
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) {
@MickhailP
MickhailP / NetworkingError.swift
Created November 9, 2022 20:15
NetworkingError
import Foundation
enum NetworkingError: Int, Error, LocalizedError {
// 100 Informational
case `continue` = 100
case switchingProtocols = 101
case processing = 102
case earlyHints = 103
@MickhailP
MickhailP / MergeSort.swift
Last active July 3, 2023 09:49
MergeSort
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
@MickhailP
MickhailP / BinarySearch.swift
Created November 21, 2022 16:36
BinarySearch
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 {
@MickhailP
MickhailP / RotateArray.swift
Created November 22, 2022 20:01
RotateArray
//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:
@MickhailP
MickhailP / .swiftlint.yml
Created December 3, 2022 06:47
SwiftLint settings
disabled_rules:
- trailing_whitespace
- colon
- comma
- control_statement
- vertical_whitespace
- switch_case_alignment
- opening_brace
- comment_spacing
- trailing_newline
@MickhailP
MickhailP / Bundle-Decodable.swift
Created December 23, 2022 04:12
GenericBundelDecoder
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.")
@MickhailP
MickhailP / RecursiveSum.swift
Created January 10, 2023 17:35
AlgorithmsRecursiveSum
func recursiveSum(_ nums: [Int]) -> Int {
if nums.count == 0 {
return 0
} else {
return nums[0] + recursiveSum(Array(nums.dropFirst()))
}
}
@MickhailP
MickhailP / QuickSort.swift
Created January 10, 2023 19:30
AlgorithmsQuickSort
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 {