You are viewing a single comment's thread. Return to all comments →
# Solution based on counting number of unique values
c=$(cat | tr ' ' "\n" | sort -n -u | wc -l)
[ $c -eq 1 ] && echo EQUILATERAL
[ $c -eq 2 ] && echo ISOSCELES
[ $c -eq 3 ] && echo SCALENE
can you please explain this?
The solution is based on sorting and counting distinct values using wc i.e word count. So for instance, if you have input as 2,2,1 the value of variable c would be 2 since it counts both these 2's as a single 2 alongwith 1. So logically your input 2,2,1 becomes 2,1 and word count gives you a count of distinct lines and thus variable c holds a value of 2 and thus its isoceles.
And if the input is 3,3,3 then all these 3's are counted once and then the wc command would return the value of variable c as 1 and thus it would be equilateral.
This is perhaps the best solution which beats my solution. Thanks for sharing it :)