Skip to content

Instantly share code, notes, and snippets.

View hpcx82's full-sized avatar

hpcx hpcx82

  • Shanghai
View GitHub Profile
@hpcx82
hpcx82 / gcd.scala
Created June 5, 2012 13:06
Great common dividor
object sqrtTest extends App
{
def sqrt(x: Double) =
{
def sqrtIter(guess: Double): Double =
{
if(isGoodEnough(guess)) guess
else sqrtIter(improve(guess))
}
def improve(guess: Double) =
@hpcx82
hpcx82 / sqrt.scala
Created June 5, 2012 13:02
牛顿迭代法求平方根
object sqrtTest extends App
{
def sqrt(x: Double) =
{
def sqrtIter(guess: Double): Double =
{
if(isGoodEnough(guess)) guess
else sqrtIter(improve(guess))
}
def improve(guess: Double) =
@hpcx82
hpcx82 / loopunless.scala
Created June 5, 2012 12:49
Create loops using scala function
object LoopUnlessTest extends App
{
def loop(body: => Unit): LoopUnlessCond =
new LoopUnlessCond(body)
protected class LoopUnlessCond(body: => Unit)
{
def unless(cond: => Boolean)
{
body
@hpcx82
hpcx82 / Node.scala
Created June 4, 2012 15:19
Use scala to exchange left/right children of a binary tree
package handson
class Node(l: Node, r: Node, n: String)
{
var left = l
var right = r
var name = n
}
@hpcx82
hpcx82 / printBrackets.pl
Created June 4, 2012 15:18
#algorithm# Given the number, print all pairs of '(' and ')'
use strict;
use warnings;
my @output;
sub printBrackets($$)
{
my ($nLeft, $nRight) = @_;
# recursion end condition
if($nLeft == 0 && $nRight == 0)
{
@hpcx82
hpcx82 / data.c
Created May 31, 2012 08:24
extern of array
extern char array[] = {1, 2, 3, 4};
@hpcx82
hpcx82 / p4change.sh
Created May 30, 2012 09:49
Create a changelist from command line
#
# create a named changelist with certain description
#
# Usages:
# p4change
# p4change "This is my changelist"
#
if [ ! "$1" ]
@hpcx82
hpcx82 / g++47.sh
Created May 30, 2012 09:46
Compile & Run C++/Java/Scala as a script language
export exe=`echo $1 | awk -F. '{print $1}'`
#make sure the g++ version is 47
g++ $1 -std=c++11 -o $exe
./$exe
@hpcx82
hpcx82 / gist:2831226
Created May 29, 2012 22:44
给你一个数组,找出这个数组中是否有两个数的和为某个指定的值
// 给你一个数组,找出这个数组中是否有两个数的和为某个指定的值
// int array[] = {1, 4, 12, -1, 6, 4},
// sum = 10, return true;
// sum = 100, return false;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
public final class ArraySum
@hpcx82
hpcx82 / assignment-operator-hidden.cpp
Created May 29, 2012 02:15
Demonstrate the operator hidden problem and solution
#include "stdafx.h"
#include <iostream>
using namespace std;
/**
* operator= is generated implicitly by compiler, and derived class will hide ones from base class.
* If you want to use base class's operator=, there are 2 solutions.
*