Mar-06-2021, 02:35 AM
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:
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!
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!