Note: This is just an interesting approach to the problem. There are special built in methods(at least in Ruby) that can do this task. It is easy to increment just one letter but when there are more than one letter then it can be a bit challanging. First we should inrement second letter unless it is ‘Z’ when it is ‘Z’ we should increment first letter etc. Here one can use ‘if’ statement and check if second letter is ‘Z’ make it ‘A’ and increment first letter but I decided to implement this without using ‘if’ statement. Thus my solution for this problem is with modulos and integer division operations. In the Algebra to make domains closed people use modulos. For example let’s consider this Galois field: { 0, 2, 3, …, 25 where arithmetic is performed modulo 26 } This domain is closed because of modulo 26 in other words modulo 26 has good feature that it does not allow elements to be out of this domain. For example 2 + 26 in general arithmetic is 28 but with modulo 26 it is 2. So the values are repeating. In this case we do not need ‘if’ to say for example if result of element incrementation is more than 25 then element is equal to 0. I’ve just used same logic to replace ‘if statement’. This is my function:
comments powered by Disqus