Skip to content

Instantly share code, notes, and snippets.

View jayz5's full-sized avatar

Jay Zahn jayz5

View GitHub Profile
@jayz5
jayz5 / longest2Csubstring.swift
Created July 11, 2018 13:50
given a string, find its longest substring that contains two unique characters
let str = "abcbbbbcccbdddadacb"
var maxLength = 0
var startForMax = 0
// recurse
// complexity: O(N)
func search(start: Int) {
let i = start
var j = start + 1
// The key to this problem is In-Order traversal and remember the value of last visited node
func verifyBST(node: Node?, lastValue: inout Int?) -> Bool {
guard let node = node else { return true }
// since we're doing in-order traversal,
// the lastValue from previous traversal path must be <= current value
if let prevVal = lastValue, prevVal > node.data { return false }
// visit left
if !verifyBST(node: node.left, lastValue: &lastValue) { return false }
func createDocFile(fileName: String, content: String) {
// get the document directory url
guard let docDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
// choose the location of the file to be withinthe document directory
let fileURL = docDir.appendingPathComponent(fileName)
print("file url: ", fileURL)
do {
// creating or appending to a file by writing to the file url
func deleteDocFile(fileName: String) {
guard let docDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let fileURL = docDir.appendingPathComponent(fileName)
print("deleting file url: ", fileURL)
try? FileManager.default.removeItem(at: fileURL)
}
import Foundation
var dwi: DispatchWorkItem? = nil
dwi = DispatchWorkItem {
print("in dispatch work item")
for i in 0..<1000 {
if dwi!.isCancelled { break }
print("dispatch item \(i)")
usleep(500000)
}
extension FileManager {
func checkFileSizes(in folderURL: URL) {
do {
let resourceKeys: [URLResourceKey] =
[.fileSizeKey, .fileAllocatedSizeKey, .isRegularFileKey, .isDirectoryKey]
for url in try contentsOfDirectory(at: folderURL,
includingPropertiesForKeys: resourceKeys,
options: [])
{
let resourceValues = try url.resourceValues(forKeys: Set(resourceKeys))
func checkFileSizesDemo() {
guard let docDir = FileManager.default.urls(for: .documentDirectory,
in: .userDomainMask).first else {
return
}
do {
let testDir = docDir.appendingPathComponent("test")
try FileManager.default.createDirectory(at: testDir,
withIntermediateDirectories: true,