1
\$\begingroup\$

Every year, Dyalog Ltd. holds a student competition. The challenge there is to write good APL code. However, this is a (common theme is manipulation of data of any type and structure, especially special handling of empty data) version of the Phase I problems – open to any language. Enjoy!

Objective

Write ten complete programs (or functions, etc.) to solve the below problems using any one language. The point is that you must select a language that is good at various tasks, as opposed to the common golfing strategy of choosing the right tool for the (single) task. You may not use one solution as subroutine in another solution, and your score is the total byte count of all ten programs together.

Passing all the test cases is sufficient to be a valid submission.

Closing Date

18 August 2016.

Related: possibility of real world prizes

If you chose to use Dyalog APL (free download), you may also want to participate in the original competition, but note that the objective there is to submit quality code – not necessarily short code. If you are a student, you can win USD 100 for Phase I. Phase II (not included here) can win you up to USD 2000 and a free trip to Scotland.

The problems

Problem 1 – Statistics - Mean

Write a program that takes a numeric array and gives the mean (average) of the array.

Test cases:
[1,2,3,4,5,6]3.5
[]0
1717
[[1,2,3,4],[5,9]]4

Problem 2 – Statistics - Median

Write a program that takes numeric data and gives the median of the data. The median is the number separating the higher half of the data from the lower half. The median can be found by arranging all the observations from lowest value to highest value and picking the middle one. If there is an even number of observations, then there is no single middle value; the median is then defined to be the mean of the two middle values.

Test cases:
[1,2,3,4,5,6,7,8,9]5
[[1,8,2],[7,3,6,4,5]]4.5
[]0
77
[1,1,1,9,1]1
[1,3,9,1]2

Problem 3 – Statistics - Mode

Write a program that takes a numeric list and gives the mode, most common value, of the list. If more than one number occurs the greatest number of times, return all such numbers.

Test cases:
[2,2,1,4,3,2,5,1,2]2 or [2]
[][]
[1,2,3,4,1,2][1,2]

Problem 4 – Just Meshing Around

Write a program that takes strings or two numeric lists and gives the inputs shuffled into a string/list formed by alternately taking successive elements from each input. (This is known as a Faro shuffle.) The inputs do not have to be the same length. Begin with the first input.

Test cases:
'MENS' 'EKES''MEEKNESS'
'Dyalog' 'APL''DAyPaLlog'
'APL' 'Dyalog''ADPyLalog'
[1,3,5,7] [2,4,6,8][1,2,3,4,5,6,7,8]
'' 'Hello''Hello'
'Hello' '''Hello'
'' ''''

Problem 5 – You're Unique, Just Like Everyone Else

Write a program that takes a list or string and gives elements or characters that occur only once in the input.

Test cases:
[1,2,3,4,5][1,2,3,4,5]
[1,2,3,4,5,4,3,2,1][5]
'hello world''he wrd'
'aabbcc'''
''''

Problem 6 – Shorter Ones to the Front

Write a program that takes a list and sorts it by the length of each element. Note: an element of the list can be single number, in which case it must be counted as length 1.

Test cases:
['one','two','three','four','five','six']['one','two','six','four','five','three']
[[2,4,3],[4,5],1,[7,3]][1,[4,5],[7,3],[2,4,3]] (sorting is not necessary)
[][]
[[],[2,2],1][[],1,[2,2]]

Problem 7 – 3's and 5's

Write a program that takes a numeric list and returns all elements that are divisible by 3 or 5.

Test cases:
[1,2,3,4,5,6,7,8,9,10][3,5,6,9,10]
[][]
[-1,-2,-3,-4,-5,-6,-7,-8,-9,-10][-10,-9,-6,-5,-3] (sorting is not necessary)

Problem 8 – Separating Out the Negative

Write a program that takes a numeric list and gives a two element list whose first element contains the values less than 0 (zero) in the input and the second element contains all values greater than or equal to 0 (zero).

Test cases:
[0,1,-2,3,-4,-5,6,7,8,-9,10][[-2,-4,-5,-9],[0,1,3,6,7,8,10]]
[1,2,3,4,5][[],[1,2,3,4,5]
[-1,-2,-3,-4,-5][[-1,-2,-3,-4,-5],[]]
[][[],[]]

Problem 9 – Delimited Text

Write a program that takes two strings where the first string is delimiters in the second string. The program should give the delimited text as a list of strings.

Test cases:
',' 'comma,delimited,values'['comma','delimited','values']
' ' 'break up words'['break','up','words']
',' ','['','']
',' 'hello world'['hello world']
',' ''['']
',;' 'multi,delimited;values'['multi','delimited','values']

Order Total

Write a program which takes list of prices and a list which indicates the count ordered of each corresponding item and gives the total cost for the order.

Test cases: [2.99,4.99,1.99] [5,0,2]18.93
[2.99,4.99,1.99] [0,0,0]0
[123.00] [0]0
[2.00 0.00] [3,4]6
[] []0


I have explicit permission to post this challenge here from the original author of the competition. Feel free to verify by following the provided link and contacting the author. I gave the original competition author the link to this page within the minute I posted it, so they can check if I submit someone's answer as my own.

\$\endgroup\$
40
  • 1
    \$\begingroup\$ Multi-part challenge and duplicates... \$\endgroup\$
    – Leaky Nun
    Aug 10, 2016 at 18:10
  • 1
    \$\begingroup\$ @Adám Requiring the mean and median of [] to return 0 seems arbitrary. Something like NaN would make more sense. Also, I fail to see what the common theme to all tasks is \$\endgroup\$
    – Luis Mendo
    Aug 10, 2016 at 18:33
  • 1
    \$\begingroup\$ @LeakyNun Not so clear that you couldn't answer... \$\endgroup\$
    – Adám
    Aug 10, 2016 at 18:38
  • 1
    \$\begingroup\$ Does not make the specs clear. \$\endgroup\$
    – Leaky Nun
    Aug 10, 2016 at 18:40
  • 1
    \$\begingroup\$ @Rainbolt, Adám, If there is no sort of interaction between the individual parts, a common theme is no sufficient relation to justify a multi-part challenge. For the full reasoning, see the meta post linked by DJMcMayhem. There is no good reason not to split this into multiple challenges. \$\endgroup\$ Aug 10, 2016 at 19:47

1 Answer 1

2
\$\begingroup\$

Pyth, 88 86 84 bytes

Problem 1: Mean (6 bytes)

.O.n]Q

Test suite.

Problem 2: Median (23 21 bytes)

.O,hJ.x.<S*2K.n]QlK]0eJ
.O<.x.<S*2K.n]QtlK]02

Test suite.

Problem 3: Mode (7 bytes)

{.x.M/Q

Test suite.

Problem 4: Interleave (3 bytes)

.iF

Test suite.

Problem 5: Unique (7 bytes)

fq1/QTQ

Test suite.

Problem 6: Sort (6 bytes)

ol.n]N

Test suite.

Problem 7: Divisibility (10 bytes)

f!*%T3%T5Q

Test suite.

Problem 8: Separate (10 bytes)

,Jf<T0Q-QJ

Test suite.

Problem 9: Split (12 bytes)

AQu.ncRHGG]H
u.ncRHGQ]E

Test suite.

Problem 10: Dot-product (4 bytes)

s*VF

Test suite.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.