# You have the test data already sorted by decreasing likelihood that
# the user will click the ad.

# The following code combine multiple ad_ids per display_id into
# a single cell.

test <- data.frame(
    display_id=rep(358,3),
    ad_id=c(29817, 173006, 257311)
    )

submission <- aggregate(
    formula = ad_id ~ display_id,
    data = test,
    FUN = function(x) paste(x, collapse = " ")
    )
    