Electricity Bill

The electricity bill is calculated depending of a number of parameters:

  • The current gas price (GasP): float value.

  • The current renewable energies price (RenewEP): float value.

  • The electricity consumed in kWh (Consum): int value.

  • The day of the week (Day): int value.

The bill is calculated by multipling Consum by the price of the electricty (in kWh), namely EP, which is determined by the following rules:

  • When GasP is strictly greater that the double of the RenewEP, then EP is GasP + RenewEP. In this case the following discounts are applied:
    • 25% discount is applied if Consum is greater than or equal to 100 kWh.

    • 10% discount is applied if Consum is between 10 kWh and 100 kWh (the first included and the later excluded).

    • No discount is applied otherwise.

  • When GasP is lower than or equal to the double of RenewEP, then EP is the maximum between GasP and RenewEP. In this case a discount of 15% is applied but only if Consum is greater than 100 kWh.

  • The bill is cut in half during the weekend (saturday and sunday), indepedently of the rest of the values.

The data provided to calculate the bill might contain errors. Hence, before calculating the bill, the data must be checked to determine whether is valid or not. If some data is incorrect then an error code (int) must be generated according to the following format:

  • If any of the prices is negative then the hundreds digit in the error code must be 1.

  • If Consum is null or negative then the tens digit must be 1.

  • If the int in Day is not correct then the units digit must be 1.

For example, in case where the Gas price is negative and the day is 9 then the error code generated should be 101. In case everything is incorrect (some price is negative, Consum is 0 and the day is not between 1 and 7, then the error code should be 111.

Implement the following Python functions in the module ebill (file ebill.py).


The first funcion is:

ebill_check(GasP, RenewEP, Consum, Day)

such that

given GasP, RenewEP, Consum and Day as specified above.

returns two values:

  • a bool which value is True if there is an error or False otherwise.

  • an int with either the error code as described above, or 0 if there is no error.

For example:


>>> ebill_check(10.0, 3.0, 200, 1)
(False, 0)

>>> ebill_check(-7.0, 3.0, 9, 2)
(True, 100)

>>> ebill_check(7.0, -3.0, -10, 2)
(True, 110)

>>> ebill_check(-7.0, -3.0, -10, 8)
(True, 111)

Additional doctests for validation are available in the ebill_check-test.txt file.


The second funcion is:

ebill_eval(GasP, RenewEP, Consum, Day)

such that

given GasP, RenewEP, Consum and Day as specified above, and all are valid values.

calculates the final price to pay of the electricity bill and returns the integer part of that amount.

For example:


>>> ebill_eval(10.0, 3.0, 200, 1)
1950

>>> ebill_eval(7.0, 3.0, 200, 2)
1500

>>> ebill_eval(7.0, 3.0, 50, 3)
450

>>> ebill_eval(7.0, 3.0, 9, 2)
90

Additional doctests for validation are available in the ebill_eval-test.txt file.


The third (and main) funcion is:

ebill_calc(GasP, RenewEP, Consum, Day)

such that

given GasP, RenewEP, Consum and Day as specified above,

returns two values:

  • a bool which value is True if there is an error and False otherwise.

  • an int that will be either the error code if the previous value is True, or the integer part of the final price to pay calculated as specified above, otherwise.

For example:


>>> ebill_calc(10.0, 3.0, 200, 1)
(False, 1950)

>>> ebill_calc(7.0, 3.0, 200, 2)
(False, 1500)

>>> ebill_calc(7.0, 3.0, 50, 3)
(False, 450)

>>> ebill_calc(7.0, 3.0, 9, 2)
(False, 90)

>>> ebill_calc(-7.0, 3.0, 9, 2)
(True, 100)

>>> ebill_calc(7.0, -3.0, -10, 2)
(True, 110)

>>> ebill_calc(-7.0, -3.0, -10, 8)
(True, 111)

Additional doctests for validation are available in the ebill_calc-test.txt file.

Warning

The last function implementation must call both previous functions. It is in our own interest since the code for this last function becomes much more simple if you use those.