Skip to content

Instantly share code, notes, and snippets.

@johnelf
johnelf / quickSort.java
Created April 12, 2021 04:57
quick sort with arrays
package sorting;
public class QuickSortWithArray {
public static void main(String[] args) {
int[] arrs = new int[10];
for (int i = 0; i < 10; i++) {
arrs[i] = (int) (Math.random() * 100);
}
@johnelf
johnelf / rand_what_to_eat.rb
Last active December 4, 2018 02:35
What to eat?
def what_to_have(list, count)
result = {}
(1..count).each do |n|
key = list.sample
if result[key]
result[key] = result[key] + 1
else
result[key] = 1
end
end
@johnelf
johnelf / rfc793
Created March 11, 2016 04:27
TCP
Transmission Control Protocol
Functional Specification
+---------+ ---------\ active OPEN
| CLOSED | \ -----------
+---------+<---------\ \ create TCB
| ^ \ \ snd SYN
@johnelf
johnelf / bbs.spec
Last active August 29, 2015 14:19
Spec file
Name: bbs
Summary: BBS
Version: 1.0
Release: 1
URL: example.com
Source: %{name}-%{version}-%{release}.tar.gz
License: MIT
Group: Applications/Internet
BuildArch: noarch
Requires: java-1.7.0-openjdk, java-1.7.0-openjdk-devel
@johnelf
johnelf / simple_rsa.java
Created September 1, 2014 05:41
简单RSA算法
package com.dcl;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static Map int2Char = new HashMap<Integer, Character>() {{
put(0, 'a');
type SoccerFixtures = List[(String, String)]
def doCombine(teams: List[String], res: SoccerFixtures): SoccerFixtures = teams match {
case Nil => res
case pick :: others =>
doCombine(others, others.map(o => (pick, o)) ::: res)
}
doCombine(List("a", "b", "c", "d"), List()).sorted
def combine(teams: List[String]) = doCombine(teams, List())
@johnelf
johnelf / contravariant.scala
Last active August 29, 2015 14:02
Scala类型系统
class Pen {
def getInfo = "a pen"
}
class Chalk extends Pen {
override def getInfo = "a pencil that can be erased by eraser"
}
class WhiteBoardPen extends Pen {
override def getInfo = "a white board pen that can write on window wall"
def printList(args: TraversableOnce[_]): Unit = {
args.foreach(print)
println()
}
def count(ls: List[Int]): Unit = {
def doCount(src: List[Int], res: List[Int]): Unit = src match {
case Nil => printList(res)
case _ => for (element <- src) {
doCount(src.diff(List(element)), res :+ element)
@johnelf
johnelf / scala-exercises-for-beginner.scala
Last active August 29, 2015 13:57
The following exercises have come from of a course that I give on Functional Programming. I have assigned them difficulty ratings to make it a bit more exciting. Download the compilable source code from here or find it below. Enjoy :)
// You are not permitted to use these List methods:
// * length
// * map
// * filter
// * ::: (and variations such as ++)
// * flatten
// * flatMap
// * reverse (and variations i.e. reverseMap, reverse_:::)
// This also means you are not permitted to use for-comprehensions on Lists.
// You are permitted to use the functions you write yourself. For example, Exercise 2 may use Exercise 1 or Exercise 3.
@johnelf
johnelf / week-1.scala
Last active August 29, 2015 13:57
Assignment for Scala
package recfun
import common._
object Main {
def main(args: Array[String]) {
println("Pascal's Triangle")
for (row <- 0 to 10) {
for (col <- 0 to row)
print(pascal(col, row) + " ")
println()