Sha256: 5cd7017724d4fc5dcf4bb3cc0727b5130eaf638d053f11b9d71d37b8cbec45e1

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

#!/usr/bin/env bash

set -o errexit   # Stop script on command error
set -o nounset   # Error out if accessing undefined variable name
set -o pipefail  # Error out if any step in a pipe errors out

if [[ $# -ne 4 ]]; then
  echo "Usage: $0 [equilateral | isosceles | scalene] <s1> <s2> <s3>"
  exit 2 # Improper inputs
fi

triangle_type=$1
s1=$2
s2=$3
s3=$4

assert() {
  # Takes a numerical inequality in a string.
  # Pipes it through to bc
  # Float-proof numeric comparison :
  [[ $( echo $1 | bc -l ) -eq 1 ]]
}

valid_triangle() {
  # Takes three sides lengths (numeric) $1, $2, $3
  # If any side is zero, returns false
  assert "$1 == 0" || assert "$2 == 0" || assert "$3 == 0" && return 1
  # If triangle doesn't meet inequality requirement, returns false
  assert "$1 + $2 <= $3" || assert "$1 + $3 <= $2" || assert "$2 + $3 <= $1" && return 1
  return 0
}

if ! valid_triangle $s1 $s2 $s3; then
  echo "Sides do not meet triangle inequality requirement."
  echo "Given a <= b <= c and a, b, c != 0, a + b >= c"
  exit 1
fi

equilateral() {
  assert "$1 == $2" && assert "$1 == $3"
}

isosceles() {
  assert "$1 == $2" || assert "$1 == $3" || assert "$2 == $3"
}
  
scalene() {
  assert "$1 != $2" && assert "$1 != $3" && assert "$2 != $3"
}

# Bash Ternary Operator:
# (boolean value/calculation) && action if true || action if false
# Works because of boolean shortcutting.
# If boolean clause is false, it doesn't evaluate the other side of &&
# If first thing is false, it evaluates the item after ||,
# but if first two are true, doesn't bother evaluating last part.
$triangle_type $s1 $s2 $s3 && exit 0 || exit 1

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trackler-2.2.1.85 tracks/bash/exercises/triangle/example.sh
trackler-2.2.1.84 tracks/bash/exercises/triangle/example.sh
trackler-2.2.1.83 tracks/bash/exercises/triangle/example.sh