LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with an algorithm (https://www.linuxquestions.org/questions/programming-9/help-with-an-algorithm-923728/)

Calin956 01-14-2012 05:36 AM

help with an algorithm
 
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

ButterflyMelissa 01-14-2012 06:52 AM

...ehrm..is this a school assignment? If so, sorry, kid. We dont do someone else's homework... :)

If not, expand a little. What programm language would you use? What is it for? More info...of course, if it's NOT an assignment... :)

Thor

Snark1994 01-14-2012 06:57 AM

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 :)

Calin956 01-14-2012 07:46 AM

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#

Snark1994 01-15-2012 04:37 AM

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.)

Calin956 01-15-2012 07:13 AM

i can't quite imagine how to use BFS here, but thank's for trying to help me, i'll magenage i think ;)

Snark1994 01-15-2012 07:27 AM

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:

Code:

queue = [ (134,2) , (224,2) , (233,2) , (334,3) , ... , (24,12) , (23,13) ]
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 :)

Hope this helps,


All times are GMT -5. The time now is 09:22 AM.