Tracks
/
jq
jq
/
Exercises
/
Assembly Line
Assembly Line

Assembly Line

Learning Exercise

Introduction

Numbers

From the manual

jq supports the same set of datatypes as JSON - numbers, strings, booleans, arrays, objects (which in JSON-speak are hashes with only string keys), and "null".

Let's focus on numbers.

Numbers and numeric operators

All numbers, whether integers or otherwise, are IEEE754 double precision floating point numbers. This limits us to 53 bits of precision.

The usual operators are available to use with numbers:

  • arithmetic: +, -, *, /, %

  • comparison: ==, !=, <, <=, >=, >

  • standard math functions

    For one-input functions, pipe the value into the function

    $ jq -n '(1 | atan) * 4'
    3.141592653589793
    

    For two-input functions, the functions will ignore input and expect the inputs as parameters (recall parameters are separated by semicolons)

    $ jq -n 'pow(2; 10)'
    1024
    

    Semi-colon is the separator for function arguments, not comma.

Other jq expressions

To solve the exercise, you will need to know about conditional expressions.

Conditional Expressions

jq uses an if-then-else expression for conditional expressions. As an expression, it is placed in a pipeline.

Then syntax is: if CONDITION then TRUE_EXPR else FALSE_EXPR end.

The else clause is optional in jq v1.7, but it is required in jq v1.6.

42 | if . < 33 then "small" else "larger" end
# => "larger"

Additional conditions use elif

42 | if . < 33 then "small"
     elif . < 67 then "medium"
     else "large"
     end
# => "medium"

Instructions

In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from 0 (off) to 10 (maximum).

At its slowest speed (1), 221 cars are produced each hour. The production increases linearly with the speed. So with the speed set to 4, it should produce 4 * 221 = 884 cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. The following table shows how speed influences the success rate:

  • 1 to 4: 100% success rate.
  • 5 to 8: 90% success rate.
  • 9: 80% success rate.
  • 10: 77% success rate.

You have two tasks.

1. Calculate the production rate per hour

Calculate the assembly line's production rate per hour, taking into account its success rate.

2. Calculate the number of working items produced per minute

Calculate how many completed, working cars are produced per minute.

Edit via GitHub The link opens in a new window or tab
jq Exercism

Ready to start Assembly Line?

Sign up to Exercism to learn and master jq with 12 concepts, 74 exercises, and real human mentoring, all for free.