12
\$\begingroup\$

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!

\$\endgroup\$
6
  • \$\begingroup\$ "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) \$\endgroup\$ – Kaddath Mar 2 at 14:35
  • \$\begingroup\$ English is not my native either. I just wanted to be absolutely clear and objective about what I wanted. \$\endgroup\$ – Andrew Mar 2 at 14:59
  • \$\begingroup\$ Adding new rules that invalidate the majority of already-posted answers seems a little unkind, to say the least. \$\endgroup\$ – Dominic van Essen Mar 5 at 17:16
  • \$\begingroup\$ 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). \$\endgroup\$ – Andrew Mar 5 at 17:33
  • \$\begingroup\$ 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)? \$\endgroup\$ – nope Mar 14 at 12:53

10 Answers 10

4
\$\begingroup\$

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.

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

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.

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

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.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ 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 :) \$\endgroup\$ – Zaelin Goodman Mar 2 at 16:26
  • 1
    \$\begingroup\$ Try it online! of course :) \$\endgroup\$ – mazzy Mar 2 at 16:30
  • 2
    \$\begingroup\$ 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! \$\endgroup\$ – Zaelin Goodman Mar 2 at 16:32
  • \$\begingroup\$ @mazzy thanks for that I will implement your idea soon! \$\endgroup\$ – Wasif Mar 2 at 16:33
  • 1
    \$\begingroup\$ @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 :) \$\endgroup\$ – Zaelin Goodman Mar 2 at 16:33
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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)
\$\endgroup\$
2
\$\begingroup\$

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).

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

Zsh, 104 96 56 bytes

a=`<$0` b=$1;<<<${#a//[^$b]}
b=`<$0` a=$1;<<<${#b//[^$a]}

Try it online! Try it* online! Try it online!

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 <<<.)

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

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.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ I suppose there's a good reason, but why don't you just do this? \$\endgroup\$ – Dominic van Essen Mar 3 at 12:58
  • \$\begingroup\$ @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! :) \$\endgroup\$ – Noodle9 Mar 3 at 13:01
  • \$\begingroup\$ @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 \$\endgroup\$ – Noodle9 Mar 3 at 13:15
  • \$\begingroup\$ Sorry, but disqualified by the new rule. They are dependent, which is out by the new rule. \$\endgroup\$ – Andrew Mar 5 at 17:38
  • \$\begingroup\$ @Andrew You shouldn't change the rules like that after people have taken the time to answer your OP. \$\endgroup\$ – Noodle9 Mar 5 at 18:38
0
\$\begingroup\$

Charcoal, 40 bytes

I⊗№⁺´”´””yI⊗№⁺´´yS”S

Try it online!

I⊗№⁺”yI⊗№⁺´´yS”´”´”S

Try it online!

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

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

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!

\$\endgroup\$
6
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – Kaddath Mar 2 at 14:52
  • 3
    \$\begingroup\$ @Kaddath Same would apply to my answer. \$\endgroup\$ – Adám Mar 2 at 15:02
  • \$\begingroup\$ @Adám I don't know APL enough, I thought in yours each function read its own source code and replaced the letter, and not each other's code \$\endgroup\$ – Kaddath Mar 2 at 16:31
  • 1
    \$\begingroup\$ @Kaddath No, they do read each others' code, but your method might be viable should OP ever decide to prohibit reading each other. \$\endgroup\$ – Adám Mar 2 at 17:10
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – user81655 Mar 3 at 9:32

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.