Skip to content

Instantly share code, notes, and snippets.

import Foundation
// Complete the arrayManipulation function below.
func arrayManipulation(n: Int, queries: [[Int]]) -> Int {
var items = Array(repeating: 0, count: n)
print("\(items)")
for query in queries {
let s = query[0] - 1
let e = query[1]
let k = query[2]
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long int N,K,p,q,sum,i,j,max=0,x=0;
@gabrielepalma
gabrielepalma / static.swift
Last active December 18, 2018 15:24
Static/dynamic dispatch
struct Dog : Animal {
func eat() {
print("dog eats")
}
func talk() {
print("dog talk")
}
}
protocol Animal {
@gabrielepalma
gabrielepalma / auth.swift
Created January 5, 2019 04:16
Authentication
func loginUser(_ request: Request) throws -> Future<AuthorizedUser>
{
let minimumTimeToLiveForRefreshToken : Double = 60 * 60 * 24 * 30 // One month
let user = try request.requireAuthenticated(User.self)
guard let userId = user.id else {
throw Abort(HTTPResponseStatus.internalServerError)
}
let accessTokenString = try user.createJwt(usage: JWTConfig.AccessToken.usage, expiration: JWTConfig.AccessToken.expiration).0
let promise = request.eventLoop.newPromise(of: AuthorizedUser.self)
DispatchQueue.global().async {
func store(_ req: Request) -> Future<Bool> {
return req.eventLoop.future(true)
}
func submit(_ req: Request) throws -> Future<HTTPStatus> {
return try req.content.decode(Bool.self).then({ res -> EventLoopFuture<Bool> in
return store(req)
}).map({ bool -> HTTPStatus in
.ok
})
@gabrielepalma
gabrielepalma / manager.swift
Last active January 10, 2019 08:41
NetworkManager
class NetworkManager : NetworkManagerProtocol {
var networkConfiguration : NetworkConfigurationProtocol
init(networkConfiguration : NetworkConfigurationProtocol) {
self.networkConfiguration = networkConfiguration
}
func makeRequest(request: Request) -> Promise<Void> {
return firstly {
makeRequestInternal(request: request)
@gabrielepalma
gabrielepalma / wait.swift
Created January 11, 2019 12:48
Using wait in future
func deleteStuff(_ request: Request) throws -> Future<SomeKindOfType>
{
let promise = request.eventLoop.newPromise(of: SomeKindOfType.self)
DispatchQueue.global().async {
guard let decodedBody = try? request.content.decode(ExpectedRequestBody.self).wait() else {
promise.fail(error: Abort(HTTPResponseStatus.badRequest))
return
}
try? SomeFluentType.query(on: request).filter(somefilter).delete().wait()
import Foundation
extension Sequence where Element: Hashable {
public func mode() -> [Element] {
var freq = [Element:Int]()
self.forEach { freq[$0] = (freq[$0] ?? 0)+1 }
var max = freq.values.max()
return freq.filter { $0.1 == max }.map { $0.0 }
}
}
class Solution {
func inorderTraversal(_ root: TreeNode?) -> [Int] {
var stack: [(TreeNode, Bool)] = (root != nil) ? [(root!, true)] : []
var output:[Int] = []
while let (node, unvisited) = stack.popLast() {
guard unvisited else {
output.append(node.val)
print(node.val)
continue
}