Live data from Hacker News

Calculate n + 1 without using + or - or * or /

news.ycombinator.com

11–19 of 19 posts

Re: Calculate n + 1 without using + or - or * or /

#11
def add_1(x): return sum([1,x])

Python.

Too simple? I didn't use any of the signs, and it demonstrates knowledge of the python standard library!

EDIT: Whoops, seems the idea of using the sum() function (or operator) has been dismissed elsewhere. Time to study some more languages, I think.

Re: Calculate n + 1 without using + or - or * or /

#13
I have the proper Enterprise version:

    def increment(a):
        import urllib2
        url = "http://www.html2xml.nl/Services/Calculator/Version1/Calculator.asmx/Add?a=%d&b=1" % a       
        result = urllib2.urlopen(url).read() 
        from xml.dom.minidom import parseString
        result = parseString(result)
        return int(result.getElementsByTagName('int')[0].childNodes[0].data)

Re: Calculate n + 1 without using + or - or * or /

#14

I have the proper Enterprise version: def increment(a): import urllib2 url = "http://www.html2xml.nl/Services/Calculator/Version1/Calculator.asmx/Add?a=%d&b=1" % a result = urllib2.urlopen(url).read() from xml.dom.minidom import parseString result = parseString(result) return int(result.getElementsByTagName('int')[0].childNodes[0].data)

I'm pretty sure that's not a valid solution. Its the same as using Math.sum(). While it evaluates your resourcefulness, the point of the question, I think, is to figure out your knowledge and creative thinking

Re: Calculate n + 1 without using + or - or * or /

#15
post #3

how about this (example in ruby): def plus_1(x); arr = []; arr[x] = true; return arr.size; end

Funny, this is the first thing I thought of, but then realized it would only work if x>0

lol .. but also wont work for decimals
Post reply on HN