result = this.op02_Replace_X_with_KS(result); result = this.op03_Replace_All_Y_to_I(result); result = this.op04_Change_C_to_S_if_followed_By_I_or_E(result); result = this.op05_Remove_C_If_Followed_By_K(result); result = this.op06_Change_SCH_To_SK( result ); result = this.op07_Change_CH_To_K_If_Followed_By_R( result ); result = this.op08_Change_All_C_To_K_Not_Followed_By_H( result ); result = this.op09_Replace_KN_With_N_At_Beginning( result ); result = this.op10_Remove_Redundant_Consonants( result );

return result; }

}

The code isn't the prettiest, but I was in a rush. Anyhow, I made darn sure that the function names are related to the action of the function itself. I didn't do much debugging, I started late at night, and I just want something to do before bed.

Feedback....Was that problem too easy? I wanted to make our first little contest problem / language comparison fairly easy, but I'm not too sure if it was too easy, or maybe, a bit too hard for some of the newer folks around here.

So, it took about 15minutes for the main part and about two hours killing a python bug and a Manta bug (Python doesn't like index variables in while loops that "look" like they can go out of range. Thus the exception handle).

If I knew python better I would have been done in a 20-30 minutes. I took a pretty brut force aproach. I didn't use any loops unless needed to save lines. I just used replace tricks. I could have used "text" instead of "T", but python convention fall between the two because of the way I was using indexing.

If the rules had allowed it, I could have saved space by running rule 10 first.

Also, I made assuptions that the input text would be exactly as you gave - strings in quotes with no caps. Quotes offer both problems and blessing in python. Dealing with Caps would have changed my approach.

Anyways - it looks just like the php program - but I swear I didn't copy.

Finally got off my duff and posted a Java solution. Code block followed by an output block. Next time, I'll post a ACM problem with input and output file test cases so that you can easily check them - plus it will be a bit harder and, hopefully, a fun challenge.

Code:

import java.util.regex.*;

public class StringCheese { public static String rewrite(String s) {

//I like this method of removing the first word and last word special cases s = " " + s + " ";

Does replace actually replace all occurances of the rule or just one? I noticed that you have 3 replaces for rule 5 when it looks like only the last one was needed.

It replaces all, but only does a single pass (I think). So "ccc" gets turned into "cc" (not "c"). So, ccccck turns into cck. Then you need 2 ck passes to change the ck to k. It works for the examples you gave, but I think it won't work for others - I have to check. Rule 10 at the begining would have help A LOT in this case.

Quote:

Rule 8 is good! I like that kind of thinking. Sure is a hell of a lot faster to write then my looping structure.

hehe - I thought it was a cheat

Quote:

Rule 10 is interesting. What does while T[x] mean? While index x in T exists continue, right? If so, it seems like the x < L is redundant. If not, you should have replaced L with len(T) instead.

I had a time with that loop. The T[x] is a remnant and should have been taken out. Python is a little wierd about using indexing in loops. I think it has to do with Python "dynamic typing" rules. You don't declare variable types (unless you have to). So python assumes the worst BEFORE attempting the loop. Thus the exception handle (my last resort). I was gonna try declaring x, but I had already posted.

Counting what I would call 'functioning lines of code' I came up with the following results:

Code:

PHP 17 locJava & C# 18 locPython 25 locFoxPro 28 loc

I didn't count things like print, sout, method signatures or returns (if they could have been merged with a functional statement). Only the loc needed to directly solve the problem.

It looks like the php solution benefited quite a bit from the ability to specify a matching regex and a regex replacement, whereas all the other solutions used a string replacement. If I were able to do this with my own solution in Java, it would shrink by approximately 5 loc (and I am looking!).

Double check my counting to see if you agree.

I nominate Dude-X to list the awards (unless he decides to provide a C++ Boost solution).

Counting what I would call 'functioning lines of code' I came up with the following results:

Code:

PHP 17 locJava & C# 18 locPython 25 locFoxPro 28 loc

I didn't count things like print, sout, method signatures or returns (if they could have been merged with a functional statement). Only the loc needed to directly solve the problem.

It looks like the php solution benefited quite a bit from the ability to specify a matching regex and a regex replacement, whereas all the other solutions used a string replacement. If I were able to do this with my own solution in Java, it would shrink by approximately 5 loc (and I am looking!).

Double check my counting to see if you agree.

I nominate Dude-X to list the awards (unless he decides to provide a C++ Boost solution).

Counting what I would call 'functioning lines of code' I came up with the following results:

Code:

PHP 17 locJava & C# 18 locPython 25 locFoxPro 28 loc

I didn't count things like print, sout, method signatures or returns (if they could have been merged with a functional statement). Only the loc needed to directly solve the problem.

It looks like the php solution benefited quite a bit from the ability to specify a matching regex and a regex replacement, whereas all the other solutions used a string replacement. If I were able to do this with my own solution in Java, it would shrink by approximately 5 loc (and I am looking!).

Double check my counting to see if you agree.

I nominate Dude-X to list the awards (unless he decides to provide a C++ Boost solution).

I could shave some off of that PHP solution too...

Oh, you and your php. We'll see how well that language scales when we leave the little league game and actually do something useful.

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum