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.
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:
How did I achieved it?
Well, these are the cases I took into account while programming:
- 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)
- 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.
- 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.
- 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 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
def listToInt(myList): myInt = int(''.join(myList)) return myInt def intToList(myInt): strInt = str(myInt) myList = list(strInt) return myList def calculate(strInput): if (len(strInput) == 1): return int(strInput) intOldSalary = int(strInput) listNum = list(strInput) listHalf1 = list(listNum[:int(len(listNum)/2)]) listHalf2 = list(listNum[int(len(listNum)/2):]) boolHasEdge = bool(len(listNum)%2 != 0) if boolHasEdge: strEdge = listHalf2.pop(0) if listToInt(listHalf2)> int(str(listToInt(listHalf1))[::-1]): if boolHasEdge: if strEdge != "9": strEdge = str(int(strEdge)+1) strTempList = str(listToInt(listHalf1)) else: strEdge = "0" intTempList = listToInt(listHalf1) intTempList += 1 strTempList = str(intTempList) intResult = int(strTempList+strEdge+strTempList[::-1]) else: strTempList = str(listToInt(listHalf1)+1) intResult = int(strTempList+strTempList[::-1]) else: if boolHasEdge: intResult = int(str(listToInt(listHalf1)) + strEdge + str(listToInt(listHalf1))[::-1]) else: intResult = int(str(listToInt(listHalf1)) + str(listToInt(listHalf1))[::-1]) return intResult strInput = input() print(calculate(strInput)) |
Enjoy it! 🙂