- A few code golf problems
- 16 Nov 2018 01:08:06 pm
- Last edited by lirtosiast on 17 Nov 2018 12:48:37 pm; edited 2 times in total
Below are six small code-golf brainteasers that I originally wrote for PPCG SE but are now off-topic for that site. The solutions mostly involve tricks that I've used or seen in the past.
Code:
Please hide any spoilers if you solve the problems.
EDIT: Fixed a couple of reference solutions.
Code:
Problem 1
Output the following list: {10,15,21,28}; that is, the 4th through 7th triangular numbers.
//15 bytes:
seq(X(X+1)/2,X,4,7
Problem 2
Given an integer in Ans representing an integer between 1 and 10000000, output the number of factors of 2 that divide it.
//31 bytes
Ans→X
0→Y
While X/2=int(X/2
Y+1→Y
X/2→X
End
Y
Problem 3
Given a list with 2 or more elements in Ans, output 0 if all of its elements are the same, and a nonzero value if any of its elements are different.
//6 bytes
max(Ans≠Ans(1
Problem 4
Given a list in Ans of at least 4 integers from 1 to 6, determine if there is a sublist of four elements that form either two pairs or four of a kind: for example, 5,5,6,6, 1,4,4,1, and 3,3,3,3 are all sublists. Output a nonzero value iff there is at least one.
//??? bytes
Problem 5
Given a list in Ans of positive integers, calculate their harmonic mean.
//8 bytes
dim(Ans)/sum(1/Ans
Problem 6
Given a list of integers in Ans, find the sum of the elements greater than its minimum.
//10 bytes
sum(Ans*(Ans>min(Ans
Please hide any spoilers if you solve the problems.
EDIT: Fixed a couple of reference solutions.