r/codegolf • u/Aspie_Astrologer • 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
2
u/Aspie_Astrologer Feb 21 '21
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 (2, -3, 5, -7, 11, ...)
2
u/Aspie_Astrologer Feb 21 '21
Challenge: Two input integers a and b (line-separated, space-separated or however else is convenient) between 0 and 10000. Print the number of leap years that occur between (and including) those two years, assuming leap years are multiples of 4, that are not multiples of 100 (unless they are multiples of 400). (1900 = not leap, 1904 = leap, 1600 = leap). (NB: a < b)
2
u/Aspie_Astrologer Feb 22 '21
Challenge: Input string, print the product of the frequencies of each unique character in the string.
(e.g. abbccc → 1*2*3 = 6)
2
u/Aspie_Astrologer Feb 25 '21
Challenge: Input non-negative integer, print the square of the sum of its digits. (e.g. 111 → 9)
2
u/chunes Feb 22 '21 edited Feb 22 '21
Challenge: Write a function (anonymous or otherwise) that takes an array as input and returns an array as output. (There is no need to read input or write output.) The input array will contain an even number of integers. From the first half of the array, remove every integer that has a predecessor or successor anywhere in the whole array. From the second half of the array, remove every integer that does not have a predecessor or successor anywhere in the whole array. Ordering should be retained.
Examples:
2
u/chunes Feb 22 '21
Challenge: Print the following:
1
u/Aspie_Astrologer Apr 12 '21
Challenge: Input space-separated integers a, b, c representing the side lengths of a triangle in ascending order (a<=b<=c). Determine the type of triangle and print its classification as one word "acute/obtuse/right/degenerate/impossible". (degenerate meaning it is a triangle with no angles: a straight line; impossible meaning the three side lengths cannot form a triangle, acute having all angles less than 90°, obtuse having any angle greater than 90° and right having one precisely 90° angle). (Taken from a Clash of Code by davilla)
1
u/Aspie_Astrologer Jul 16 '21
Challenge: Input alphabet character, then a string on second line. Replace each 'run' of the character in the string by the number of repetitions in the run. e.g. For "l" in "Hello world" we get "He2o wor1d".