Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Hotels are often rated using stars to represent their score. A five-star hote

ID: 3715493 • Letter: 1

Question

1. Hotels are often rated using stars to represent their score. A five-star hotel may have a king-size bed, a kitchen, and two TVs; a one-star hotel may have cockroaches and a leaky roof, a. Write a subroutine called printstar that will produce a histogram to show the star rating for hotels shown in the following hash. The printstar function will be given two parameters: the name of the hotel and the number of its star rating. (Hint: sort the hash keys into an array. Use a loop to iterate through the keys, calling the printstar function for each iteration.) %hotels-("Pillowmint Lodge" => "5". "Buxton Suites" "5" "The Middletonian" => "3". "Notchbelow" "Rancho El Cheapo" => "1". "Pile Inn" (OUTPUT) Hotel Category Notchbelow The Middletonian ** Pillowmint Lodge **I Pile Inn Rancho El Cheapo Buxton Suites ae b. Sort the hotels by stars, five stars first, one star last. Can you sort the hash by values so that the five-star hotels are printed first, then four, and so forth? (See http:/lalvinalexander.com/perlledulgandalplaa00016.) Hotel Category Buxton Suites Pillowmint Lodge * Notchbelow The Middletonian ** Pile Inn Rancho El Cheapo ae

Explanation / Answer

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

sub printstar
{
printf "%-18s| ",$_[0];
  
for(my $i=0;$i<$_[1];$i=$i+1)
{
print "*";
}
  
printf " ";
}

#create hash for hotels

my %hotels=("Pillowmint lodge" => 5 ,"Buxten suites" =>5 , "The middletonian" =>3,"Notchbelow" =>4,"Rancho El Cheapo" =>1 , "Pile Inn" =>2 );

my $count=keys %hotels; #Total hotels
my @hotel= keys %hotels;  
my @star= values %hotels ;


print "Hotel Category ---------------------------- ";
for( my $i=0; $i < $count ;$i=$i+1)
{
printstar($hotel[$i],$star[$i]); # calling a printstar function

}
print "--------------------------- ";

print " Hotel Category ----------------------------- ";

# sorting of hotels according to star in revrese order

foreach my $hotel ( reverse sort { $hotels{$a} <=> $hotels{$b} } keys %hotels )
{
printf "%-18s| %-6s| ",$hotel,$hotels{$hotel};  
}
print "----------------------------- ";

OUTPUT : -