The weekly challenge 325 - Task 2: Final Price
1 #!/usr/bin/env perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-325/#TASK2 3 # 4 # Task 2: Final Price 5 # =================== 6 # 7 # You are given an array of item prices. 8 # 9 # Write a script to find out the final price of each items in the given array. 10 # 11 # There is a special discount scheme going on. If there’s an item with a lower 12 # or equal price later in the list, you get a discount equal to that later 13 # price (the first one you find in order). 14 # 15 ## Example 1 16 ## 17 ## Input: @prices = (8, 4, 6, 2, 3) 18 ## Output: (4, 2, 4, 2, 3) 19 ## 20 ## Item 0: 21 ## The item price is 8. 22 ## The first time that has price <= current item price is 4. 23 ## Final price = 8 - 4 => 4 24 ## 25 ## Item 1: 26 ## The item price is 4. 27 ## The first time that has price <= current item price is 2. 28 ## Final price = 4 - 2 => 2 29 ## 30 ## Item 2: 31 ## The item price is 6. 32 ## The first time that has price <= current item price is 2. 33 ## Final price = 6 - 2 => 4 34 ## 35 ## Item 3: 36 ## The item price is 2. 37 ## No item has price <= current item price, no discount. 38 ## Final price = 2 39 ## 40 ## Item 4: 41 ## The item price is 3. 42 ## Since it is the last item, so no discount. 43 ## Final price = 3 44 # 45 # 46 ## Example 2 47 ## 48 ## Input: @prices = (1, 2, 3, 4, 5) 49 ## Output: (1, 2, 3, 4, 5) 50 # 51 # 52 ## Example 3 53 ## 54 ## Input: @prices = (7, 1, 1, 5) 55 ## Output: (6, 0, 1, 5) 56 ## 57 ## Item 0: 58 ## The item price is 7. 59 ## The first time that has price <= current item price is 1. 60 ## Final price = 7 - 1 => 6 61 ## 62 ## Item 1: 63 ## The item price is 1. 64 ## The first time that has price <= current item price is 1. 65 ## Final price = 1 - 1 => 0 66 ## 67 ## Item 2: 68 ## The item price is 1. 69 ## No item has price <= current item price, so no discount. 70 ## Final price = 1 71 ## 72 ## Item 3: 73 ## The item price is 5. 74 ## Since it is the last item, so no discount. 75 ## Final price = 5 76 # 77 ############################################################ 78 ## 79 ## discussion 80 ## 81 ############################################################ 82 # 83 # We walk from the beginning of @prices to its end. In so doing, 84 # we check all elements from that element to the end to see if one 85 # is less than or equal in price. If yes, we calculate the new 86 # discounted price and stop walking to the end for this iteration. 87 # Then we check whether there is a discounted price and use either 88 # that or the original price if there isn't. 89 90 use v5.36; 91 92 final_price(8, 4, 6, 2, 3); 93 final_price(1, 2, 3, 4, 5); 94 final_price(7, 1, 1, 5); 95 96 sub final_price( @prices ) { 97 say "Input: (" . join(", ", @prices) . ")"; 98 my @result = (); 99 foreach my $i (0..$#prices) { 100 my $discounted = -1; 101 foreach my $j ($i+1..$#prices) { 102 if($prices[$j] <= $prices[$i]) { 103 $discounted = $prices[$i] - $prices[$j]; 104 last; 105 } 106 } 107 if($discounted >= 0) { 108 push @result, $discounted; 109 } else { 110 push @result, $prices[$i]; 111 } 112 } 113 say "Output: (" . join(", ", @result) . ")"; 114 }