WeBWorK Problems

NameForNumber

NameForNumber

by Bruce Yoshiwara -
Number of replies: 2
I'm guessing that something like NameForNumber might be useful to create a problem in which the student needs to type in the words corresponding to some decimal representation.

For example, if the problem says to write out "23", the student would be expected to type in "twenty-three".

Would this be hard to code?
In reply to Bruce Yoshiwara

Re: NameForNumber

by Davide Cervone -
Well, the NameForNumber method of the MathObjects is for turing 1 into "first", 2 into "second", and so on, and it only works up to nine, as I recall (then uses "n-th" so 10 becomes "10-th", and so on).

Depending on how high you wanted to go, it should not be too hard to do what you want. Something like

    sub NameFor {
      my $n = shift;
      my @name = ("zero","one","two","three","four","five","six",
                  "seven","eight","nine","ten","eleven","twelve","thirteen");
      my @tens = ("","","twenty","thirty","forty","fifty",
                  "sixty","seventy","eighty","ninety");
      return $name[$n] if $n &lt= 13;
      return "eighteen" if $n == 18;
      return $name[$n-10]."teen" if $n < 20;
      return $tens[$n / 10] if $n % 10 == 0;
      return $tens[floor($n / 10)]."-".$name[$n % 10];
    }
should convert integers between 0 and 99 (inclusive) to strings. Then you can use str_cmp() to test the student answer.

Hope that's what you are looking for.

Davide