in reply to key-values swapped

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?

Replies are listed 'Best First'.
Re^2: key-values swapped
by Anonymous Monk on Feb 18, 2005 at 10:00 UTC
    Hi Corion,
    Thanks for your valuable information.