Jaccard index: Difference between revisions

→‎{{header|Factor}}: whoops, remove redundant pairings
(→‎{{header|Wren}}: Added self pairings - every time I look at this the task description has changed!)
(→‎{{header|Factor}}: whoops, remove redundant pairings)
Line 19:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: assocs formatting grouping kernel math prettyprint sequences sets ;math.combinatorics
prettyprint sequences sequences.repeating sets ;
 
: jaccard ( seq1 seq2 -- x )
Line 26 ⟶ 27:
 
{ { } { 1 2 3 4 5 } { 1 3 5 7 9 } { 2 4 6 8 10 } { 2 3 5 7 } { 8 } }
[ 2 <combinations> ] [ 2 repeat 2 group append ] bi
dup [ 2dup jaccard "%[%d, %] %[%d, %] -> %u\n" printf ] cartesianassoc-each</lang>
{{out}}
<pre>
{ } { } -> 1
{ } { 1, 2, 3, 4, 5 } -> 0
{ } { 1, 3, 5, 7, 9 } -> 0
Line 35 ⟶ 36:
{ } { 2, 3, 5, 7 } -> 0
{ } { 8 } -> 0
{ 1, 2, 3, 4, 5 } { } -> 0
{ 1, 2, 3, 4, 5 } { 1, 2, 3, 4, 5 } -> 1
{ 1, 2, 3, 4, 5 } { 1, 3, 5, 7, 9 } -> 3/7
{ 1, 2, 3, 4, 5 } { 2, 4, 6, 8, 10 } -> 1/4
{ 1, 2, 3, 4, 5 } { 2, 3, 5, 7 } -> 1/2
{ 1, 2, 3, 4, 5 } { 8 } -> 0
{ 1, 3, 5, 7, 9 } { } -> 0
{ 1, 3, 5, 7, 9 } { 1, 2, 3, 4, 5 } -> 3/7
{ 1, 3, 5, 7, 9 } { 1, 3, 5, 7, 9 } -> 1
{ 1, 3, 5, 7, 9 } { 2, 4, 6, 8, 10 } -> 0
{ 1, 3, 5, 7, 9 } { 2, 3, 5, 7 } -> 1/2
{ 1, 3, 5, 7, 9 } { 8 } -> 0
{ 2, 4, 6, 8, 10 } { } -> 0
{ 2, 4, 6, 8, 10 } { 1, 2, 3, 4, 5 } -> 1/4
{ 2, 4, 6, 8, 10 } {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