How to resolve the algorithm Old Russian measure of length step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Old Russian measure of length step by step in the Scala programming language

Table of Contents

Problem Statement

Write a program to perform a conversion of the old Russian measures of length to the metric system   (and vice versa).

It is an example of a linear transformation of several variables.

The program should accept a single value in a selected unit of measurement, and convert and return it to the other units: vershoks, arshins, sazhens, versts, meters, centimeters and kilometers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Old Russian measure of length step by step in the Scala programming language

Source code in the scala programming language

import scala.collection.immutable.HashMap

object OldRussianLengths extends App {

  private def measures = HashMap("tochka" -> 0.000254,
    "liniya"-> 0.000254, "centimeter"-> 0.01,    "diuym"-> 0.0254, "vershok"-> 0.04445,
    "piad"  -> 0.1778,   "fut"       -> 0.3048, "arshin"-> 0.7112, "meter"  -> 1.0,
    "sazhen"-> 2.1336,   "kilometer" -> 1000.0, "versta"-> 1066.8, "milia"  -> 7467.6
  ).withDefaultValue(Double.NaN)

  if (args.length == 2 && args(0).matches("[+-]?\\d*(\\.\\d+)?")) {
    val inputVal = measures(args(1))

    def meters = args(0).toDouble * inputVal

    if (!java.lang.Double.isNaN(inputVal)) {
      printf("%s %s to: %n%n", args(0), args(1))
      for (k <- measures) println(f"${k._1}%10s:  ${meters / k._2}%g")
    }
  } else println("Please provide a number and unit on the command line.")

}


  

You may also check:How to resolve the algorithm Concurrent computing step by step in the BASIC programming language
You may also check:How to resolve the algorithm Count in octal step by step in the Simula programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Yabasic programming language
You may also check:How to resolve the algorithm A+B step by step in the Elm programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Python programming language