[Hacker Rank_Linux Shell]문제 풀이#Bash_1 본문

IT/코테문제

[Hacker Rank_Linux Shell]문제 풀이#Bash_1

호랑구야 2022. 3. 2. 09:01

Linux Shell > Bash > Looping and Skipping

 

Looping and Skipping | HackerRank

Let's play with for loops, a little more. Let's skip numbers!

www.hackerrank.com

 
for i in {1..99..2}
do
    echo $i
done

read name
echo "Welcome $name"

Linux Shell > Bash > Looping with Numbers

 

Looping with Numbers | HackerRank

Let's experiment with 'For' loops

www.hackerrank.com

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

Linux Shell > Bash > The World of Numbers

 

The World of Numbers | HackerRank

Let's move on to playing with numbers in Bash.

www.hackerrank.com

read X
read Y

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

Linux Shell > Bash > Comparing Numbers

 

Comparing Numbers | HackerRank

Simple comparisons between numbers: Greater than, less than, equal to.

www.hackerrank.com

read X
read Y
if ((X < Y)); then
    echo X is less than Y;
elif ((X > Y)); then
    echo X is greater than Y;
else
    echo X is equal to Y;
fi

Linux Shell > Bash > Getting started with conditionals

 

Getting started with conditionals | HackerRank

-

www.hackerrank.com

read char

if [[ $char == "Y" || $char == "y" ]]; then
    echo "YES";
else
    echo "NO";
fi

Linux Shell > Bash > More on Conditionals

 

More on Conditionals | HackerRank

Three sides of a triangle are provided to you. Is the Triangle Scalene, Equilateral or Isosceles?

www.hackerrank.com

read X
read Y
read Z

if [[ $X == $Y && $Z == $Y ]]
then
    echo "EQUILATERAL";
elif [[ $X != $Y && $Z != $Y && $X != $Z ]]
then
    echo "SCALENE";
else
    echo "ISOSCELES";
fi


Linux Shell > Bash > Arithmetic Operations

 

Arithmetic Operations | HackerRank

Math in shell scripts.

www.hackerrank.com

# echo "scale = 3; $result" | bc -l 을 사용했을 때
# round가 적용되지 않아 다른 코드를 참고한 결과
# 아래와 같이 printf를 함께 사용한다
# scale 디폴트가 0이므로 우선 4로 지정한 후
# %.3f를 이용해 3자리로 맞춘다

read result
 
printf "%.3f" $(echo "scale = 4; $result" | bc -l)

 

 

반응형
Comments