Posts

Showing posts from July, 2008

Mousetrap in C language

Just for fun, and performances are horribles as well in new C version, performances are great until 5000 cards, so I need to remove some dust from my good old ANSI C book :D New version, compatible with gcc compiler. To create and use the executable, gcc Mousetrap.c Then call the file Mousetrap 5, for example, to obtain 1,3,2,5,4 Works quickly until 5000 results, requires a monster PC for 1000000 Google limit (algo is still not perfect, but this can generate an entire deck, instead of getting only that index, for each index) #include <stdlib.h> int *perfectDeck(int cards); int main(int argc, char **argv){ if(--argc){ *argv++; int i = 0, cards = atoi(*argv); if(cards > 0){ int *result = perfectDeck(cards); printf("%i", result[i++]); while(i printf(" %i", result[i++]); free(result); } } return 0; } int *perfectDeck(int cards){ i...

Google Code Jam: Mousetrap in 4 languages - GAME OVER

First of all, I would like to thank the Google team for Code Jam opportunity, and its organization: AMAZING! Yesterday I have lost my Round without a single commit. This time I have not excuses, it was simply my fault. I did not understand properly first two problems, or better, expected results, but I was sure the reason was my poor English knowledge, and I wouldn't be silly, asking silly questions (some question was silly enough, so, next time, I'll be less shy than this time). Accordingly, since Code Jam is a challenge, I chose to solve the most difficult problem, the Mousetrap. First of all, because the problem, and the expected result, was so simple to understand, secondly, because it was the best one for points :geek: Problem Analysis Ok, we had 2 hours to analyse the problem, to find the best solution, and to commit results, and this time I did not even download the test, because I would like to be sure that my solution was good enough to solve the problem. Count positio...

JXON - A little update loads of documentation

I have updated my lightweight JXON library right now, lightly improving them, and adding a consistent documentation for each method as well. At this point, the last thing I could do about JXON, is to talk about what exactly is, and what is not at all. JXON IS An intermediate, well defined, layer between native JavaScript variables, and a generic page content (their representation inside the layout) A fast and cross-browser JS to XML, and vice-versa, parser A human comprehensive way to represent JavaScript Object Notation via XML using a natural, lightweight, and unambiguous, schema, that will make XSL files creation, manipulation, and maintenance, more clear and natural than ever, working over both XPath, and semantic data types nodes with optional keys for object members A simple and crossbrowser global object, with few methods to transform JavaScript raw data into a valid (x)HTML layout page, or portion An advanced, XSLT based, enterprise ready way to represent d...

JXON - Lossless JavaScript to XML Object Notation convertion

It seems to be the time to talk about JavaScript template engines , and DocumentFragments , so here I am with my last idea: a JavaScript XML Object Notation transformer . Introduction There are a lot of ways to interact between the client, and the server, in a W3 friendly way, such Ajax, the classic remote scripting, Comet, and last, but not least, and probably first of all, XML . The latter " protocol " is one of the most flexible, compatible, and cross platform one you can find in the IT sector, in a word: universal. Over XML we are loaded of different standards to transport every kind of data, which one should be compatible with browsers, parsers, and preferably with JavaScript DOM implementation as well. One of the main limitations I have spotted about XML to JavaScript and vice-versa conversion, is the data type, usually lost, and the prolixity of JavaScript data representation. JSON is great, but whtat's up if we destroy its nature? The JavaScript Object Notation m...

Google Code Jam, or better, how to feel a 100% idiot !

Update 2 Congratulations! The results of the Google Code Jam 2008 Qualification Round are now official, and we're happy to announce that you have advanced to Round 1 Cheers :D ----------------------------------------- Update Guys, I do not know what Google judges will think about him/her, but in my mind, the qualification winner, for Python language, is the user camrdale , currently in position 40 of global score grid. That code is extremely clear (come on, that is Python, how can be different!), essential, and the logic is probably not the perfect one, but is so linear that you can understand the problem even without reading them. Well done camrdale, and good luck for the contest :geek: ----------------------------------------- Guys, honestly, I did not think about that kind of selection only to participate, bloody hell! First of All, the selection started at 11pm, and since I am an employee, and an hard worker as well, I though: who care? I have 24 hours to partecipate . This is ...

JavaScript Relator Object, aka unobtrusive informations

Have you never though about add, modify, or remove a generic information from a variable, and without changing variable itself? This is all about what my last simple creation does. Relator Object Concept The main purpose for this object is to associate every kind of information, value, or function, into a generic variable, whatever it is, and without modifying its native state. To obtain this result, I have used a 1:1 relationship between a stack that will contain every stored variable, and another one that will contain related objects. Stack = [1, 2, 3]; RelatedObject = [{}, {}, {}]; // add a property RelatedObject[Stack.indexOf(2)].description = "Number 2"; // remove a property Stack.splice(0, 1); RelatedObject.splice(0, 1); // situation Stack = [2, 3]; RelatedObject = [{description:"Number 2"}, {}]; Using above strategy we obtain 2 benefits: The Stack does not cause memory leaks both Stack and RelatedObject are alw...

An alternative JavaScript development pattern

A common pattern to develop JavaScript libraries is to use different behaviours inside methods, and based on browser, or browser plus its version. For about 80% of cases, these behaviours are Internet Explorer dedicated. This approach is good enough, otherwise we could not have a wide variety of libraries to choose, but is this way to develop libraries the best one? These are some pro concepts: libraries could be usually merged into a single file, so we can add only one script tag, and for every browser libraries maintenance or improvements are focused into one, or more, constructor, function, or method The expected result is, usually, a method that is capable to understand which browser is running them, and what to do for that specific case. // common libraries development pattern function myGorgeusLib(){}; myGorgeusLib.prototype.sayHello = function(){ if(self.attachEvent) attachEvent("onload", function(){alert("Hello")}); else if(self.addEventLis...

He wrote: I'd like to know your opinion about JS2 ...

... and here I am. My opinion about ECMAScript 4 is not that simple to explain. I would like to have a language, whatever it is, that is powerful, truly natively cross browser, and widely adopted from every kind of device. It seems I am talking about ActionScript, since Flash Player respects, aproximatly 90%, my desires, but I am not. I "come" from Macromedia world, as a Certified ActionScript 2.0 Developer (no design guys, only pure AS2 OOP), and I have used, for years, Flash as production environment. Looking at the little "JS2 monster" they are creating, I could smile, thinking about problems I had for those years with an environment that would like to be compatible with both old and new code, loads of unresolved bugs, and never perfectly cross browser ... we just have them, it is JavaScript, with its numerous implementations. At the same time, I would love to see JavaScript 2 power, in a bytecode based VM, but what I do not want, is a partial implementation of t...

scary eval to create the most safe environment ever

Are you worried about Array redefinition? Are you worried about eval function redefinition? Are you worried about whatever you want redefinition? STOP With the incredible eval feature "discovered" few days ago, it possible to create the most safe environment ever for every kind of purpose, starting from JSON evaluation, arriving to total protected native constructors . const safeEnvironment = function(A, B, D, F, N, O, R, S){ const Array = A, Boolean = B, Date = D, Function= F, Number = N, Object = O, RegExp = R, String = S, eval = function(String){ return self.eval("(" + String + ")"); }; return new function(){}; }( Array, Boolean, Date, Function, Number, Object, RegExp, String ); With above piece of code you can evaluate every kind of script without to be worried about common ...

SCARY EVAL ... and the "futuristic" solution

Image
Planet Earth, Year 2000 eval("document." + myformName + "." + myInputName + ".value"); Satellite Moon, Year 2004 eval is evil, nobody should use them, " except for json evaluation " Planet Earth 2.0, Year 2008 OMG somebody could penetrate my closures!!! Web Reflection, few days later First of all, it was in the MDC , so if everbody is surprised, it is because we did not read that page, or simply we did not try that code (please correct that page, the second argument is everything but not the same of with statement). Secondly, please, DO NOT CHANGE THAT FEATURE !!! What I mean is that JavaScript is an amazing language, where everything could be changed , thanks to its intrinsically dynamic nature. If we are worried about this fake news: we should worry, before, about XSS in our site (after SQL injections) if we are sure about missed XSS possibility, we should be worried about external libraries if we are sure about third parts libraries, we shoul...