diff --git a/TowersOfHanoi.java b/TowersOfHanoi.java new file mode 100644 index 0000000..56b12fa --- /dev/null +++ b/TowersOfHanoi.java @@ -0,0 +1,20 @@ +public class TowersOfHanoi { + + public void solve(int n, String start, String auxiliary, String end) { + if (n == 1) { + System.out.println(start + " -> " + end); + } else { + solve(n - 1, start, end, auxiliary); + System.out.println(start + " -> " + end); + solve(n - 1, auxiliary, start, end); + } + } + + public static void main(String[] args) { + TowersOfHanoi towersOfHanoi = new TowersOfHanoi(); + System.out.print("Enter number of discs: "); + Scanner scanner = new Scanner(System.in); + int discs = scanner.nextInt(); + towersOfHanoi.solve(discs, "A", "B", "C"); + } +} \ No newline at end of file diff --git a/permutations.py b/permutations.py new file mode 100644 index 0000000..7a5b32f --- /dev/null +++ b/permutations.py @@ -0,0 +1,9 @@ +def permutations(str): + if len(str) <= 1: + return [str] + else: + perms = [] + for ch in permutations(str[:-1]): + for i in xrange(len(ch)+1): + perms.append(ch[:i] + str[-1] + ch[i:]) + return perms \ No newline at end of file