News for hackers. Stuff that matters.
Re:Collection Of Puzzles For Programmers by rauljara on Saturday August 18, 2012 @01:29PM (#4401669)
I mostly agree with you in that there are perfectly valid reasons for learning similar languages. The quote is too strong, too absolute. But I still have a weakness for the quote because learning languages that alter your thinking is so much more useful than learning ones that don't.

As to ruby/python, I only have a superficial knowledge of Python, so please correct me if I'm wrong, but my understanding is that ruby blocks let you do thinks you can't in python. Rspec is one of the examples that gets talked about a lot. But if I were asked to sum all the numbers divisible by both 3 and 5 between 1 and 100 in Ruby, I'd do it like this

  (1..100).select{|a| a % 3 == 0}.select{|a| a % 5 == 0}.inject(&:+)
In my mind, that is a completely different way of solving the problem than by running a loop. My understanding is that you can't really do something like that conveniently in Python. Though, like I said, I really don't know python, so please do correct me if I have that wrong.
Re:Collection Of Puzzles For Programmers by Thrymr on Saturday August 18, 2012 @01:35PM (#4401694)

  sum([x for x in xrange(1, 101) if x % 3 == 0 and x % 5 == 0])
edit: actually sum the list. Of course, it's even shorter if you realize you can write it as:

  sum([x for x in xrange(1, 101) if x % 15 == 0])
Re:Collection Of Puzzles For Programmers by jgeralnik on Saturday August 18, 2012 @02:18PM (#4401807)
You're better off making that a generator:

sum((x for x in xrange(1, 101) if x % 3 == 0 and x % 5 == 0))

Notice the parenthesis instead of square brackets. Your version actually creates a list in memory. A generator generates the items one by one and doesn't need to store them all at the same time.

Re:Collection Of Puzzles For Programmers by dbaupp on Saturday August 18, 2012 @03:04PM (#4401954)
You can just write it as

  sum(x for x in xrange(1,101) if x % 3 == 0 and x % 5 == 0)
and skip the inner set of parenthesis; the parenthesis on a generator expression are not needed if it is the only argument to a function.
Re:Collection Of Puzzles For Programmers by darkestkhan on Sunday August 19, 2012 @03:34AM (#4403496)
Can't you all think for a bit? All of These solutions are O (n) in both time and space... there is solution that will take O (1) time and space - and I think that you learned it in math class at (latest) high school (hint: arithmetic progression)
Re:Collection Of Puzzles For Programmers by dbaupp on Sunday August 19, 2012 @03:47AM (#4403510)
Seriously? The discussion is about language features, not optimal algorithms.

One could imagine the question was actually "sum BusyBeaver(n) for n divisible by 3 and 5 between 1 and 100", i.e. no closed form. In Python:

  sum(BusyBeaver(x) for x in xrange(1,101) if x % 3 == 0 and x % 5 == 0)
(And yes, if one was being really pedantic, one could replace the condition with x % 15 == 0.)
Re:Collection Of Puzzles For Programmers by darkestkhan on Sunday August 19, 2012 @03:57AM (#4403522)
The thing is that people start using those libraries w/o thinking before - and theirs' solutions end up doing much more work than is needed
Re:Collection Of Puzzles For Programmers by delinka on Sunday August 19, 2012 @02:22PM (#4404708)
This could be said of most anything high level.
Re:Collection Of Puzzles For Programmers by the_cat_kittles on Saturday August 18, 2012 @04:12PM (#4402168)
true, but xrange(1,101) is so tiny, who cares!
Re:Collection Of Puzzles For Programmers by RegEx on Saturday August 18, 2012 @02:29PM (#4401847)
Despite the answers provided by fellow HNers, even if there wasn't a loop-less way to handle the scenario in Python, I assert that taking advantage of a language feature doesn't necessarily equate to "altering your thinking". You may find yourself, though, altering your thinking after defining language features have accumulated to a certain degree.
Re:Collection Of Puzzles For Programmers by Thrymr on Saturday August 18, 2012 @02:43PM (#4401890)
I agree, the quote is more applicable to conceptually very different languages (e.g. Lisp, Forth, and Smalltalk) than to clever features of particular languages.
Re:Collection Of Puzzles For Programmers by vuknje on Saturday August 18, 2012 @03:16PM (#4402001)
Or in javascript..

  new Array(100).join(1).split(1)
    .map((function() { var i = 0; return function() {return ++i;}})())
    .filter(function(x) { return !(x % 15); })
    .reduce(function(x, a) { return a += x; }, 0);