Skip to content

Instantly share code, notes, and snippets.

View davejlin's full-sized avatar

Dave Lin davejlin

  • Northrop Grumman
  • Columbia, Maryland
View GitHub Profile
@davejlin
davejlin / Employees-Friends-Divisions Association Mapping
Created October 29, 2017 16:35
Map the associations between employees, their friends and divisions
// Problem: Employees-Friends-Divisions Association Mapping
// Input 1. employeesInput: 3 comma separated data fields: id, name, division
// Input 2. friendsInput: 2 comma separated data fields: ids of 2 employees who are friends
//
// Task 1: Associate each employee with his list of friends
// Task 2: Count the following:
// 1. The number of employees in each division
// 2. The number of employees per division who have friends outside of his division
@davejlin
davejlin / Rectangles Inside Image Array
Created October 29, 2017 16:09
Find the rectangles inside the input image array
// Problem: Find the rectangles inside the input image array
// Input: An array representing an image, where 1 represents pixel on, 0 off
// Task 1: Find the rectangle (top-left coordinate, width, height) in an array containing a single rectangle
// Task 2: Find all the rectangles inside an array containing multiple rectangles
// image1 expected result: top-left = (2,3), width = 4, height = 3
let image1 = [
[1,1,1,1,1,1,1,1],
@davejlin
davejlin / Queue with Two Stacks in Swift
Last active March 29, 2023 09:21
Implement Queue With Two Stacks in Swift
struct Stack<Element> {
var items = [Element]()
var count : Int {
return items.count
}
mutating func push(_ item: Element) {
items.append(item)
}
@davejlin
davejlin / Route Encoder
Last active April 28, 2017 19:02
Route encoder for Multi-Stop Routes (C#)
using ICSharpCode.SharpZipLib.Tar;
using ICSharpCode.SharpZipLib.GZip;
public class RouteEncoder
{
private static ILogger Logger = LoggerExtensions.GetLogger(nameof(RouteEncoder));
public void EncodeRoute(Route route, Stream responseStream)
{
string xmlContent = ComposeRouteXMLContent(route.DestinationLocations);
if (xmlContent != null)
@davejlin
davejlin / EncodeRoute
Last active April 28, 2017 18:45
Encode Route Waypoint Data to tar.gz (C# )
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
public struct WayPoint
{
public string address1;
public string address2;
public GeoCoordinate geoCoord;
}
@davejlin
davejlin / TimeUtility
Created March 14, 2017 18:19
Time Utilities
import Foundation
class TimeUtility {
static let iso8601DateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
static let timeZoneOffsetFormat = "ZZZZZ"
static let en_USNSLocaleID = "en_US_POSIX"
static func covertToISO8601String(_ date: Foundation.Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Foundation.Locale(identifier: TimeUtility.en_USNSLocaleID)
@davejlin
davejlin / TripType
Last active April 21, 2017 18:07
TripType
import Foundation
enum TripType : CustomStringConvertible, Equatable {
case user
case calendar(CalendarTripSubtype)
case predicted(PredictedTripSubtype)
case shared
case unknown
static func fromString(_ input: String?) -> TripType {
@davejlin
davejlin / Mock Predicted Trips and Locations
Last active April 21, 2017 18:08
Mock predicted trips and locations in SyncResponseModelBuilder
import ObjectMapper
func handleResponse(_ responseObject: LocationTripResponseMappable, source: AnalyticsConstants.LOCATION_TRIP_SYNC_SOURCE, requestParameters: [String:Any]) {
let responseObject = createMockResponse()
...
}
fileprivate func createMockResponse() -> LocationTripResponseMappable {
let id1 = "id1"
let id2 = "id2"
@davejlin
davejlin / ApplyLimit Calendar
Last active April 21, 2017 18:09
applyLimit limit number of calendar entries based on calendar date with performance results
// 1.361s
func applyLimit(toData data: [EkEventDao]) -> [EkEventDao] {
var datesDict:[String:Int] = [:]
var limitedData: [EkEventDao] = []
for datum in data {
let dateKey = datum.startDate.startOfDay.description
let value = datesDict[dateKey]
if value == nil { datesDict[dateKey] = 0 }
guard datesDict[dateKey]!<Const.MAX_ENTRIES_PER_DAY else { continue }
@davejlin
davejlin / Delayer
Last active April 21, 2017 18:09
Delayer for iOS Swift
import Foundation
protocol DelayerProtocol {
func delay(forSeconds seconds: Double, closure: () -> Void)
}
class Delayer: DelayerProtocol {
func delay(forSeconds seconds: Double, closure: () -> Void) {
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {