ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
you have a number and 5 operations
1) add one to a digit ... 3 points
2) decrese one from a digit ... 2 points
3) interchange 2 digits ... 7 points
4)add a digit... 10 points
5) delet a digit... 12 points
and you have to make the given number a palindrom with a total of minimal points
for example if the number is 234 , decrese 4 by 2 and you get 232 and 4 points
While agreeing with Thor, even if it were a homework I would feel happy to point you at a Breadth First Search, which is what you probably want to be implementing
yeah, actually it's an example of a problem i might be geting at my exams and i've been trying but can't pull it trough...
it doesn't matter what language, but mously he asks us to work in C#
Right - next time say so in your question, 'cos it helps people know what level of support they're working on
Do you have any code that you've written to attempt the problem? Have you tried implementing the BFS I linked you to? (there's informal pseudocode on the wikipedia article I linked you to, but I'm almost certain there's better pseudocode on the web, or I can explain it if you get stuck.)
Ahaha, okay. I'll explain anyway, to see if that makes it clearer...
Quote:
1) Subtract 1 from a digit = 2 points
2) Add 1 to a digit = 3 points
3) Interchange 2 digits = 7 points
4) Add a digit = 10 points
5) Delete a digit = 12 points
The goal is a palindrome using the minimum number of points.
Our queue of numbers to investigate starts out with out first number (234). We remove it from the queue, and then find all the numbers we can create from it using rule 1 (134, 224, 233) and all the numbers from rule 2 (334, ...). Our queue now looks like this:
where each entry is a pair of (number, point cost to get to number), and the list is sorted by point cost. We now remove the first element from the list (ie. the one with lowest cost), check if it's a palindrome, and if not then repeat the step of adding all the possible numbers we can create from this new element. Remember to sort the list afterwards. Keep on removing the first element from the list and checking and adding until you find an element which is a palindrome.
Having written that, it does become obvious that you're going to get a really big queue really quickly. In that case, it might be better to use a depth limited search if space becomes an issue.
Of course, if you have an alternative approach which you also have problems with then do post back and we'll try to help you
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.