Skip to content

Instantly share code, notes, and snippets.

View pokutuna's full-sized avatar
👁️
👄👁👂

pokutuna pokutuna

👁️
👄👁👂
View GitHub Profile
use strict;
use warnings;
use feature 'say';
use JSON::PP qw(encode_json);
my $hash = {
num => 1
};
say encode_json($hash); # => {"num":1}

HCI演習/エルゴ演習メモ

教科書あったほうがよいのでは

VisualizingData

  • 「プロセッシングの教科書」ではなく「ビジュアライジング」(可視化)の本です

基本的なルール

@annotation.tailrec
def findTheCubedNum(num: BigInt, cache: Map[String, List[BigInt]]): BigInt = {
val cubed = num.pow(3)
val key = cubed.toString.sortWith(_ < _)
val nums = cubed :: cache.getOrElse(key, List[BigInt]())
if (nums.length == 5) return nums.min
findTheCubedNum(num + 1, cache.updated(key, nums))
}
val answer = findTheCubedNum( BigInt(1), Map[String, List[BigInt]]() )
import scala.io.Source
val code = Source.fromURL("http://projecteuler.net/project/cipher1.txt").mkString.trim.split(",").map(_.toInt.toChar).toList
def decode(pass: List[Char], code: List[Char]): List[Char] =
code.zipWithIndex.map( p => p._1 ^ pass(p._2 % pass.length) toChar )
val frequentlyWord = "the".r
def isGraphicChar(c: Char): Boolean = return if (c < ' ' || '~' < c) false else true
val passes = for (a <- 'a' to 'z'; b <- 'a' to 'z'; c <- 'a' to 'z') yield List(a, b, c)
def isPrime(n: Int): Boolean = (2 to math.sqrt(n).toInt).forall(n % _ != 0)
def nextCorner(st: Stream[Int]) = st.zip(st.tail).map( n => n._2 + (n._2 - n._1 + 8) )
val upperRight: Stream[Int] = 1 #:: 3 #:: nextCorner(upperRight)
val upperLeft: Stream[Int] = 1 #:: 5 #:: nextCorner(upperLeft)
val lowerLeft: Stream[Int] = 1 #:: 7 #:: nextCorner(lowerLeft)
val lowerRight: Stream[Int] = 1 #:: 9 #:: nextCorner(lowerRight)
val zipped: Stream[List[Int]] = Stream.from(1).map( idx =>
List(upperRight(idx), upperLeft(idx), lowerLeft(idx), lowerRight(idx)) )
def sumOfDigits(num: BigInt): Int = num.toString.map(_.getNumericValue).sum
val result = ( for (a <- 1 until 100; b <- 1 until 100) yield sumOfDigits(BigInt(a).pow(b)) ).max
println(result) // => 972
def isCircular(num: BigInt): Boolean = num.toString == num.toString.reverse
def addReverse(num: BigInt): BigInt = num + BigInt(num.toString.reverse)
def isLychrel(num: BigInt, time: Int = 0): Boolean = {
if (time == 50) return true
val n = addReverse(num)
if (isCircular(n)) return false else isLychrel(n, time + 1)
}
def consistsOfSameNumber(num: Int): Boolean = {
val sorted = num.toString.sortWith(_ < _)
List(2, 3, 4, 5, 6).map(num * _).forall(_.toString.sortWith(_ < _) == sorted)
}
println(Stream.from(100000).find(consistsOfSameNumber).get) // => 142857
def isPrime(n: Int): Boolean = (2 to math.sqrt(n).toInt).forall(n % _ != 0)
val primes: Stream[Int] = Stream.from(2).filter(isPrime)
def primeProgressionLength(prime: Int, progression: Stream[Int] = primes): Option[Int] = {
def sumLength(max: Int, seq: Seq[Int], sum: Int = 0, len: Int = 0): Option[Int] = {
if (sum > max) return None
if (sum == max) return Some(len)
sumLength(max, seq.tail, sum + seq.head, len + 1)
}
def isPrime(n: Int): Boolean = (2 to math.sqrt(n).toInt).forall(n % _ != 0)
val groupedPrimes= Stream.from(2).filter(isPrime).dropWhile(_ < 1000).takeWhile(_ < 10000).toList.groupBy(_.toString.sortWith(_ < _))
def findArithmeticProgression(nums: List[Int]): Option[List[Int]] = {
@annotation.tailrec
def rec(h: Int, tail: List[Int]): Option[List[Int]] = {
if (tail.length < 2) return None
tail.find( n => tail.contains(n + (n - h)) ) match {
case Some(n) => return Some(List(h, n, n + (n - h)))