Metric Converter

Level: Beginner 15–30 min

Concepts: Algorithms

Solutions: C# | TypeScript | Python


You are to write a metric to imperial unit converter. The code should only focus on the algorithms, no need to need to worry about a UI.

There are four types of conversion to be implemented:

  • Kilometers to Miles
    • Takes kilometers to convert and returns the result in miles
    • For conversion use 1 kilometer = 0.621371 miles
  • Celsius to Fahrenheit
    • Takes the temperature in Celsius and returns the result in Fahrenheit
    • For conversion use the following formula: (TemperatureInCelsius * 1.8) + 32
    • E.g. 30C * 1.8 + 32 = 86F
  • Kilogram to Pound
    • Takes the number of kilograms to convert and returns pounds
    • For conversion use the following formula: (Kilograms / 0.45359237)
    • E.g. 5kg / 0.45359237 = 11.02311310 pounds
  • Liters to Gallons
    • Takes number of liters to convert and the TargetUnit (Either US or UK)
    • Handles conversion to both US and UK gallon
    • For US gallon use 3.785411784 liters = 1 US gallon
    • For UK gallon use 4.54609 liters = 1 UK gallon

Reference Walkthrough

Reference implementations in C#, TypeScript, and Python live at tddbuddy-reference-katas/metric-converter. Thirteen scenarios cover all four conversion dimensions — length (Kilometers → Miles), temperature (Celsius → Fahrenheit, including the -40 crossover), weight (Kilograms → Pounds), and volume (Liters → US Gallons, Liters → UK Gallons) — shared across all three languages, each a single pure function convert(value, from, to) → number. A thirteenth scenario locks down that unsupported pairs raise a domain error; the converter is one-directional metric → imperial.

This kata ships in Agent Full-Bake mode at high gear (F1 tier): the algorithm is small enough to land as one commit per language, with a brief walkthrough noting there are no builders — but the nine units are modelled as typed enums (C# enum, TypeScript string-literal union, Python StrEnum) rather than bare strings. The closed set of recognized units is the ubiquitous language, and a single convert(value, from, to) entry point with a pattern-matched dispatch reads as the spec sentence — “convert value from unit to unit” — instead of spreading one concept over five overloaded methods. See the repo’s Gears section for when high gear is the right call.