Design Patterns: Structural · Coding problem

Plug Two Legacy Sensors Into One Interface

A free coding problem from the Adapter Pattern skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

The app reads temperatures through one interface, TemperatureSensor.celsius(). Two devices you can't change report temperatures their own way:

  • LegacyFahrenheitSensor.readTenthsOfFahrenheit() - tenths of a degree Fahrenheit (986 means 98.6 °F)
  • KelvinProbe.readMilliKelvin() - millikelvin (293150 means 293.15 K)

Complete the two adapters so both devices work as a TemperatureSensor:

  • FahrenheitSensorAdapter.celsius() - C = (F - 32) × 5 / 9
  • KelvinProbeAdapter.celsius() - C = K - 273.15

Notice that Solution and its average() never know which device they're talking to - that's what the adapters buy you.

In Python: the devices' methods are read_tenths_of_fahrenheit() and read_milli_kelvin(), and TemperatureSensor is an abstract base class.

Input: a count q, then q readings F <tenths> or K <millikelvin>. Output: each reading in Celsius to 1 decimal, then the average of all readings.

Example

Input:
3
F 986
K 293150
F 320

Output:
37.0 C
20.0 C
0.0 C
average 19.0 C

Constraints

  • 1 <= q <= 1000
  • Readings are between -100 °C and 200 °C.

How it's graded

  • Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Adapter Pattern

Solve it Plug Two Legacy Sensors Into One Interface free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime