How to resolve the algorithm Comma quibbling step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Comma quibbling step by step in the jq programming language

Table of Contents

Problem Statement

Comma quibbling is a task originally set by Eric Lippert in his blog.

Write a function to generate a string output which is the concatenation of input words from a list/sequence where:

Test your function with the following series of inputs showing your output here on this page:

Note: Assume words are non-empty strings of uppercase characters for this task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Comma quibbling step by step in the jq programming language

Source code in the jq programming language

def quibble:
  if length == 0 then "" 
  elif length == 1 then .[0]
  else (.[0:length-1] | join(", ")) + " and " + .[length-1]
  end
  | "{" + . + "}";

( [], ["ABC"],  ["ABC", "DEF"],  ["ABC", "DEF", "G", "H"]) | quibble

jq -n -r -f Comma_quibbling.jq
{}
{ABC}
{ABC and DEF}
{ABC, DEF, G and H}


  

You may also check:How to resolve the algorithm String matching step by step in the K programming language
You may also check:How to resolve the algorithm Harmonic series step by step in the Delphi programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the PL/I programming language