87
\$\begingroup\$

We say a string is non-discriminating if each of the string's characters appears the same number of times and at least twice.

Examples

  • "aa!1 1 !a !1" is non-discriminating because each of the characters , !, a and 1 appear three times.
  • "abbaabb" is not non-discriminating because b appears more often than a.
  • "abc" is also not non-discriminating because the characters don't appear at least twice.

Task

Write a non-discriminating program or function which returns a truthy value if a given string is non-discriminating, and a falsy value otherwise.

That is, the program run on its own source code should return a truthy value.

Each submission must be able to handle non-empty strings containing printable ASCII, as well as all characters appearing in the source code of the submission.

Test Cases

Truthy:

<your program's source code> "aaaa" "aa!1 1 !a !1" "aabbccddeeffgg" "1Q!V_fSiA6Bri{|}tkDM]VjNJ=^_4(a&=?5oYa,1wh|R4YKU #9c!#Q T&f`:sm$@Xv-ugW<P)l}WP>F'jl3xmd'9Ie$MN;TrCBC/tZIL*G27byEn.g0kKhbR%>G-.5pHcL0)JZ`s:*[x2Sz68%v^Ho8+[e,{OAqn?3E<OFwX(;@yu]+z7/pdqUD" 

Falsy:

"a" "abbaabb" "abc" "bQf6ScA5d:4_aJ)D]2*^Mv(E}Kb7o@]krevW?eT0FW;I|J:ix %9!3Fwm;*UZGH`8tV>gy1xX<S/OA7NtB'}c u'V$L,YlYp{#[..j&gTk8jp-6RlGUL#_<^0CCZKPQfD2%s)he-BMRu1n?qdi/!5q=wn$ora+X,POzzHNh=(4{m`39I|s[+E@&y>" 
\$\endgroup\$
18
  • 4
    \$\begingroup\$ @Laikoni are we able to abuse comments to get this to work? \$\endgroup\$ Commented Mar 1, 2018 at 14:36
  • 8
    \$\begingroup\$ As a side note, I love challenges where I can use other entries to test my entry's validity. \$\endgroup\$ Commented Mar 1, 2018 at 14:38
  • 3
    \$\begingroup\$ @MagicOctopusUrn I think that he did say in the sandbox that's allowed, since it can't be observably determined. \$\endgroup\$ Commented Mar 1, 2018 at 14:38
  • 12
    \$\begingroup\$ Exactly. Even if you somehow manage to ban comments in an objective fashion, then what about unused string literals ect? Anyway, I think the scoring gives incentive to avoid comments as much as possible. \$\endgroup\$ Commented Mar 1, 2018 at 14:42
  • 5
    \$\begingroup\$ I get it's just a puzzle, but the conflation of "non-discriminating" with "all identifiable labeled member types existing in exactly equal parts" is mildly disturbing... To "discriminate" means "to tell the difference between", and to unfairly do this means to treat or judge someone unfairly based on seeing them as different from another class of people. Of course, keep going with the fun! \$\endgroup\$ Commented Mar 1, 2018 at 22:06

44 Answers 44

1
2
1
\$\begingroup\$

Pyth, 30 bytes

 "&8<MQSlqr{"&q1lJ{hMrSz8<1hJ 

Leading spaces necessary.

Try it online!

The actual program is just &q1lJ{hMrSz8<1hJ. I just prepended the string "&8<MQSlqr{" to make it non-discriminating. But to make the string not print itself, I had to add a space, so I added 2 spaces.

&q1lJ{hMrSz8<1hJ q1l (1 == len( J{ J = deduplicate( hM map(lambda a: a[0], r 8 length_encode( Sz sorted(input()) ) ) ) ) & and <1hJ (1 < J[0]) 

length_encode here (r <any> 8) takes a sequence and outputs the length of each run of the same character, ex. "aaabbcc" becomes [[3, "a"], [2, "b"], [2, "c"]].

So this takes the input, sorts it to put in length encode, and takes the first element of each list in the resulting list (e.g. the earlier example would become [3, 2, 2]). This gives a count of how many times characters occur. Then it's deduplicated (the earlier example would become [3, 2]), and J is set to that.

Then it checks if the length is 1, i.e. there is only 1 unique number of times a character occurs, and if that is > 1, i.e. >= 2.

There might be a built-in to replace rSz8 or hMrSz8 but I can't find one.

\$\endgroup\$
1
\$\begingroup\$

Perl 6, 58 57 bytes

{.max==.min>()**()}o{(bag .comb){*}}###=>>abccggiinnoxx 

Try it online!

Turns out the three character version is slightly shorter than the two character one.

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

Julia 1.7, 88 bytes

x->(n=-[-all([])][];t=count.(x∪x,x);all(c->c==t[n]>n,t))####,,...;;>aacooouuu∪∪∪ 

Attempt This Online!

Ungolfed and unrestricted version:

x -> ( chars=count.(unique(x),x); all(c->c==chars[1]>1,chars) ) 

The logic ensures that each character's count is equal to the count of the first character and greater than 1. Julia 1.8 includes the relevant function allequal.

Golfing in Julia:

  • unique(x) is usually substituted with x∪x to save bytes. Here, it is done to reduce the number of ( and ) used.

  • Generally, Int and Bool are not converted automatically and need to be specified. It was a challenge to conjure up a 1 from the leftover characters, but I came up with -[-all([])][], which relies on -true == -1 and -[-1][] == 1.

\$\endgroup\$
3
  • \$\begingroup\$ -3 bytes: x->(n=-[-all([])][];t=count.(x∪x,x);all(@.t==t[n]>n))####,,...;;>aacooouuu∪∪∪ \$\endgroup\$ Commented Nov 19, 2022 at 18:06
  • \$\begingroup\$ With the source restriction (all characters must appear the same number of times and at least twice), your solution comes to 92 bytes: x->(n=-[-all([])][];t=count.(x∪x,x);all(@.t==t[n]>n))####,,,-..;;>>@@@aacccooouuu∪∪∪ \$\endgroup\$ Commented Nov 19, 2022 at 18:43
  • \$\begingroup\$ Oh nevermind, I forgot to change the source in the source variable too, so I thought it was valid. Thanks \$\endgroup\$ Commented Nov 19, 2022 at 19:51
1
\$\begingroup\$

Go, 444 420 bytes

func(s string)bool{m,c:=make(map[rune]int),make(map[int][]rune) for _,d:=range s{m[d]++} for o,a:=range m{if a<1+1{return 1<000000000000} c[a]=append(c[a],o)} //abbbbbbbbbbbcccccccdddddddddeeffffffff //gggggggggiiiiiiiikkkkkkkkkkllllllllllmmmmmoooooo //pppppppprssssssssstttttttuuuuuuu // 11111111<<<<<<<<<<::::::::: //___________+++++++++,,,,,,,====== //{{{{{{{{}}}}}}}}((((((()))))))[[[[[[]]]]]] return len(c)==1} 

Attempt This Online!

Adds characters as comments and whitespace to make it non-discriminating.

Discriminated version, 165 163 bytes

func(s string)bool{m,c:=make(map[rune]int),make(map[int][]rune) for _,d:=range s{m[d]++} for o,a:=range m{if a<2{return 1<0} c[a]=append(c[a],o)} return len(c)==1} 

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 57 54 40 36 bytes

#~Max~2==Min@#&@*Counts 2*aCiostux& 

Try it online!

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

Perl 6, 132 100 bytes

my &f={{$_[0]>1&&[==] $_}(.comb.categorize({$_}).values)}#myy ff[]00>>11()cobbattggrriizzvvlluuss## 
\$\endgroup\$
0
\$\begingroup\$

Pyth, 66 bytes

 " &&))//0<<==??@@IIJNNVVaallqq{"zJ.{zVJ=aY/zN)I&q1l.{Y<1@Y01.?0 " 

F'jl3xmd'9Ie$MN;TrCBC/tZIL*G27byEn.g0kKhbR%>G-_5pHcL0)JZ`s:*[x2Sz68%v^Ho8+[e,{OAqn?3Egy1xX&debug=0" rel="nofollow noreferrer">Try it online!

Pyth (indented) | Python 3 (translation)
 | def a(A,B): | c=list(A) | c.append(B) | return c | Y=[] | z=input() " &&))//0<<==??@@IIJNNVVaallqq{" | " &&))//0<<==??@@IIJNNVVaallqq{" z | print(z) J.{z | J=set(z) VJ | for N in J: =aY/zN) | Y=a(Y,z.count(N)) I&q1l.{Y<1@Y0 | if 1==len(set(Y)) and 1<Y[0]: 1 | print(1) .? | else: 0 | print(0) " | "" 
\$\endgroup\$
0
\$\begingroup\$

APL, 19 72 bytes

This is my second attempt at golfing and also second program in APL. I cant figure out if I can do an unnamed lambda within a named function. If I can do that I can shave some bytes and make it more readable. Was happy to land on a solution so quick #I♥APL

i ← ⍞ ◊ 2 ≤ ∨ / { ⍴ i ∩ ⍵ } ¨ ∪ i[⍋i]

i←{⍞}◊{}◊{}◊◊2≤∨/∨/∨/∨/{∪⍴i∩∪⍵}¨∪i[∪⍋i]⍝⍝⍝⍝⍞⍞⍞←←←222≤≤≤¨¨¨∩∩∩⍴⍴⍴⍵⍵⍵[[[]]]⍋⍋⍋ 

Try Apl Online Note you will need to change ⍞ to your string as character input is not supported in the online platform

Explination

Basically it gets character input and saves as i then sorts i and makes a list of the number of occurances of each unique element in i then it reduces the list with a logical or to check that there are the same number of each unique element and finally checks if there are at least 2 of each unique element.

Feel free to tear into my solution! I need to learn and I am still very new to APL and golfing.

Edit!!!

I am an idiot (as was kindly pointed out in the comments) and forgot to make my program non-deterministic. FAIL.

quack!

\$\endgroup\$
1
  • \$\begingroup\$ I cant figure out if I can do an unnamed lambda within a named function. You could ask in the APL orchard. \$\endgroup\$ Commented Mar 5, 2018 at 13:47
0
\$\begingroup\$

Jotlin, 116 bytes

{c(it).map({(_,ebb)->ebb}).l{a.none({icmp_2->icmp_2<2||icmp_2!=a[0]||",,,-->>llnnoooaettt<<<!!=[[[000!]]]".l==""})}} 

Full program code:

val f: (String)->Boolean = {c(it).map({(_,ebb)->ebb}).l{a.none({icmp_2->icmp_2<2||icmp_2!=a[0]||",,,-->>llnnoooaettt<<<!!=[[[000!]]]".l==""})}} val trues = listOf( "aaaa", "aa!1 1 !a !1", "aabbccddeeffgg", "1Q!V_fSiA6Bri{|}tkDM]VjNJ=^_4(a&=?5oYa,1wh|R4YKU #9c!#Q T&f`:sm$@Xv-ugW<P)l}WP>F'jl3xmd'9Ie\$MN;TrCBC/tZIL*G27byEn.g0kKhbR%>G-.5pHcL0)JZ`s:*[x2Sz68%v^Ho8+[e,{OAqn?3E<OFwX(;@yu]+z7/pdqUD", "{c(it).map({(_,ebb)->ebb}).l{a.none({icmp_2->icmp_2<2||icmp_2!=a[0]||\",,,-->>llnnoooaettt<<<!!=[[[000!]]]\".l==\"\"})}}" ) val falses = listOf( "a", "abbaabb", "abc", "bQf6ScA5d:4_aJ)D]2*^Mv(E}Kb7o@]krevW?eT0FW;I|J:ix %9!3Fwm;*UZGH`8tV>gy1xX<S/OA7NtB'}c u'V\$L,YlYp{#[..j&gTk8jp-6RlGUL#_<^0CCZKPQfD2%s)he-BMRu1n?qdi/!5q=wn\$ora+X,POzzHNh=(4{m`39I|s[+E@&y>" ) for (t in trues) { val r1 = f(t) if (!r1) throw AssertionError("'$t' gave the wrong value, ($r1 != true)") } for (t in falses) { val r1 = f(t) if (r1) throw AssertionError("'$t' gave the wrong value ($r1 != false)") } 

Uses a check for lowercase equaling an empty string to get rid of extra characters.

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

Pascal (FPC), 288 280 bytes

35 distinct characters * 8 times

var c:char;g:array[0..222]of byte;w,v:byte;begin;repeat read(c);inc(g[ord(c)]);v:=g[ord(c)]until eof;foR w:=0to 222do if(g[w]>=2)and(g[w]<>v)oR(v<2)then v:=0;wRitE(v>0)End.,,,,,,,.....0000::<<<<<<====>>>>>[[[]]]abbbbbccddffffgghhhhhhiiilllllllnnppppppptuuuuuuuvwwwyyyyyEEEEEERRRRR 

Try it online!

-8 bytes by kicking out y from the program! (See below)
Now for the reduced range of characters, but still covering printable ASCII.

This is the basic program:

var c:char;g:array[0..255]of byte;w,v,y:byte;begin;repeat read(c);inc(g[ord(c)]);v:=g[ord(c)]until eof;for w:=0to 255do if(g[w]>=2)and(g[w]<>v)or(v<2)then y:=2;write(y=0)end. 

The task was juggling between number of spaces, ;s and ()s and it seems that less than 8 characters on all of them is impossible.
There are more than 8 of es and rs in the basic program, so I introduced some capital letters or I would need to increase number of occurences of other characters.

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

PowerShell Core, 75 bytes

-!(($ps=$args| group |% co*|gu).count-1)*!($ps-le1)###1alnn*%=.raclette!.=% 

Try it online!

Takes the input as a splatted string

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

JavaScript (V8), 193 bytes

(a,m={},p=[...a])=>p.map(p=>m[p]=(m[p]||0)+1)&&!p.find(dinf=>m[dinf]!=m[a[0]])&&m[a[0]]>1 //,,,,,,{{{{{{{}}}}}}}+++++++findfindfindfindfind||||||111111!!!!!!//////00000()()()()>>>>&&&&aaa...=mp 

Try it online!

Definitely a huge amount of improvement possible, but it's a hard challenge! Simultaneously trying to reduce the number of different characters and how frequently the most common character is used.

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

APL(Dyalog Unicode), 24 bytes SBCS

∧/2(≤,=/)(≢'∧2≤,=≢⌸'⊢⊢)⌸ 

Try it on APLgolf!

Explanation

 ⌸ Key: applies a function for each unique element (as left argument) and their indices (as right argument) ( ) ⊢ Right argument ⊢ '∧2≤,=≢⌸' An unused string with additional characters to make the function non-discriminating '∧2≤,=≢⌸'⊢⊢ Function train X (A f g) Y ←→ A f (X g Y). Since both f and g are ⊢, it just returns the right argument ≢ Tally (length) (≢'∧2≤,=≢⌸'⊢⊢)⌸ How many times each unique element appears ( ) / (N-wise) reduce = =/ For 2-wise reduction: which adjacent elements are equal , Concatenate ≤ ≤,=/ Function train X (f g h) Y ←→ (X f Y) g (X h Y) 2(≤,=/) Which elements are greater or equal than 2 end which adjacent elements are equal (this condition is redundant but short) / Reduce ∧ And ∧/2(≤,=/)(≢'∧2≤,=≢⌸'⊢⊢)⌸ Solution: whether each element appear at least 2 times and the same number of times as others 
\$\endgroup\$
0
\$\begingroup\$

Swift 6, 156 124 112 bytes

39 31 28 unique, 4 of each

["."] let cc$={[]almpS in{Set($0).count<1&+1&&1&<<1<=$0[+0]}((almpS+"").map{[]eiou in(+almpS.count{$0==eiou})})} 

Try it on SwiftFiddle!

The function you want is cc$(_:).

Characters Used

\n"$&()+.01<=S[]aceilmnoptu{} (where \n is a literal newline)

Notes

  • The array literal on the first line is intentionally unused — I was able to use the 2 extra double quotes to store unused characters without needing a comment (which would have cost me 4 bytes worth of forward slashes).
  • The square brackets at the beginnings of some closures are empty capture lists.
  • I used newlines instead of spaces to take advantage of Swift's statement separation rules.
\$\endgroup\$
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.