# HW3 Solution

This post is the solution to the third homework, hw3.

## Exercise 1 (perl1): a loop

This is basically the loop from the first homework, hw1. It is important to recognize and reuse what you have done and tested before: you can get to the result faster and with less chance of mistakes.

Here is the solution:

# Create a variable to store how many random were generated at the end. $howmany = 0; $total = 0; while ($total < 27) { $random = int(rand(11)); $howmany++; $total += $random; } print " Finished: I generated $howmany random numbers.\n"; |

You can also find it in codepad.

Some remarks:

- assigning 0 to $howmany and $total before the loop (called initializing) is not needed since perl will put zero in new variables anyway, but it may make the code clearer (and avoid mistakes in the next exercise);
- writing “$howmany++” or “$howmany += 1” or “$howmany = $howmany + 1” is the same;
- some of you subtracted 1 from $howmany at the end, while others did not: the homework was not very clear on this point so both choices are fine.

## Exercise 2 (perl2): a loop in a loop

Here we need to put the loop from the previous exercise in another loop, like this:

$howmany = 0; for ($i = 0; $i < 100; $i++) { $total = 0; while ($total < 27) { $random = int(rand(11)); $howmany++; $total += $random; } } $average = $howmany/100; print " Finished: I generated $average random numbers on average.\n"; |

The code is also in codepad.

Remarks:

- here you really need to initialize $total, otherwise the inner while loop will run only one time (do you see why?);
- note the two level of indentation: it makes evident that there are two blocks of code, one inside the other;

## Exercise 3 (perl3): play with numbers, again!

A simple exercise about looping on the values of an array:

$sum = 0; @numbers = (24, -4, 2.42, 494.02, 120, 0, 1, 0, 1.22); foreach $i (@numbers) { $sum += $i; } $count = $#numbers + 1; $average = $sum/$count; $variance_acc = 0; foreach $i (@numbers) { $variance_acc += ($i - $average)**2; } $stddev = sqrt($variance_acc/($count - 1)); print "Average: $average\nStandard deviation: $stddev\n";

The code in codepad.

Remarks:

- we already know that the array has more than one element, so the code is fine like this; however it would be better to check that $count is not 1 otherwise we will divide by 0 to compute the standard deviation;

## Exercise 4 (perl4): play with strings!

Here is the code:

@sequences = ('AGAGTATATACA', 'AGAGATATCCACACAC', 'CCCATAATATATAT', 'GAGAGAAAAAAA'); $seq_number = 0; for $seq (@sequences) { $seq_number++; $qual = 'i' x length($seq); print ">seq-$seq_number\n$seq\n+\n$qual\n"; }

The code in codepad.

Remarks:

- some of you used a loop print the quality string: it works but it is better to use the
*x*operator, since it is shorter and faster; - at least one of you put spaces in the sequence name, which does not follow the fasta/q format!

Code should strive to be correct, easy to read and fast to run. If you write less code you have less space to put mistakes and you can understand it more easily.