Python URI Online Judge (1021): what’s wrong with my code?

Asked

Viewed 1,261 times

0

Enunciation:

Read a floating point value to two decimal places. This value represents a monetary value. Next, calculate the smallest number of possible banknotes and coins into which the value can be broken down. Banknotes are 100, 50, 20, 10, 5, 2. The possible currencies are 1, 0.50, 0.25, 0.10, 0.05 and 0.01. Show below the list of notes necessary.

Entree

The input file contains a floating point value N (0 N 1000000.00).

Exit

Print the minimum amount of banknotes and coins needed to exchange the initial value, as shown in the example provided.

Note: Use dot (.) to separate the decimal part.

(https://www.urionlinejudge.com.br/judge/pt/problems/view/1021)


My code:

# -*- coding: utf-8 -*-

valor = float(input())
cem_r = int(valor // 100)
valor %= 100
cinquenta_r = int(valor // 50)
valor %= 50
vinte_r = int(valor // 20)
valor %= 20
dez_r = int(valor // 10)
valor %= 10
cinco_r = int(valor // 5)
valor %= 5
dois_r = int(valor // 2)
valor %= 2
um_r = int(valor // 1)
valor %= 1
cinquenta_c = int(valor // 0.50)
valor %= 0.50
vintecinco_c = int(valor // 0.25)
valor %= 0.25
dez_c = int(valor // 0.10)
valor %= 0.10
cinco_c = int(valor // 0.05)
valor %= 0.05
um_c = int(valor // 0.01)
print("""NOTAS:
{} nota(s) de R$ 100.00
{} nota(s) de R$ 50.00
{} nota(s) de R$ 20.00
{} nota(s) de R$ 10.00
{} nota(s) de R$ 5.00
{} nota(s) de R$ 2.00
MOEDAS:
{} moeda(s) de R$ 1.00
{} moeda(s) de R$ 0.50
{} moeda(s) de R$ 0.25
{} moeda(s) de R$ 0.10
{} moeda(s) de R$ 0.05
{} moeda(s) de R$ 0.01""".format(cem_r, cinquenta_r, vinte_r, dez_r, cinco_r,
                                 dois_r, um_r, cinquenta_c, vintecinco_c, dez_c,
                                 cinco_c, um_c))

I performed all the tests available on the site and, in all, worked as expected.

Even so I keep getting the message "Wrong Answer (100%)", which means I missed 100% of the question.

What do you mean? Where’s my mistake?

2 answers

0

You are receiving the message from Wrong Answer (100%) due to rounding gaps.

To resolve this issue correctly, according to all the restrictions that are passed to us by the platform of URI, we should use the following code:

valor = float(input())

notas = [100, 50, 20, 10, 5, 2]
moedas = [1, 0.50, 0.25, 0.10, 0.05, 0.01]

print('NOTAS:')
for nota in notas:
    qt_notas = int(valor / nota)
    print('{} nota(s) de R$ {:.2f}'.format(qt_notas, nota))
    valor -= qt_notas * nota

print('MOEDAS:')
for moeda in moedas:
    valor = round(valor, 2)
    qt_moedas = int(valor / moeda)
    print('{} moeda(s) de R$ {:.2f}'.format(qt_moedas, moeda))
    valor -= qt_moedas * moeda

Note that in this code I have implemented two lists. One named after notes, which stores the notes of 100, 50, 20, 10, 5 and 2 real and, another, named after coins which stores the coins of 1, 0.50, 0.25, 0.10, 0.05 and 0.01 pennies.

Also note that when we execute this code, the cursor is blinking in the upper left corner of the screen. At this moment we must enter a real value, ie a value of type float() and press enter.

Having done this the code will then execute the two blocks of for. The first block for will display the amounts of notes. In this case, each number of notes will be the ratio between value and note, converted into whole. The second for will display the amounts of coins. In this case, each amount of coins will be the ratio between value and coin, converted into whole.

OBSERVING:

The value of the variable value, used to calculate the amount of coins must be a rounded real value to 2 decimal places, as shown on the line...

valor = round(valor, 2)

This artifice must be implemented due to the fact that currencies lower than 1 real will always represent fractions of 1 real.

For the record, this issue has already been tested, submitted and properly approved on the URI website.

  • Another possibility is to multiply all values by 100, work with integers and, if necessary, divide the values by 100 when printing.

0

Hello, good afternoon!

The sum of the value of the notes and coins, at the end, gave difference of cents due to rounding that is not present in its code.

When you display two decimal places only, truncate, it does not round, just cut: 7854.68999 in this case is 7854.68

When we use rounding it then sends the value down or up depending on the third house: 7854.68999 in this case is 7854.69

Use this value in your code to test: 7854.69 when it shows the amount of banknotes and coins, add it all and the value will be 7854.68

Follow your code below with a rounding function:

# -*- coding: utf-8 -*-

def arredondar(num):
    return float('%g' % (num))

valor = float(input())

cem_r = int(arredondar(valor) // 100)
valor %= 100
cinquenta_r = int(arredondar(valor) // 50)
valor %= 50
vinte_r = int(arredondar(valor) // 20)
valor %= 20
dez_r = int(arredondar(valor) // 10)
valor %= 10
cinco_r = int(arredondar(valor) // 5)
valor %= 5
dois_r = int(arredondar(valor) // 2)
valor %= 2
um_r = int(arredondar(valor) // 1)
valor %= 1
cinquenta_c = int(arredondar(valor) // 0.50)
valor %= 0.50
vintecinco_c = int(arredondar(valor) // 0.25)
valor %= 0.25
dez_c = int(arredondar(valor) // 0.10)
valor %= 0.10
cinco_c = int(arredondar(valor) // 0.05)
valor %= 0.05
um_c = int(arredondar(valor) // 0.01)

print("""NOTAS:
{} nota(s) de R$ 100.00
{} nota(s) de R$ 50.00
{} nota(s) de R$ 20.00
{} nota(s) de R$ 10.00
{} nota(s) de R$ 5.00
{} nota(s) de R$ 2.00
MOEDAS:
{} moeda(s) de R$ 1.00
{} moeda(s) de R$ 0.50
{} moeda(s) de R$ 0.25
{} moeda(s) de R$ 0.10
{} moeda(s) de R$ 0.05
{} moeda(s) de R$ 0.01""".format(cem_r, cinquenta_r, vinte_r, dez_r, cinco_r,
                                 dois_r, um_r, cinquenta_c, vintecinco_c, dez_c,
                                 cinco_c, um_c))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.