Python Forum

Full Version: coding problem from older book
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I've taken up coding as a hobby and have been going through some vintage programming books looking for coding problems and challenges to try. I've found one in an old book about Commodore 64 BASIC programming that reads as follows:

"In early January, a shopkeeper marked down some calendars from $2.00 to a lower price. He sold his entire stock in one day for $603.77. How many did he have?"

Since I'm presently working on learning Python, I decided to try and implement a solution in Python. Here's what I came up with:

total_profit = 603.77
original_price = 2.00
for i in range(int((original_price*100)), 0, -1):
    if ((total_profit*100) % i == 0):
        sale_price = i/100
        calendars_sold = int(total_profit/sale_price)
        break

print("Sale price =", sale_price)
print("Number of calendars sold =", calendars_sold)
Which produces the output:

Output:
Sale price = 1.73 Number of calendars sold = 349
I'm convinced this is the correct answer, but I'm not convinced that the way I implemented it is the most efficient or intuitive way of reaching the right answer. If anyone has any suggestions as to how I could have approached it clearer or more simply, it'd be much appreciated.

The only thing I'd add is that the book that the original problem came from didn't offer a solution in BASIC, so my solution isn't an adaptation of any other kind of code—I came up with it purely on my own.

Thanks!
This is very good start. There are only couple of minor things.

total_profit = 603.77
original_price = 2.00
for price in range(int(original_price * 100), 0, -1):
    if not (total_profit * 100) % price: # brackets again not necessary, but maybe make it a bit clear
        sale_price = price / 100
        calendars_sold = int(total_profit / sale_price)
        break
 
print(f"Sale price is {sale_price}")
print(f"Number of calendars sold is {calendars_sold}")
no need of all brackets you use, better use f strings, you can take advantage of 0 being considered False value...
Then there is the problem that **possibly** there are multiple solutions. You can also take advantage of string formatting

total_profit = 603.77
original_price = 2.00
for price in range(int(original_price * 100), 0, -1):
    if not (total_profit * 100) % price: # brackets again not neccessary, but maybe make it a bit clear
        print(f"Sale price is {price / 100:.2f}")
        print(f"Number of calendars sold is {total_profit / price:.0f}")
Alternatively you can work with decimal.Decimal objects

from decimal import Decimal
total = Decimal("603.77")
price = Decimal("2.00")
while price > 0:
    items_sold = total / price
    if not items_sold % 1:
        print(f"Sale price is {price}")
        print(f"Number of calendars sold is {items_sold}")
    price -= Decimal("0.01")
In any case you can opt for defining a function

from decimal import Decimal

def get_items_sold(total, price):
    while price > 0:
        items_sold = total / price
        if not items_sold % 1:
            yield price, items_sold 
        price -= Decimal("0.01")

if __name__ == '__main__':
    total = Decimal("603.77")
    price = Decimal("2.00")
    for price, items_sold in get_items_sold(total, price):
        print(f"Sale price is {price}")
        print(f"Number of calendars sold is {items_sold}")
Here get_items_sold is generator (note the yield). If you use return instead of yield it will return after first price.
Some comments:

- name total_profit is misleading. Correct name could be revenue or sales_revenue

- total sales revenue ends with 7 and this is result of quantity and price multiplication. So possible ending digits of factors can be 1, 7 and 3, 9. This could be used for 'more efficient' solution. However, this is would be solution for specific amount.
Thanks for the detailed replies! I'll work with these suggestions and see if I can optimize my code. Thanks again, much appreciated.