Python – Algorithms About Palindromes

About 3 weeks ago, I have enlisted myself in some course for algorithms. There, one of the guys mentioned a web site for web contests and I have decided to take a look. The site is called Timus.ru and the algorithmic problems are visible here. They can be resolved in almost any language and they are interesting for guys like me, who are entering this whole new world.

Python

Thus, after taking two trivial problems (literally A+B and sum of numbers in string), I found a more interesting one. It was called Salary and its description is here:


1123. Salary
Time limit: 1.0 second
Memory limit: 64 MB
All employees of SKB Kontur like to get their salaries. Often and in large quantities. But the company management is of a bit different opinion and pays out strictly once a month. After some consultations the employees decided that if one of the parameters (frequency of payment) was fixed it was possible to change the second parameter (amount of the money paid out). They contrived the following scheme. A group of employees who proudly call themselves mathematics and mechanics faculty graduates visits the management and using their mathematical authority claims that the computers in the company’s accounts department will work more efficiently if salaries of all the employees take the form of palindromes. As you know, a numerical palindrome is a number that does not change when you read it from right to left. For example, 12344544321 is a palindrome and 12345543210 is not. Of course, the management had to agree with this proposal, but upon one condition: each employee had to re-count his or her salary so that the salary took the form of the least possible palindrome that is greater than or equal to the original salary. You are asked to help the employees of SKB Kontur.
Input
consists of one string containing the original salary of an employee. The string is not longer than 2001 symbols.
Output
should consist of one string containing the new salary calculated according to the above rules.

input
12341321

output
12344321


Yup, after some reading, I tought it would be just trivial, but it was not. There are about 6 cases, which should be analyzed and in order to realize it, you should start working on this. Anyhow, I started and I kind-of spent about 2 hours behind the PC (doing other stuff as well), until I managed to get the passing score like this:

atimus.ru

How did I achieved it?

Well, these are the cases I took into account while programming:

  1. There is a possibility that the salary of the person is in one digit. (I hope you never have such a salary even per hour, but it is possible)
  2. There is a possibility that the salary is in even numbers or in odd. Thus, we should take the edge. In my code, it is strEdge.
  3. There is a possibility, that the salary of someone is 2288 or 22988. In this case, the new salary should be 2332 or 23032. In order to calculate these two, you should be pay attention. Strings in Python are not mutable, thus I was going through lists all the time.
  4. I think that is all!

Before coming with the code, I would like to express my gratitude by honorably mentioning the WingWare Sales Team for providing me the WingWare Python IDE for a review in a really prompt manner. The review of the IDE is coming either this or next week. So, the following code is indeed, coded with WingWare, as it says on the banner 🙂

coded-with-logo-129x66                  Here comes the code:

Enjoy it! 🙂

About

VBA Developer

Tagged with: , , , ,