I took Ric's solution and expanded it to better understand the steps
involved. My brain doesn't yet think in terms of @ [:
NumMovies=: 26
NumStudents=: 100
Groups=: NumStudents $ i.3
Votes=: ?. NumStudents$NumMovies
v=: Groups,. Votes
VotesFreqTbl=: (~.v) ,. (((~.v) i. v) #/. v)
VotesFreqTbl=: VotesFreqTbl \: 2{"1 VotesFreqTbl NB. sort by votes across
groups
VotesFreqTbl=: (~: 1{"1 VotesFreqTbl) # VotesFreqTbl NB. deduplicate movie
entries
MovieProgram=: (0{"1 VotesFreqTbl) ((3&{.))/. VotesFreqTbl NB. take top 3
choices for each
MovieProgram
It's more verbose. I found it easier to understand when tying it back to
the vocabulary page examples.
This statement could use some simplification. It was too heavy on
parentheses for my taste -- even though that's how I started building it. I
would take one piece and then compose it with it the next.
VotesFreqTbl=: (~.v) ,. (((~.v) i. v) #/. v)
(nub v) stitch (((nub v) index v) count per group v)
When I was writing it, I would start with each piece in parens to work out
what it was doing.
It seems like that might be a good application of a cap or a fork. I
haven't gotten to that level of understanding yet. Could someone please
help in the syntax?
In my mind, it would be like this:
(~. ] ,. ~.] i. ] #/. ]) v
Or maybe there is a better way.
Thanks for the help
On Wed, Aug 28, 2013 at 12:01 AM, Ric Sherlock <[email protected]> wrote:
> The code below follows Brian's approach but attempts to simplify some of
> the array manipulation.
>
> NumMovies=: 26
> NumStudents=: 100
> Groups=: NumStudents $ i.3
>
> Votes=: NumStudents ?.@$ NumMovies
>
> VotesTbl=: Groups ,/@(~.@[ ,."0 2 ({.,#)/.~/.) Votes
>
> VotesTbl=: (] \: {:"1) VotesTbl NB. sort by votes across groups
>
> VotesTbl=: (#~ [: ~: 1&{"1) VotesTbl NB. deduplicate movie entries
> MovieProgram=: ({."1 (3&{.)/. ]) VotesTbl NB. take top 3 choices for each
> group
>
>
>
>
> On Wed, Aug 28, 2013 at 7:58 AM, Joe Bogner <[email protected]> wrote:
>
> > Thanks Brian - this looks like it solve it. I will study it to get a
> better
> > understanding of how to solve problems like this. Thanks for adding
> > comments too. It makes the steps more clear.
> >
> >
> > On Tue, Aug 27, 2013 at 8:55 AM, Brian Schott <[email protected]
> > >wrote:
> >
> > > freqcount=: (\: {:"1)@(~. ,. #/.~)
> > > CT =: 100
> > > picks =: CT ?.@$ 26
> > > groups =: CT $ i. 3
> > > f =: groups <@freqcount/. picks NB. freqcounts of picks
> > > g =:; (i. 3) ((|.&.|:)@>@]<@:,.[)"0 f NB. appending group #s
> > > s =: \:~ g NB. sorting
> > > r =: (i.26) ((0,~])\:~@{~1&{"1@]i./ [)s NB. remove duplicates
> > > t =: |.({:"1@]</.])\:~&.(|.&.|:) r NB. rearrange
> > > 3&{. each t NB. first 3
> > >
> > > The above may be a solution, but is not well tested. Instead of movie
> > > names, there are movie numbers.
> > >
> > >
> > >
> > >
> > >
> > >
> > > On Mon, Aug 26, 2013 at 11:22 PM, Joe Bogner <[email protected]>
> > wrote:
> > >
> > > > I solved this problem today at work in R using a loop and an
> > intermediate
> > > > data.table. I generalized it and then tried to solve it in J. I
> wasn't
> > > even
> > > > sure where to start.
> > > >
> > > > Let's say you have 100 students and 26 movies that need to be shared
> > by 3
> > > > groups of students. Each student has picked their favorite movie. I
> > need
> > > to
> > > > give 3 movies to each of the 3 groups based on the group's favorite
> > > > choices. A movie can't be shared by groups.
> > > >
> > > >
> > > > movies=:(65 + i. 26) { a.
> > > >
> > > > CT=:100
> > > >
> > > >
> > > > NB. movie;student;age group
> > > >
> > > > students=:i.CT
> > > >
> > > > groups=:(CT $ (i.3))
> > > >
> > > > picks=: ((?. CT $ # movies) { movies);students;groups
> > > >
> > > >
> > > > NB. get an idea of what the most common choices are across all groups
> > > >
> > > > key=. > 0{"1 picks
> > > >
> > > > values=. CT $ 1
> > > >
> > > > sum=: +/
> > > >
> > > > counts=. (~.key) ; (i.~key) sum/. values
> > > >
> > > >
> > > > NB. sidenote how do I get this table format?
> > > >
> > > > NB. |-----|---|
> > > >
> > > > NB. |M | 1 |
> > > >
> > > > NB. |-----|---|
> > > >
> > > > NB. |W| 1 |
> > > >
> > > >
> > > >
> > > > Any advice on how to tackle this in J?
> > > >
> > > >
> > > > This is the solution in R
> > > >
> > > >
> > > > movies <- LETTERS
> > > >
> > > > CT <- 100
> > > >
> > > > students <- rep(0:(CT-1))
> > > >
> > > > groups <- sample(0:2,100,replace=T)
> > > >
> > > > picks <- data.table(data.frame(movie=sample(movies,100,
> > > > replace=T),student=students, group=groups))
> > > >
> > > >
> > > >
> > > > movie.groups <- picks[,.N,by=list(movie,group)]
> > > >
> > > > movie.groups[,Taken:=F]
> > > >
> > > >
> > > > ugroups <- unique(groups)
> > > >
> > > > for(i in 1:length(ugroups)) {
> > > >
> > > > g<-ugroups[i]
> > > >
> > > > top3 <- movie.groups[group==g & Taken==F,][order(-N)][1:3]
> > > >
> > > >
> > > > setkey(top3,movie,group)
> > > >
> > > > setkey(movie.groups,movie,group)
> > > >
> > > > movie.groups[top3, ForGroup:=T,nomatch=0]
> > > >
> > > >
> > > > setkey(top3, movie)
> > > >
> > > > setkey(movie.groups,movie)
> > > >
> > > > movie.groups[top3, Taken:=T,nomatch=0]
> > > >
> > > > }
> > > >
> > > >
> > > >
> > > > >movie.groups[ForGroup==T,list(movie,group)][order(group)]
> > > >
> > > > movie group
> > > >
> > > > 1: P 0
> > > >
> > > > 2: T 0
> > > >
> > > > 3: W 0
> > > >
> > > > 4: F 1
> > > >
> > > > 5: H 1
> > > >
> > > > 6: N 1
> > > >
> > > > 7: D 2
> > > >
> > > > 8: E 2
> > > >
> > > > 9: K 2
> > > >
> ----------------------------------------------------------------------
> > > > For information about J forums see
> http://www.jsoftware.com/forums.htm
> > > >
> > >
> > >
> > >
> > > --
> > > (B=) <-----my sig
> > > Brian Schott
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> > >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm