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 connectSticks(_ sticks: [Int]) -> Int { | |
let sticks = sticks.sorted() | |
var connSticks = [Int]() | |
var stick = sticks[0]+sticks[1] | |
connSticks.append(stick) | |
for i in 2..<sticks.count { | |
stick = stick + sticks[i] | |
connSticks.append(stick) |
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
struct ContentView: View { | |
@State private var name = "" | |
var body: some View { | |
Form { | |
TextField("Enter your name", text: $name) | |
Text("Your name is \(name)") | |
} | |
} | |
} |
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 topkFrequentWords(_ words: [String], _ k: Int) -> [String] { | |
var hashMap = [String:Int]() | |
for word in words { | |
hashMap[word] = (hashMap[word] ?? 0) + 1 | |
} | |
var keys = Array(hashMap.keys) | |
keys.sort { | |
let f1 = hashMap[$0] ?? 0 // get ob associated w/ key 1 | |
let f2 = hashMap[$1] ?? 0 // get ob associated w/ key 2 |
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
medium.com/@sorenlind/three-ways-to-enumerate-the-words-in-a-string-using-swift-7da5504f0062 | |
extension String { | |
func words() -> [String] { | |
let range = self.startIndex..<self.endIndex | |
var words = [String]() | |
self.enumerateSubstrings(in: range, options: NSString.EnumerationOptions.byWords) { (substring, _, _, _) -> () in | |
words.append(substring ?? "") | |
} |
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
//https://medium.com/@sorenlind/three-ways-to-enumerate-the-words-in-a-string-using-swift-7da5504f0062 | |
extension String { | |
func words() -> [String] { | |
let range = self.startIndex..<self.endIndex | |
var words = [String]() | |
self.enumerateSubstrings(in: range, options: NSString.EnumerationOptions.byWords) { (substring, _, _, _) -> () in | |
words.append(substring ?? "") | |
} |
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 first intuition is figuring out the sum we are looking for is actually sum /2 and it should be even. | |
//Then, the next inuition is recognizing the problem converts to finding a subset with that sum. | |
func canPartition(_ nums: [Int]) -> Bool { | |
let total = nums.reduce(0,+) | |
if total%2 != 0 { | |
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
//The first intuition is figuring out the sum we are looking for is actually sum /2 and it should be even. | |
//Then, the next inuition is recognizing the problem converts to finding a subset with that sum. | |
func canPartition(_ nums: [Int]) -> Bool { | |
let total = nums.reduce(0,+) | |
if total%2 != 0 { | |
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
class API { | |
let service: NetworkService | |
init(_ service: NetworkService = URLSession.shared) { | |
self.service = service | |
} | |
func send(request: URLRequest, completionHandler: @escaping CompletionBlock) { | |
service.make(request: request, completionHandler: completionHandler) | |
} |
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
class HomeViewController: UIViewController { | |
private let user: User | |
private let apiService: ApiService | |
private lazy var name = UILabel() | |
init(user: User, apiService: ApiService) { | |
self.user = user | |
self.apiService = apiService | |
super.init(nibName: nil, bundle: nil) | |
} |
NewerOlder