View Full Version : Code Challenge! v1.1
Algorithm
01-07-2003, 01:04 AM
Welcome to version 1.1 of beetle's Code Challenge! Let's get right down to business, shall we?
The Challenge
Create a function (or method) that reverses the order of the words in a given string, while maintaining proper punctuation. For example, reversing this challenge would result in the string below.
Below string the in result would challenge this reversing, example for. Punctuation proper maintaining while, string given a in words the of order the reverses that (method or) function a create.
Keep the following in mind when coding: Words can contain letters, numbers, or a combination of the two. Don't worry about spelling or grammar.
Do not assume the initial string will have proper punctuation. spaces may be omitted,capitalization may be disregarded, and multiple puntuation marks may be unusually combined..., or left off entirely
In such cases, you should still establish proper punctuation in the returned string, where possible.
Make sure to discriminate between the period, the decimal point, and the ellipsis (...).
For simplicity's sake, you may assume that the ' character will always represent an apostrophe, and that a sentence will never begin with a proper noun.
Bonus points if you allow for emoticons! :)
The Rules
Violations of any of these rules will result in disqualification. No JScript-only functions or methods, must be ECMAscript compliant
Code must be completely independent (cannot rely on any external classes or libraries)
The code must be able to run, without errors, on the latest versions of Internet Explorer, Mozilla, and Netscape.
Once you've submitted your code, you're done. Your submitted post cannot be edited, and any resubmissions will be ignored.
Grading
Submitted code will be graded on a scale from 1 to 10. Attention will be paid to the functionality, efficiency, simplicity, and brevity of your approach. Although shorthand is allowed, multiple assignments will still be counted as multiple lines.
The winner of this contest will receive the dubious honor of presenting the next one!
The deadline for this challenge is Sunday, January 12, at 9pm Pacific time.
Good luck!
Originally posted by Algorithm
The code must be able to run, without errors, on all JavaScript 1.5 enabled browsers (IE6, Moz, etc.)
Considering Moz is the only browser which implements Javascript 1.5, I figure you might want to revise those rules.
I assume you meant to say that the code must be able to run in an ECMAScript 3rd Edition (http://www.ecma.ch/ecma1/STAND/ECMA-262.HTM) compliant interpretter, without implementation-dependent features?
Algorithm
01-07-2003, 01:42 AM
Actually, I meant that it needs to be able to run without errors in the latest browsers. The rules have been updated accordingly.
whammy
01-07-2003, 02:13 AM
I would do this, and I know I can using regular expressions, but I'm looking for challenges that are a bit more practical. ;)
caldasgsm
01-07-2003, 11:02 PM
I would do this, and I know I can using regular expressions, but I'm looking for challenges that are a bit more practical.
I agree.. I would like to be part of this challenges but, this algorithm/function things, are a bit boring...:rolleyes:
Can't you guys come up with something better than word/number manipulation...:(
ether a function a little more complex, that or a full application with functionalities, that are actually fun to develop...:p
In sum.. instead of asking to write a algorithm for a (almost)predefined logic... can you actually ask for the logic...;)
ca_redwards
01-08-2003, 12:30 AM
Originally posted by whammy
I would do this, and I know I can using regular expressions, but I'm looking for challenges that are a bit more practical. ;)
My challenge was to utilize my recently revised HTML() bookmarklet library (http://www.angelfire.com/ca/redwards/html__.calendar.html) in the consturction of a clientlet expression builder. Some who have looked at my less-than-4K calendar, have expressed that while the underlying JavaScript is fairly easy to read, it can get pretty confusing if too many chained functions and/or nested parentheses are used.
Perhaps I should open my own challenge thread for it, but I'd like to see if anyone on here can best my builder code. As it stands, it is about 1.5K alone (but relies upon the library which is 1.8K).
Care to hack a few bytes, anyone? :)
brothercake
01-08-2003, 01:23 AM
sorry i'm gonna be a sponge as well ... but how do you determine 'proper' punctuation? What's your reference source, and how can you determine it anyway in an inverse-sentence - where the word-order itself is grammatically illegal?
whammy
01-08-2003, 01:31 AM
That's another reason I didn't want to take it on... you'd have to update your code for all kinds of punctuation or other things that you hadn't taken into account to begin with. And accounting for "incorrect" punctuation in a string that hasn't been defined already (and with no rules to go by, i.e. "If in the original string, a word is followed by a comma, it must be modified so that the next word in the original string is the one followed by a comma when reversed", to start with) is a bit daunting (to me, anyway!).
Algorithm, did you write such a script? If so, can we try to break it? :D
I think one of the things that made it fun for beetle was to make something as elegant as he could think of, and then see what other people could do, and whether it could be improved upon, since it's a great learning experience for all involved.
Something like a super-short Mod 10 CC Check script that validates card type AND the mod 10 check, or an improvement on my latitude-longitude DMS to Decimal converter, or anything at all that people could actually use would be an improvement. :)
beetle
01-08-2003, 01:48 AM
Originally posted by caldasgsm
I agree.. I would like to be part of this challenges but, this algorithm/function things, are a bit boring...:rolleyes:
Can't you guys come up with something better than word/number manipulation...:(
ether a function a little more complex, that or a full application with functionalities, that are actually fun to develop...:pI agree that something more involving/complex could be more invigorating than simple algorithms and functions. However, having been at the helm of two of these challenges (with the concept first being my own) I can say that even with relatively simple challenges it can be difficult to track, test, and 'grade' the submissions, this all done in spare time, mind you.
Remember, you don't have to participate. If you don't like the challenge, then don't join. Create your own. That's all I did after all. Perhaps the mods could be persuaded to create a 'Challenge' sub-forum ;)
I'm no super-experienced programmer, but algorithm development is pretty darn important. Some of us are less experienced at it than other, and I, for one, enjoy any such challenges. You see, I intend to pursue a BS/MS in Computer Science in less than a couple of years. Until I have the benefit of classroom instruction and assignments, I have to challenge and educate myself. This forum is big enough to handle what everyone wants.
I can certainly concede that Algorithm's challenge is a bit odd and most certainly useless outiside of this forum post, but I think it's meant to be one of those mountains you climb 'because it's there'. I'm gonna give it a shot, and I'm darn close to getting it right :D
P.S. I DO have a good idea for my next challenge ;)
Originally posted by beetle
I'm no super-experienced programmer, but algorithm development is pretty darn important.
Very true. However if you're only interested in creating algorithms, you may want to consider a different language than Javascript. Algorithms are language-independent, and there are certainly better languages than Javascript to implement them, in terms of speed, efficiency (pointers/references for example), and capability (reading from files, etc).
beetle
01-08-2003, 02:32 AM
Originally posted by jkd
Very true. However if you're only interested in creating algorithms, you may want to consider a different language than Javascript. Algorithms are language-independent, and there are certainly better languages than Javascript to implement them, in terms of speed, efficiency (pointers/references for example), and capability (reading from files, etc). Yes, I realize that. But, the nature of javascript makes it easy to share and test quickly, ideal qualities for simple challenges like this. I spend my own time working with PHP and C++.
But I do agree, even with stuff other than what you've mentioned. I was lamenting today that I could not Ungreedy a regex in JS :rolleyes:
Algorithm
01-08-2003, 06:27 AM
Originally posted by whammy
Algorithm, did you write such a script? If so, can we try to break it? :DWell, no. :o It was pretty much the only thing I could think of on short notice.
Regardless of the inanity of the task, I still encourage everyone to participate, if for no other reason than to ensure the next challenge won't be even more pointless. ;)
Steven_Smith
01-08-2003, 06:56 AM
My humble thoughts on the coding challenge.
I think that it is a great idea.
I use coding forums to ask questions of other scripters when I am stuck, getting an error or just plain lost in a script. I can get help quick and fast, and I sometimes wonder what I would do without it.
I also use it to sharpen my skills. I log-in every now and then to see if I can help people and to see what other people are trying to do. Doing this takes my mind of what scripts I am creating, but keeps my mind in a scripting mode.
The coding challenges could feed my mind and keep it thinking the way I have too all day.
Coding challenges gets a :thumbsup: :thumbsup: from me!
Thanks
Steve
beetle
01-08-2003, 07:22 AM
Well, I'm finished. I know it's not perfect, but I've taken it as far as I've prepared to go. I'll take my grade as-is ;) I just don't have the free-time to do more.String.prototype.reverseSentence = function()
{
var reverse = this.split(/\b/).reverse();
for ( var ind, i=0; ( Boolean( word = reverse[i] ) && Boolean( c = word.charAt(0) ) ); i++ )
{
if ( /^[A-Z]/.test( word ) )
{
reverse[i] = word.toLowerCase();
ind = ( reverse[i+2] ) ? i+=2 : 1;
reverse[ind] = reverse[ind].charAt(0).toUpperCase() + reverse[ind].substring(1);
}
}
reverse = reverse.join('');
var pats = [
/ \(\.\.\.( )?\) /g,
/(\)) ([^\(]+) (\()/g,
/^(.)(.*)/,
/\(ellipsis\)/g,
/(\d+)(.)(\d+)/g
];
var reps = [
") ellipsis (",
" ($2) ",
"$2$1",
"(...)",
"$3$2$1"
];
for ( var i=0; ( pat = pats[i] ); reverse = reverse.replace( pat, reps[i++] ) ) {};
return reverse;
}If I count right, I've got 11 lines here :D
beetle
01-08-2003, 12:44 PM
Note: I've just noticed that some of my older code was still in this version. The first for loop should look like thisfor ( var ind, i=0; ( word = reverse[i] ); i++ )The way it is posted above still works, just is longer and creates an unecessary variable each loop.
oops :rolleyes:
Grade it as you will.
ca_redwards
01-09-2003, 12:25 AM
Originally posted by Algorithm
Create a function (or method) that reverses the order of the words in a given string, while maintaining proper punctuation. For example, reversing this challenge would result in the string below.
Below string the in result would challenge this reversing, example for. Punctuation proper maintaining while, string given a in words the of order the reverses that (method or) function a create.
Here's my wordy entry (it works as above, though)...
function reverseSentence(txt)
{
w=/(\d+(\.\d+)*|(\w|\'))+/g; s=/[a-z]+/g; b=/[A-Z]+/g; c=/(A|a|Aa)/g; p=/(\.|\?|\!)+/g;
function flip(t)/*reverses like-minded brackets*/
{
yn=false;oc='<[{()}]>';t=t.split('');
for(var i=0;i<t.length;i++)
{
x=oc.indexOf(t[i]);
if(x>-1){yn=true;t[i]=oc.charAt(oc.length-x-1)}
};
if(yn){t=t.reverse()};
t=t.join('');
return t
};
function oCase(t)/*summarizes capitalization*/
{
return t.replace(s,'a').replace(b,'A')
};
function reCase(t,o)/*string to lower/upper/proper/same case*/
{ if(o.match(c))
{ t=t.toLowerCase();
if(o=='A'){t=t.toUpperCase()};
if(o=='Aa'){t=t.split('');t[0]=t[0].toUpperCase();t=t.join('')}
};
return t
};
txt=' '+txt;/*leading space added...*/
wheat=txt.match(w).reverse();chaff=txt.split(w).reverse();/*threshes wheat from input text!*/
x=chaff[0];chaff[0]=chaff[chaff.length-1];chaff[chaff.length-1]=x;/*swaps 1st/last punctuation*/
wheat[0]=reCase(wheat[0],'Aa');wheat[wheat.length-1]=reCase(wheat[wheat.length-1],'a');/*force 1st/last case*/
for(var i=0;i<wheat.length;i++)
{ if(chaff[i].match(p)){wheat[i]=reCase(wheat[i],'Aa')};/*Capitalize new sentence.*/
if(i<chaff.length-1)
{ if(chaff[i+1].match(p)){wheat[i]=reCase(wheat[i],'a')}/*Lowercase end of sentence.*/
};
chaff[i]=flip(chaff[i]);chaff[i]+=wheat[i]
};
return(chaff.join('').split('').slice(1).join(''))/*...space taken away!*/
};
I tried to use \b to split the input, but decimal points and apostrophes became problematic. Instead, I opted to write a regular expression that catches all of the "wheat" while letting go of the chaff (punctuation too).
It occurs to me that I never used my capitalization classifier (oCase) and its associated regular expressions (s & b). Instead, I just blindly change the case of words that start/end sentences. And to make it all complete, I change the case of the very first/last words of the whole input.
A tricky thing was getting all of the matching brackets to look right. For these, I not only had to exchange the opening and closing brackets (since everything had been reversed already), but I had to reverse which word they bordered upon (thus, those tokens are reversed).
ca_redwards
01-09-2003, 12:48 AM
Can I be forgiven for omitting ONE CHARACTER? It's the quote character in function flip...
Originally posted by ca_redwards
Here's my wordy entry (it works as above, though)...
function reverseSentence(txt)
{
w=/(\d+(\.\d+)*|(\w|\'))+/g; s=/[a-z]+/g; b=/[A-Z]+/g; c=/(A|a|Aa)/g; p=/(\.|\?|\!)+/g;
function flip(t)/*reverses like-minded brackets*/
{
yn=false;oc='<[{(")}]>';t=t.split('');
for(var i=0;i<t.length;i++)
{
x=oc.indexOf(t[i]);
if(x>-1){yn=true;t[i]=oc.charAt(oc.length-x-1)}
};
if(yn){t=t.reverse()};
t=t.join('');
return t
};
function oCase(t)/*summarizes capitalization*/
{
return t.replace(s,'a').replace(b,'A')
};
function reCase(t,o)/*string to lower/upper/proper/same case*/
{ if(o.match(c))
{ t=t.toLowerCase();
if(o=='A'){t=t.toUpperCase()};
if(o=='Aa'){t=t.split('');t[0]=t[0].toUpperCase();t=t.join('')}
};
return t
};
txt=' '+txt;/*leading space added...*/
wheat=txt.match(w).reverse();chaff=txt.split(w).reverse();/*threshes wheat from input text!*/
x=chaff[0];chaff[0]=chaff[chaff.length-1];chaff[chaff.length-1]=x;/*swaps 1st/last punctuation*/
wheat[0]=reCase(wheat[0],'Aa');wheat[wheat.length-1]=reCase(wheat[wheat.length-1],'a');/*force 1st/last case*/
for(var i=0;i<wheat.length;i++)
{ if(chaff[i].match(p)){wheat[i]=reCase(wheat[i],'Aa')};/*Capitalize new sentence.*/
if(i<chaff.length-1)
{ if(chaff[i+1].match(p)){wheat[i]=reCase(wheat[i],'a')}/*Lowercase end of sentence.*/
};
chaff[i]=flip(chaff[i]);chaff[i]+=wheat[i]
};
return(chaff.join('').split('').slice(1).join(''))/*...space taken away!*/
};
That allows "quoted" words to be handled correctly. Otherwise, they'd come out wrong.
That allows "quoted" words to be handled correctly. Otherwise, they'd come out wrong. yields Wrong out come they'd, otherwise. Correctly handled be to words" quoted "allows that. but should return Wrong out come they'd, otherwise. Correctly handled be to words "quoted" allows that.
whammy
01-09-2003, 12:49 AM
Very interesting "wheat" and "chaff" variables there. ;)
Roelf
01-13-2003, 09:02 AM
Who won?
I thought of a new challenge, i could put it up, if the winner of this one doesn't have enough inspiration :rolleyes:
Roelf
Algorithm
01-13-2003, 09:34 AM
Just getting to that. :)
THE WINNERS ARE:
Good Sport Award:
beetle -- 7/10
Some Other Guy:
Steven_Smith -- n/a
Overall Contest Winner:
ca_redwards -- 8/10
Congratulations! :thumbsup:
ca_redwards
01-13-2003, 07:53 PM
Thank you! I have your next challenge... Code Challenge! v2.0 (http://www.codingforums.com/showthread.php?s=&threadid=12893) :D
Originally posted by Algorithm
Just getting to that. :)
THE WINNERS ARE:
Good Sport Award:
beetle -- 7/10
Some Other Guy:
Steven_Smith -- n/a
Overall Contest Winner:
ca_redwards -- 8/10
Congratulations! :thumbsup:
Steven_Smith
01-13-2003, 08:13 PM
WooHoo :cool:
I won the some_other_guy award! With a score of 'n/a'
They really love me they really do. (Sally Fields)
:p
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.