Skip to content

Instantly share code, notes, and snippets.

View abhin4v's full-sized avatar

Abhinav Sarkar abhin4v

View GitHub Profile
@abhin4v
abhin4v / lyrics.js
Last active August 29, 2015 17:35
Provides an ubiquity command: "lyrics", to find the lyrics for a song. Type the song in the form "Song <by> Artist". Provides suggestions as you type. Uses lyricwiki.org API for fetching lyrics and last.fm webservices API for providing suggestions.
/*
* Author: Abhinav Sarkar <[email protected]>
* License: MPL 1.1
*
* Provides an ubiquity command: "lyrics", to find the lyrics for a song.
* Usage: Type the song in the form "Song <by> Artist". Provides suggestions as you type.
* Uses lyricwiki.org API for fetching lyrics and last.fm webservices API for
* providing suggestions.
*/
@abhin4v
abhin4v / Fibonacci.java
Created November 24, 2009 18:46
Python-style Generator in Java
package net.abhinavsarkar.util;
/**
* A (infinite) Fibonacci number generator.
*
* @author Abhinav Sarkar
*/
public class Fibonacci extends Generator<Integer> {
@abhin4v
abhin4v / WritableFileUrl.java
Created May 4, 2010 18:24
A writable file type URL
package net.abhinavsarkar.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
@abhin4v
abhin4v / SPOJ-PRIME1.scala
Created May 13, 2010 17:40
My solution to SPOJ problem PRIME1 (https://www.spoj.pl/problems/PRIME1/) in Scala.
import Stream.cons
import java.io.{BufferedWriter, PrintWriter}
import Console.{readInt, readLine}
import Math.{sqrt, ceil}
import scala.collection.mutable.{HashMap => MHashMap}
/*
My solution to SPOJ problem PRIME1 (https://www.spoj.pl/problems/PRIME1/) in Scala.
Unfortunately it takes more time than expected and hence fails in SPOJ submission.
*/
@abhin4v
abhin4v / EvaluatedStream.scala
Created May 15, 2010 13:51
How to get the currently evaluated items in a Stream in Scala
import annotation.tailrec
import collection.immutable.List
import collection.immutable.Stream.{Empty,Cons}
object EvaluatedStream {
def evaluatedItems[T](stream: => Stream[T]): List[T] = {
@tailrec
def inner(s: => Stream[T], acc: List[T]): List[T] = s match {
case Empty => acc
@abhin4v
abhin4v / SPOJ-PRIME1-UsingFutures.scala
Created May 16, 2010 13:33
Solution to SPOJ problem PRIME1 using Futures
import collection.immutable.HashMap
import Stream.cons
import java.io.{BufferedWriter, PrintWriter}
import Console.{readInt, readLine}
import Math.{sqrt, ceil}
import concurrent.ops.future
import concurrent.FutureTaskRunner
import actors.scheduler.ExecutorScheduler
import java.util.concurrent.Executors.newFixedThreadPool
import java.util.concurrent.CountDownLatch
@abhin4v
abhin4v / SpojPrime1Actors.scala
Created May 16, 2010 18:40
Solution to SPOJ PRIME1 using Scala Actors
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.locks.ReentrantLock
import collection.immutable.HashMap
import Stream.cons
import java.io.{BufferedWriter, PrintWriter}
import Console.{readInt, readLine}
import Math.{sqrt, ceil}
import actors.Actor
import collection.mutable.ListBuffer
@abhin4v
abhin4v / SpojCmpls.scala
Created June 3, 2010 13:24
Solution to SPOJ problem "Complete the Sequence!" in Scala. Written in Scala 2.8. https://www.spoj.pl/problems/CMPLS/
/* Solution to SPOJ problem "Complete the Sequence!"
* (https://www.spoj.pl/problems/CMPLS/) in Scala. Written in Scala 2.8.
*/
object SpojCmpls {
def main(args: Array[String]) {
val noOfTestCases: Int = readInt
1 to noOfTestCases foreach { i =>
val input = readLine.trim.split(" ").map { _ toInt }
val s = input(0)
(defn capitalize [#^String s]
"capitalizes String s"
(if (and s (not (zero? (count s))))
(apply str (conj (rest s) (Character/toUpperCase (.charAt s 0))))
s))
(defn trim [s]
"trims leading and trailing whitespace in the String s"
(let [trim-leading
(fn [s]
(ns net.abhinavsarkar
(:use
[clojure.contrib.math :only (sqrt ceil)]
[clojure.contrib.import-static]))
(import-static java.lang.Integer parseInt)
(declare primes)
(defn- divisible? [x y] (zero? (mod x y)))