Skip to the content

The butcher's tally

Problem

Don Nacho, the butcher, writes down the whole day on a single sheet and in his own shorthand: what he weighed, what he charged and how many bags the customer took all fit on the same line. At closing time only one thing matters to him, how many kilos he sold, and the other numbers are in the way.

Write a function that takes note, a string of at most 200 characters (it may be empty), and returns, as a whole number, the sum of the kilos that show up in it. A weight in kilos is a whole number followed by the letters kg, with however many spaces in between, or with none at all: "3 kg", "3 kg" and "3kg" all say three kilos. Don Nacho never writes down half kilos, so every weight is a whole number.

Any other number on the note is a price or a count of bags, and it does not add up. The word kilos spelled out does not count either. And neither does a kg with no number before it: in "chicken at 90 per kg" nothing was sold, because there is another word between the 90 and the kg.

With "steak 3 kg, 420 pesos; chops 2kg, 2 bags" you return 5. If the note has no weight in kilos, you return 0.

Examples

  • The example

    "steak 3 kg, 420 pesos; chops 2kg, 2 bags" → 5

  • A single order

    "cutlet 1 kg" → 1

  • The note has no kilos

    "3 bags and 250 pesos" → 0

  • Kilos with two digits

    "ribs 12 kg and 15kg of trimmings" → 27

  • The price per kilo does not count

    "chicken at 90 per kg, 2 kg of wings" → 2

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def kilos_in_note(note):
    pass

JavaScript

function kilosInNote(note) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →