How to resolve the algorithm Department numbers step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Department numbers step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

There is a highly organized city that has decided to assign a number to each of their departments:

Each department can have a number between   1   and   7   (inclusive). The three department numbers are to be unique (different from each other) and must add up to   12. The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.

Write a computer program which outputs all valid combinations.

Possible output   (for the 1st and 14th solutions):

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Department numbers step by step in the UNIX Shell programming language

Source code in the unix programming language

function main {
  set -- Police Sanitation Fire
  typeset -i pw=${#1} sw=${#2} fw=${#3}
  printf '%s' "$1"
  shift
  printf '\t%s' "$@"
  printf '\n'
  for (( p=2; p<8; p+=2 )); do
    for (( s=1; s<8; ++s )); do
      if (( s == p )); then
        continue
      fi
      (( f = 12 - p - s ))
      if (( f == s || f == p || f < 1 || f > 7 )); then
        continue
      fi
      printf "%${pw}d\t%${sw}d\t%${fw}d\n" "$p" "$s" "$f"
    done
  done
}

main "$@"


  

You may also check:How to resolve the algorithm Sort numbers lexicographically step by step in the ATS programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the Go programming language
You may also check:How to resolve the algorithm Test a function step by step in the Pascal programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the Java programming language
You may also check:How to resolve the algorithm Stirling numbers of the first kind step by step in the jq programming language