Guest User

Untitled

a guest
May 8th, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.55 KB | None | 0 0
  1. //lab 1
  2.  
  3. #!/usr/bin/perl
  4. #
  5.  
  6. $file = $ARGV[0];   #takes in the command line argument
  7.  
  8. open(FH, $file) || die "Error - Could not open $file"; #opens the file or dies
  9.  
  10. @contents  = <FH>;
  11.  
  12. @words = format_input(@contents);
  13.  
  14. %wordcount = create_hash(@words);
  15.  
  16. @sorted_words = sort_words(%wordcount);
  17.  
  18. $average_length = average_length(@sorted_words);
  19.  
  20. output(@sorted_words, $average_length, $file);
  21.  
  22.  
  23.  
  24. #format_input
  25. #
  26. #splits the input file into different lines, converts the words to lowercase
  27. #and returns a list of the words
  28. sub format_input{
  29.   foreach $L(@contents){
  30.     chomp($L);              #perldoc -f chomp
  31.     $L = lc($L);            #converts all the word to lower case for comparison
  32.     $L =~ s/[[:punct:]]//g; #removes punctuation from the words
  33.   }
  34.   $text = "@contents"; #changes the list of lines into one big string
  35.   @words = split(/ /, $text); #splits the string up by each word
  36.   return @words;
  37. }
  38.  
  39. #create_hash
  40. #
  41. #associates the list of words with a list of counts that counts how many times
  42. #each word appears.
  43. #
  44. #
  45. sub create_hash{
  46.   foreach $L(@words){       #for each word determines if it is unique or not
  47.     if($wordcount{$L}==0){  #counts each word
  48.       $wordcount{$L}++;
  49.     }
  50.     else{$wordcount{$L}++;}
  51.   }
  52.   $wordcount{""}=0;         #changes the count of the blankspaces to zero
  53.   return %wordcount;        #quick fix for not counting blankspaces in the hash
  54.                             #could just use regex here...
  55. }
  56.  
  57. sub sort_words{             #uses perl's built in sort function to sort the hash
  58.   @sorted_words = sort { $wordcount{$b} <=> $wordcount{$a}  } keys %wordcount;
  59.   return @sorted_words;     #sorts the hash by value (count of each word)
  60. }
  61.  
  62. #average_length
  63. #
  64. #finds the average length of each word by taking the length of each
  65. #element of the list and then dividing by the total length of the list
  66. #
  67.  
  68. sub average_length{        
  69.   foreach $L(@sorted_words){
  70.     $count += length $L;
  71.   }
  72.   $average_length = ($count/(scalar @sorted_words));
  73.   return $average_length;
  74. }
  75.  
  76. #Nicely formatted output for the assignment
  77. #
  78. #
  79. sub output{
  80.   print "An analysis of $file:\n------------------------------------------\n";
  81.   print "There are ".scalar(@sorted_words)." unique words in the file.\n\n";
  82.   print "The average length of each word is $average_length\n\n";
  83.   print "The 5 most common words are:\n";
  84.   for($i=0;$i<5;$i++){
  85.     print "\"@sorted_words[$i]\" occured $wordcount{@sorted_words[$i]} times.\n";
  86.   }
  87.   print "-------------------------------------------\n";
  88. }
  89. close(FH);
  90.  
  91. #!/usr/bin/perl
  92. #
  93. #game_guess.pl
  94. #
  95.  
  96. #check_guess
  97. #
  98. #makes sure that the guess is a valid guess
  99. #i.e. that the guess is between 0..10
  100. sub check_guess{
  101.   @bin = (0 .. 10);  #bin of possible guesses
  102.   $guessd = @_[0];    
  103.   $flag = ($guessd ~~ @bin);
  104.   while($flag < 1){   #asks the user for another guess  
  105.     print "I don't have that many fingers...guess again:\n";
  106.     $guessd = <>;     #won't count guesses out of range in total guess
  107.     $flag = ($guessd ~~ @bin);
  108.   }
  109.   $flag = 0;
  110.   return $guessd;
  111. }
  112.  
  113. $secret = int(rand(11));  #randomly picks the number of fingers
  114. $count = 0;               #behind back
  115.  
  116.  
  117. do {print "How many fingers am I holding behind my back?\n";
  118.   $guess = <>;
  119.   $guess = check_guess($guess);
  120.   if($guess > $secret){
  121.     print "Your guess is too high\n";
  122.   }
  123.   elsif($guess < $secret){
  124.     print "Your guess is too low\n";
  125.   }
  126.   $count++;
  127.  
  128.   if(($count % 5)==0){      #after every 5 guesses, randomly picks a new integer
  129.     $secret = int(rand(11));    
  130.   }
  131.  
  132. } while($guess != $secret);
  133.  
  134. #congratulatory message after you are right
  135. print "Correct! I was holding $secret fingers behind my back, it took you $count tries to guess though!\n";
  136.  
  137. #!/usr/bin/perl
  138. #Takes in your name as a command line argument.  Prints out your name
  139. #and counts the number of characters in your name
  140. #
  141. #Reverses all the characters in your name
  142. #Prints your initials
  143. #And prints your name backwards
  144.  
  145. #Takes in the command line argument as your name
  146.  
  147. $name = join(" ", @ARGV);     #takes the list of command line arguments as string
  148. $namecount = join("", @ARGV);
  149. @initials = @ARGV;
  150. for(@ARGV){             #reverses each part of your name
  151.   $_ = reverse $_;
  152. }
  153. $reversed = join(" ", @ARGV);   #joins together reversed name
  154. for(@initials){                 #generates your initials from the string
  155.   $_ = uc(substr($_,0,1));
  156. }
  157. $initials = join(".", @initials);    
  158. $backwards = reverse($name);      #generates your name backwards
  159.  
  160.  
  161. #output commands...
  162. #subtracts 1 for the \n character.  alternatively, could use the perl
  163. #command chomp() (perldoc -f chomp) to remove this character...
  164.  
  165. print "Hello $name\nYou have ".(length($namecount))." characters in your name\n";
  166. print "Reversed: $reversed\n";  
  167. print "Your initials are $initials.\n";
  168. print "Your name backwards is $backwards\n";
  169.  
  170. #!/usr/bin/perl
  171. #
  172. #calculates how many days it will be until your next birthday
  173. #
  174.  
  175.  
  176. $bmonth = 11; #default birthday month, November
  177. $bday = 27;   #default birthday day
  178.  
  179. @current_time = localtime();  # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
  180. @daysinmonth = (31,28,31,30,31,30,31,31,30,31,30,31); #defines the number of days months
  181.  
  182. $total_bday = num_birthday($bmonth, $bday, $daysinmonth); #enumerates birthday
  183. $total_day  = num_today(@current_time, $daysinmonth);     #enumerates current day
  184.  
  185. $days_until_birthday = has_birthday_passed($total_bday, $total_day, @current_time); #has your birthday already passed this year?
  186. $days_until_birthday = leap_year($days_until_birthday, @current_time, $bmonth); #is there a leap day in between today and your birthday?
  187.  
  188. output($bmonth, $bday, $days_until_birthday, @current_time); #prints information
  189.  
  190. #num_birthday
  191. #
  192. #counts the number of days from 1/1 to your birthday
  193. sub num_birthday{
  194.   for($i = 0; $i< ($bmonth-1); $i++){
  195.     $total_bday += $daysinmonth[$i];
  196.   }
  197.  
  198.   $total_bday += $bday;
  199.   return $total_bday;
  200. }
  201.  
  202. #num_today
  203. #
  204. #counts the number of days since 1/1
  205. sub num_today{
  206.   for($i = 0; $i< ($current_time[4])-1; $i++){
  207.     $total_day +=$daysinmonth[$i];
  208.   }
  209.   $total_day +=$current_time[3];
  210.   return $total_day;
  211. }
  212.  
  213. #has_birthday_passed
  214. #
  215. #checks if your birthday has already occurred this year
  216. #if it has, we use 365-(difference), otherwise we can just
  217. #use the different in days
  218.  
  219. sub has_birthday_passed{
  220.   if($total_bday < $total_day){
  221.    $days_until_birthday = 365 - abs($total_bday - $total_day);
  222.   }
  223.   else{
  224.    $days_until_birthday = abs($total_bday - $total_day);
  225.   }
  226.   return $days_until_birthday;
  227. }
  228.  
  229. #leap_year
  230. #
  231. #every leap year is defined as a year which is divisible by 4 but not 100
  232. #if your birthday hasn't occurred yet this year, checks if the current year is a
  233. #leap year.
  234. #
  235. #if your birthday has already occurred this year, checks if the next year is a leap
  236. #year
  237.  
  238. sub leap_year{
  239.   if($bmonth > $current_time[4]){
  240.    if((@current_time[5] % 4 == 0) && (@current_time[5] % 100 != 0)){
  241.     $days_until_birthday++;
  242.    }
  243.   }
  244.   elsif(((@current_time[5]+1) % 4 == 0) && ((@current_time[5]+1) % 100 != 0)){
  245.    $days_until_birthday++;
  246.   }
  247.   return $days_until_birthday;
  248. }
  249.  
  250. #output
  251. #
  252. #formats the output nicely.  if today is your birthday, it prints happy birthday
  253. #otherwise it will print the number of days until your birthday
  254.  
  255. sub output{
  256.   if(($bmonth-1 == @current_time[4]) && ($bday == @current_time[3])){
  257.     print "Happy Birthday!\n";
  258.   }
  259.   else{
  260.    print "If your birthday is on $bmonth/$bday, it is $days_until_birthday days until your birthday\n";
  261.   }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment