Skip to content

Instantly share code, notes, and snippets.

@qingant
qingant / contest.js
Last active February 15, 2020 03:36
const assignVehicles = (vehicles, drivers) => {
vehicles = JSON.parse(JSON.stringify(vehicles))
drivers = JSON.parse(JSON.stringify(drivers))
vehicles.sort((a, b) => a.year - b.year)
drivers.sort((a, b) => b.age - a.age)
// console.log('Sorted:', vehicles, drivers)
let result = {
vehicles: {},
drivers: {}
@qingant
qingant / range.js
Last active October 10, 2022 12:35
// Problem Set below:
// Task: Implement a class named 'RangeList'
// A pair of integers define a range, for example: [1, 5). This range includes integers: 1, 2, 3, and 4.
// A range list is an aggregate of these ranges: [1, 5), [10, 11), [100, 201)
/*
RangeList is a strict ordered range list:
1. For any [a, b] in RangeList, a < b
2. For any [a1, b1], [a2, b2] in RangeList with index i, j, given i < j, a1 < b1 < a2 < b2
const main = async () => {
console.log('Hello World');
}
if (import.meta.main) {
main()
}
import { * } from './test.ts';
console.log('This is Test1');
import { encode, RedisValueOf, RedisParser, show } from "https://raw.githubusercontent.com/qingant/tiny-redis/master/mod.ts";
import { parse as argParse } from "https://deno.land/std/flags/mod.ts";
const { args } = Deno;
const config = argParse(args);
const opts = {
port: config.p || 6666,
hostname: config.h || "127.0.0.1"
};
type RedisString = string;
type RedisBulkString = string;
type RedisNumber = number;
type RedisArray = RedisValue[];
type RedisNil = 'nil';
type RedisError = string;
type RedisValue = RedisString|RedisBulkString|RedisNumber|RedisArray|RedisNil|RedisError;
#[derive(Debug, Clone)]
pub enum RValue {
RStr(String),
// RBulkString(ByteString),
RNumber(f64),
Error(String),
RList(Vec<RValue>),
Nil,
}
export interface RedisString {
tag: "RedisString";
value: Uint8Array;
} // binary safe byte string
export interface RedisBulkString {
tag: "RedisBulkString";
value: Uint8Array;
}
const _encode = (value: RedisValue): Uint8Array | Uint8Array[] => {
switch (value.tag) {
case "RedisNumber":
const v = `:${value.value}\r\n`;
return stringToBuffer(v);
case "RedisError":
return packRedisString(value.value, "-");
case "RedisString":
return packRedisString(value.value, "+");
case "RedisBulkString":
@qingant
qingant / peano.py
Created February 8, 2022 10:53
Peano Natural Numbers in Python
'''
Peano Natural Numbers ( https://en.wikipedia.org/wiki/Peano_axioms )
'''
class Nat(object):
def __init__(self, prev=None):
self.prev = prev
def __eq__(self, other) -> bool:
if self.prev is None and other.prev is None: