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
val n = 9 | |
val s = Math.sqrt(n).toInt | |
type Board = IndexedSeq[IndexedSeq[Int]] | |
def solve(board: Board, cell: Int = 0): Option[Board] = (cell%n, cell/n) match { | |
case (r, `n`) => Some(board) | |
case (r, c) if board(r)(c) > 0 => solve(board, cell + 1) | |
case (r, c) => | |
def guess(x: Int) = solve(board.updated(r, board(r).updated(c, x)), cell + 1) | |
val used = board.indices.flatMap(i => Seq(board(r)(i), board(i)(c), board(s*(r/s) + i/s)(s*(c/s) + i%s))) |
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 {ChatCompletionRequestMessageRoleEnum as Role, OpenAIApi} from "openai"; | |
import {Configuration as OpenAIConfig} from "openai/dist/configuration.js"; | |
const OPENAI_API_KEY = 'XXXXXXXXXXXXXX' | |
const openai = new OpenAIApi(new OpenAIConfig({apiKey: OPENAI_API_KEY})) | |
const ask = (query) => { | |
return openai.createChatCompletion({ | |
model: 'gpt-3.5-turbo', | |
max_tokens: 2048, |
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
class NorvigSpellChecker(corpus: String, alphabet: Seq[Char] = 'a' to 'z', level: Int = 2) { | |
val words = s"[${alphabet.head}-${alphabet.last}]+".r.findAllIn(corpus.toLowerCase).toSeq | |
val count = words.groupBy(_.toSeq).mapValues(_.size) withDefaultValue 0 | |
def edit(n: Int)(word: Seq[Char]): Set[Seq[Char]] = n match { | |
case 0 => Set(word) | |
case 1 => | |
val splits = word.indices map word.splitAt | |
val deletes = splits collect {case (a, b0 +: b1) => a ++ b1} |
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
/** | |
* Solves the n-Queen puzzle in O(n!) | |
* Let p[r] be the column of the queen on the rth row (must be exactly 1 queen per row) | |
* There also must be exactly 1 queen per column and hence p must be a permuation of (0 until n) | |
* There must be n distinct (col + diag) and n distinct (col - diag) for each queen (else bishop attacks) | |
* @return returns a Iterator of solutions | |
* Each solution is an array p of length n such that p[i] is the column of the queen on the ith row | |
*/ | |
def nQueens(n: Int): Iterator[Seq[Int]] = | |
(0 until n) |
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
solve = (s, c = 0) -> if c is 81 then s else if s[x = c/9|0][y = c%9] isnt 0 then solve s, c+1 else (([1..9].filter (g) -> ![0...9].some (i) -> g in [s[x][i], s[i][y], s[3*(x/3|0) + i/3|0][3*(y/3|0) + i%3]]).some (g) -> s[x][y] = g; solve s, c+1) or s[x][y] = 0 |
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
// A simply port of Google Task's recurring task scheduler to Scala 3 ADT: | |
// https://tasks.google.com/embed/?origin=https://calendar.google.com&fullWidth=1 | |
import java.time._ | |
enum Cadence: | |
case Daily | |
case Weekly(on: Set[DayOfWeek] = Set(DayOfWeek.SUNDAY)) | |
case Monthly(on: Set[DayOfMonth] = Set(DayOfMonth.Day(1))) | |
case Yearly(on: Set[MonthDay] = Set(MonthDay.of(1, 1))) |
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.awt.*; | |
import java.math.*; | |
import java.util.regex.*; | |
import static java.util.Arrays.*; | |
import static java.util.Collections.*; | |
import static java.lang.Integer.parseInt; | |
import static java.util.AbstractMap.*; | |
import static java.lang.System.*; | |
import static java.lang.Math.*; |
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
case class Node(value: Int, next: Option[Node]) | |
def reverse(node: Node, prev: Option[Node] = None): Node = { | |
val reversed = node.copy(next = prev) | |
node.next map {reverse(_, Some(reversed))} getOrElse reversed | |
} | |
/****************************************************************/ | |
val one = Node(1,Some(Node(2,Some(Node(3,None))))) | |
println(s"$one\n${reverse(one)}") |
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 squants.Dimensionless | |
import squants.DimensionlessConversions._ | |
import squants.market._, MoneyConversions._ | |
case class CreditCard(name: String, points: Dimensionless, checkingAPR: Dimensionless) { | |
def charge(amount: Money, fee: Dimensionless = 0 percent, flatFee: Money = 0 dollars): String = { | |
val fees = amount*fee + flatFee | |
val interestEarned = amount * checkingAPR/12 | |
val cashBack = (amount + fees)*points | |
val saved = cashBack - interestEarned - fees |
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
#!/usr/bin/env bash | |
substring=$1 | |
replace=$2 | |
current_branch=$(git name-rev --name-only HEAD) | |
echo "Replacing ${substring} with ${replace} in all new commits in ${current_branch}" | |
cd $(git root) | |
git filter-branch --msg-filter "'sed ""s/${substring}/${replace}/g""'" master..${current_branch} | |
git push -f |
NewerOlder