Arc Forumnew | comments | leaders | submitlogin
Winning a code golf contest using Arc (redux) (zck.me)
2 points by akkartik 4676 days ago | 6 comments


2 points by akkartik 4676 days ago | link

I'm resubmitting http://arclanguage.org/item?id=16697 because we didn't do it justice the first time around. I could swear I read it, and yet apparently I didn't notice many things:

a) The difference between + and join, which triggered this post (http://arclanguage.org/item?id=17074)

b) The idea of overloading * analogously to +. And of making avg variadic. zck, do you want to update anarki? I'm happy to do it otherwise.

c) Quotable quote: "I thought of no simple way to do this but functionally, using recursion. The second place winner, who wrote in Python, approached this imperatively, using mutable state. We actually had to explain our solutions to each other, as the minified versions were not easy to gather the algorithm from. Programming styles can be restrictive."

All this was worthy of comment the first time it was submitted. Thanks, zck.

-----

2 points by Pauan 4676 days ago | link

I'd be interested in seeing the Python version.

-----

3 points by zck 4676 days ago | link

The python version is:

  import math,itertools as i
  def f(k,p):
  	while(len(p)>k):
  		m=()
  		for r in i.combinations(p,2):
  			d=math.sqrt(sum(map(lambda x,y:(x-y)**2,*r)))
  			if d<m:m=d;x,y=r
  		t=p.remove;t(x);t(y);p+=[map(lambda g,h:(g+h)/2,x,y)]
		print p

-----

1 point by akkartik 4675 days ago | link

It occurs to me that avg is intended to be analogous to sum. Perhaps we could have two variants, avg and mean. Which one should be variadic, thoughts?

-----

2 points by rocketnia 4675 days ago | link

Well, essentially we already have 'avg:list as the variadic version. In fact, it looks like we can shave off two characters:

  (map(fn e avg.e)r t)
  (map avg:list r t)
I don't remember ever using 'avg except maybe to average some time measurements at the REPL, and I think the idiom (avg:accum ...) comes in handy for that. But I generally don't use arithmetic operators in my programs, probably due to the subject matter I choose.

Edit: Here's another opportunity:

  (cdr(mem _ s))
  (cdr:mem _ s)

-----

1 point by akkartik 4675 days ago | link

After I wrote that comment it seemed to me that variadic is more fundamental, because you can always convert lists using apply. But you're right that it's not hard to bounce between the two.

-----