Last active
March 7, 2021 08:15
-
-
Save michalbcz/b47f9c8d74b92eb089eff98c8645db5b to your computer and use it in GitHub Desktop.
Revisions
-
michalbcz revised this gist
Mar 7, 2021 . No changes.There are no files selected for viewing
-
michalbcz revised this gist
Mar 7, 2021 . 1 changed file with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,4 @@ /* THIS PUZZLER IS SOVLED, SEE REVISION OF THIS GIST TO SEE ORIGINAL PROBLEM */ decimalValue = 9989 hexValue = toHex(code) @@ -21,7 +18,7 @@ println "DECIMAL VALUE: ${decimalValue} CONVERTED TO HEX VALUE: ${hexValue}" */ def toHex(number) { next = Double.valueOf(Math.floor(number / 16)).toLong() def remainder = number % 16 // debug print to see what happens inside of this method // println next + ", " + remainder -
michalbcz revised this gist
Mar 7, 2021 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,7 @@ /* THIS CODE HAS A BUG, CAN YOU FIND IT ? */ /* HINT: It's about scope of variables. Do you know how variable declaration in Groovy works? What is difference between variable declaration with and without "def" keyword ? */ decimalValue = 9989 hexValue = toHex(code) -
michalbcz created this gist
Mar 7, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ /* THIS CODE HAS A BUG, CAN YOU FIND IT ? */ decimalValue = 9989 hexValue = toHex(code) assert hexValue == '2705' // safe net expectedValue = Integer.toHexString(decimalValue) assert hexValue == expectedValue : "Your impl should work as expected. Result should be: ${expectedValue}" println "DECIMAL VALUE: ${decimalValue} CONVERTED TO HEX VALUE: ${hexValue}" /* ------------------------ METHODS -------------------------- */ /* note you can use Java's Integer#toHexString() this is just for practicing purposes */ def toHex(number) { next = Double.valueOf(Math.floor(number / 16)).toLong() remainder = number % 16 // debug print to see what happens inside of this method // println next + ", " + remainder // translate 10 -> A, 11->B .... 15->F if (remainder > 9) { remainder = (((int)'A') + (remainder - 10)) as char } // end recursion if (next == 0) { return remainder } return toHex(next) + remainder.toString() }