Skip to content

Instantly share code, notes, and snippets.

@amuradyan
amuradyan / ApplesAndOranges.scala
Created December 9, 2020 15:49
Hackerrank `Apples and oranges` problem
import java.io._
import java.math._
import java.security._
import java.text._
import java.util._
import java.util.concurrent._
import java.util.function._
import java.util.regex._
import java.util.stream._
@amuradyan
amuradyan / NumberLineJumps.scala
Created December 9, 2020 16:24
Hackerrank `Number Line Jumps` problem
import java.io._
import java.math._
import java.security._
import java.text._
import java.util._
import java.util.concurrent._
import java.util.function._
import java.util.regex._
import java.util.stream._
@amuradyan
amuradyan / BreakingTheRecords.scala
Last active December 10, 2020 22:02
Hackerrank `Breaking the Records` problem
import java.io._
import java.math._
import java.security._
import java.text._
import java.util._
import java.util.concurrent._
import java.util.function._
import java.util.regex._
import java.util.stream._
@amuradyan
amuradyan / TheBirthdayBar.scala
Created December 12, 2020 11:49
Hackerrank `The Birthday Bar` problem
// https://www.hackerrank.com/challenges/the-birthday-bar/problem
import scala.io._
import scala.annotation.tailrec
object Solution {
// Complete the birthday function below.
def birthday(s: Array[Int], d: Int, m: Int): Int = {
@amuradyan
amuradyan / DayOfTheProgrammer.scala
Created December 29, 2020 19:46
Hackerrank `Day of the Programmer` problem
import scala.io._
trait Date {
def toDDMMYYYY: String = ???
}
case class ConcreteDate(day: Int, month: String, year: Int) extends Date {
override def toDDMMYYYY = s"$day.$month.$year"
}
case object EmptyDate extends Date
@amuradyan
amuradyan / MigratoryBirds.scala
Created December 30, 2020 12:17
Hackerrank `Migratory Birds` problem
// https://www.hackerrank.com/challenges/migratory-birds/problem
//
// Input Output
// +-----------------------+----------+
// | 6 | 4 |
// | 1 4 4 4 5 3 | |
// +-----------------------+----------+
// | 11 | 3 |
// | 1 2 3 4 5 4 3 2 1 3 4 | |
// +-----------------------+----------+
import scala.collection.mutable.Stack
class Peg(val disks: Stack[Int], val name: String)
def solveHanoi_1(n: Int): Unit = {
val A = Peg(Stack[Int](1 to n: _*), "A")
val B = Peg(Stack[Int](), "B")
val C = Peg(Stack[Int](), "C")
val evenSeq = Seq((A, B), (A, C), (B, C))
@amuradyan
amuradyan / BasicScala.sc
Last active November 6, 2021 10:31
Գրադարան Քեմփ - Ստեփանավան
// Purpose: show scala basic features in terms of grammar, readability, shortness, easy translation from human to machine
// How many of you know a programming language?
// Opening with the fizzbuzz
for (i <- Range.inclusive(1, 16)) {
println(
if (i % 3 == 0 && i % 5 == 0) "FizzBuzz"
else if (i % 3 == 0) "Fizz"
else if (i % 5 == 0) "Buzz"
else i
@amuradyan
amuradyan / ChurchEncodings.worksheet.sc
Last active May 24, 2022 18:22
Some Church encodings
type Boolian = (Any, Any) => Any
val troo = (a: Any, _: Any) => a
val fols = (_: Any, b: Any) => b
// Logic
val not = (b: Boolian) => b(fols, troo)
not(troo) == fols
// How to Write Controllable Futures in Scala | Rock the JVM
// https://www.youtube.com/watch?v=72kurKY7fX8
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Promise
val aFuture = Future {
42
}