## WeBWorK Problems

### Is it possible to pair text and numeric values in hash arrays?

by tim Payer -
Number of replies: 2

Greetings,

Is it possible to pair text and numeric values in hash arrays and still have the numeric values function as numeric?

My efforts to do so have failed. Specifically I would like for the two numeric variables of $rh and$rf to NOT be equal to each other as well as their corresponding text representations of $rht and$rft.

The error I am having is that the text representations are all defaulting to “one” instead of matching their respective numeric values.

I can make these work together just fine in an if-else condition, but I would like to use the more streamlined hash arrays if possible.

Can you see where I have made yet another glitch?

Thanks for you continued help.

These if-else conditions work just fine:

# $rht = ratio count in text for urban mosquitoes: if($rh ==1) {

$rht = "one"; } elsif ($rh ==2 ) {

$rht = "two"; }elsif ($rh ==3 ) {

$rht = "three"; }elsif ($rh ==4 ) {

$rht = "four"; }else {$rht = "five";

}

# $rft = ratio count in test for forest mosquitoes: if($rf ==1) {

$rft = "one"; } elsif ($rf ==2 ) {

$rft = "two"; }elsif ($rf ==3 ) {

$rft = "three"; }elsif ($rf ==4 ) {

$rft = "four"; }else {$rft = "five";

}

But I would like the hash arrays (used in the code block below) to behave the same way with fewer lines of code.

Thanks, for the help….

# DESCRIPTION

# Probability

# WeBWorK problem written by Tim Payer, <tsp1@humboldt.edu>

# ENDDESCRIPTION

## DBsubject(Probability)

## DBchapter(Random Variables)

## DBsection(Expectation)

## Institution(Humboldt State University)

## Author(Tim Payer)

## KEYWORDS(probability, independence, notation)

DOCUMENT();

"PGstandard.pl",

"MathObjects.pl",

"PGML.pl",

"parserFormulaUpToConstant.pl",

"niceTables.pl",

"PGunion.pl",

"parserPopUp.pl",

);

Context("Numeric");

Context()->flags->set(

tolerance => 0.0001,

tolType => "absolute",

);

# To switch off the algorithmic data un-comment the following 4 lines:

# and then Comment (#) the 4 random assignments that follow.

# $h = 12; # urban percentage #$f = 21;  # forest percentage

# $rh = 3; # ratio count of urban mosquitoes #$rf = 2;

$h = random(9, 17,1); # urban percentage$f = random(19, 31,1); # forest percentage

# Establishing ratio count of urban mosquitoes and forcing

# unequal forest mosquito population ratios:

$rh = list_random('1','2','3','4','5'); # ratio count of urban mosquitoes %rht =(1=>'one',2=>'two',3=>'three',4=>'four',5=>'five'); # ratio count in text do {$rf = list_random('1','2','3','4','5');   # ratio count of forest mosquitoes

%rft =(1=>'one',2=>'two',3=>'three',4=>'four',5=>'five');  # ratio count in text

} until ($rf !=$rh);

$rft =$rft{$rf};$rht = $rht{$rf};

## providing for plural and singular wordings but this is not working??

if($rf != 1) {$ins ="mosquitoes";

} else {

$ins ="mosquito"; }$popup1 = PopUp(

["probability notation:", "P(C)", "P(H)", "P(F)"], "P(H)");

$ans1 = Compute("$rh/($rh+$rf)");

$popup2 = PopUp( ["probability notation:", "P(C)", "P(H)", "P(F)"], "P(F)");$ans2 = Compute("$rf/($rh+$rf)"); BEGIN_PGML 4B.1) With the recent spread of the Zika virus in South America through mosquitoes of the genus _Aedes_, entomologists have posed the question as to which of two species has the higher incidence of the virus. Suppose that the following incidence of Zika was determined among the two species from a large sample study: _Aedes aegypti_, which tends to favor the urban dwellings of humans, was found to have an incidence of [$h]% Zika carrier, while _Aedes albopictus_, a forest dweller, was found to have an incidence of [$f]% Zika carrier. Given that only these two species of mosquito exist on the outskirts of a small town with a population ratio between them of [$rht] _A.aegypti_ for every [$rft] _A.albopictus_ [$ins], find the following probabilities using the given declared event variables with correct probability notation.

Event variable declaration for the random draws of a mosquito from the outskirts of the town:

[C] = Event that a randomly drawn _Aedes_ mosquito is a carrier for the Zika virus.

[H] = Event that a randomly drawn _Aedes_ mosquito is of the _Aedes aegypti_ species.

[F] = Event that a randomly drawn _Aedes_ mosquito is of the _Aedes albopictus_ species.

4B.1a)  Find the probability of randomly drawing a _Aedes aegypti_ mosquito.

[$popup1->menu]* = [______] 4B.1b) Find the probability of randomly drawing a _Aedes albopictus_ mosquito. [$popup2->menu]*  = [______]

4B.1c)  Find the probability of randomly drawing a _Aedes_ mosquito that is a carrier for the Zika virus.

4B.1d)  Given that an _Aedes_ mosquito is a carrier for Zika, find the probability that the mosquito is of the _Aedes albopictus_ species.

4B.1e)  Is the Zika carrier status for the _Aedes_ mosquito independent of which _Aedes_ species is drawn? Support your answer with the correct probability.

END_PGML

WEIGHTED_ANS( ($popup1)->cmp, 25 ); WEIGHTED_ANS( ($ans1)->cmp, 15 );

WEIGHTED_ANS( ($popup2)->cmp, 20 ); WEIGHTED_ANS( ($ans2)->cmp, 30 );

BEGIN_PGML_SOLUTION

*SOLUTION*

Solution to arrive in 2017....

h = [$h] f = [$f]

rh = [$rh] rf = [$rf]

rht = [$rht] rft = [$rft]

ins = [$ins] ans1 = [$ans1]

ans2 = [ans2] [\begin{aligned}&\\ [c]-\sqrt{\frac{[$am] x}{[$bm]}} &=[$c] -\sqrt{\frac{[$m]\cdot[$a] x}{[$m]\cdot[$b]}} = [$c]  -\frac{\sqrt{[$a] x}}{\sqrt{[$b]}}

&& \text{Cancel the common factor and then split the root.}\\

&=\frac{[$c]\sqrt{[$b]}}{ \sqrt{[$b]}}-\frac{\sqrt{[$a] x}}{\sqrt{[$b]}} && \text{Create a common denominator.}\\ &=\frac{[$c]\sqrt{[$b]} -\sqrt{[$a] x}}{\sqrt{[$b]}} && \text{Combine in to a single fraction.}\\ &=\frac{\left([$c]\sqrt{[$b]} -\sqrt{[$a] x}\right)\sqrt{[$b]}}{\sqrt{[$b]}\cdot\sqrt{[$b]}} && \text{Multiply the numerator and denominator by the denominator.}\\ &=\frac{\left([$c]\sqrt{[$b]\cdot [$b]}-\sqrt{[$a] \cdot [$b] x}\right)}{[$b]} && \text{Distribute through the numerator and reduce the denominator.}\\ &=\frac{\left([$c] \cdot [$b] -\sqrt{[$ab] x}\right)}{[$b]} && \text{Reduce the square root factors.}\\ &=\frac{[$cb] - \sqrt{[$ab] x}}{[$b]} && \text{Combine numerator factors.}

\end{aligned}]

END_PGML_SOLUTION

ENDDOCUMENT();

### Re: Is it possible to pair text and numeric values in hash arrays?

by Danny Glin -
There is a typo in your code:
You have
$rht =$rht{$rf}; which should probably be$rht = $rht{$rh};

When I use your code with this change, the text of the numbers show up properly, and the answers evaluate the way they should.

Danny

### Re: Is it possible to pair text and numeric values in hash arrays?

by Davide Cervone -
I'm not sure why you don't just do something like this:
@NumberToName = ("zero","one","two","three","four","five");

$rh = random(1,5,1); # ratio count of urban mosquitoes do {$rf = random(1,5,1); } until ($rf !=$rh);   # ratio count of forest mosquitoes

$rht =$NumberToName[$rh];$rft = $NumberToName[$rf];

There is certainly no need to recreate the hash for each of the two variables, and certainly no need to do it inside the do-until loop. There's really no need to a hash at all, as an array does just fine for this (since your hash keys are just small integers).