DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: Counting the discounts

Weekly Challenge 325

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. It's a great way for us all to practice some coding.

Challenge, My solutions

Task 1: Consecutive One

Task

You are given a binary array containing only 0 or/and 1.

Write a script to find out the maximum consecutive 1 in the given array.

My solution

This is pretty straight forward, so doesn't need much explanation. I have two variables. max_count stores the maximum ones found so far, while current_count is the current run of ones found. Both start with 0.

I then have a loop that iterates through each input. If the value is 1, I increment the current_count value. If this is greater than the max_count value, I increment that too.

def consecutive_ones(ints: list) -> int:
    max_count = 0
    current_count = 0

    for num in ints:
        if num == 1:
            current_count += 1
            if current_count > max_count:
                max_count = current_count
        else:
            current_count = 0

    return max_count
Enter fullscreen mode Exit fullscreen mode

The Perl solution follows the same logic.

Examples

$ ./ch-1.py 0 1 1 0 1 1 1
3

$ ./ch-1.py 0 0 0 0
0

$ ./ch-1.py 1 0 1 0 1 1
2
Enter fullscreen mode Exit fullscreen mode

Task 2: Final Price

Task

You are given an array of item prices.

Write a script to find out the final price of each items in the given array.

There is a special discount scheme going on. If there’s an item with a lower or equal price later in the list, you get a discount equal to that later price (the first one you find in order).

Solution

So like most solutions, I start by writing the unit tests first, following TDD principles. I then have some boiler plate code that creates a main function that calls the function itself, in this case the final_price function.

And as soon as that is done GitHub Copilot has written the function and docstring for me. Copilot never ceases to amaze me. At work I was struggling with understand a nginx configuration file that was written by some one else. Copilot's explanation made everything make sense, and answered some questions I asked. This is something that Stack Overflow simple cannot do.

Nearly every developer should be making good use of AI agents today, especially given that Copilot now has a very generous free tier for Visual Studio Code. For me, work pay for the premium plans.

I read an interesting article this week about how Canva are encouraging potential employees to use AI in interviews. It discusses how they frame questions so AI can assist, but still require the interviewee to turn the requirements in to working code.

Any way, back to my Copilot's solution. I'll let Copilot explain it.

The final_price function calculates the final price for each item in a list after applying a specific discount rule. For each item in the prices list, the function looks for the first subsequent item (to the right) whose price is less than or equal to the current item's price. If such an item is found, its price is used as a discount for the current item; otherwise, no discount is applied (the discount remains zero).

The function iterates through the list using a for-loop with index i. For each item at position i, it initializes a discount variable to zero. Then, it uses another for-loop with index j (starting from i + 1) to search for the first qualifying discount. If it finds a price at j that is less than or equal to the price at i, it sets discount to that value and breaks out of the inner loop. After determining the discount, it subtracts the discount from the original price and appends the result to the solution list (array in Perl). Once all items have been processed, the function returns the solution list containing the final prices after discounts.

This approach ensures that each item's discount is determined only by the first qualifying item to its right, which matches the typical "next smaller or equal element" discount pattern seen in some shopping scenarios. The function is straightforward but has a time complexity of O(n^2) due to the nested loops, which could be a consideration for very large input lists.

def final_price(prices: list) -> list:
    solution = []
    for i in range(len(prices)):
        discount = 0
        for j in range(i + 1, len(prices)):
            if prices[j] <= prices[i]:
                discount = prices[j]
                break
        solution.append(prices[i] - discount)

    return solution
Enter fullscreen mode Exit fullscreen mode

Like the first task, the Perl solution is a transliteration of the Python one.

Examples

$ ./ch-2.py 8 4 6 2 3
[4, 2, 4, 2, 3]

$ ./ch-2.py 1 2 3 4 5
[1, 2, 3, 4, 5]

$ ./ch-2.py 7 1 1 5
[6, 0, 1, 5]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)