Skip to content

Instantly share code, notes, and snippets.

Jay Zahn jayz5

Block or report user

Report or block jayz5

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View CheckFileSizesExample.swift
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,
View FileManager+chckFileSizes.swift
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))
View sleepForAsynchronous.swift
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)
}
View deleteDocFile.swift
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)
}
View createDocFile.swift
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
View verifyBST.swift
// 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 }
@jayz5
jayz5 / longest2Csubstring.swift
Created Jul 11, 2018
given a string, find its longest substring that contains two unique characters
View longest2Csubstring.swift
let str = "abcbbbbcccbdddadacb"
var maxLength = 0
var startForMax = 0
// recurse
// complexity: O(N)
func search(start: Int) {
let i = start
var j = start + 1
You can’t perform that action at this time.