# HG changeset patch # User Christian Urban # Date 1606438524 0 # Node ID d29cd5883c7bc19beac2a14eb6c9be45ed2156d7 # Parent e87462c9b89558a4fc59ce922c5e030a2e294a15 updated diff -r e87462c9b895 -r d29cd5883c7b pre_marking1/collatz.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pre_marking1/collatz.scala Fri Nov 27 00:55:24 2020 +0000 @@ -0,0 +1,50 @@ +// Basic Part about the 3n+1 conjecture +//================================== + +// generate jar with +// > scala -d collatz.jar collatz.scala + +object CW6a { // for purposes of generating a jar + +def collatz(n: Long): Long = + if (n == 1) 0 else + if (n % 2 == 0) 1 + collatz(n / 2) else + 1 + collatz(3 * n + 1) + + +def collatz_max(bnd: Long): (Long, Long) = { + val all = for (i <- (1L to bnd)) yield (collatz(i), i) + all.maxBy(_._1) +} + +//collatz_max(1000000) +//collatz_max(10000000) +//collatz_max(100000000) + +/* some test cases +val bnds = List(10, 100, 1000, 10000, 100000, 1000000) + +for (bnd <- bnds) { + val (steps, max) = collatz_max(bnd) + println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}") +} + +*/ + +def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0 + +def is_hard(n: Long) : Boolean = is_pow(3 * n + 1) + +def last_odd(n: Long) : Long = + if (is_hard(n)) n else + if (n % 2 == 0) last_odd(n / 2) else + last_odd(3 * n + 1) + + +//for (i <- 130 to 10000) println(s"$i: ${last_odd(i)}") +//for (i <- 1 to 100) println(s"$i: ${collatz(i)}") + +} + + + diff -r e87462c9b895 -r d29cd5883c7b pre_marking1/collatz_test.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pre_marking1/collatz_test.sh Fri Nov 27 00:55:24 2020 +0000 @@ -0,0 +1,143 @@ +#!/bin/bash + +# to make the script fail safely +set -euo pipefail + + +out=${1:-output} + +echo "" > $out + +echo `date` >> $out +echo >> $out +echo "Below is the feedback and provisional marks for your submission" >> $out +echo "for the Preliminary Part of Part 1 (Scala). Please note all marks are provisional until" >> $out +echo "ratified by the assessment board -- this is not an official" >> $out +echo "results transcript." >> $out +echo "" >> $out + +echo "The feedback for your submission for collatz.scala" >> $out +echo "" >> $out + +# marks for CW6 basic part +marks=$(( 0 )) + +# compilation tests + +function scala_compile { + (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> c$out 1> c$out) +} + +# functional tests + +function scala_assert { + (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -nc -i "$1" -- "$2" -e "" 2> /dev/null 1> /dev/null) +} + +# purity test + +function scala_vars { + (egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|mutable|util.control|new Array' c$out 2> /dev/null 1> /dev/null) +} + + +### compilation test + +echo -e "collatz.scala runs?" >> $out + +if (scala_compile collatz.scala) +then + echo -e " --> success" >> $out + tsts0=$(( 0 )) +else + echo -e " --> SCALA DID NOT RUN collatz.scala\n" >> $out + tsts0=$(( 1 )) +fi + +# var, .par return, ListBuffer test +# + +if [ $tsts0 -eq 0 ] +then + echo -e "collatz.scala does not contain VARS, RETURNS etc?" >> $out + + if (scala_vars collatz.scala) + then + echo -e " --> test failed\n" >> $out + tsts=$(( 1 )) + else + echo -e " --> success" >> $out + tsts=$(( 0 )) + fi +fi + + +echo >> $out + +### collatz tests + +if [ $tsts -eq 0 ] +then + echo "collatz.scala tests:" | tee -a $out + echo " collatz(1) == 0" | tee -a $out + echo " collatz(6) == 8" | tee -a $out + echo " collatz(9) == 19" | tee -a $out + echo " collatz(9000) == 47" | tee -a $out + + if (scala_assert "collatz.scala" "collatz_test1.scala") + then + echo " --> success" | tee -a $out + marks=$(( marks + 1 )) + else + echo " --> one of the tests failed" | tee -a $out + fi +fi + +### collatz-max tests + +if [ $tsts -eq 0 ] +then + echo " collatz_max(10) == (19, 9)" | tee -a $out + echo " collatz_max(100) == (118, 97)" | tee -a $out + echo " collatz_max(1000) == (178, 871)" | tee -a $out + echo " collatz_max(10000) == (261, 6171)" | tee -a $out + echo " collatz_max(100000) == (350, 77031)" | tee -a $out + echo " collatz_max(1000000) == (524, 837799)" | tee -a $out + # echo " collatz_max(2) == (1, 2) || collatz_max(2) == (0, 1)" | tee -a $out + echo " collatz_max(2) == (1, 2)" | tee -a $out + echo " collatz_max(77000) == (339, 52527)" | tee -a $out + + if (scala_assert "collatz.scala" "collatz_test2.scala") + then + echo " --> success" | tee -a $out + marks=$(( marks + 1 )) + else + echo " --> one of the tests failed" | tee -a $out + fi +fi + +### last-odd tests + +if [ $tsts -eq 0 ] +then + echo " last_odd(113) == 85" | tee -a $out + echo " last_odd(84) == 21" | tee -a $out + echo " last_odd(605) == 341" | tee -a $out + + if (scala_assert "collatz.scala" "collatz_test3.scala") + then + echo " --> success" | tee -a $out + marks=$(( marks + 1 )) + else + echo " --> one of the tests failed" | tee -a $out + fi +fi + + + +## final marks +echo >> $out +echo "Overall mark for the Preliminary Part 1 (Scala)" | tee -a $out +echo " $marks" | tee -a $out + + diff -r e87462c9b895 -r d29cd5883c7b pre_marking1/collatz_test1.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pre_marking1/collatz_test1.scala Fri Nov 27 00:55:24 2020 +0000 @@ -0,0 +1,8 @@ + + +assert(CW6a.collatz(1) == 0) +assert(CW6a.collatz(6) == 8) +assert(CW6a.collatz(9) == 19) +assert(CW6a.collatz(9000) == 47) + + diff -r e87462c9b895 -r d29cd5883c7b pre_marking1/collatz_test2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pre_marking1/collatz_test2.scala Fri Nov 27 00:55:24 2020 +0000 @@ -0,0 +1,16 @@ +//def myassert(cond : => Boolean) = { +// try { +// assert(cond) +// } catch { +// case _ : Throwable => System.exit(1) +// } +//} + +assert(CW6a.collatz_max(10) == (19, 9)) +assert(CW6a.collatz_max(100) == (118, 97)) +assert(CW6a.collatz_max(1000) == (178, 871)) +assert(CW6a.collatz_max(10000) == (261, 6171)) +assert(CW6a.collatz_max(100000) == (350, 77031)) +assert(CW6a.collatz_max(1000000) == (524, 837799)) +assert(CW6a.collatz_max(2) == (1, 2)) +assert(CW6a.collatz_max(77000) == (339, 52527)) diff -r e87462c9b895 -r d29cd5883c7b pre_marking1/collatz_test3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pre_marking1/collatz_test3.scala Fri Nov 27 00:55:24 2020 +0000 @@ -0,0 +1,4 @@ + +assert(CW6a.last_odd(113) == 85) +assert(CW6a.last_odd(84) == 21) +assert(CW6a.last_odd(605) == 341) diff -r e87462c9b895 -r d29cd5883c7b pre_marking1/mk --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pre_marking1/mk Fri Nov 27 00:55:24 2020 +0000 @@ -0,0 +1,25 @@ +#!/bin/sh +###set -e + +trap "exit" INT + +files=${1:-*/pre1} + +for sd in $files; do + cd $sd + echo $sd + touch . + cp ../../../../marking1/collatz_test.sh . + cp ../../../../marking1/collatz_test1.scala . + cp ../../../../marking1/collatz_test2.scala . + cp ../../../../marking1/collatz_test3.scala . + ./collatz_test.sh output1 + rm collatz_test.sh + rm collatz_test1.scala + rm collatz_test2.scala + rm collatz_test3.scala + cd .. + cd .. +done + + diff -r e87462c9b895 -r d29cd5883c7b pre_solution1/collatz.scala --- a/pre_solution1/collatz.scala Wed Nov 25 18:25:15 2020 +0000 +++ b/pre_solution1/collatz.scala Fri Nov 27 00:55:24 2020 +0000 @@ -31,6 +31,7 @@ */ + def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0 def is_hard(n: Long) : Boolean = is_pow(3 * n + 1) @@ -41,6 +42,7 @@ last_odd(3 * n + 1) + //for (i <- 130 to 10000) println(s"$i: ${last_odd(i)}") //for (i <- 1 to 100) println(s"$i: ${collatz(i)}") diff -r e87462c9b895 -r d29cd5883c7b pre_testing1/collatz.scala --- a/pre_testing1/collatz.scala Wed Nov 25 18:25:15 2020 +0000 +++ b/pre_testing1/collatz.scala Fri Nov 27 00:55:24 2020 +0000 @@ -6,20 +6,11 @@ object CW6a { // for purposes of generating a jar -/* def collatz(n: Long): Long = if (n == 1) 0 else if (n % 2 == 0) 1 + collatz(n / 2) else 1 + collatz(3 * n + 1) -*/ -def aux(n: Long, acc: Long) : Long = - if (n == 1) acc else - if (n % 2 == 0) aux(n / 2, acc + 1) else - aux(3 * n + 1, acc + 1) - - -def collatz(n: Long): Long = aux(n, 0) def collatz_max(bnd: Long): (Long, Long) = { val all = for (i <- (1L to bnd)) yield (collatz(i), i)