Advertisement
kwasinski

Simple Perl Test

Dec 2nd, 2022
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.78 KB | None | 0 0
  1. #!bin/perl -w
  2.  
  3. sub identifyTax($)
  4. {
  5.     my ($transactions) = @_;
  6.  
  7.     my $fee = 5;
  8.     my $maximumTocalculateTax = 100;
  9.     my $minimumTransactionsPerMonth = 3;
  10.     my $numberOfMonthsToChargeFee = 0;
  11.  
  12.     foreach my $date (keys %{$transactions})
  13.     {
  14.         $_ = $date;
  15.  
  16.         my $currentMonth = $1 if m/\-(\d{2})\-/;
  17.         my $numberOfTransactionsForCurrentMonth = 0;
  18.         my $numberOfTransactionsWithinTheSameMonthAbove100 = 0;
  19.         my $chargeFeeInThisMonth = 0;
  20.  
  21.         foreach my $date (keys %{$transactions})
  22.         {
  23.             $_ = $date;
  24.  
  25.             my $monthToCheck = $1 if m/\-(\d{2})\-/;
  26.  
  27.             $numberOfTransactionsForCurrentMonth++
  28.                 if $monthToCheck eq $currentMonth;
  29.  
  30.             $numberOfTransactionsWithinTheSameMonthAbove100++
  31.                 if $transactions->{$date} >= 100;
  32.  
  33.         }
  34.  
  35.         if ($numberOfTransactionsForCurrentMonth >= 1 && $numberOfTransactionsWithinTheSameMonthAbove100 >= 3)
  36.         {
  37.             $chargeFeeInThisMonth = 1;
  38.         }
  39.  
  40.         $numberOfMonthsToChargeFee++ if $chargeFeeInThisMonth == 1;
  41.     }
  42.  
  43.     return $numberOfMonthsToChargeFee * $fee;
  44.  
  45. }
  46.  
  47. sub sumTransactions($)
  48. {
  49.     my ($transactions) = @_;
  50.     my $totalBalance = 0;
  51.  
  52.     foreach my $date (keys %{$transactions})
  53.     {
  54.         $totalBalance += $transactions->{$date};
  55.     }
  56.  
  57.     return $totalBalance;
  58. }
  59.  
  60. my $transactionsArrayRef =  [100, 100, 100, -10];
  61. my $transactionDatesArrayRef =  ['2020-12-31', '2020-12-22', '2020-12-03', '2020-12-29'];
  62.  
  63.  
  64. my %transactions;
  65. @transactions{@{$transactionDatesArrayRef}} = @{$transactionsArrayRef};
  66.  
  67. my $tax = identifyTax(\%transactions);
  68. my $totalBalance = sumTransactions(\%transactions);
  69.  
  70. $totalBalance -= $tax;
  71. print "total balance " . $totalBalance;
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement