# Autogrammatic pairs

For today's task, we have two programs, P and Q, both in the same language. Each of them receives a single-character input.

If P receives character K, P says how many times K appeared in Q. (You can use any output format for P or Q; they need not be the same, the only requirement is that your format can represent any nonnegative integer uniquely). Vice versa: If Q gets K, Q says how many times K was in P.

Create P and Q, which must not be identical programs. To further cement this, P and Q must be minimal (remove any amount of characters from either and it doesn't work as required).

Whoever made the combined lengths of P and Q the shortest wins (tiebreak: earlier post). Have fun!

• "P and Q must not carry out what has been outlined before if a positive amount of characters is removed from either" -> is this a convoluted way to say programs have to be minimal? (maybe it's just me that finds this unclear, note that english is not my mother tongue) – Kaddath Mar 2 at 14:35
• English is not my native either. I just wanted to be absolutely clear and objective about what I wanted. – Andrew Mar 2 at 14:59
• Adding new rules that invalidate the majority of already-posted answers seems a little unkind, to say the least. – Dominic van Essen Mar 5 at 17:16
• I'll roll these back. I put one in place hoping for "less trivial" pairs; the other can stay (7 out of 11 answers given have independent versions already so the second would be way less disruptive). – Andrew Mar 5 at 17:33
• How would input work for languages such as Whitespace? Inputting spaces would work, but how would you input a tab or newline (Whitespace is written with only spaces, tabs, and newlines)? – nope Mar 14 at 12:53

# PowerShell, 63 bytes

Inspired by @Wasif's awesome PowerShell answer

-6 bytes thanks to mazzy!

$P={$Q-replace"[^$args]"|% le*}$Q={$P-replace"[^$args]"|% le*}


Try it online!

# PowerShell, Independent Functions, 109 103 93 77 bytes

This version does not require the functions to be in the same file; they are completely independent of one another.

$P={"Q$P"-replace"\|P|[^$args]"|% le*}$Q={"P$Q"-replace"\|Q|[^$args]"|% le*}


Try it online!

For either answer, it costs 2 bytes to make the input character case-sensitive.

• Didn't know that we can omit ,'' in -replace, +1 – Wasif Mar 2 at 16:41
• smart Independent Functions - (oO) – mazzy Mar 2 at 17:23

# APL (Dyalog Unicode), 29 bytes (SBCS)

## Mutually dependent lambdas

P←{≢⍸⍵=⎕CR'Q'}
Q←{≢⍸⍵=⎕CR'P'}


Try it online!

P←… establish a function with the following definition:

{} dfn; the argument (a character) is ⍵:

⎕CR''Character Representation of the function called … (as a character matrix)

⍵= 2D mask indicating where the character is equal to elements of that matrix

⍸ɩndices where true

≢ tally them

## Mutually independent lambdas (35 bytes)

P←{≢⍸⍵=⎕CR'P'⋄QQ}
Q←{≢⍸⍵=⎕CR'Q'⋄PP}


Try it online!

The only difference from the above is that each function reads its own source, and that each has a dummy statement containing supplemental characters that ensure that they have identical counts of all characters. ⋄ is the statement separator, but the second statement will never be reached (it'd cause a vale error anyway) because the function terminates with the value of the first non-assigning statement.

# PowerShell, 128 122 bytes

function P($x){cd Function:;(gc Q)-replace"[^$($x)]"|% le*} function Q($x){(gc $psCommandPath)[0]-replace"[^$($x)]"|% le*}  Try it online! -6 bytes Thanks to mazzy and Zaelin Goodman Two functions work in different ways. P() will read Q()'s source code and count occurrences of the given character. Q() will read the source of the first line in the script (Which is source of function P()), and count occurrences of the given character. I Couldn't be more creative than this solution to follow the rules of the question, otherwise if I made two functions identical, I could save some bytes. • Found a shorter way to get the contents of Q, and replaced the method in Q with the shorter one from P; also, you don't need the $() around $x in the strings; all together this goes down to 111 bytes. I think, because P and Q are different letters, you could use the same method to get content in both, but I left them different because I'm unsure on that. Try it online :) – Zaelin Goodman Mar 2 at 16:26 • Try it online! of course :) – mazzy Mar 2 at 16:30 • If you're allowed to use a scriptblock as a function, which I believe is legal, you can actually trim this down much further, to a wonderfully-accidental 69 bytes; but this also hinges on the difference in letters being enough of a difference to count - which I think the challenge is ambiguous on Try it online! – Zaelin Goodman Mar 2 at 16:32 • @mazzy thanks for that I will implement your idea soon! – Wasif Mar 2 at 16:33 • @mazzy Ah, nice catch! I didn't know you could leave out the empty string on replace; that just might save some golfs I have stuck in limbo :) – Zaelin Goodman Mar 2 at 16:33 # Jelly, 24 bytes P “ṾċḷḤ”Ṿċḷ“”Ḥ  Try it online! Q “ṾċḷḤ”Ṿċḷ”“Ḥ  Try it online! P and Q are very similar, they both contain two of each of the same set of six characters. The only difference is “” vs ”“ (empty list vs a single open quote character). ### How? “ṾċḷḤ”Ṿċḷ..Ḥ - Link: character, C “ṾċḷḤ” - list of characters = ['Ṿ', 'ċ', 'ḷ', 'Ḥ'] Ṿ - un-evaluate Jelly code = ['“', 'Ṿ', 'ċ', 'ḷ', 'Ḥ', '”'] .. - either: - “” - empty list = [] - ”“ - character literal = '“' ḷ - yield the left argument of this dyadic atom (C) ċ - count occurrences (of C in the un-eval list) Ḥ - double  # R, version-dependent, 110 84 bytes Edit: -26 bytes with inspiration from user81655's answer Note: Function-pairs #1 and #2 below depend on the version-dependent output formatting of R functions, and work on my locally-installed R version 3.2.1, but unfortunately not on version 3.5.2 which is installed on TIO. A TIO-compliant (but longer) adjusted version of #1 can be tried here: try it. 1: functions that read each other's source code: 84 = 61+23 bytes p=function(c,x=q)sum(el(gregexpr(c,capture.output(x),f=T))>0) q=function(c,x=p)p(c,x)  2: functions that read their own source code, but not each other's: 142 = 2x 71 bytes p=function(c,pr=2,q=1)sum(el(gregexpr(c,capture.output(p),f=T))>pr-q-q) q=function(c,qr=2,p=1)sum(el(gregexpr(c,capture.output(q),f=T))>qr-p-p)  3: 'proper quine'-like functions that don't read their own source code: 236 = 2x 118 bytes p=function(c,q=1,p=1)2*sum(el(gregexpr(c,sQuote('function(c,p=1,q=1)2*sum(el(gregexpr(c,sQuote(),f=T))>p-q)'),f=T))>q-p) q=function(c,p=1,q=1)2*sum(el(gregexpr(c,sQuote('function(c,q=1,p=1)2*sum(el(gregexpr(c,sQuote(),f=T))>q-p)'),f=T))>p-q)  # R, version-dependent, 150 bytes New function-pair to comply with the newly-added rule that functions cannot be anagrams of each other 4: functions that read their own source code, but not each other's, and are not anagrams of each other: 150 = 2x 75 bytes p=function(z)sum(el(gregexpr(z,chartr("yz","zy",capture.output(p)),f=T))>0) q=function(y)sum(el(gregexpr(y,chartr("zy","yz",capture.output(q)),f=T))>0)  # JavaScript (V8), 51 bytes p=(k,s='q='+q)=>s.split(k).length-1 q=k=>p(k,'p='+p)  Try it online! Works assuming we're allowed to read the other program (grey area but not explicitly disallowed in the rules). # Zsh, 104 96 56 bytes a=<$0 b=$1;<<<${#a//[^$b]}  b=<$0 a=$1;<<<${#b//[^$a]}  Both programs are independent (they don't read each other's source). I found the best way to tackle the challenge was to make two self-counting programs with the same character counts. Using assignment to expand the simple ${#${$(<&0)//[^\$1]}} feels cheap, so do take a look at my older programs.

(*My second attempt hits a fork limit on TIO, so its TIO link uses a modified version that uses the non-forking echo instead of <<<.)

# Python 3, 106 100 96 bytes

Saved 10 bytes thanks to Dominic van Essen!!!

import inspect
f=lambda c:inspect.getsource(g).count(c)
g=lambda c:inspect.getsource(f).count(c)


Try it online!

Two mutually dependent lambdas that simply read each other's source code and return the input character count.

• I suppose there's a good reason, but why don't you just do this? – Dominic van Essen Mar 3 at 12:58
• @DominicvanEssen Not allowed to be the same function, does a different variable name count? I took it more in the spirit than to the letter! :) – Noodle9 Mar 3 at 13:01
• @DominicvanEssen Having re-read the OP, it does say: which must not be identical programs. So it's allowed since they're not identical - thanks! :D – Noodle9 Mar 3 at 13:15
• Sorry, but disqualified by the new rule. They are dependent, which is out by the new rule. – Andrew Mar 5 at 17:38
• @Andrew You shouldn't change the rules like that after people have taken the time to answer your OP. – Noodle9 Mar 5 at 18:38

# Charcoal, 40 bytes

Ｉ⊗№⁺´”´””yＩ⊗№⁺´´yＳ”Ｓ


Try it online!

Ｉ⊗№⁺”yＩ⊗№⁺´´yＳ”´”´”Ｓ


Try it online!

The first program is simply my answer to Counting characters and the second is a trivial rearrangement.

# JavaScript, 85 63 bytes

p=k=>('q='+p).split(k).length-1
q=k=>('p='+q).split(k).length-1


Mutually independent functions (thanks to user81655). Each function reads its own source code since the character counts are the same after the concatenation.

Try it online!

• I feel this answer is borderline, because the 2 functions have to be in the same program fo it to work. I reckon this is not explicitly stated in the question that each program cannot read the other's source code, but it explicitly asked for 2 separate programs – Kaddath Mar 2 at 14:52
• This solution can be trivially modified to not read each other's code with p=k=>('q='+p).split(k).length-1 and q=k=>('p='+q).split(k).length-1. The character counts happen to be correct if you make each function read itself. – user81655 Mar 3 at 9:32