How to resolve the algorithm Integer comparison step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Integer comparison step by step in the jq programming language

Table of Contents

Problem Statement

Get two integers from the user. Then,   display a message if the first integer is: the second integer.

Test the condition   for each case separately,   so that   all three comparison operators are used   in the code.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Integer comparison step by step in the jq programming language

Source code in the jq programming language

# compare/0 compares the first two items if they are numbers,
# otherwise an "uncomparable" message is emitted.

def compare:
  def english:
    if .[0] < .[1] then "less than"
    elif .[0] == .[1] then "equal to"
    else "greater than"
    end;
  if (.[0]|type) == "number" and (.[1]|type) == "number" then
        "\(.[0]) is \(english) \(.[1])"
  else
       "\(.[0]) is uncomparable to \(.[1])"
  end ;

compare

$ jq -s -r -f Integer_comparison.jq
1 2
1 is less than 2

$ jq -s -r -f Integer_comparison.jq
1 "a"
1 is uncomparable to a

  

You may also check:How to resolve the algorithm N-smooth numbers step by step in the Raku programming language
You may also check:How to resolve the algorithm Ternary logic step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the Oforth programming language
You may also check:How to resolve the algorithm Quine step by step in the Whitespace programming language