| Bytes | Lang | Time | Link |
|---|---|---|---|
| 034 | Raku Perl 6 rakudo | 240719T190541Z | bb94 |
| 040 | Haskell + hgl | 240624T215634Z | corvus_1 |
| 045 | Haskell | 240624T230341Z | corvus_1 |
| 028 | MATLAB | 240625T101259Z | Wolfie |
| 053 | R | 240624T072452Z | Ric |
| 025 | 05AB1E | 240624T102025Z | Kevin Cr |
| 053 | JavaScript ES6 | 240622T170914Z | Arnauld |
| 006 | MATL | 240623T172553Z | Luis Men |
| 041 | Charcoal | 240623T123004Z | Neil |
| 063 | Octave | 240623T120556Z | Tom Carp |
| 014 | Uiua SBCS | 240623T021452Z | chunes |
| 081 | Retina 0.8.2 | 240623T002856Z | Neil |
| 133 | Python 3.8 prerelease | 240622T184410Z | squarero |
| 011 | Jelly | 240622T172800Z | Jonathan |
Raku (Perl 6) (rakudo), 50 34 bytes
Takes a list of Rational numbers.
{(@$_ X==@$_).rotor($_)>>.sum.max}
Haskell + hgl, 56 40 bytes
-16 bytes thanks to Wheat Wizard
xMl<bg<fl(jn eq)<m(gk$(/)<ν<*ʃ"/"*^ν)
Semi-ungolfed:
parseFraction = gk $ (/) < ν <* ʃ"/" *^ ν
f = lengthOfLargest < toBag < filter (join (==)) < map parseFraction
Haskell, 53 45 bytes
xM cnT<(wx<l*^jn eqo<m(gk$(/)<ν<*ʃ"/"*^ν))
Using the approach of Luis' MATL answer. Semi-ungolfed Pseudocode:
parseFraction = gk $ (/) < ν <* ʃ"/" <*> read
-- elementwise equality
eqo :: Eq a => [a] -> [a] -> [Bool]
eqo = liftA2 (==)
f = maxBy count < splitAt length < (join eqo < m parseFraction)
MATLAB, 28 bytes
x=num2str(x);max(sum(x==x'))
How it works
- Given an input char e.g.
x='1/3,4/12,2/14,3/21,4/28' - Convert it to a numeric array
- Use implicit expansion to make a matrix comparing all elements to each other
sumto get the count of how many elements each is equal to (including itself)maxto get the desired output
The question doesn't (currently) stipulate the input must be a string, if the input were an array of values [1/3,4/12,2/14,...] then the num2str isn't needed and this drops to 15 bytes.
R, 53 bytes (as a script)
max(0,table(sapply(parse(,,commandArgs()[-1]),eval)))
Explanation
max( # max between 0 and contingency table (can be -inf)
0, # by default max return -Inf if all Nan so a 0 corrects this
table( # contingency table of number of occurences
sapply( # map te expression list to eval
parse( # parse input as an expression() list
,,commandArgs()[-1]), # input args
eval))) # eval evaluates the fraction to double
Extra:
If you accept a function then the code is 41 bytes long. In this case the input is a semicolon-separated string.
\(x)max(0,table(sapply(parse(,,x),eval)))
05AB1E, 25 bytes
…0/0Kε'/¡`©_i.±«ë®/]{γéθg
Input as a list of strings.
Try it online or verify all test cases.
Explanation:
Unfortunately, dividing by 0 in 05AB1E or 05AB1E (legacy) acts as a no-op, so will result in the nominator itself when done manually or the string itself when using evals (in Elixir, Python or Batch), instead of the for this challenge preferred -INF/NaN/INF. So instead, things have to be done manually..
…0/0K # Remove any "0/0" items from the (implicit) input-list
ε # Map over the remaining fractions:
'/¡ '# Split it on "/"
` # Pop and push the values of the pair to the stack
© # Store the top value (the denominator) in variable `®` (without popping)
_i # Pop and if this is 0:
.± # Get the sign of the nominator (-1 if <0; 0 if 0; 1 if >0)
« # Append that to each value in the (implicit) input-list
ë # Else (we're not dividing by 0):
®/ # Simply divide the nominator by the denominator `®`
] # Close both the if-else statement and map
{ # Sort the mapped values
γ # Split it into equal adjacent groups
é # Sort these groups by length
θ # Pop and keep the last/longest group
g # Pop and push its length
Some minor equal-bytes alternatives:
ε...©...ë®/could beε...ëy.E: evaluate [.E] the current string of the map [y] as Elixir code.éθgcould be€gà: get the length of each group [€g] and leave the maximum [à] of those.
PS: {γéθg cannot be D.M¢ (Duplicate [D] the list; pop and push the most frequent item [.M]; get the count of that in the list ¢) to save a byte, because it won't work correctly with inner lists (caused by the «). And although the « could alternatively be >ç for +1 byte, then D.M¢ would still incorrectly result in [] if the input only contains "0/0" items.
JavaScript (ES6), 53 bytes
Expects an array of strings such as ["1/3","4/12","2/14",...].
a=>Math.max(...a.map(o=x=>o[x=eval(x)]=x==x&&-~o[x]))
Commented
a => // a[] = input array
Math.max( // return the maximum value:
...a.map(o = // o = object to keep track of counters
x => // for each value x in a[]:
o[ // update o[x]
x = eval(x) // where x is first eval()'uated
] = // to:
x == x && // false if x is NaN
-~o[x] // or o[x] + 1 otherwise
) // end of map()
) // end of Math.max()
Without eval(), 54 bytes
Takes an array of [numerator, denominator] pairs.
a=>Math.max(...a.map(o=([x,y])=>o[x/=y]=x==x&&-~o[x]))
MATL, 6 bytes
U&=sX>
Try it online! Or verify all test cases.
How it works
U % Implicit input. Convert to numerical vector
&= % Matrix of pair-wise equality comparisons
s % Sum of each column
X> % Maximum. Implicit display
Charcoal, 41 bytes
≔EE⪪S,⮌I⪪ι/×⊟ι⎇Σι∕¹Σι≕math.infθI⌈EθΣEθ⁼ιλ
Try it online! Link is to verbose version of code. Explanation:
≔EE⪪S,⮌I⪪ι/×⊟ι⎇Σι∕¹Σι≕math.infθ
Convert the fractions into floating-point values but using -inf, nan or inf as appropriate.
I⌈EθΣEθ⁼ιλ
Take the Cartesian product of the list with itself, compare each pair of elements, take sums, and output the maximum.
Octave, 63 bytes
Anonymous function which takes a row vector of fractions as its input.
@(x)max([sum([y=isinf(x)&x>0;y&x<0]'),~isnan([c,f]=mode(x))*f])
That started off nice and short, and then ballooned!
It is based around the mode function to calculate the frequency of elements.
Annoyingly Octave's mode function seems to have a bug in it whereby it doesn't treat inf as being equal to inf. So [~,f]=mode([inf inf inf]) returns 1. This differes from MATLAB proper which returns 3 for the same thing. As a result we have to do a lot of extra work to count the number of positive and negative infinities.
@(x)
max([ % The largest count overall.
sum([ % Row-wise sum the infinity checks to get counts of negative and positive infinities.
y=isinf(x)&x>0; % Check for positive infinities (logical array)
y&x<0 % Check for negative infinities (logical array)
]'),
~isnan( % Check if the most common element is NaN (only true if all NaN)
[c,f]=mode(x) % Find the most common element and how many of it there are.
)*f % 0 if all NaN, or element count otherwise
])
Without this mode bug, it would be 27 characters:
@(x)~isnan([c,f]=mode(x))*f
Uiua SBCS, 14 bytes
/↥⊂0⊕⧻⊛.▽≠NaN.
/↥⊂0⊕⧻⊛.▽≠NaN.
▽≠NaN. # remove NaNs (0/0s only)
⊕⧻⊛. # group by length of each value
⊂0 # prepend zero so max of empty list is zero instead of ¯∞
/↥ # maximum
Retina 0.8.2, 81 bytes
\d+
$*
G`1
+r`(1*)(\3)*\b/(1+)\b
$#2,$3/$1
1+/
;
O`
m`^(.+)(¶\1)*$
1$#2$*
O^`
\G1
Try it online! Takes input on separate lines but link is to test suite that splits on commas for convenience. Explanation:
\d+
$*
Convert to unary.
G`1
Remove 0/0 entries.
+r`(1*)(\3)*\b/(1+)\b
$#2,$3/$1
Convert the remaining entries to continued fractions.
1+/
;
Remove the trailing n/0 remnant.
O`
Sort the continued fractions.
m`^(.+)(¶\1)*$
1$#2$*
Count how many there are of each, in unary.
O^`
Sort descending.
\G1
Convert the largest to decimal, but if there were only 0/0 fractions, then there is nothing left at this point, so we get a final result of 0.
Python 3.8 (pre-release), 134 133 bytes
-1 byte: if i[-2:]>'/0'else → if'/0'<i[-2:]else.
lambda*x:(s:=[eval(i)if'/0'<i[-2:]else'n+-'[round((n:=eval(i[:-2]))/(abs(n)+.1))]for i in x])and max([(j!='n')*s.count(j)for j in s])
Takes input like f('-1/5', '-2/10', '0/0', '0/0', '0/0').
Try it online!
Explanation
lambda*x: # Defines lambda
(s:=[ ]) # Walrus operator
eval(i) # Evaluates fraction...
if'/0'<i[-2:] # ...if the denominator isn't 0
else'n+-'[ ] # Otherwise, choose one of 3 characters
round((n:=eval(i[:-2]))/(abs(n)+.1)) # chosen by the sign of the numerator
for i in x # Do this for each of the 5 arguments
and # Ignores first argument since it's truthy
s.count(j) # Count number of times each item appears
(j!='n')* # ...if it isn't 0/0 --> 'n'
[ for j in s] # Do this for each item in s
max( ) # Finds the largest count
Jelly, 11 bytes
⁾/÷yⱮV⁼þ`SṀ
A monadic Link that accepts the list of strings and yields the count.
How?
⁾/÷yⱮV⁼þ`SṀ - Link: list of lists of characters A
⁾/÷ - list of characters -> "/÷"
Ɱ - map across A with:
y - translate (replace '/' with '÷')
V - evaluate as Jelly code
` - use that as both arguments of:
þ - table of:
⁼ - equal?
S - sum (columns)
Ṁ - maximum