75
\$\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\$
  • 4
    \$\begingroup\$ @Laikoni are we able to abuse comments to get this to work? \$\endgroup\$ – Magic Octopus Urn Mar 1 '18 at 14:36
  • 6
    \$\begingroup\$ As a side note, I love challenges where I can use other entries to test my entry's validity. \$\endgroup\$ – Magic Octopus Urn Mar 1 '18 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\$ – Erik the Outgolfer Mar 1 '18 at 14:38
  • 11
    \$\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\$ – Laikoni Mar 1 '18 at 14:42
  • 4
    \$\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\$ – ErikE Mar 1 '18 at 22:06

37 Answers 37

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 "

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\$
  • \$\begingroup\$ I cant figure out if I can do an unnamed lambda within a named function. You could ask in the APL orchard. \$\endgroup\$ – Laikoni Mar 5 '18 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\$

Whispers v2, 1750* 1305 bytes

> 1
> InputAll
>> ∪2
>> #2
>> #3
>> 4÷5
>> ⌊6⌋
>> L⋅7
>> Each 8 3
>> 9ᴺ
>> 2ᴺ
>> 10=11
>> 6>1
>> 12⋅13
>> Output 14














           ###########################00000000000000000000000000001111111111111111111112222222222222222222222222333333333333333333333333334444444444444444444444444445555555555555555555555555555666666666666666666666666666777777777777777777777777777788888888888888888888888888889999999999999999999999999999============================AAAAAAAAAAAAAAAAAAAAAAAAAAAAEEEEEEEEEEEEEEEEEEEEEEEEEEEEIIIIIIIIIIIIIIIIIIIIIIIIIIIILLLLLLLLLLLLLLLLLLLLLLLLLLLLOOOOOOOOOOOOOOOOOOOOOOOOOOOOaaaaaaaaaaaaaaaaaaaaaaaaaaaacccccccccccccccccccccccccccchhhhhhhhhhhhhhhhhhhhhhhhhhhhlllllllllllllllllllllllllllnnnnnnnnnnnnnnnnnnnnnnnnnnnnpppppppppppppppppppppppppppttttttttttttttttttttttttttuuuuuuuuuuuuuuuuuuuuuuuuuu÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺᴺ∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪∪⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌊⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋⌋

Try it online!

* Golfs made while explaining it. You can see the original here

Interesting task, fairly boring restriction.

Unfortunately, due to the fact that every line must start with either >> or >, this forces the number of each character to be disproportionately large. Luckily, Whispers ignores every line that doesn't match one of its regexes, all of which require the line to begin with >. Therefore we just have a large character dump at the end of the program. In addition to this, Whispers, being designed for mathematical operations, struggles when applied to a question. Overall, this means that the task is interesting to attempt, but the source code requirements are a bit boring.

If we strip all the unnecessary characters from the program, we end up with

> 1
> InputAll
>> ∪2
>> #2
>> #3
>> 4÷5
>> ⌊6⌋
>> L⋅7
>> Each 8 3
>> 9ᴺ
>> 2ᴺ
>> 10=11
>> 6>1
>> 12⋅13
>> Output 14

Try it online!

which is the part of the code we're actually interested in.

How that works

Here we conduct two key tests: the count of each is the same and the counts are greater than 1. However, the code for these two tests are shared between them, so a complete walkthrough of the program is a more effective method of explaining this.

We start with the shared code:

> InputAll
>> ∪2
>> #2
>> #3
>> 4÷5

Here, we take the input and store it on line 2 (> InputAll). We then create a nique version of that string (i.e. the input without duplicate characters). With our next two lines, we take the length of the untouched input (>> #2) and the number of unique characters in the input (>> #3). Finally, we divide the former by the latter.

Why? Let's have a look at the two inputs of aabbcc and abbaabb. With the first string, the division becomes 6÷3 = 2, and the second results in 7÷2 = 3.5. For non-discriminating strings, the result of this division is a) An integer and b) Greater than 1. Unfortunately, this also applies to some non-discriminating strings, such as abbccc, so we have to perform one more test.

>> ⌊6⌋
>> L⋅7
>> Each 8 3
>> 9ᴺ
>> 2ᴺ
>> 10=11

Note: line 6 is the result of the division.

First, we take the floor of the division, because Python strings cannot be repeated a float number of times. Then we reach our Each statement:

>> L⋅7
>> Each 8 3

3 is a reference to line 3, which contains our deduplicated input characters, so we map line 8 (>> L⋅7) over this list. Line 8 multiplies the character being iterated over by the floor of our division (>> ⌊6⌋). This creates an array of the deduplicated characters, repeated n times.

The results from our two previous strings, aabbcc and abbaabb, along with abcabc, would be aabbcc, aaabbb and aabbcc respectively. We can see that the first and last two are identical to their inputs, just with the letters shuffled around, whereas the middle one isn't (it has one less b). Therefore, we simply sort both the input and this new string, before comparing for equality:

>> 9ᴺ
>> 2ᴺ
>> 10=11

Finally, we need to check that the number of characters in the string is 2 or greater. Luckily, the division we performed earlier will always result in a value greater than 1 if a character repeats more than once, meaning we just need to assert that line 6 is greater than 1:

>> 6>1

Now we have two booleans, one for each test. They both need to be true, so we perform a logical AND (boolean multiplication) with >> 12⋅13, before finally outputting the final result.

\$\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\$

Wolfram Language (Mathematica), 57 54 bytes

(Apply[Equal]@#)~And~(1<Last[#])&@*Counts

yEqd1<L*Co&

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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