In: Computer Science
Convert the following code from awk to Perl: (please don't use a2p (awk to perl) command)
Here was the original problem:
Calculate the grade for each student that appears in the data file. You may calculate the grade based on total earned points, divided by total possible points. Or, for 10 points extra credit; Use the weighted totals for this course; available in the syllabus and on the course home page.
Output a list of students, their grade as a percentage and a letter grade. For simplicity; use this letter grade scale;
The following sample output has been provided as a guide. Your solution must be based on the data in the Lab03-data.csv file. No points will be awarded to solutions that are based on copy-pasting the sample output and printing it with awk.
Name Percent Letter Andrew 75.21 C Chelsey 92.21 A Shane 77.64 C Ava 79.76 C Sam 62.34 D
Here is some of the sample data from the file (Lab03-data.csv)
Student | Catehory | Assignment | Score | Possible |
Sam | Quiz | Q07 | 68 | 100 |
Sam | Final | FINAL | 58 | 100 |
Sam | Survey | WS | 5 | 5 |
Andrew | Homework | H01 | 25 |
100 |
Here is the AWK code that needs to be converted to PERL
BEGIN{
FS = ","
print "Name\tPercent\tLetter"
a=0
}
{
if(a==0){
a+=1
}
else{
sum[$1$2] += $4
total[$1$2] += $5
students[$1]++
categories[$2]++
}
}
END{
#for(b in sum)
# percent=(sum[b]/total[b])*100
for (b in students){
Homework=(sum[b"Homework"]/total[b"Homework"])*0.10
Lab=(sum[b"Lab"]/total[b"Lab"])*.30
Final=(sum[b"Final"]/total[b"Final"])*.15
Quiz=(sum[b"Quiz"]/total[b"Quiz"])*.40
Survery=(sum[b"Survey"]/total[b"Survey"])*.05
percent=(Homework+Lab+Final+Quiz+Survery)*100
printf "%s\t%.2f\t",b,percent
if(percent>=90 && percent<=100)
print "A";
else if(percent>=80 &&
percent<90)
print "B";
else if(percent>=70 &&
percent<80)
print "C";
else if(percent>=60 &&
percent<70)
print "D";
else
print "E"
}
}
Answer:
Note:
Program Code Screen Shot:
Sample Output:
Sample Input Data: Lab03-data.csv
Student,Catehory,Assignment,Score,Possible
Chelsey,Homework,H01,90,100
Chelsey,Homework,H02,89,100
Chelsey,Homework,H03,77,100
Chelsey,Homework,H04,80,100
Chelsey,Homework,H05,82,100
Chelsey,Homework,H06,84,100
Chelsey,Homework,H07,86,100
Chelsey,Lab,L01,91,100
Chelsey,Lab,L02,100,100
Chelsey,Lab,L03,100,100
Chelsey,Lab,L04,100,100
Chelsey,Lab,L05,96,100
Chelsey,Lab,L06,80,100
Chelsey,Lab,L07,81,100
Chelsey,Quiz,Q01,100,100
Chelsey,Quiz,Q02,100,100
Chelsey,Quiz,Q03,98,100
Chelsey,Quiz,Q04,93,100
Chelsey,Quiz,Q05,99,100
Chelsey,Quiz,Q06,88,100
Chelsey,Quiz,Q07,100,100
Chelsey,Final,FINAL,82,100
Chelsey,Survey,WS,5,5
Sam,Homework,H01,19,100
Sam,Homework,H02,82,100
Sam,Homework,H03,95,100
Sam,Homework,H04,46,100
Sam,Homework,H05,82,100
Sam,Homework,H06,97,100
Sam,Homework,H07,52,100
Sam,Lab,L01,41,100
Sam,Lab,L02,85,100
Sam,Lab,L03,99,100
Sam,Lab,L04,99,100
Sam,Lab,L05,0,100
Sam,Lab,L06,0,100
Sam,Lab,L07,0,100
Sam,Quiz,Q01,91,100
Sam,Quiz,Q02,85,100
Sam,Quiz,Q03,33,100
Sam,Quiz,Q04,64,100
Sam,Quiz,Q05,54,100
Sam,Quiz,Q06,95,100
Sam,Quiz,Q07,68,100
Sam,Final,FINAL,58,100
Sam,Survey,WS,5,5
Program Code to Copy:
#!/usr/bin/perl
use strict;
use warnings;
# open the csv file from the command
line
open (my $inFile, '<', $ARGV[0]) or die $!;
# get the header of the file
my $header=<$inFile>; #First line is read
here
# declare and initialize the iterative
variable
my $i =0;
# define the hash variable
my %data = ();
# define an array to hold the
assignment names
my @testNames;
# loop through each line in the
file
while (my $line = <$inFile>)
{
# record separator
chomp $line;
# split the line at delimiter ','
# and set the values into the array called
# fields
my @fields = split "," , $line;
# from the array of fields, get the assignment
Name
# set it to array testNames at index i
$testNames[$i] = $fields[0];
# push the values of the respective score and
possible scores
# as list into hash data
push (@{$data{$testNames[$i]}}, ($fields[3],
$fields[4]));
# increment the iterative variable
$i = $i + 1;
}
# close the file
close $inFile;
# print the header
printf("%-8s %8s \t %s \n", "Name", "Percent", "Letter");
# loop through each key and its
respective data
foreach my $key ( sort keys %data )
{
# get the values at each key(Assignment)
my @eachListValues = @{ $data{$key}};
# set the total to 0
my $total = 0;
# set the $possibleTotal to 0
my $possibleTotal = 0;
# set the average value to 0
my $average = 0;
# initialize to zero
$i = 0;
# loop through each value in the array
list
# of each assignment
foreach my $val (@eachListValues)
{
# since there are two
values stored at a time
# that is score and its
respective possible score
# score values, so, in
the even positions, the score
# is stored and at odd
positions, the possible scores
# are stored.
# if i is even, then add
val to the total
if($i % 2 == 0)
{
# total the $val
$total += $val;
}
# if i is odd, then add
val to the possibleTotal
else
{
$possibleTotal += $val;
}
# increment the i
variable
$i += 1;
}
# find the average of each student
$average = ($total / $possibleTotal) *
100;
# define the grade variable to hold the
# grade value
my $grade="";
# conditions to check if the average is
# within 90-100 set the grade to "A"
if( $average >= 90 && $average <=
100)
{
$grade = "A";
}
# conditions to check if the average is
# within 80-90 set the grade to "B"
elsif( $average >= 80 && $average
< 90)
{
$grade = "B";
}
# conditions to check if the average is
# within 70-80 set the grade to "C"
elsif( $average >= 70 && $average
< 80)
{
$grade = "C";
}
# conditions to check if the average is
# within 60-70 set the grade to "D"
elsif( $average >= 60 && $average
<70)
{
$grade = "D";
}
# Otherwise set the grade to "E"
else
{
$grade = "E";
}
# display the values
printf("%-8s %8.2f \t %4s\n", $key, $average,
$grade);
}