![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() ![]() |
SMAC #4 [09-08] - Eliminanagram (Beginner)
Attached to this post is the test data for Eliminanagram.
Eliminanagram (Beginner) The nice thing about this problem is there are many different ways to solve it. Since the problem requires use of basic counting techniques and manipulating strings, the problem is fairly open to different approaches. Some approaches are easier than others, but all are just as correct. Here is one approach, directly derived from the problem. You are given the definition of an Eliminanagram, and all you need to do is apply the logic to test if the two words satisfy the definition. That process will look something like: - Take the first letter in the first word - Look for it somewhere else in the first or second word - If it can't be found, then the word is not an eliminangram - If it can be found, then delete both occurrences and repeat - If there are still letters left over in the second word then repeat this process again taking the first letter from the second word - If no letters remain, then the word is an eliminanagram This will get you full points. But we are doing a lot more work than we need to, and can make various observations to turn this into a very simple task: Observation #1: isEliminanagram?( a, b ) <==> isEliminanagram?( a+b, "" )What does that mean? If a and b are an eliminanagram, then the combined string a+b with the empty string "" must be an eliminanagram. Why? From our definition, it doesn't matter what word we pick the duplicates from. So moving letters to and from each word is okay. Therefore, moving the entire second word to the first word must also be okay. Observation #2: If any letter occurs an odd number of times, then it can not be fully eliminated. Why? From our definition, any letter can only be eliminated by matching it with the same letter elsewhere. So letters can only be eliminated in duplicate pairs. Thus, for any letter there must be a total of 2*k occurrences of that letter, for some positive integer k, or else the letter can not be eliminated. Note that a number of the form x=2*k, means x is an even number. In other words, x mod 2 == 0. Conclusion: From these two observations, we obtain the following algorithm: - Combine the input into a single string (By Observation 1) - Make sure that each letter appears an even number of times - The above is satisfied if and only if the two words are eliminanagrams (By Observation 2) Interestingly enough, the two people who used this algorithm got perfect. The others had longer solutions derived from the definition, so they missed a couple cases for not handling the tiny nuances of the implementation. I think this clearly demonstrates the benefit of breaking down problems into something simpler to solve. Freaky Chris's perfect solution in Python: Python Syntax (Toggle Plain Text)
Arla's perfect solution in Java: Java Syntax (Toggle Plain Text)
The official solution in Python: Python Syntax (Toggle Plain Text)
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges! Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Aug 2008
Location: Ontario
Posts: 177
Rep Power: 1
![]() |
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)
Damn. I just checked my answers and I figured out where I went wrong. After I found a match I reset a for loop variable to one, but then the for loop went again and it bumped it up to 2. So what separated me from perfect was: x = 1; instead of x = 0;
Oh well. ![]() ![]()
__________________
What can you put in a bucket full of water to make it lighter? |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Mar 2005
Posts: 407
Rep Power: 4
![]() |
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)
Don't ya hate it when that happens...
I had a large piece of code go fairly wrong because it had <= in it instead of < It was the suck! (using my best l33t speak) |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Aug 2008
Location: Ontario
Posts: 177
Rep Power: 1
![]() |
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)
Yeah. One time for robotics, the project wouldn't build because a file needed a newline at the end. Took forever to figure out the problem.
__________________
What can you put in a bucket full of water to make it lighter? |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Mar 2005
Posts: 407
Rep Power: 4
![]() |
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)
Eh, better than it compiling but not working the way you expected, those are much tougher, at least without a compile you're not going anywhere, when it compiles but is missing one line, or a break, or something like that and does something totally wrong it's really hard to spot at times.
|
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Aug 2008
Location: Ontario
Posts: 177
Rep Power: 1
![]() |
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)
Quote:
![]() ![]() Things like, it won't actuate or it will drive forward unless button a and button b are pressed and...... Just awful.
__________________
What can you put in a bucket full of water to make it lighter? |
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)
My first perfect lol, shame its in beginner. And yet i can't even get any form of solution to the other problems, o well.
well done guys ![]() Chris
__________________
Steven Skiena - Algorithms ,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],brainf**k -- It's such a pretty language |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() ![]() |
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)
Quote:
Yeah, that happens. I'm surprised the tests even found it. That's a very minor error that only hurts you 1/10th of the time. The funny part is the one test file it didn't work on was the very first example in the PDF. ![]() mississippi mamma If you had tested it on all the examples in the PDF you would have caught it. ![]()
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges! Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download. |
|
|
|
|
|
|
#9 | |
|
Expert Programmer
|
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)
Quote:
__________________
Steven Skiena - Algorithms ,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],brainf**k -- It's such a pretty language |
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Aug 2008
Location: Ontario
Posts: 177
Rep Power: 1
![]() |
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)
Oh well.
I thought I did... guess not.
__________________
What can you put in a bucket full of water to make it lighter? |
|
|
|
![]() |
| Bookmarks |
| Tags |
| smac beginner solutions |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| SMAC #3 [07-08] - Results | Sane | Software Design and Algorithms | 4 | Aug 6th, 2008 8:23 PM |
| SMAC #3 [07-08] - Julius Caesar (Beginner) | Sane | Software Design and Algorithms | 0 | Aug 6th, 2008 11:41 AM |
| SMAC #2 [06-08] - Swapping Stories (Beginner) | Sane | Software Design and Algorithms | 1 | Jul 1st, 2008 2:35 PM |
| SMAC #1 [05-08] - Mobile (Beginner) and "n Reach k" (Junior) | Sane | Software Design and Algorithms | 2 | Jun 3rd, 2008 9:10 AM |
| SMAC #1 [05-08] - Results | Sane | Software Design and Algorithms | 6 | Jun 2nd, 2008 5:56 PM |