Skip to content

Instantly share code, notes, and snippets.

View ytyubox's full-sized avatar
😎
TooFastToSlow

Tsungyu Yu ytyubox

😎
TooFastToSlow
View GitHub Profile
//
// ContentView.swift
// Layout
//
// Created by Yu Yu on 2022/8/21.
//
import SwiftUI
struct ContentView: View {
import XCTest
import struct Foundation.UUID
extension UUID {
internal init(_ uint8s: UInt8...) {
var list:[UInt8] = Array(repeating: 0, count: 16)
for (index, value) in uint8s.enumerated() where index < 16 {
list[15-index] = value
}
self.init(list: list)
}
import Foundation
import XCTest
final class DecodeTests: XCTestCase {
func testMeta1() throws {
let rawJson =
"""
{
"a1": {
final class FibTests: XCTestCase {
class DPO1 {
func fib(_ n: Int) -> Int {
var dp = [0,1]
if n < 2 {return dp[n]}
for i in 2...n {
dp[i&1] = dp[0] + dp[1]
}
return dp[n&1]
}
@ytyubox
ytyubox / BinaryTree+Iterator.swift
Last active October 25, 2021 16:44
Binary tree with early break, continue logic
import Foundation
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
private class SolutionDP1 {
// DP
public func splitArray(_ nums: [Int],_ m: Int) -> Int {
let n = nums.count
/**
```
dp [n+1:2]
[
min, max, max, ....
import Foundation
/// https://leetcode.com/problems/container-with-most-water/
private class Solution {
/// Area = d * h
/// which
/// d === i - j
/// h === min(Ai, Aj)
/// whatever pair of i, j, tallest one is more possible to have greater value
/// so that will move the shorter one to the center
///
//
/*
* Created by 游宗諭 in 2021/9/2
*
* Using Swift 5.0
*
* Running on macOS 12.0
*/
import XCTest
@testable import DataCutter
@propertyWrapper struct Byte<Value>: ByteDecode {
func decodeValue(from data: Data) throws {
let value = data
.subdata(in: inner.range)
.withUnsafeBytes { $0.load(as: Value.self) }
inner.value = .already(value)
}
// MARK: - EqualKeyPath
struct EqualKeyPath<T, A> where A: Equatable {
init(on: T.Type, for k1: KeyPath<T, A>) {
self.k1 = k1
}
let k1: KeyPath<T, A>
func equalFor(_ a: T, _ b: T) -> Bool {
a[keyPath: k1] == b[keyPath: k1]