The Friday Fragment

It’s Friday, and time again for a new Fragment: my weekly programming-related puzzle.

This Week’s Fragment

A recent Car Talk Puzzler challenged listeners to find two temperatures where the Celsius and Fahrenheit readings had the same two digits, but reversed.  For example, 28 Celsius is 82 Fahrenheit.  It’s an easy problem to solve by hand, but what fun is that?  This is one where writing a bit of code will yield an answer faster than working it out by hand.  Or, better still, offer a chance to try some new coding approaches.  So, our programming version of this puzzle is:

Write code to find two (two-digit) temperatures where the (rounded) Celsius and Fahrenheit readings have the same digits, only reversed.

Use any language you’d like; you may want to try it in multiple languages to compare.  You might even try an esoteric language like LOLCODE or Shakespeare.

If you want to “play along”, post the solution as a comment or send it via email.   To avoid “spoilers”, simply don’t expand comments for this post.  That is, unless you want to see what languages others used to deliberately try something different.  Language wars can be such good sport.

Last Week’s Fragment – Solution

Last week’s fragment was one of those puzzles sometimes given as interview questions for programming and engineering jobs:

You have eight balls; seven weigh the same, but one is lighter than the others.  Using a balance, how can you find the “oddball” in only two weighings?

Congratulations to Wayne who solved it swimmingly, and described the solution nicely:

Weigh three balls against another three balls. If they weigh the same, you simply weigh the other two to find the lighter one. If one set of three balls was lighter than the other though, weigh two of the balls in the lighter set of three against each other. If one is lighter, that’s it, else it’s the third one you didn’t weigh the second time.

I guess that means you’re hired!

Share This:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Google Buzz
  • RSS

7 thoughts on “The Friday Fragment

  1. Joel Odom

    Python. You can expand the call to range as needed:

    for c in range(-99999, 10000):
    f = 9*c/5 + 32
    if str(abs(c)) == str(abs(f))[::-1]:
    print ‘%s C / %s F’ % (c, f)

    Outputs:

    -26274 C / -47262 F
    28 C / 82 F
    4157 C / 7514 F

  2. Wayne Johnston

    Sorry, boring old Smalltalk since I ‘think’ in Smalltalk…
    10 to: 99 do: [:f | | c | c := (f – 32) * 5 / 9. c rounded printString reverse = f printString ifTrue: [Transcript cr; show: f printString, ‘ F = ‘, c rounded printString, ‘ C’]]
    — output —
    61 F = 16 C
    82 F = 28 C

  3. Derek Post author

    Here’s a simple PHP implementation:

    for ($celsius=10; $celsius<99; $celsius++) {
    $fahren = round($celsius * 9/5 + 32);
    if (strval($fahren) == strrev(strval($celsius))) {
    echo " Match for Celsius = " . $celsius;
    }
    }

    And here's a LOLCODE implementation that partially works in my modified version of Jeff Jones' PHP parser (http://tetraboy.com/lolcode/). By partially, I had to modify lol_core.php to support the math operators and strrev, and there still were remaining problems. I wonder why there aren’t any production-ready LOLCODE compilers?

    HAI
    BTW FIND YR SELLSEEUS IS FARHAI SWITCHED
    MUST HAS STDIO
    VISIBLE “HAI”
    SELLSEEUS IZ 10
    IM IN YR LOOP
    IZ CONVERTIN(&SELLSEEUS&) == STRREV(“”.&SELLSEEUS$)
    VISIBLE “LOL “.&SELLSEEUS&
    KTHX
    SELLSEEUS UPUP!
    IZ SELLSEEUS BIGGER THAN 99? KTHXBYE
    KTHX
    SO IM LIKE CONVERTIN WITH TEMPTURE ANSR
    ANSR IZ PRODUKT OF TEMPTURE AN 9
    ANSR IZ QUOSHUNT OF ANSR AN 5
    ANSR IZ SUM OF ANSR AN 32
    I FOUND MAH ANSR
    KTHX
    KTHXBYE

  4. Spencer

    I tried Shakespeare. Unfortunately, the only implementation of Shakespeare doesn’t support the language’s own GOTO functionality; the examples error out. Therefore, there’s no way to loop.

    Thus I wasn’t able to finish the code. Here’s what I got, though:

    A Demonstration in One Act.

    Hamlet, an odd fellow who enjoys strange conversation.
    Macbeth, a distinguished yet quiet gentleman who’s found himself in the wrong play entirely.
    Ulysses, some dude with a spear.

    Act I: Hamlet’s Madness.
    Scene I: The Speech.

    [Enter Hamlet and Macbeth]
    Hamlet:
    Open your mind!

    Macbeth:
    You are as warm as the sum of a peaceful bold cunning hero and a fine summer’s day.

    [Exit Macbeth]
    [Enter Ulysses]
    Hamlet:
    You are as noble as Macbeth.
    [Exit Ulysses]

    Scene II: In which Ulysses and Macbeth are insulted and reverse positions.

    [Enter Ulysses]
    Hamlet:
    You are as little as the remainder of the quotient between yourself and me.
    Ulysses:
    Speak your mind!
    Hamlet:
    Open your heart!

    [Exit Ulysses]
    [Enter Macbeth]
    Hamlet: You are as little as the quotient between yourself and me.
    Macbeth: Speak your mind!
    [Exeunt]

    Scene III: The Test of a Man.

    [Enter Hamlet and Macbeth]
    Hamlet: Are you better than a face?
    Macbeth: If so, let us proceed to scene II.
    [Exeunt]

  5. Spencer

    hey so for cent in range(10, 100):
    anyways so faren = int(((9.0/5.0)*float(cent)) + 32)
    omg so dude if (str(faren))[::-1] == str(cent):
    like totally print str(faren) + “F / ” + str(cent) + “C\n” right

    hey um yeah
    anyways broseph

Comments are closed.