How to resolve the algorithm Department numbers step by step in the AWK programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Department numbers step by step in the AWK 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 AWK programming language
Source code in the awk programming language
# syntax: GAWK -f DEPARTMENT_NUMBERS.AWK
BEGIN {
print(" # FD PD SD")
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
You may also check:How to resolve the algorithm Minimum positive multiple in base 10 using only 0 and 1 step by step in the Tcl programming language
You may also check:How to resolve the algorithm Knapsack problem/Bounded step by step in the zkl programming language
You may also check:How to resolve the algorithm Simulate input/Keyboard step by step in the Nim programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Racket programming language