Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 7th, 2008, 3:22 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,199
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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)
  1. inFile = open("elimi.in", "r")
  2. outFile = open("elimi.out", "w")
  3. words = ""
  4.  
  5. for lines in inFile:
  6. words += lines
  7.  
  8. for letters in words:
  9. if letters != '\n':
  10. if (words.count(letters) % 2) == 0:
  11. z = 1
  12. else:
  13. z = 0
  14. break
  15.  
  16. if z == 1:
  17. outFile.write("Yes\n")
  18. else:
  19. outFile.write("No\n")
  20.  
  21. inFile.close()
  22. outFile.close()


Arla's perfect solution in Java:

Java Syntax (Toggle Plain Text)
  1. package eliminagram;
  2.  
  3. import java.io.*;
  4.  
  5. public class Eliminagram {
  6.  
  7. public static boolean eliminanagram(String input)
  8. {
  9. //First check the length of the input, if it's not divisible by two it can't possibly be an eliminanagram
  10. if ((input.length() % 2) != 0)
  11. return false;
  12. //Java defaults each value in a new array to 0
  13. int[] alphabet = new int[26];
  14. int value;
  15. for (char c: input.toCharArray())
  16. {
  17. value = c - 97;
  18. if (value >= 0 & value <27)
  19. alphabet[value]++;
  20. else
  21. //If any values are outside of the expected range, return false
  22. return false;
  23. }
  24. //Now we have made a count of all the values, simply check each is divisible by 2
  25. for (int i: alphabet)
  26. if ((i % 2)!=0) return false;
  27. //If we've got to here, ever letter is divisible by 2, therefore this is an eliminanagram, return true;
  28. return true;
  29. }
  30.  
  31. public static void main(String[] args) {
  32. // Get the input, should be two text strings
  33. try
  34. {
  35. int inputCount =0;
  36. String text;
  37. String allTogether = "";
  38. BufferedReader in = new BufferedReader(new FileReader("elimi.in"));
  39. while ((text = in.readLine()) != null)
  40. {
  41. inputCount++;
  42. allTogether+=text;
  43. }
  44. if (inputCount!=2)
  45. System.out.println("Unexpected number of input text strings in input file, program aborting");
  46. else
  47. {
  48. //Now we have enough to check for whether this is an Eliminanagram
  49. boolean isEliminanagram = eliminanagram(allTogether);
  50. try
  51. {
  52. BufferedWriter out = new BufferedWriter(new FileWriter("elimi.out"));
  53. if (isEliminanagram)
  54. out.write("Yes\n");
  55. else
  56. out.write("No\n");
  57. out.close();
  58. }
  59. catch (Exception e)
  60. {
  61. e.printStackTrace();
  62. }
  63. }
  64. //Now close the input since we are done with it
  65. in.close();
  66. }
  67. catch (Exception e)
  68. {
  69. e.printStackTrace();
  70. }
  71. }
  72.  
  73. }


The official solution in Python:

Python Syntax (Toggle Plain Text)
  1. dat = open("elimi.in", "r").read()
  2. text = ''.join(dat.split('\n')[:2])
  3.  
  4. def go(text):
  5. for l in text:
  6. if(text.count(l)%2 != 0):
  7. return "No"
  8. return "Yes"
  9.  
  10. f = open("elimi.out", "w")
  11. f.write("%s\n"%go(text))
  12. f.close()
Attached Files
File Type: zip elimi.zip (2.5 KB, 2 views)
__________________
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.
Sane is offline   Reply With Quote
Old Oct 7th, 2008, 4:33 PM   #2
Weird Fishes
Hobbyist Programmer
 
Weird Fishes's Avatar
 
Join Date: Aug 2008
Location: Ontario
Posts: 177
Rep Power: 1 Weird Fishes is on a distinguished road
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.
Weird Fishes is offline   Reply With Quote
Old Oct 7th, 2008, 5:17 PM   #3
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 407
Rep Power: 4 Arla is on a distinguished road
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)
Arla is offline   Reply With Quote
Old Oct 7th, 2008, 5:19 PM   #4
Weird Fishes
Hobbyist Programmer
 
Weird Fishes's Avatar
 
Join Date: Aug 2008
Location: Ontario
Posts: 177
Rep Power: 1 Weird Fishes is on a distinguished road
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.
Weird Fishes is offline   Reply With Quote
Old Oct 7th, 2008, 5:26 PM   #5
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 407
Rep Power: 4 Arla is on a distinguished road
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)

Quote:
Originally Posted by Weird Fishes View Post
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.
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.
Arla is offline   Reply With Quote
Old Oct 7th, 2008, 5:31 PM   #6
Weird Fishes
Hobbyist Programmer
 
Weird Fishes's Avatar
 
Join Date: Aug 2008
Location: Ontario
Posts: 177
Rep Power: 1 Weird Fishes is on a distinguished road
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)

Quote:
Originally Posted by Arla View Post
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.
Yeah, we (I) get plenty of that there too.
Things like, it won't actuate or it will drive forward unless button a and button b are pressed and...... Just awful.
Weird Fishes is offline   Reply With Quote
Old Oct 8th, 2008, 4:55 AM   #7
Freaky Chris
Expert Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 541
Rep Power: 2 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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
Freaky Chris is offline   Reply With Quote
Old Oct 8th, 2008, 11:16 AM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,199
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)

Quote:
Originally Posted by Weird Fishes View Post
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.

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.
Sane is offline   Reply With Quote
Old Oct 8th, 2008, 11:52 AM   #9
Freaky Chris
Expert Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 541
Rep Power: 2 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)

Quote:
Originally Posted by Sane View Post
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.
Ouch thats even more of a slap in the face
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old Oct 8th, 2008, 3:51 PM   #10
Weird Fishes
Hobbyist Programmer
 
Weird Fishes's Avatar
 
Join Date: Aug 2008
Location: Ontario
Posts: 177
Rep Power: 1 Weird Fishes is on a distinguished road
Re: SMAC #4 [09-08] - Eliminanagram (Beginner)

Oh well.

I thought I did... guess not.
Weird Fishes is offline   Reply With Quote
Reply

Bookmarks

Tags
smac beginner solutions

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC