Lesson 3


Project

As we see from the last lesson, there are a few flaws on the working program shown. One of the flaws is when the table specified by the user is too small (the row * column is less than possible permutation of the string), the table would not show all the anagrams. One of the ways to solve this issue is returning a warning whenever the table size is too small. To do this, we need a function that return the number of possible permutations. Here's the formula for finding the total number of possible combinations of a string.



Mountain View

[1]

where n is the number of characters in a string.

According to the formula , the factorial of n is divided by products of the factorial of number of times each character appears in the string. For example, for the string “bookkeeper”. There are 10 number of character in the string, so the n is 10.

n = 10

'b' appear 1 time

'o' appear 2 time

'k' appear 2 time

'e' appear 3 time

'p' appear 1 time



so the fomula should be,

10! / 1!2!2!3!1! = 10!/2!2!3!.

So, I will guide you through the algorithm in applying this in a function.



while string is not empty :

choose one character and delete the character and it's repetition from the string

subtract the length of new string from that of previous string before delection of the character and store the value in a list.

factorialize each of the integers in the list and multiple them.

divide then n factorial by the result you obtain in the previous statement.

return the result.

Then all you have to do is make a if statement to find whether the table size specified by the user is smaller the value return the function above. It's up to you to choose what to do if the 'if' statement's condition is fulfilled.



Auto-Optimization

Is there any way that we can make program to generate a table without the user specifying the row and column? Well , there are a few way to do this. One of these way is by using using the factors of of the number of possible combinations of a string.

For integer more than 1, each integer would have at least a pair of factors. So, all we to do is make a function which obtain a list of factors of the integer input. Then we can choose two factors such that when the bigger integer of the two are subtracted by the smaller one , the difference is smaller than the other possible pairings.

The easiest way to find these two factors is by taking the two consecutive integer located in the middle of the list containing increasing factors of the integer. For example, let's say we have an integer of 24. The function would return a list [1,2,3,4,6,8,12,24]. We find the length of list and divide it by 2. In this case, the length is 8 and we would get 4 by dividing it by 2.

We are now going to retrieve the integer located at the fourth and fifth position. Since the index of a list started with 0, we must retrieve the element with index 3 and 4 in this case.

This is the algorithm for finding the factors of an Integer.

if the integer is 1:

return 1

else :

check whether the integer can be divided by each integer from 1 to n (n is the integer we want to find factors of)

each time the integer is divisible , insert the number which divide the integer into a list.

return list



Here's the algorithm to find suitable row and column.

if the value of parameter is 1 :

return a tuple of (1,1) where the row and column would be one

else :

find the length of list and divide it by two.

use the value obtained to retrieve the two integers in the middle of the list.

return a tuple of the two integers.

Using the tuple, we can insert the tuple value into the row and column. Look, we can made a problem that automatically select the row and column.



There are many more features that we can add to this program.

Here's a few features that you can add to your program:

1) You can modify your program so that it can check whether two strings are anagrams of each other.

2) You can also modify your program so that it can take in two different words and make anagrams of out of them.

There are endless possibilities on how to further improve our codes. As a computing student, you should try your best to find different ways to improve your program.




[1] pic taken from wikipedia. Link : http://upload.wikimedia.org/math/3/0/b/30bef67d91f970141720b5823314ae7f.png



Go to Conclusion

Back to Lesson 2 Part C

Back to Index