Skip to content

Instantly share code, notes, and snippets.

case class ListNode[+T](data: T, next: Option[ListNode[T]]) object List {
def newList[T](data: T) = newListFromNode(new ListNode(data, None))
def newListFromNode[T](node: ListNode[T]) = new List(node)
private def travse_iter[A](head:Option[ListNode[A]], fn: (ListNode[A]) => Unit): Unit = {
head match {
case Some(node) => {
fn(node)
List.travse_iter(node.next, fn)
@xudifsd
xudifsd / memory.cpp
Last active August 29, 2015 14:24
$./a.out start forking 1.75534
#include <iostream>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#define SIZE (40 * 1024 * 1024 * 1024L)
static inline double get_time_diff_double(struct timeval end, struct timeval start)
{
@xudifsd
xudifsd / safe-coding
Created July 10, 2015 02:44
avoid accidentally `git checkout .`, run as `safe-coding &`, exit with fg and ctrl-c
#!/bin/bash
while :
do
git add .
sleep 1
done
@xudifsd
xudifsd / qsort.clj
Created August 31, 2015 08:42
quick sort
(defn qsort [l]
(if (empty? l) '()
(let [f (first l)
smaller (filter #(<= % f) (rest l))
bigger (filter #(> % f) (rest l))]
(concat (qsort smaller) [f] (qsort bigger)))))
@xudifsd
xudifsd / Main.java
Last active September 30, 2015 04:30
Fibonacci
/*
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Given a sequence {an}, how many non-empty sub-sequence of it is a prefix of fibonacci sequence.
A sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
The fibonacci sequence is defined as below:
#!/usr/bin/env python
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def printTreeWithDistance(root, k):
def impl(root, path):
struct ListNode<'a, T> where T: PartialOrd + 'a {
val: &'a T,
next: Option<Box<ListNode<'a, T>>>,
}
fn reverse_list<'a, T>(head: Option<Box<ListNode<'a, T>>>) -> Option<Box<ListNode<'a, T>>>
where T: 'a + PartialOrd {
if let Some(mut p) = head {
let mut tail = None;
loop {
@xudifsd
xudifsd / tree.rs
Last active April 10, 2016 06:30
rust implementation of binary tree insertion
pub struct Tree<T> {
root: Link<T>,
size: usize,
}
type Link<T> = Option<Box<Node<T>>>;
struct Node<T> {
elem: T,
left: Link<T>,
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import traceback
import sqlite3
import logging
from Queue import Queue
from Queue import Empty
@xudifsd
xudifsd / ImmutableMap.java
Created January 24, 2018 03:58
I often found myself thinking in clojure's PersistentHashMap, this data structure is indeed marvel. I also often need this data structure in some project, and have to include the whole clojure.jar in project even if I didn't need any other functionality provided by clojure. So I clean some unnecessary code of PersistentHashMap from clojure and a…
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/