This file contains hidden or 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 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 | |
} |
This file contains hidden or 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 | |
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 } | |
} | |
} |
This file contains hidden or 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 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() |
This file contains hidden or 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 NetworkManager : NetworkManagerProtocol { | |
var networkConfiguration : NetworkConfigurationProtocol | |
init(networkConfiguration : NetworkConfigurationProtocol) { | |
self.networkConfiguration = networkConfiguration | |
} | |
func makeRequest(request: Request) -> Promise<Void> { | |
return firstly { | |
makeRequestInternal(request: request) |
This file contains hidden or 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 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 | |
}) |
This file contains hidden or 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 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 { |
This file contains hidden or 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 Dog : Animal { | |
func eat() { | |
print("dog eats") | |
} | |
func talk() { | |
print("dog talk") | |
} | |
} | |
protocol Animal { |
This file contains hidden or 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
#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; |
This file contains hidden or 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 | |
// 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] |