4
\$\begingroup\$

The challenge is to assign people to tasks randomly~. From stdin you get 2 lines. Line one is a comma-separated list of names. Line 2 is a comma-separated list of jobs. The output required is one line per task person combo. The line should be formatted as Name:task. If there are not enough people for jobs or vice verse then they will not be output.

~ Randomly is defined as each run with the same input has a reasonably different output.

Example Input:

Bob,John,Susan,Josie,Kim,Tom
Bins,Floor,Windows

Example Output:

Bob:Windows
Susan:Bins
Kim:Floor
\$\endgroup\$
  • \$\begingroup\$ Can there be only two possible outputs? \$\endgroup\$ – Leaky Nun May 20 '16 at 9:13
  • 1
    \$\begingroup\$ Also, usually we don't like to restrict on the input format. \$\endgroup\$ – Leaky Nun May 20 '16 at 9:14
  • \$\begingroup\$ @KennyLau what makes you think there could only be two possible outputs? \$\endgroup\$ – leumas95 May 20 '16 at 9:18
  • \$\begingroup\$ @KennyLau Also, sorry about specifying the input format, I actually don't care just thought I should specify something. I am used to stack overflow being really precise about the details provide for questions assumed it might extend here :) \$\endgroup\$ – leumas95 May 20 '16 at 9:21
  • \$\begingroup\$ I mean, can we randomly choose one from only 2 possible outputs? \$\endgroup\$ – Leaky Nun May 20 '16 at 9:28
3
\$\begingroup\$

05AB1E, 7 bytes

Code:

.r².r)ø

Try it online

Following the input/output spec strictly (16 bytes):

.r².r)øvy`r?':?,

Explained:

.r                # radomize first input
  ².r             # randomize second input
     )ø           # merge the lists to an array of pairs cutting out extras
       vy`r?':?,  # format output

Try it online

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Java 7, 240 239 238 bytes

import java.util.*;class M{public static void main(String[]a){String[]p=a[0].split(",");List j=new ArrayList(Arrays.asList(a[1].split(",")));for(int i=0;j.size()>0;)System.out.println(p[i++]+":"+j.remove((int)(Math.random()*j.size())));}}

Can most likely be golfed more with some arrays magic, instead of List j=new ArrayList(Arrays.asList(a[1].split(",")));..

Ungolfed:

Try it here

import java.util.*;

class M{
    public static void main(String[] a){
        String[] p = a[0].split(",");
        List j = new ArrayList(Arrays.asList(a[1].split(",")));
        for(int i = 0; j.size() > 0;){
            System.out.println(p[i++] + ":" + j.remove((int)(Math.random() * j.size())));
        }
    }
}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Jelly, 13 bytes

Œ!€X€ZL’$ÐfŒṘ

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Pyth, 6 bytes

CmO.pd

Try it online!

How it works:

CmO.pd
        (implicitly) takes the evaluated input
 m   d  for each "line":
   .p       generate all permutations
  O         choose one randomly
C       truncating transpose

Following strictly the input/output format:

(16 bytes)

jjL\:CmO.pcd\,.z

Try it online!

How it works:

jjL\:CmO.pcd\,.z
              .z  takes all lines
      m    d      for each line:
          c \,        split by ","
        .p            generate all permutations
       O              choose one randomly
     C            truncating transpose
 jL\:             join every pair by ":"
j                 join all pairs by newlines
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Realy nice, I have only ever done golf in python, I should learn pyth \$\endgroup\$ – leumas95 May 20 '16 at 9:19
  • \$\begingroup\$ The last edit seems to break the random on each run part. but still cool :), ohh you updated again nvm \$\endgroup\$ – leumas95 May 20 '16 at 9:26
  • \$\begingroup\$ This edit should be random. \$\endgroup\$ – Leaky Nun May 20 '16 at 9:28
1
\$\begingroup\$

Mathcad, [tbd] bytes

enter image description here

csort(M,n) sorts matrix M on column n. augment joins two arrays columnwise. runif(n,a,b) returns a vector of n uniformly-distributed numbers over the range a..b. concat(a,b) concatenates strings a and b.

With due apologies to Messrs Gilbert and Sullivan.


Mathcad scoring system yet to be determined (must get round to posting something on Meta ...). However, this solution clearly wouldn't be a contender for anything other than a Programming Language specific leaderboard. Taking a rough keyboard equivalence, and noting that Mathcad uses keyboard shortcuts for entering programming and some other operators, it takes around 100 "bytes".

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Q, 38 Bytes

t:{$[n:&/#:'x:{(-#x)?x}'x;+n#/:x;0N]}

NOTE.- input and output format not exactly as proposed (see test)

Test

."\\S ",$7h$.z.t    /To accomplish a random output we mofify seed based on internal clock
t(`Bob`John`Susan`Josie`Kim`Tom; `Bins`Floor`Windows)
/generates (`Susan`Windows;`Kim`Bins;`John`Floor) (a different result each run)

Explanation

(-seqLen)?seq Shuffles the sequence

x:{(-#x)?x}'x shuffles each sequence of x (argument with sequence of names and sequence of works) and reassign to x

n:&/#:'x Assigns to n the minimum over (&/) each sequence lengths (#:'x)

$[n;+n#/:x;0N] If n (if n is not 0), executes flip n take of each seq, Else returns null

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ruby, 69 bytes

n=gets.split(/,|\n/).shuffle;puts gets.split(',').map{|j|n.pop+':'+j}

Ungolfed:

names = gets.split(/,|\n/).shuffle
jobs = gets.split(',')
jobs.each do |job|
    name = names.pop
    puts "#{name}:#{job}"
end
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Firefox 30-57), 82 bytes

(a,b,s=a=>a.sort(_=>Math.random()-.5),c=s(b))=>[for(x of s(a))if(c[0])[x,c.pop()]]

Uses bogus sort to shuffle. Version which accepts two string parameters of comma-separated names and returns lines of colon-separated matchups for 110 bytes, where \n represents the literal newline character:

(a,b,s=a=>a.sort(_=>Math.random()-.5),c=s(b.split`,`))=>[for(x of s(a.split`,`))if(c[0])x+':'+c.pop()].join`\n`
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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