Skip to content

Instantly share code, notes, and snippets.

@michalbcz
Last active March 7, 2021 08:15
Show Gist options
  • Select an option

  • Save michalbcz/b47f9c8d74b92eb089eff98c8645db5b to your computer and use it in GitHub Desktop.

Select an option

Save michalbcz/b47f9c8d74b92eb089eff98c8645db5b to your computer and use it in GitHub Desktop.

Revisions

  1. michalbcz revised this gist Mar 7, 2021. No changes.
  2. michalbcz revised this gist Mar 7, 2021. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions decimal_to_hex_puzzler.groovy
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,4 @@
    /* 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 ?
    */
    /* 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()
    remainder = number % 16
    def remainder = number % 16

    // debug print to see what happens inside of this method
    // println next + ", " + remainder
  3. michalbcz revised this gist Mar 7, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions decimal_to_hex_puzzler.groovy
    Original 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)
  4. michalbcz created this gist Mar 7, 2021.
    38 changes: 38 additions & 0 deletions decimal_to_hex_puzzler.groovy
    Original 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()
    }