If you are not sure if your solution is correct you need to ask yourself the following questions:

  1. What do I expect?
    Manually create the correct solution for simple input parameters, on paper.
  2. What does my current solution produce?
    Find a way to verify your paper solutions with your programmatic solution. With Perl, the module Data::Dumper is very helpfull when looking at data structures:
    use Data::Dumper; print Dump \%test; print Dump \%reversed_test; # a better name than %test1
  3. Are the examples I tested generic enough to cover all cases?
    This is hard - you need to think about the cases that can possibly occur in your input data, and then consider how your code handles these cases to produce the output. In many cases of program verification, it helps to find invariants, that is, things that always are true. For example, for a hash with unique keys and unique values, reverse_hash( reverse_hash( %test )) should always be equal to the hash %test itself. Can you think about an invariant that is true for a hash with duplicate values? Can you think about an invariant that is true for a hash with duplicate keys?

In reply to Re: key-values swapped by Corion
in thread key-values swapped by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.