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
0..10 | % { python GuessClosedForm.py "i ** $_"; Write-Host } |
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 jhm.JHMSolver | |
import jhm.SolverUsingFlatMap | |
import jhm.SolverUsingFor | |
import jhm.SolverUsingFoldLeft | |
object JHMWorksheet { | |
implicit val repetitions: Int = 20 //> repetitions : Int = 20 | |
// Adapted from code found on: http://stackoverflow.com/questions/9160001/how-to-profile-methods-in-scala ... | |
// UPDATE: see the following article for why this is a bad idea... http://shipilev.net/blog/2014/nanotrusting-nanotime/ |
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
package jhm | |
case class SolverUsingFoldLeft( digits: String, total: Int, maxDigitsPerNumber: Int = 5 | |
) extends JHMSolver(digits, total, maxDigitsPerNumber) { | |
case class PartialSolution( val partialStr: String, val partialNumber: Int, | |
val subTotal: Int, val numbersInReverse: List[Int] ) | |
{ | |
def runningSubTotal = subTotal + partialNumber | |
def isInitialSolution = partialStr == "" |
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
package jhm | |
case class SolverUsingFor( digits: String, total: Int, maxDigitsPerNumber: Int = 5 | |
) extends JHMSolver(digits, total, maxDigitsPerNumber) { | |
override def solve(digits: String, total: Int, maxDigitsPerNumber: Int = 5): List[List[Int]] = for { | |
leftCount <- (1 to maxDigitsPerNumber).toList | |
if leftCount <= digits.length | |
(leftStr, rightStr) = digits.splitAt(leftCount) | |
if (leftCount == 1 || leftStr(0) != '0') // No numbers may start with '0' (except zero) |
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
package jhm | |
case class SolverUsingFlatMap( digits: String, total: Int, maxDigitsPerNumber: Int = 5 | |
) extends JHMSolver(digits, total, maxDigitsPerNumber) { | |
override def solve(digits: String, total: Int, maxDigitsPerNumber: Int = 5): List[List[Int]] = { | |
def getSolutionsForNextNumber(leftCount: Int): List[List[Int]] = { | |
val (leftStr, rightStr) = digits.splitAt(leftCount) | |
if (leftCount > 1 && leftStr(0) == '0') List.empty // Disallow numbers starting with zero |
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
package jhm | |
abstract class JHMSolver(digits: String, total: Int, maxDigitsPerNumber: Int = 5) | |
{ | |
def solve(digits: String, total: Int, maxDigitsPerNumber: Int = 5): List[List[Int]] | |
lazy val solutions = solve(digits, total, maxDigitsPerNumber) | |
def getSolutionsAsString(): String = solutions match { | |
case Nil => "IMPOSSIBLE" |
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 calc { | |
param ( | |
[String] $str, | |
[int] $target | |
) | |
foreach ($len in 1..5 ) { | |
$firstStr = $str.SubString(0, $len) | |
if ($firstStr.Length -lt $len) { | |
break | |
} |