简体   繁体   中英

Python translation from C for CS50 greedy algorithm

So, we all know that CS50 is a big class and lots to learn. Here's another issue I'm having with Python now. The syntax took a little while to get right with all the indentation changes but the logic seems to be very similar. The code works up until you put in .99 or even 1.20. BUT, and it's a big but, I can't debug in the cloud9 ide with Python.....? Idk. I just started Python this week so I'm sure it's a language oriented issue and I just need to figure that out. Hope you can help me. Thanks.

#Greedy algorithm converted to python
import sys
import os

c = float(input("How much change is owed? "))
i = 0
while (c<0 or c==0):
    print("Please input a positive amount...")

while (c>.24):

    i  += 1
    c=(c-.25)

while (c>.1 or c==.1):

    i += 1
    c=(c-.1)


while (c>.05 or c==.05):

    i += 1
    c=(c-.05)


while (c>.01 or c==.01):

    i += 1
    c=(c-.01)

print("%i coin(s) needed to make the change." % i)

You're likely running into rounding issues with float . After converting it to cents and using integers, it works:

#Greedy algorithm converted to python
import sys
import os

c = float(input("How much change is owed? "))
i = 0
while (c<0 or c==0):
    print("Please input a positive amount...")
c = round(c * 100)
while (c>=25):

    i  += 1
    c=(c-25)

while (c>=10):

    i += 1
    c=(c-10)


while (c>=5):

    i += 1
    c=(c-5)


while (c>=1):

    i += 1
    c=(c-1)

print("%i coin(s) needed to make the change." % i)

My C# implementation seems off but look below for it's python implementation. Using cs50's header file.

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int greedyAlgorithm(float change);

//main
int main(void)
{
    float change;
    do
    {
        printf("What is the change owed\n");
        change = get_float("$: ");
    }while(change<0);
    greedyAlgorithm(change);
}

//greedyAlgorithm implementation
int greedyAlgorithm(float change)
{
    int coins[4] = {25, 10, 5, 1};
    int amount, totalCoins, tmpNumber, remainder, quarterCounter, dimeCounter, nickelCounter, pennyCounter;
    amount = round(change * 100);
    tmpNumber = 0;
    remainder = 0;
    totalCoins = 0;
    quarterCounter =0;
    dimeCounter = 0;
    nickelCounter = 0;
    pennyCounter = 0;
    
    if (amount >= coins[0])//quarter
    {
        remainder = amount % coins[0];
        tmpNumber = amount - remainder;
        totalCoins = totalCoins + (tmpNumber / coins[0]);
        quarterCounter = tmpNumber / coins[0];
        amount = remainder;
        printf("Quarters: %i\n", quarterCounter);
    }
        if (amount >= coins[1])//dime
    {
        remainder = amount % coins[1];
        tmpNumber = amount - remainder;
        totalCoins = totalCoins + (tmpNumber / coins[1]);
        dimeCounter = tmpNumber / coins[1];
        amount = remainder;
        printf("Dimes: %i\n", dimeCounter);
    }
        if (amount >= coins[2])//nickle
    {
        remainder = amount % coins[2];
        tmpNumber = amount - remainder;
        totalCoins = totalCoins + (tmpNumber / coins[2]);
        nickelCounter = tmpNumber / coins[2];
        amount = remainder;
        printf("Nickels: %i\n", nickelCounter);
    }
        if (amount >= coins[3])//penny
    {
        remainder = amount % coins[3];
        tmpNumber = amount - remainder;
        totalCoins = totalCoins + (tmpNumber / coins[3]);
        pennyCounter = tmpNumber / coins[3];
        amount = remainder;
        printf("Pennies: %i\n", pennyCounter);
    }
    printf("Total Coins: %i\n", totalCoins);
    return 0;
}```
**Python equivalent**
#get user input
import math


def greedyAlgorithm(change):
    coins = [25, 10, 5, 1] #coins denominations
    tmpNumber = 0
    remainder = 0
    totalCoins = 0
    quarterCounter =0
    dimeCounter = 0
    nickelCounter = 0
    pennyCounter = 0
    amount = round(int(change*100))
    if amount >= coins[0]: #quarter
        remainder = amount % coins[0]
        tmpNumber = amount - remainder
        totalCoins = totalCoins + (tmpNumber / coins[0])
        quarterCounter = tmpNumber / coins[0]
        amount = remainder
        print(f"Quarters:  {int(quarterCounter)}")
    
    if amount >= coins[1]: #dime
    
        remainder = amount % coins[1]
        tmpNumber = amount - remainder
        totalCoins = totalCoins + (tmpNumber / coins[1])
        dimeCounter = tmpNumber / coins[1]
        amount = remainder
        print(f"Dimes:  {int(dimeCounter)}")
    
    if amount >= coins[2]: #nickle
    
        remainder = amount % coins[2]
        tmpNumber = amount - remainder
        totalCoins = totalCoins + (tmpNumber / coins[2])
        nickelCounter = tmpNumber / coins[2]
        amount = remainder
        print(f"Nickels:  {int(nickelCounter)}")
    
    if amount >= coins[3]:#penny
    
        remainder = amount % coins[3]
        tmpNumber = amount - remainder
        totalCoins = totalCoins + (tmpNumber / coins[3])
        pennyCounter = tmpNumber / coins[3]
        amount = remainder
        print(f"Pennies: {int(pennyCounter)}")
    print(f"Total Coins: {int(totalCoins)}")
    
            
print("What is the change owed")
print("$: ", end="")
change = float(input())
greedyAlgorithm(change)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM