0% found this document useful (0 votes)
471 views4 pages

OS Hackerrank Challenge 123

This document contains solutions to challenges on the Hackerrank platform. It includes 15 answers demonstrating various Bash scripting concepts like conditionals, loops, reading input, arithmetic operations, string manipulation, sorting/filtering arrays, and using commands like head, tail, tr, sort, uniq, grep, sed, paste. The challenges cover topics like looping, comparing numbers, conditionals, sorting arrays, slicing/filtering arrays, concatenating arrays, and counting array elements.

Uploaded by

Tejas Rane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
471 views4 pages

OS Hackerrank Challenge 123

This document contains solutions to challenges on the Hackerrank platform. It includes 15 answers demonstrating various Bash scripting concepts like conditionals, loops, reading input, arithmetic operations, string manipulation, sorting/filtering arrays, and using commands like head, tail, tr, sort, uniq, grep, sed, paste. The challenges cover topics like looping, comparing numbers, conditionals, sorting arrays, slicing/filtering arrays, concatenating arrays, and counting array elements.

Uploaded by

Tejas Rane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Hackerrank Challenge 1

Ans 1: Let's Echo


echo "HELLO"

Ans 2: Looping and Skipping


read c
if [[($c == 'y') || ($c == 'Y')]]
then
echo "YES"
    else
    echo "NO"
fi

Ans 3: A Personalized Echo


read name
echo "Welcome $name"

Ans 4: Looping with Numbers


for i in {1..50}
do
echo $i
done

Ans 5: The World of Numbers


read X
read Y
echo $((X+Y))
echo $((X-Y))
echo $((X*Y))
echo $((X/Y))

Ans 6: Comparing Numbers


read X
read Y
if (( $X > $Y ))
then
echo "X is greater than Y"
fi
if (( $X == $Y))
then
echo "X is equal to Y"
fi
if(( $X < $Y))
then
echo "X is less than Y"
fi

Ans 7: Getting started with conditionals


read word
if [[($word == 'y') || ($word == 'Y')]]
then
echo "YES"
elif [[($word == 'n') || ($word == 'N')]]
then
echo "NO"
fi

Ans 8: More on Conditionals


read x
read y
read z
if ((($x == $y) && ($y == $z)))
then
echo "EQUILATERAL"
elif ((($x == $y) || ($x == $z) || ($y == $z)))
then
echo "ISOSCELES"
else
echo "SCALENE"
fi

Ans 9: Arithmetic Operations


read x
printf "%.3f\n" `echo "$x" | bc -l`

Ans 10: Compute the Average


read num
ctr=$num
sum=0
while [ $ctr -gt 0 ]
do
read x
sum=$((sum + x))
ctr=$((ctr - 1))
done
printf "%.3f\n" `echo "$sum/$num" | bc -l`

Hackerrank Challenge 2

Ans 1: Head of a Text File #1


head -c 20
Ans 2: Middle of a Text File
head -22 | tail -11
Ans 3: Tail of a Text File #2
tail -c 20
Ans 4: 'Tr' Command #3
tr -s ' '
Ans 5: Sort Command #4
sort -n -r
Ans 6: Sort Command #5
sort -k2 -n -r -t$'\t'
Ans 7:'Uniq' command #4
uniq -u
Ans 8: Read in an Array
while read line
do
arr=(${arr[@]} $line)
done
echo ${arr[@]}

Ans 9: Slice an Array


arr=($(cat))
echo ${arr[@]:3:5}

Ans 10: Filter an Array with Patterns


arr=($(cat))
echo ${arr[@]/*[aA]*/}
Ans 11: Concatenate an array with itself
arr=($(cat))
arr=("${arr[@]}" "${arr[@]}" "${arr[@]}")
echo ${arr[@]}

Ans 12: Count the number of elements in an Array


arr=($(cat))
echo ${#arr[@]}

Ans 13: Remove the First Capital Letter from Each Element


arr=($(cat))
echo ${arr[@]/[A-Z]/.}

Ans 14: 'Grep' - A
grep -iwe "the\|that\|then\|those"

Ans 15: 'Sed' command #5


sed -E 's/([0-9]{4}) ([0-9]{4}) ([0-9]{4}) ([0-9]{4})/\4 \3 \2 \1 /g'

Hackerrank Challenge 3

Ans 1: Sort Command #1


sort
Ans 2: Sort Command #2
sort -r
Ans 3: 'Uniq' Command #1
uniq
Ans 4: 'Uniq' Command #2
uniq -c | cut -c7-
Ans 5: Paste - 3
paste -s
Ans 6: Paste - 4
paste - - -
Ans 7: Lonely Integer - Bash
read
arr=($(cat))
echo "${arr[@]}" | tr ' ' '\n' |sort | uniq -u | tr '\n' ' '

You might also like