Skip to content

Instantly share code, notes, and snippets.

View piyush23dez's full-sized avatar

Piyush piyush23dez

  • United States
View GitHub Profile
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)
struct ContentView: View {
@State private var name = ""
var body: some View {
Form {
TextField("Enter your name", text: $name)
Text("Your name is \(name)")
}
}
}
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
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 ?? "")
}
//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 ?? "")
}
//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
//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
}
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)
}
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)
}
typealias CompletionBlock = (Data?) -> Void
protocol NetworkService {
func make(request: URLRequest, completionHandler: @escaping CompletionBlock)
}
extension URLSession: NetworkService {
func make(request: URLRequest, completionHandler: @escaping CompletionBlock) {
let task = self.dataTask(with: request) { data, _, _ in
completionHandler(data)