View Problem

Subdivide A Problem To A Pool Of Workers (No Shared Data)

Take a hard to compute problem and split it up between multiple worker threads. In your solution, try to fully utilize available cores or processors. (I'm looking at you, Python!)

Note: In this question, there should be no need for shared state between worker threads while the problem is being solved. Only after every thread completes computation are the answers recombined into a single output.



(In python syntax)

["ab", "we", "tfe", "aoj"]

In other words, a list of random strings.


(In python syntax)

[ ["ab", "ba", "aa", "bb", "a", "b"], ["we", "ew", "ww", "ee", "w", "e"], ...

In other words, all possible permutations of each input string are computed.
using concurrent

// as per Java answer, doesn't duplicate chars from input string, i.e. no 'aa'
const class PermGen : Actor
new make(ActorPool pool) : super(pool) {}

Void permutations(Str prefix, Str w, Str[] pset)
n := w.size
if (n == 0)
if (!pset.contains(prefix))
n.times { permutations(prefix + w[], w[0..<it] + w[it+1..<n], pset) }

override Obj? receive(Obj? msg)
Str word := msg
wordSubPerm := Str[,]
for (Int i := 0; i < word.size; i++)
for (Int j := i; j < word.size; j++)
permutations("", word[i..j], wordSubPerm)
return wordSubPerm

class SolutionXX
static Void main()
pool := ActorPool() { maxThreads = 8 }
futures := Future[,]
["ab", "we", "tfe", "aoj"].each { futures.add(PermGen(pool).send(it)) }
futures.each { echo(it.get) }

Submit a new solution for fantom
There are 13 other solutions in additional languages (clojure, cpp, fsharp, groovy ...)