Skip to content

Instantly share code, notes, and snippets.

@lazyval
lazyval / sortByBits.scala
Created September 30, 2012 15:20
Sorting integers by the numbers of bits set
/*
You have given n numbers from 1 to n. You have to sort numbers with increasing number of set bits.
Example: n = 5 => Output: 1,2,4,3,5
*/
def numberOfBitsSet(i: Int) = {
// http://en.wikipedia.org/wiki/Hamming_weight implementation
val j = i - ((i >> 1) & 0x55555555);
#!/usr/bin/python
# -*- coding: utf-8 -*-
filename = "dict.csv"
lookup = {}
with open(filename,"r") as file:
for line in file:
(key, value) = line.split(" ")[0:2]
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
dict = {
0: "часов",
1: "час",
2: "часа",
implicit val bdStringView = StringView.of[BigDecimal] as { _.toString }
def makeStringValue[T](implicit sv: StringView[T]) = new StringValue {
def getString(value: AnyRef) = sv(value.asInstanceOf[T])
}
bdStringValue = makeStringValue[BigDecimal]
strStringValue = makeStringValue[String]
qtyStringValue = makeStringValue[Long](StringView.of[Long] as { _.toString }
@lazyval
lazyval / dirwatcher.scala
Created August 15, 2012 16:16 — forked from eberle1080/dirwatcher.scala
Recursive directory watcher in Scala
/**
* dirwatcher.scala
*
* Uses the Java 7 WatchEvent filesystem API from within Scala.
* Adapted from:
* http://download.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java
*
* @author Chris Eberle <[email protected]>
* @version 0.1
*/
@lazyval
lazyval / Build.scala
Created August 1, 2012 23:20
sbt config
// project/Build.scala
import sbt._
import Keys._
import AndroidKeys._
object General {
val settings = Defaults.defaultSettings ++ Seq(
scalaVersion := "2.9.2"
)
CREATE OR REPLACE TRIGGER UPD_INF AFTER
UPDATE OF SALARY ON hr.employees FOR EACH ROW
DECLARE
dif NUMBER;
str VARCHAR2(3000);
e hr.employees%ROWTYPE;
BEGIN
dif := TRUNC((TRUNC(sysdate) - TRUNC(e.hire_date))/365,0);
e := :old;
str := 'Номер сотрудника:' || e.employee_id ||
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void readFromFile() {
string line;
ifstream myfile ("tolstoy.txt");
@lazyval
lazyval / PictureLoader.scala
Created May 11, 2012 20:07
Casbah's GridFS upsert operation
private def upsert(filename: String, extension: String, is: InputStream) {
gridfs.findOne(filename) match {
case Some(_) => println(filename + " is already in database")
case None =>
gridfs(is) {
file =>
file.filename = filename
file.contentType = "image/" + extension
}
}
package com.github.omnomnom
import java.util.concurrent.Semaphore
import concurrent.Lock
object SleepingBarber extends App {
val NCustomers = 12
val NChairs = 5
val CutTime = 10
val VisitInterval = 5