Last active
March 10, 2018 16:19
-
-
Save d78ui98/796f574050b77578b53e10401940c15e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# USAGE | |
# hanoiScript.sh [n] [from] [to] [by] | |
if [[ -z $1 ]] ; then | |
echo "error: No arguments were passed" | |
exit 1 | |
fi | |
# hanoi() | |
# It Recursive function to ring from first pole to another pole | |
# with the help of a third pole | |
# called (n^2 - 1) times | |
hanoi() | |
{ | |
local rings="$1" | |
local fromPole="$2" | |
local toPole="$3" | |
local viaPole="$4" | |
if [[ "$rings" > "1" ]] ; then | |
hanoi $(($rings - 1)) $fromPole $viaPole $toPole | |
hanoi 1 $fromPole $toPole $viaPole | |
hanoi $(($rings - 1)) $viaPole $toPole $fromPole | |
else | |
echo "Moving ring from pole $fromPole to pole $toPole" | |
fi | |
} | |
# arguments | |
from=${2:-1} | |
to=${3:-2} | |
by=${4:-3} | |
hanoi $1 $from $to $by | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment