Skip to content

Instantly share code, notes, and snippets.

@modalsoul
modalsoul / bottles.py
Created February 27, 2014 03:32
Pythonで99 bottles
#!/usr/bin/python
def plural(num):
if num == 0:
return "No more bottles"
elif num == 1:
return "1 bottle"
else:
return str(num) + " bottles"
def overLylic(num):
class Lylic(num:Int) {
val currentPlural = plural(num)
val nextPlural = plural(num-1)
def plural(x:Int) = {
x match {
case 0 => "No more bottles"
case 1 => "1 bottle"
case _ => x + " bottles"
}
defmodule Crypt do
def encrypt(numbers) do
min = Enum.min(numbers)
(product(numbers, 1)/min)*(min + 1)
end
def product([h|t],res), do: product(t, res * h)
def product([],res), do: res
end
IO.puts to_string(Crypt.encrypt([1,2,3]))
object Catalan {
def myCatalan(m:Int, n:Int, accum:Int):Int = {
(m>0 && n>0, m==n) match {
case (false, _) => accum
case (_, true) => myCatalan(m, n-1, accum + 1)
case (_, false) => myCatalan(m-1, n, accum + 1)
}
}
}
@modalsoul
modalsoul / ExtendedOption.hx
Created July 26, 2014 04:36
Option.getOrElse() like Scala in Haxe.
import haxe.ds.Option;
class ExtendedOption {
public static function getOrElse<T>(opt:Option<T>, defValue:T) {
return switch(opt) {
case Some(v): v;
case None: defValue;
}
}
}
@modalsoul
modalsoul / partialSum.scala
Created September 25, 2014 13:55
Partial Sum Problem (n < 20)
def partialSum(n:Int, a:Seq[Int], k:Int, i:Int = 0, sum:Int = 0):Boolean = {
if(i == n) sum == k
else partialSum(n, a.tail, k, i+1, sum+a.head) || partialSum(n, a.tail, k, i+1, sum)
}
val a = Seq(1,2,4,7)
println(partialSum(a.length, a, 13))
println(partialSum(a.length, a, 15))
@modalsoul
modalsoul / PrimeNum.scala
Created April 8, 2015 02:54
Scalaで素数判定その4
import scala.math.sqrt
object PrimeNum {
def main(args:Array[String]):Unit = {
val num:Long = scala.io.StdIn.readLong
if(isPrime(num)) println(num + " is Prime Number.")
else println(num + " is NOT Prime Number.")
}
def fizzbuzz(args:Int){
(1 to args).foreach({ i =>
if((i%3)==0 && (i%5)==0)
println("FizzBuzz")
else if(i % 3 == 0)
println("Fizz")
else if(i % 5 == 0)
println("Buzz")
else
println(i)
@modalsoul
modalsoul / emulateButtonClick.java
Created May 20, 2012 14:41
Android UI test sample emulate button click
// Emulate button click...
Button button = (Button)mCurrentActivity.findViewById(jp.modal.soul.uiTestSample.R.id.button1);
TouchUtils.clickView(this, button);
@modalsoul
modalsoul / getAndCheckView2.java
Created May 20, 2012 14:46
Android UI test sample assert transited state
// Check transited state...
textView = (TextView)mCurrentActivity.findViewById(jp.modal.soul.uiTestSample.R.id.transitedTextView);
assertEquals(textView.getText().toString(), "Transited.");