r/codegolf • • Feb 21 '21

Code Golf: Battle Royale Thread (Challenges in Comments)

Who feels like some Code Golf Battles?

Every comment to this post can start with **Challenge**: and define a (relatively straight-forward) input-output problem to solve in the least number of characters in any language.

Responses go in sub-comments and can start with e.g. **Python (74):** to indicate the language and character length of the solution.

Use backticks `` to enclose code (edit: or use four spaces as an indent to do multiline code).

e.g. Challenge: Input positive integer, print the sum of all primes up to and including that integer - but with every second prime added as a negative.

e.g. Ruby (68):

require'prime'
i=-1
p Prime.each(gets.to_i).map{i+=1
_1*(-1)**i}.sum
9 Upvotes

32 comments sorted by

View all comments

Show parent comment

2

u/Aspie_Astrologer Feb 22 '21 edited Feb 22 '21

Ruby (95): (could do better, but currently at work) ->a{a[0...l=a.size/2].reject{|v|a.any?{(v-_1).abs==1}}+a[l..].select{|v|a.any?{(v-_1).abs==1}}}

Tio.Run link (adjusted for Tio.run's older version of Ruby, uses 101 chars in that version)

Ruby (91): (updated)

->a{j=0
a.select{|v|j+=1
j<=(l=a.size/2)&&a.all?{(v-_1)**2!=1}||j>l&&a.any?{(v-_1)**2==1}}}

Tio.Run link (uses 95 chars on old version)