Homework: reviewing Perl basics

Review homework (hw3)

If you have Linux on your laptop you can download the program and change it with a text editor like we did in the lab. Note that your Linux may be a little different, so you may need to looks for the Terminal and the Text Editor, but they are there somewhere. If you do not have Linux yet, you can use codepad. This task is codedhw3” (not hw-3 or hw-03…. right?).

Exercise 1 (perl1): a loop

Prepare a script that will generate random numbers between 0 and 10 until the sum of the generated numbers is less than 27.
The script has to print how many numbers were generated (not the sum, and not the single numbers).

Exercise 2 (perl2): a loop in a loop

Consider the script you prepared in the previous exercise. You want to test (on average) how many numbers you extract before reaching the sum of 27. You can run the script “perl1” a number of times (say 100)… but we want to do something wiser.

To do this you prepare a for loop that will iterate 100 times the while loop of the previous exercise.

At the end you’ll print “I extracted random numbers for 100 times, and on average I extracted $howmany numbers before reaching 27″.

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

Given the following array:

@numbers = (24, -4, 2.42, 494.02, 120, 0, 1, 0, 1.22);

complete the script to print the mean and the standard deviation of the numbers included in the array.

Exercise 4 (perl4): play with strings!

Given the following array:

@sequences = ('AGAGTATATACA', 'AGAGATATCCACACAC', 'CCCATAATATATAT', 'GAGAGAAAAAAA');

complete the script in order to print all the sequences of the array in FASTA format, giving to each sequence the name “seq-number“, where number is a progressive number.
BONUS: If you are able to, print the sequences in FASTQ format, using as quality as many “i” as needed.

How to submit your work

Once you checked that your script is okay, you must submit the work done filling this form. Remember to put your login ID, and the correct homework ID (this is hw3, okay?). Finally we have a little poll!

Tell us how is it going with Perl...

  • I can understand examples, but when doing my scripts I sometimes have troubles (60%, 3 Votes)
  • I'm doing quite well: can understand examples and write my own scripts (40%, 2 Votes)
  • Can understand written examples, can't write ANYTHING in Perl (0%, 0 Votes)
  • I haz no idea (0%, 0 Votes)

Total Voters: 5

Loading ... Loading ...
Tagged

8 thoughts on “Homework: reviewing Perl basics

  1. Gloria says:

    Ciao! Io ho un problema con il quarto esercizio. A parte la fantasia con cui ho fatto la prima parte, cosa vuol dire il bonus? Cioè devo mettere una “i” per ogni lettera della sequenza? Come faccio però a sapere quante lettere ha ogni elemento/sequenza dell’array?!

    • proch says:

      Ciao,
      hai capito perfettamente! Per sapere quante lettere ha ogni sequenza ti serve una funzione che prenda come input una variabile e ti dia come output il numero di caratteri che ha. Prova a cercarla (generalmente si fa cosi: capisci cosa ti serve e lo cerchi)

      In questo caso ti aiuta la funzione length($stringa) che ti restituisce il numero di caratteri della stringa!

      • GiòGiò says:

        Ciao proch! Senti, sarà anche che sono un po’ rimba io, ma non ho capito una cosa… Devo associare ad ogni nucleotide una “i” ?????????

        • Andrea Telatin says:

          Ciao!
          Non sei rimba, semplicemente vogliamo stampare in formato “FASTQ” che prevede di mettere un carattere di qualità per ogni base. Ora, non essendo noi un vero sequenziatore che ha dei parametri per stimare la qualità di ciascuna base, la mettiamo uguale per ogni base e pari a “I”. Quindi si, avremo la stringa di sequenza con le quattro basi e la stringa di qualità (farlocca) con un solo possibile valore.

  2. Gloria says:

    Ok grazie! Quindi se io metto ad esempio $caratteri = length($elemento) mi dovrebbe dare il numero di caratteri dell’elemento dell’array?

  3. Matteo says:

    Per fortuna ogni volta che sto per porre una domanda l’ha già fatta qualcuno 🙂
    Mi associo all’ultima domanda di Gloria!

  4. Irene says:

    Ciao! Ho provato a fare il primo punto creando prima un array con i numeri da 0 a 10 e poi un ciclo for in modo che estraesse ad ogni ciclo un numero random dall’array e lo sommasse fino a quando la somma fosse < 27. Dopo dovrei stampare il numero dei cicli.Ho capito bene? Il problema è che mi stampa i numeri da 0 a 27 ma non quelli random. Come faccio? Grazie mille!

    • Andrea Telatin says:

      Domanda: a cosa ti serve l’array? Non puoi direttamente generare un numero random senza passare per l’array?

      Riformulando il problema: devi fare un’operazione (generare numeri random e sommarli in una variabile “somma”) FINTANTO CHE non si verifica una condizione (ovvero la variabile somma è supera un certo valore) non si verifica.

      While = fintanto che…

Leave a Reply to Gloria Cancel reply

Your email address will not be published. Required fields are marked *