CodeWars Shell Question
Clock
#https://www.codewars.com/kata/55f9bca8ecaa9eac7100004a
h=$1
m=$2
s=$3
echo "(($h*60+$m)*60+$s)*1000" | bc
EvenOrOdd
#https://www.codewars.com/kata/53da3dbb4a5168369a0000fe
EvenOrOdd()
{
if(($1%2==0)); then
echo "Even"
else
echo "Odd"
fi
}
EvenOrOdd "$1"
Multiply
#https://www.codewars.com/kata/50654ddff44f800200000004
#!/bin/bash -e
a=$1
b=$2
echo "($a*$b)" | bc
Opposite
#https://www.codewars.com/kata/56dec885c54a926dcd001095
#!/bin/bash
echo "- $1" | bc
WhileLoop
#https://www.codewars.com/kata/582cd9033c1acf1d45000052
#!/bin/bash
countToTwenty() {
i=1
while ((i<=20));do
echo "Count: $i"
((i++))
done
}
countToTwenty() {
for i in {1..20};do echo "Count: $i" ;done
}
KeepHydrated
#https://www.codewars.com/kata/582cb0224e56e068d800003c
#!/bin/bash
time=$1
# The fun begins here.
echo "$time/2" | bc
ExpressionMatter
#https://www.codewars.com/kata/5ae62fcf252e66d44d00008e
a=$1
b=$2
c=$3
ar=($( echo "$1 * ($2 + $3)" | bc )
$( echo "$1 * $2 * $3" | bc )
$( echo "$1 + $2 + $3" | bc )
$( echo "$1 + $2 * $3" | bc )
$( echo "$1 * $2 + $3" | bc )
$( echo "($1 + $2) * $3" | bc ))
IFS=$'\n'
echo "${ar[*]}" | sort -nr | head -n1
StringRepeat
https://www.codewars.com/kata/57a0e5c372292dd76d000d7e
#!/bin/bash
repeat=$1
string=$2
ret=""
for((i=repeat;i>=1;i--)); do
ret=$ret$string
done
echo $ret
Reverser
#https://www.codewars.com/kata/58069e4cf3c13ef3a6000168
# echo $1
echo $1 | rev | bc
Mumbling
#https://www.codewars.com/kata/5667e8f4e3f572a8f2000039
#!/bin/bash
accum () {
str=$1
strlen=${#str}
ans=""
for((i=1;i<=strlen;++i)); do
tmp=""
c=${str:i-1:1}
for((j=1;j<=i;++j)); do
tmp=$tmp$c
done
tmp=${tmp,,}
tmp=${tmp^}
ans=$ans"-"$tmp
done
ans=${ans:1}
echo $ans
}
accum "$1"
MultiplesOf3or5
#https://www.codewars.com/kata/514b92a657cdc65150000006
#!/bin/bash
n=$1
sum=0
for((i=0;i<n;++i)); do
if((i%3==0 || i%5==0)); then
((sum+=i))
fi
done
echo $sum
Twiceasold
#https://www.codewars.com/kata/5b853229cfde412a470000d0
#!/bin/sh
dad_years_old=$1
son_years_old=$2
echo $((dad_years_old-2*son_years_old)) | tr -d -
exit
WhattimeisitinJapan
#https://www.codewars.com/kata/59c53183aeb2842585000116
TZ='Asia/Tokyo' date "+%Y-%m-%dT%H:%M%z"
ReturnTheLastModifiedfile
#https://www.codewars.com/kata/59bfe5f15227ddf81900007b
ls -t | head -1
RemoveFirstAndLastCharacter
#https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0
function removeChar() {
str=$1
len1=${#str}
len1=$[len1-1]
echo $str | cut -c 2-$len1
}
removeChar $1
RemoveAnchorFromURL
#https://www.codewars.com/kata/51f2b4448cadf20ed0000386
#!/bin/bash
echo $1 | cut -d "#" -f 1
Grasshopper
#codewars.com/kata/55f73be6e12baaa5900000d4
laLigaGoals=$1
copaDelReyGoals=$2
championsLeagueGoals=$3
echo $((laLigaGoals+copaDelReyGoals+championsLeagueGoals))
ThirdAngleOfATriangle
#https://www.codewars.com/kata/5a023c426975981341000014
echo "180-$a-$b" | bc