This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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 |