Python Forum
coding problem from older book
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
coding problem from older book
#1
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!
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thanks for the detailed replies! I'll work with these suggestions and see if I can optimize my code. Thanks again, much appreciated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Installing Older Version Of Numpy & Pandas knight2000 6 1,772 Aug-30-2023, 10:58 AM
Last Post: knight2000
  Armachat coding problem gad969 4 1,308 Mar-12-2023, 03:12 PM
Last Post: gad969
  Coding problem portfolio grade max70990 1 732 Dec-11-2022, 12:30 PM
Last Post: Larz60+
  remove files from folder older than X days kerzol81 2 8,645 Jan-03-2020, 11:55 PM
Last Post: snippsat
  merging lists, dedup and keeping order, in older pythons Skaperen 3 3,163 Oct-19-2018, 01:30 AM
Last Post: ODIS
  Begginer coding problem wiktor 2 3,215 Jan-26-2018, 06:24 PM
Last Post: wiktor
  IR coding problem DPaul 3 2,760 Jan-09-2018, 08:02 AM
Last Post: DPaul
  new user coding problem mattkrebs 1 3,016 Mar-21-2017, 12:45 PM
Last Post: buran
  Coding problem VISHU 3 4,825 Mar-16-2017, 06:54 AM
Last Post: Larz60+
  Incredibly basic coding problem AndyF 12 8,508 Feb-11-2017, 03:52 PM
Last Post: AndyF

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020