20
\$\begingroup\$

Your task is to write a program or function that takes as input a list of positive integers and determines if there are any two integers in the list, that both appear the same number of times.

For example the list

[1, 2, 3, 2]

Has one 1 and one 3 so it satisfies the condition.

But the list

[3, 2, 2, 1, 2, 3]

does not satisfy the condition since every member appears a unique number of times.

Additionally your program should not satisfy this condition when its source code is interpreted as a list of bytes.

For example

abcb

Would not be a valid submission since both a and c appear exactly once.

Here is a verifier you can use if your source is in ASCII.

Output

Your submission when executed should output one of two possible values, one when the input satisfies the condition, one when the input does not.

This is so answers will be scored in bytes with fewer bytes being better.

\$\endgroup\$
8
  • \$\begingroup\$ I feel like the scoring is going to be basically "minimize the number of distinct characters", since you can probably pad in useless copies to make them appear 1..n times for a triangular score. \$\endgroup\$
    – xnor
    Mar 13, 2020 at 19:34
  • \$\begingroup\$ @xnor That seems like a good strategy. \$\endgroup\$
    – Wheat Wizard
    Mar 13, 2020 at 19:35
  • \$\begingroup\$ How about providing a scoring program for the programs? In some languages, the program will not be able to evaluate itself as the inputs are specified as integers, not bytes or strings. \$\endgroup\$
    – Xcali
    Mar 13, 2020 at 21:49
  • \$\begingroup\$ @Xcali Scoring is just regular code-golf. \$\endgroup\$
    – Wheat Wizard
    Mar 13, 2020 at 21:57
  • 1
    \$\begingroup\$ @S.S.Anne I don't think I can provide a universal solution validator, but I can make one for ASCII. \$\endgroup\$
    – Wheat Wizard
    Mar 14, 2020 at 16:58

17 Answers 17

12
\$\begingroup\$

05AB1E, 6 bytes

D¢DD¢Q

Try it online! or validate more test cases.

Outputs 1 if each item appears a unique number of times, 0 otherwise.

D           # duplicate the input
 ¢          # count occurences of each item in the input
  DD        # triplicate the list of occurence counts
    ¢       # count occurences of each occurence count in the list of occurence counts
     Q      # check whether this is equal to the list of occurence counts

Without the source restriction, this could be 4 bytes: ¢Ð¢Q.

\$\endgroup\$
2
  • \$\begingroup\$ Nice one! I was about to post an answer (21 bytes though, 6 unique bytes.. so nvm, haha). Here a test suite of your answer. (Enjoy the weekend!) \$\endgroup\$ Mar 13, 2020 at 22:50
  • \$\begingroup\$ @KevinCruijssen Thanks :D \$\endgroup\$
    – Grimmy
    Mar 13, 2020 at 23:02
10
\$\begingroup\$

Jelly, 1+2+3+4 = 10 bytes

ĠQQẈQƑẈĠĠĠ

A monadic Link which accepts a list and returns an empty list, [], if the input satisfies the condition, or a list with a list containing a one, [[1]] if the input does not satisfy the condition.

Try it online!

How?

Without the source code restriction we could do the four byte ĠẈQƑ: Ġ, group indices by value; length of each; Ƒ, is invariant under; Q de-duplication.

So for the \$1+2+3+4=10\$:

ĠQQẈQƑẈĠĠĠ - Link: list, L
Ġ          - group indices by value -> list of lists (which when flattened would contain the integers 1 to len(L) inclusive)
 Q         - de-duplicate (a no-op)
  Q        - de-duplicate (again a no-op)
   Ẉ       - length of each (group) 0> how many times each element appeared
     Ƒ     - is invariant under?:
    Q      -   de-duplication -> 0 if satisfies, 1 if not
      Ẉ    - length of each -> [] if satisfies, [1] if not
       Ġ   - group indices by value -> [] if satisfies, [[1]] if not
        Ġ  - group indices by value (a no-op)
         Ġ - group indices by value (another no-op)
\$\endgroup\$
6
\$\begingroup\$

Python 2, 136 126 120 bytes

A slight improvement of @xnor's solution, posted with permission. Source code has 15 distinct characters.

n=input()
print(not
sum(())==sum(sum(r==m
for
m
in
n)==sum(u==m
for
m
in
n)for
r
in
n
for(u)in(n)if(r==u)==(r==n)))#ouu)

Try it online!

Input: A string representation of a list/iterable, from stdin.
Output: True if the list has 2 elements with the same frequency, False otherwise.

Base solution

n=input()  # read a list/iterable from stdin 
           # This is Python2, so the input string is implicitly evaluated
print(0 != sum(     # return True if sum is not 0 (aka at least one of the following is True)
  sum(r==m for m in n)==sum(t==m for m in n) # check if frequency of r and t are the same
  for r in n for t in n if r!=t              # ignore when r==t                    
))

Try it online!

Old solution using exec

Python 2, 158 bytes

e=s=110;"tt)e";exec"e=%c%x%c%x%c%x%cx:%ce%c(set(x))-%ce%c(set(%c%x%c(x%cc%c%c%ct,x)))"%(e-2,10,e-1,11,100,10,0x20,e-2,e,s-2,s,s-1,10,s--2,0x2e,s--1,120-2-1,s)

Try it online!

Input: a list or iterable.
Output: a positive number if the list has 2 elements with the same frequency. 0 otherwise.

Base solution

e=lambda x:len(set(x))-len(set(map(x.count,x)))

Try it online!
My approach is inspired by this answer, which discussed how to construct any Python programs using only the characters exc="%\n. The idea is to convert the program into exec"<program>", then use string modulo (%x and %c) to build program string from number.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ I don't have time to develop it, but I got a solution with 16 distinct chars without exec: TIO If you're interested, you're welcome to try making it work w/ padding (renaming variables) and post it. \$\endgroup\$
    – xnor
    Mar 14, 2020 at 6:23
  • \$\begingroup\$ Oops, the code should be this: Try it online! \$\endgroup\$
    – xnor
    Mar 14, 2020 at 6:31
  • \$\begingroup\$ It's amazing that you managed with only 16 characters! Unfortunately, the exec approach doesn't work, as I will have to add the character set ecx="%(), and your code is too long for substitution to work. However, simply padding your code with the extra characters is enough to beat my solution! 153 bytes \$\endgroup\$ Mar 14, 2020 at 8:37
  • 1
    \$\begingroup\$ Nice! This works out simpler than I thought. Feel free to post it. I wonder if you can further avoid a distinct #. No-ops can be done as or stuff, where if the thing before the or is true, the stuff won't evaluate, so it can be any string of letters, though it does not be be syntactically valid. Like, (()is(a))) could probably do a or stuff. Another place to stuff letters is as for stuff in l. See the crack to this for ideas. The colon might be trickier -- could it be a singleton? Parens also have parity constraints. \$\endgroup\$
    – xnor
    Mar 14, 2020 at 9:00
  • \$\begingroup\$ The colon can be a singleton. However, I can't seem to figure out a way to have unbalanced parentheses :( \$\endgroup\$ Mar 14, 2020 at 9:25
4
\$\begingroup\$

Zsh, \$\sum_{i=1}^{18} i = 171\$ bytes

((f--))
for u (${(u)@})((n[$#-${#@:#$u}]--))
((f-(${${(@on)n}[f]})))
#              rrrrrr[[[[[[]]]]]]]nnnnnnnn@@@@@@@@@#########{{{{{{{{{{}}}}}}}}}}}-----------))))))))


Try it online!

Tricks used: We use subtraction instead of adding. We reuse characters we need from for, (on), and (u) as parameter names. ((f--)) gets us f=-1, which saves us from using 1.

\$\endgroup\$
2
\$\begingroup\$

Raku, 66 57 bytes

*.Bag{*}.Bag{*}.all≠'l.}}}}****{{{{{{{gggggBBBBBBB'.Bag

Try it online!

10 unique characters, *.Bag{}l' and the 3 byte character . This uses .Bag{*} to get the number of times each element appears, then repeats that to get the number of times those appear and then confirms that not all of them are equal to one.

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), \$\sum\limits_{i=1}^{16}i= 136\$ bytes

Returns \$1\$ if the condition is satisfied or \$0\$ if it's not.

p=>p.map(aa=m=a=>aa|=1^(p[[,a]]||(m[p.map(m=>pp+=a==m,p[[,a]]=pp=1),pp]^=1)))|aa//((((((,,...........11111===>>>>>>>>>>>[[^mmmmmppp|||||

Try it online!

How?

To minimize the number of distinct characters, we only use map() loops and variable names made with m, a and p.

Character usage breakdown:

  + |  / |  ^ |  ) |  ] |  , |  [ |  1 |  | |  ( |  m |  a |  . |  > |  = |  p
----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----
  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16

Commented

In the following source, the variables have been renamed for readability.

a =>                       // a[] = input array, also used to keep track of the
                           //       values that have already been counted
  a.map(r = o = v =>       // r = result flag
                           // o = object used to keep track of the character counts
                           //     that have already been encountered
                           // for each value v in a[]:
    r |=                   //   set r if ...
      1 ^ (                //     ... the test below is false:
        a[[, v]] || (      //       v has already been counted, or this is ...
          o[               //         ... the first time this count is encountered:
            a.map(V =>     //           for each value V in a[]:
              n += v == V, //             increment n if V is equal to v
              a[[, v]] =   //             mark v as counted
              n = 1        //             and start with n = 1
            ),             //           end of inner map()
            n              //         
          ] ^= 1           //         toggle o[n]
        )                  //       end of count update
      )                    //     end of test
  ) | r                    // end of outer map(); return r
\$\endgroup\$
2
  • \$\begingroup\$ Can you somehow replace 1 with other symbols? \$\endgroup\$
    – l4m2
    Mar 8, 2022 at 15:10
  • \$\begingroup\$ Anyway 120 \$\endgroup\$
    – l4m2
    Mar 8, 2022 at 15:22
2
\$\begingroup\$

R, 78 bytes

all(table(table(scan()))==all(c()))#nss==bbbcccceeeeettttttaaaa(((()))))llllll

Try it online!

table(x) gives a table of the counts of each value in x (here x will be the input taken by scan); table(table(x)) counts how many times each count occurs. We therefore want to check whether all(table(table(x))==1). Adding # and all the necessary characters to obey the constraint would lead to a score of 91.

The trick is to avoid the character 1 by using instead all(c()), which is all of an empty vector, i.e. TRUE, which gets converted to 1.

I haven't looked yet into using octal codes for this challenge; they might lead to a better score.

\$\endgroup\$
2
\$\begingroup\$

Haskell (No monomorphism), 245 bytes

import Data.List
g1L=all o111111111LLLLLLLLLLaaaaaaaeeeeeeeegggggggghhhhhhhiiiiillllnnnnoooooppppprrrrsssstuu.map length.group.sort.map length.group.sort
o111111111LLLLLLLLLLaaaaaaaeeeeeeeegggggggghhhhhhhiiiiillllnnnnoooooppppprrrrsssstuu i=i==1

Try it online!


Here is a work in progress towards a better answer, it solves the problem using 9 distinct characters. However currently there are some characters that appear the same number of times (e.g. ( and )). I'm going to take a break from this, if anyone wants to use this to make a more complete answer go ahead.

s+|(ss:sss)|s==ss=1+s+|sss|1==1=s+|sss
s+|ss=1
s|+(ss:sss)|s==ss=s|+sss|1==1=ss:s|+sss
s|+ss=ss
s(ss:sss)=ss+|sss:s(ss|+sss)
s(ss)=ss
ss(1:s)=ss(s)
ss(s:ss)=1==11
ss(s)=1==1
sss(sss)=ss(s(s(sss)))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

MATL (unreleased master), 10 bytes

&u&uqaaaa&

Try it on MATL Online
Multiple test cases

Try it online!, but not really - link is to an equivalent 12 byte (non-conforming) version that works on TIO, since &u isn't available in it yet.

0 = input satisfies the condition, 1 = does not.

Actual code is &u&uqa& - take the counts of unique values in the input, take the counts of unique values in that, decrement that by one, and see if any of it is non-zero (which would mean the count was greater than 1, hence condition not satisfied). Repeating the non-zeroness check doesn't change anything, but takes a's count to 4 and so makes it occur a unique number of times.

A conforming version that can run on TIO is 888#8#u8#uqaaaa Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ You can run the commited compiler with &u in MATL Online: matl.suever.net/… \$\endgroup\$
    – Luis Mendo
    Mar 10, 2022 at 20:02
  • \$\begingroup\$ @LuisMendo Thank you, I've updated the answer. (Didn't realize it's been almost a whole month since I was last here...) \$\endgroup\$
    – Sundar R
    Apr 8, 2022 at 13:35
1
\$\begingroup\$

Wolfram Language (Mathematica), 253 bytes

(*!!!!!!!!!!!!!!!!!!!!!o*=L=L=L=L*Ln*n*n*nUUU))))))))))))))))))UUUUUUTTTTTTTTTTSSSSSSSSSSSrrnnrrrrrrrrrrLLLLLLLLiiiiiiiiiiiiii//ss&&########*#######@#####&&&&&&&&&&&&&&&&s/ss/ss/s/ss/ss/ss/ss//////((((((((((((((((a*aaaa*)Union@(l=Last/@Tally@#)!=Sort@l&

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ ( and = both appear exactly once. \$\endgroup\$ Mar 13, 2020 at 19:26
  • \$\begingroup\$ @JonathanAllan fixed \$\endgroup\$
    – ZaMoC
    Mar 13, 2020 at 21:32
1
\$\begingroup\$

Python 3, 406 \$\sum\limits_{i=1}^{26}i= 351\$ bytes

Saved \$27+28=55\$ bytes thanks to Surculose Sputum!!!

lambda l:all(l.count(a)^l.count(p)for a,p in itertools.combinations(set(l),2))
import itertools##22:::^^^^dddddffffff,,,,,,bbbbbbbuuuuuuuu........ccccccccceeeeeeeeeemmmmmmmmmmmpppppppppppprrrrrrrrrrrrsssssssssssss             (((((((((((((()))))))))))))))nnnnnnnnnnnnnnnnaaaaaaaaaaaaaaaaiiiiiiiiiiiiiiiiilllllllllllllllttttttttttttttttoooooooooooooooo

Try it online!

A veritable triple double super-sized whopper!!!
Returns False if the input (any sequence) satisfies the condition, True otherwise.

\$\endgroup\$
2
  • \$\begingroup\$ 351 bytes by reducing the number of distinct characters by 2 \$\endgroup\$ Mar 14, 2020 at 0:36
  • \$\begingroup\$ @SurculoseSputum Wow that's sweet - thanks! :-) \$\endgroup\$
    – Noodle9
    Mar 14, 2020 at 1:07
1
\$\begingroup\$

Charcoal, 40 23 bytes

⁼№θEθι⁼Eθ№θιEθ№Eθ№θλ№θι

Try it online! Link is to verbose version of code. Outputs - only if the condition is satisfied. Port of @Grimmy's 05AB1E answer. Explanation: The leading ⁼№θEθι exists to consume characters to meet the source layout restrictions and also happens to complement the result. The rest of the code is as follows:

 Eθ№ ι              List of counts of elements
    θ               In list
⁼                   Equals
      Eθ№     №θι   List of counts of counts
         Eθ№θλ      In list of counts
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 66 55 bytes

####>Count@s@@o@uuunnnntttttssssss@s&@@Counts@Counts@#&

Try it online!

Returns True if occurrence counts are not unique, and False otherwise.

                                              Counts@#  occurrence counts
                                       Counts@          count of counts
####                                &@@                 squared product
    >Count@s@@o@uuunnnntttttssssss@s                      >1

Count@s@@o@uuunnnntttttssssss@s simplifies to (Count@s)@uuunnnntttttssssss@s = Count[...[s],s] = 1. The product of positive integers can only be \$\not>1\$ if they are all 1.

\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 253 bytes

n=>new(Set)(n).size>new(Set)(n.map(e=>n.filter(r=>r==e).length)).size//(======ghgmmmmmmmaaaaaaaapppppppppppwwwwwwwwwwwllllllllllllhhhhhhhhhhhhhrrrrrrrrrrrrrggggggggggggggzzzzzzzzzzzzzzzzSSSSSSSSSSSSSSSSSttttttttttttttttnnnnnnnnnnnnnnssssssssssssssssssss

Try it online!

unavoidable new Set size length map filter

\$\endgroup\$
0
\$\begingroup\$

Japt -!, 36 bytes

ü mÊ
e       UUâââmÊmÊmÊmÊÊ





eUâ

Try it

\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 91 bytes

p=>"">p[")||]m",mp="","map"](m=>[p["map"](pa=>a-=m==pa,a=[]),mp|=p[a]>m|m>p[a],p[a]=m])==mp

Try it online!

Optimized from

A=>"">A["",E="map"](B=>[A["map"](C=>D-=B==C,D=[]),E|=A[D]>B|B>A[D],A[D]=B])>=E

Only 5 bytes of direct waste!

\$\endgroup\$
1
  • \$\begingroup\$ p=>p[")|]m,]]]]",mp="","map"](m=>[p["map"](pa=>a-=m==pa,a=[]),mp|=p[a]>m|m>p[a],p[a]=m])|mp same length more waste \$\endgroup\$
    – l4m2
    Mar 8, 2022 at 18:39
0
\$\begingroup\$

Pip, 28 bytes

gNUM_NMQUQNUUgg:_NgMUQgUQgQg

Takes the list as command-line arguments; or, with the -r flag, takes the list of lines of stdin. Outputs 1 if every item's count is unique, 0 if some items have identical counts.

Try it online!

Self-verify it online!

Explanation

gNUM_NMQUQNUUg

No-ops to get the character counts right.

g:_NgMUQgUQgQg
        g       ; List of arguments
      UQ        ; Uniquify (remove duplicates)
     M          ; Map this function to each:
   N            ;   Count of
  _             ;   function argument in
    g           ;   original list
g:              ; Assign the result back to g
         UQg    ; g with duplicates removed
            Q   ; is equal (using string comparison) to
             g  ; g
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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