it'd be interesting to see some of those sources. I'd probably use a recursive function.
what's the difference between ruby and ruby on rails?...i keep hearing all this that ruby is like the greatest, i can't really say anything because i've never really used it. How do you guys like it?
If I get my work done, I'll post prolog (clear but slow) for oddity sake.
2008/1/20, Raf Coremans <rrafje at gmail.com>:>> 2008/1/18, James Gray <james at grayproductions.net>:> >> > On Jan 18, 2008, at 3:10 PM, Radosław Bułat wrote:> >> > > On Jan 18, 2008 10:00 PM, yermej <yermej at gmail.com> wrote:> > >> > > My question is (I'm not familiar with RubyQuiz too much ): episode> > > focus on algorithm (speed) or source code (readable)?> >> > Hopefully both. > >> > James Edward Gray II> >My solution offers speed nor readability; I went for a one-liner (if youdiscount the require):$ cat rq153.rb#!/usr/bin/ruby -wrequire 'enumerator'p( ARGV. join( ' '). reverse. to_enum( :each_byte). inject( []){ |a, b| a << (a.last || "") + b.chr}. map{ |a| a.reverse}. inject( []){ |a, b| a << b.match( /^(.+).*\1/).to_a.pop }. flatten. compact. sort_by{ |a| a.size}. last)$ ./rq153.rb banana"an"$ ./rq153.rb aaabaaa"aaa"$ ./rq153.rb aaaaaa"aaa"$ ./rq153.rb ambidextrousnilDon't even think of running it on a reasonably-sized text, lest you wantyour PC to grind to a halt.Morale: ruby code *can* be ugly if you put your mind to it.Regards,Raf
p(ARGV.join(' ').reverse.to_enum(:each_byte).inject([]){|a, b| a << (a.last || "") + b.chr}.map{|a| a.reverse}.inject([]){|a, b| a << b.match(/^(.+).*\1/).to_a.pop}.flatten.compact.sort_by{|a| a.size}.last)
So i'm looking at: Ruby, Python, or Perl.what do you think I should pick. I'm leaning toward Python, because I'm used to C, and I like that style of programming, and hear Python is like that. But I read that thing on Ruby, where the maker said he taylored it to the programmer, not the computer, and I like that phisophy. Perl is probably cool, but it looks to different to me, and i don't like different so much
>> Can you give a little more insight >> into how you came to be using ruby as your programming language of >> choice instead of any of the other languages you mentioned. I know why >> you might choose ruby over C/C++ or Java, but what lead you to choose >> ruby over python, which as far as I can tell is ruby's closest neighbor.Wow, I wish there were some sort of universal wisdom involved, butI'm pretty sure it was a combination of pragmatism and a personalbias toward a particular sort of elegance and aesthetics in languagedesign that appeals to my own sensibilities. (In other words, matz kicksass at language design!)Of course, some languages were easier to loathe than others. <grin>This article pretty well summarizes the sort of horror I felt whendealing with VB6: http://www.ddj.com/windows/184403996As for Python, i tended to find the experience more frustrating andless fun than writing comparable code in Ruby. Learning Ruby, for me,involved a lot of "oh, wow, cool I can do that!" Whereas learning PythonI noticed a lot more, "oh... I'm not allowed to do that."I last used Python about six years ago, and I understand some thingshave evolved or improved since then (don't they have something moreakin to Ruby blocks now? And list comprehensions?)But anyway, a few examples of things that rubbed me the wrong wayabout Python. (Note, these may be things that Python peopleabsolutely love about the language!)I found the distinction between expressions and statements, and therestrictions on where one or the other could occur in the syntax, to bevery rigid and unhelpful.For example, the syntax is: if expression: statement elif expression: statement else: statementAnd assignments are not allowed in expressions.Thus, code that I wanted to write in Python like this, is illegal: while match = tagOrTextRegexp.search(html, pos): pos = match.end() gd = match.groupdict() if (val = gd.get('startTag')): attrs = parseAttrs(gd['allAttrs']) self.handleStartTag(val.lower(), attrs) elif (val = gd.get('endTag')): self.handleEndTag(val.lower()) elif (val = gd.get('text')): self.handleNonTagText(val) elif (val = gd.get('comment')): pass # ARTHUR (to KNIGHTS) Walk away. Just ignore them. else: assert 0, "unexpected match in regexp - supposed to be impossible"...ended up being written like this: while True: match = tagOrTextRegexp.search(html, pos) if match is None: break pos = match.end() gd = match.groupdict() # no assignment-in-conditional sux, Guido val = gd.get('startTag') if val is not None: attrs = parseAttrs(gd['allAttrs']) self.handleStartTag(val.lower(), attrs) else: val = gd.get('endTag') if val is not None: self.handleEndTag(val.lower()) else: val = gd.get('text') if val is not None: self.handleNonTagText(val) else: val = gd.get('comment') if val is not None: pass # ARTHUR (to KNIGHTS) Walk away. Just ignore them. else: assert 0, "unexpected match in regexp - supposed to be impossible"I found this sort of thing very annoying, as can be seen from the noteI left in the code for Guido (von Rossum, Creator of Python.) <grin>Another example would be class methods. (As opposed to instance methods.)I remember how hacky and inelegant it seemed to me that every instancemethod in Python needed to explicitly list the 'self' parameter: class Foo: def bar(self): print "bar!" f = Foo() f.barBut when I was learning Python, I had an insight and thought, well, at leastI know how to define class methods! Just leave off the self parameter! class Foo: def a_class_method(x,y) return x+yThen I should be able to call it directly on the class, with no instance passedin: Foo.a_class_method(1,2)Nope. Just doesn't work. No way to define class methods. (Dunno if thishas changed in the past six years or not.)Anyway, I don't recall many other examples anymore, but I vividly rememberfeeling frustrated in this manner often enough while learning Python that Iwould actually exclaim, "Guido!!!!" out loud at my desk when it would happen. (But I should point out that I was still having fun. After all, I enjoyedprogramming in Python _a lot_ more than Java.)Finally, I suppose this is a bit silly, but my very first few minutes withPython gave me a tangible feeling of trepidation when the followinghappened:I fired up Python (whatever the version was back then), and I got aninteractive interpreter. I was like, yay! cool! Python 2.4.1 (#1, May 27 2005, 18:02:40) Type "help", "copyright", "credits" or "license" for more information.Then I tried to quit: >>> exit 'Use Ctrl-D (i.e. EOF) to exit.' >>> quit 'Use Ctrl-D (i.e. EOF) to exit.' >>>And I thought... Oh no. Somebody actually knew exactly what Iwanted the computer to do - and actually went through the troubleto program a message to tell me I was doing it wrong.But again, while these are aspects of Python that rubbed me thewrong way, I realize that others may and do feel completelydifferently. If Ruby didn't exist, I'd have probably have usedPython for quite a while (maybe moving on to OCaml or Erlangby now, I dunno.)