Skip to content

Instantly share code, notes, and snippets.

@nhatlee
nhatlee / MergeSortedArray
Created August 19, 2024 04:03
Merge Sorted Array
/// https://leetcode.com/problems/merge-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150
/// Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
/// Output: [1,2,2,3,5,6]
///
///
func merge(_ nums1: inout [Int], _ m: Int, _ nums2: [Int], _ n: Int) {
var lastIdx = m + n - 1
var mPointer = m
var nPointer = n
while mPointer > 0 && nPointer > 0 {
@nhatlee
nhatlee / iPod.swift
Created July 28, 2020 03:00 — forked from jordansinger/iPod.swift
Swift Playgrounds iPod Classic
import SwiftUI
import PlaygroundSupport
struct iPod: View {
var body: some View {
VStack(spacing: 40) {
Screen()
ClickWheel()
Spacer()
}
// Original article here: https://www.fivestars.blog/code/swiftui-hierarchy-list.html
import SwiftUI
struct FileItem: Identifiable {
let name: String
var children: [FileItem]?
var id: String { name }
import Foundation
import XCTest
func checkPhoneWithPatterns(_ phone: String) -> Bool{
let patterns = [
"^(840|84|0|)8[0-9]{8}$",
"(840|84|0|)9\\d{8}",
"^(840|84|0|)5(2|6|8|9)\\d{7}$",
"(840|84|0|)7(0|6|7|8|9)\\d{7}",
"(840|84|0|)3[2-9]\\d{7}"
@nhatlee
nhatlee / libdispatch-efficiency-tips.md
Created February 18, 2020 13:42 — forked from tclementdev/libdispatch-efficiency-tips.md
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

I suspect most developers are using the libdispatch inefficiently due to the way it was presented to us at the time it was introduced and for many years after that, and due to the confusing documentation and API. I realized this after reading the 'concurrency' discussion on the swift-evolution mailing-list, in particular the messages from Pierre Habouzit (who is the libdispatch maintainer at Apple) are quite enlightening (and you can also find many tweets from him on the subject).

My take-aways are:

@nhatlee
nhatlee / Demo.swift
Created February 12, 2020 09:58 — forked from AliSoftware/Demo.swift
NestableCodingKey: Nice way to define nested coding keys for properties
struct Contact: Decodable, CustomStringConvertible {
var id: String
@NestedKey
var firstname: String
@NestedKey
var lastname: String
@NestedKey
var address: String
enum CodingKeys: String, NestableCodingKey {
//------------------------------------------------------------------------
// The SwiftUI Lab: Advanced SwiftUI Animations
// https://swiftui-lab.com/swiftui-animations-part1 (Animating Paths)
// https://swiftui-lab.com/swiftui-animations-part2 (GeometryEffect)
// https://swiftui-lab.com/swiftui-animations-part3 (AnimatableModifier)
//------------------------------------------------------------------------
import SwiftUI
struct ContentView: View {
@nhatlee
nhatlee / ScreenshotDetector
Last active February 26, 2019 04:07
ScreenshotDetector
import UIKit
import Photos
/// A class that detects when the user has taken a screenshot and provides it via a delegate callback.
@available(iOS 9.0, *)
open class ScreenshotDetector: NSObject {
/// An error encountered when detecting and retreiving a screenshot.
public enum Error: Swift.Error {
@nhatlee
nhatlee / LinkedList
Last active February 13, 2019 10:59
Linkedlist Recursive Reverse
class Node {
var data: Int
var next: Node?
// init(_ data: Int) {
// self.data = data
// }
init?(_ values: Array<Int>) {
var valuesCopy = values
if valuesCopy.count == 0 {
@nhatlee
nhatlee / StreamReader.swift
Created January 16, 2019 09:01 — forked from sooop/StreamReader.swift
Read a large text file line by line - Swift 3
import Foundation
class StreamReader {
let encoding: String.Encoding
let chunkSize: Int
let fileHandle: FileHandle
var buffer: Data
let delimPattern : Data
var isAtEOF: Bool = false