Skip to content

Instantly share code, notes, and snippets.

View 17-225401.scala
def rpn(str: String): Int = rpnPrime(str.split(' ').toList, Nil)
def rpnPrime(str: List[String], stack: List[Int]): Int = (str, stack) match {
case ("+"::t, y::x::zs) => rpnPrime(t, (x + y) :: zs)
case ("-"::t, y::x::zs) => rpnPrime(t, (x - y) :: zs)
case ("*"::t, y::x::zs) => rpnPrime(t, (x * y) :: zs)
case ("/"::t, y::x::zs) => rpnPrime(t, (x / y) :: zs)
case ("%"::t, y::x::zs) => rpnPrime(t, (x % y) :: zs)
case ( n::t, zs) => rpnPrime(t, (n.toInt) :: zs)
case _ => stack.head
View AlgebraCalculation.scala
import scala.language.implicitConversions
sealed trait Expr {
def +(that: Expr): Expr = Add(List(this, that))
def *(that: Expr): Expr = Mul(List(this, that))
def +(that: Int ): Expr = this + N( that)
def -(that: Int ): Expr = this + N(-that)
def *(that: Int ): Expr = this * N( that)
View AlgebraCalculation.scala
import scala.language.implicitConversions
// R is derived from Rational with some fixes.
// https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6
case class R(numer: Int, denom: Int) extends Ordered[R] {
def reduce: R = {
def gcd(x: Int, y: Int): Int = {
if (x == 0) y
else if (x < 0) gcd(-x, y)
View AlgebraCalculation.scala
import scala.language.implicitConversions
// R is derived from Rational with some fixes.
// https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6
case class R(n: Int, d: Int) extends Ordered[R] {
private def gcd(x: Int, y: Int): Int = {
if (x == 0) y
else if (x < 0) gcd(-x, y)
else if (y < 0) -gcd( x, -y)
View AlgebraCalculation.scala
sealed trait Expr
case class N(n: Int) extends Expr
case class Var(x: String, a: Int, n: Int) extends Expr
case class Add(n: Expr*) extends Expr
case class Mul(n: Expr*) extends Expr
def x(a: Int, n: Int): Var = {
Var("x", a, n)
}
View 11-233937.scala
sealed trait Expr
case class N(n: Int) extends Expr
case class Var(x: String, a: Int, n: Int) extends Expr
case class Add(n: Expr*) extends Expr
case class Mul(n: Expr*) extends Expr
def add(xs: List[Expr]): Expr = xs match {
case List() => N(0)
case List(x) => x
case _ => Add(xs.toArray: _*) // Add(WrappedArray(N(1), N(2), Var(x,2,2)))
View AlgebraCalculation.scala
sealed trait Expr
case class N(n: Int) extends Expr
case class Add(n: Expr*) extends Expr
case class Mul(n: Expr*) extends Expr
def str(e: Expr): String = e match {
case N(x) => x.toString
case Add() => ""
case Add(x) => str(x)
case Add(x, xs@_*) => str(x) ++ "+" ++ str(Add(xs: _*))
View deep_cpy.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct A{
char s[10000];
A() {
strcpy(s, "123");
}
View param.cpp
void inc(int &x) {
++x;
}
void inc(int *x) {
++*x;
}
View makeexe.2014091401.js
var fs = require("fs");
function convLE(len, v) {
var ret = "";
for (var i = 0; i < len; ++i) {
ret += String.fromCharCode(v & 0xff);
v >>= 8;
}
return ret;
}