Skip to content

Instantly share code, notes, and snippets.

View ambantis's full-sized avatar

Alexandros Bantis ambantis

View GitHub Profile
// scala 2.13.4
import $ivy.`org.scalacheck::scalacheck:1.15.3`
import org.scalacheck.Gen
import scala.collection.AbstractIterator
// 11 -> 22 -> 33
class CircularIterator extends AbstractIterator[Int] {
function deepEquals(thing1, thing2) {
let typeOfFun = (x) => (x) => (x == null ? "null" : typeof x);
const type1 = typeOfFun(thing1);
const type2 = typeOfFun(thing2);
let isNull1 = type1 == "null";
let isNull2 = type2 == "null";
if (isNull1 && isNull2) {
@ambantis
ambantis / personValidation.scala
Last active August 9, 2018 14:39
Data validation using just Scala
/* sealed */ trait Color {
def isWarm: Boolean
def isCool: Boolean = !isWarm
}
object Color {
case object Red extends Color { val isWarm = true }
case object Yellow extends Color { val isWarm = true }
case object Orange extends Color { val isWarm = false }
case object Green extends Color { val isWarm = false }
public class TicTacToe {
private int len;
private int[][] board;
public TicTacToe(int size) {
len = size;
board = new int[len][len];
}
public Character winner() {
import scala.collection.mutable.ArrayBuffer
case class TicTacToe(len: Int) {
private val board = ArrayBuffer.fill[Option[Char]](len,len)(None)
def move(player: Char, row: Int, col: Int): Unit = ???
def winner: Option[Char] =
(rowWinner ++ colWinner ++ diaWinner)
.filter(_.isDefined)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OpenAFile6 {
public String getLine(String fileName, int n)
throws IOException, FileNotFoundException, NullPointerException {
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class OpenAFile7 {
import java.io.{IOException, FileNotFoundException }
import scala.util.{Failure, Success, Try}
import scala.io.{Source,Codec}
object FileUtils {
def withFile(name: String, codec: Codec = Codec.UTF8)
(f: Source => String): Try[String] = {
var src: Source = null
try {
/**
* Given a sorted array of n integers that has been rotated an unknown number of times,
* write code to find an element in the array. You may assume that the array was originally
* sorted in increasing order.
*
* Example
* Input: find 5 in { 15, 16, 19, 20, 25, 1, 2, 4, 5, 7, 10, 14 }
* Output: 8 (index of 5 in the array)
*/
object FindInRotatedArray {
/**
* Given a sorted array of n integers that has been rotated an unknown number of times,
* write code to find an element in the array. You may assume that the array was originally
* sorted in increasing order.
*
* Example
* Input: find 5 in { 15, 16, 19, 20, 25, 1, 2, 4, 5, 7, 10, 14 }
* Output: 8 (index of 5 in the array)
*/
public class FindInRotatedArray<E extends Comparable<E>> {