This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TicTacToe { | |
private int len; | |
private int[][] board; | |
public TicTacToe(int size) { | |
len = size; | |
board = new int[len][len]; | |
} | |
public Character winner() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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>> { |
NewerOlder