Your task is to write two programs/functions, in the same language.
Program 1: Verify that two inputs are anagrams of each other.
Program 2: Verify that two inputs differ from each other at every character position.
"Verify" means, return a value you define as true for this case, and a different one otherwise.
You may assume that both inputs are the same length for both programs and use the same character sets as your programs. [Clarifying: by "use the same character sets, I mean, your programs are expected to operate correctly on any text formed by characters that are valid in your programming language. That is, if your programming language is written only in ASCII, inputs to your programs will only be ASCII, etc.]
Each program must verify these results when given your two programs as inputs (with the exception below). That is, you must write two programs that are composed of the exact same characters, but arranged completely differently.
It is not required that both programs take input in the same way, or give output in the same way.
Scoring
Modified code golf: L + 1000 x N
Where:
- L is the length of program 1 in bytes
- N is the number of character positions where both programs have the same character (usually 0). You'll need to count that manually, as Program 2 just returns a boolean.
This allows submissions in languages where it's impossible to meet both requirements, although such submissions will not be competitive.
Sample data
"print","trinp" => true, false (passes test 1, fails test 2)
"return 6","print 9;" => false, true
"sameletters","mesasteeltr" => true, true (this would be a valid submission in a language in which sameletters verifies anagrams and mesasteeltr verifies difference in every character position...)
15 Answers 15
JavaScript (ES6), 38 bytes
-2 thanks to @tata
Both functions expect (string1)(string2) and return 0 or 1. The strings are passed as arrays of characters.
Anagram
m=>r=>(r,e=a=>[1]+a.sort())^e(m)==e(r)
Differs
(e)=>(r)=>r.some((r,a)=>mt=r==e[+a])^1
-
1\$\begingroup\$ Very nice, I forgot you can take strings pre-arrayified. \$\endgroup\$Steve Bennett– Steve Bennett2025年07月23日 07:48:17 +00:00Commented Jul 23 at 7:48
-
1\$\begingroup\$
m=>r=>(r,e=a=>[1]+a.sort())^e(m)==e(r),(e)=>(r)=>r.some((r,a)=>mt=r==e[+a])^1\$\endgroup\$tata– tata2025年07月25日 07:38:58 +00:00Commented Jul 25 at 7:38
Python 3, (削除) 46 (削除ここまで) (削除) 45 (削除ここまで) 44 bytes
lambda n,i,**zipfn:sorted(i) ==sorted(n) *1
lambda*d:1not in(s==e for*tr,s,e in zip(*d))
-1 thanks to emanresu A. -1 thanks to xnor.
-
\$\begingroup\$ 45 with
1not in- although you could probably save another byte or two by swapping truthy/falsy \$\endgroup\$emanresu A– emanresu A2025年07月23日 06:43:19 +00:00Commented Jul 23 at 6:43 -
\$\begingroup\$ not sure if indents are allowed as they give syntax errors in python sometimes \$\endgroup\$Lucenaposition– Lucenaposition2025年07月23日 11:36:40 +00:00Commented Jul 23 at 11:36
-
1\$\begingroup\$ @Lucenaposition: If you define them like this, they work. \$\endgroup\$squareroot12621– squareroot126212025年07月23日 12:57:48 +00:00Commented Jul 23 at 12:57
-
1\$\begingroup\$ I think you can write
*tr,s,eand save a space in both \$\endgroup\$xnor– xnor2025年07月23日 23:48:14 +00:00Commented Jul 23 at 23:48 -
\$\begingroup\$ You can remove one more redundant letter in each function:
*t,s,eand**zipf\$\endgroup\$movatica– movatica2025年07月27日 04:11:55 +00:00Commented Jul 27 at 4:11
Jelly, 5 bytes + 0 = 5
Program 1 (verify anagrams of each other):
Œ!n§Ạ
A dyadic Link that produces 0 if the two are anagrams of each other or 1 if not.
Program 2 (verify respective characters all differ)
nẠŒ!§
A dyadic Link that produces [1] if all respective characters differ or [0] if not.
How?
Œ!n§Ạ - Link ("Program 1"): A; B e.g. A = "abc"; B = "cab"
Œ! - all permutations of {A} ["abc", "acb", "bac", "bca", "cab", "cba" ]
n - not equals {B}? (vectorises) [[1,1,1],[1,1,0],[1,0,1],[1,1,1],[0,0,0],[0,1,1]]
§ - sums [3, 2, 2, 3, 0, 2 ]
Ạ - all? 0 ( => "abc" and "cab" are anagrams of each other)
nẠŒ!§ - Link ("Program 2"): A; B e.g. A = "gfedcba"; B = "abcdefg"
n - {A} not equals {B} (vectorises) [1, 1, 1, 0, 1, 1, 1]
Ạ - all? 0
Œ! - {implicit range} all permutations [[]]
§ - sums [0] ( => respective characters do NOT all differ)
Python 3, 41 bytes
lambda a,l:sorted(a)==sorted(l)#.ipmn,f,
lambda o,e:all( map(str.find,o,e))#drst==
APL (Dyalog Extended), 6 bytes
Anagram←≡⍥∧⍝⍱=
Differs←⍱=⍝≡⍥∧
Anagram: ≡ match ⍥ when ∧ sorted (the rest is a comment)
Differs: ⍱ none = equal (the rest is a comment)
Vyxal, 6 + (0 * 1000) = 6
Program 1:
Þ⊍¬#=g
Program 2:
=g#Þ⊍¬
Takes both inputs as lists of characters. Program 2 returns 0 if all different, 1 otherwise.
Program 1 - Explained
Þ⊍¬#=g
Þ⊍ # Multiset symmetric difference
¬ # is empty?
#=g # comment to satisfy source requirements
Program 2 - Explained
=g#Þ⊍¬
= # Check where letters are the same
g # Get the minimum of that (0 = all different, 1 = match)
#Þ⊍¬ # Comment to satisfy source requirements
05AB1E, score: 8 (or 7†) bytes + (0 * 1000) = 8
- Program 1:
€{ Ëqø_P- try it online; - Program 2:
ø€Ë_Pq{- try it online.
Inputs as a pair of character-lists.
Outputs 1 for truthy and 0 for falsey. †: Could be -1 byte by outputting 0/1 for program 1 and 1/0 for program 2, by replacing the _P with à, although I personally think both programs should have the same distinct truthy/falsy results.
Been trying for a while, but haven't found anything shorter, and even finding equal-bytes alternatives without q prove difficult.. :/
Explanation:
The base programs would be:
- Program 1:
€{Ë- (3 bytes) try it online; - Program 2:
ø€Ë_P- (5 bytes) try it online.
€{ # Sort each inner list of the (implicit) input-pair
Ë # Check that both values in the pair are now the same
# (after which the result is output implicitly)
ø # Zip/transpose the (implicit) input-pair of lists; swapping rows/columns
€Ë # Check for each inner pair if the two characters are the same
_P # Check that all are falsey
# (after which the result is output implicitly)
Because the Ë is on the same position in both programs, the no-op space was added.
In addition, the q is to stop the program, making everything after it no-ops.
Bespoke, 318 bytes
Anagram
With A.I:I presume anybody might do SO much,So easy
A.I composes artwork SO simply;I am stunned
And,If ever I~am getting WRITERS-block,it is sped up with A.I
whatever QUESTION(in topics I am studying)BECOMES SETTLED
Are you thrilled?Y~e e a h h h h
however,writing anagrams confuses it
what is with A.I?problem IS:no I
Expects the two inputs separated by ASCII 0 (NUL). Prints nothing if the two inputs are anagrams; prints Stack underflow. to STDERR if they're not.
While it reads the first input, it increments counters for each character on the heap (not including 0, the separator); while it reads the second input, it decrements those same counters. Then it loops through every Unicode codepoint (except 0, the separator) and checks whether the counts are 0; if a nonzero count is found, a Stack underflow. error is intentionally triggered.
Most of the score is taken up by the necessary commands in this program. Also, instead of looping from 1,114,112 down to 0, it's 1 fewer byte to loop from 11,111,111 down to 0...so apologies if this takes a super long time to run.
Differs
anagram message: could It be Done Somehow, say, By Computer?
Amateur Opinion: IT Isnt QUITE SO decisive
while Striking, I profess~An A.I Is NO playwright
what Is In token-prediction?
math~with grids which map a WORd to a Symbol (which buried LETTERS AWAY).
use A.Is; Study them thoroughly,
Even If It SEEMS Impressive.
Expects the two inputs separated by ASCII 0 (NUL). Prints nothing if the two inputs differ at each character; prints Invalid stack argument: 0 to STDERR if they don't.
First, it reads the first input onto the stack (including 0, the separator), and reverses the stack. Then, each character in the second input is compared to the top stack value (the corresponding character of the first input); if they are equal, an Invalid stack argument: 0 error is intentionally triggered.
Without the source restriction, this program would only be 136 bytes. The rest of the letters after the necessary ones (from playwright onward) form one long comment.
Yes, the two programs are indeed anagrams - and the anagram took quite a while to compose. (But to make it fit the "differs" rule, I cheated by changing the cases of letters and adding extraneous symbols.)
-
1\$\begingroup\$ I don't know anything about this language, but it's a lot of fun to read the programs :) \$\endgroup\$Steve Bennett– Steve Bennett2025年07月26日 05:15:04 +00:00Commented Jul 26 at 5:15
-
\$\begingroup\$ @SteveBennett If you've ever programmed in Whitespace, it's very similar to that (except you can see the commands). Word lengths are all that matters, and it's a lot of fun to write the text to fit the word lengths! \$\endgroup\$Josiah Winslow– Josiah Winslow2025年07月26日 06:55:42 +00:00Commented Jul 26 at 6:55
-
1\$\begingroup\$ I understand that
≡´∨¨sorts each and compares, but how does the second function test that the inputs differ in every character position? \$\endgroup\$Adám– Adám2025年07月23日 08:08:00 +00:00Commented Jul 23 at 8:08 -
\$\begingroup\$ @Adám well, it is incorrect. Thanks for pointing that out. Will try to fix it. \$\endgroup\$panadestein– panadestein2025年07月23日 08:12:10 +00:00Commented Jul 23 at 8:12
-
\$\begingroup\$ @Adám I think it is correct now. If there is at least one match, the second fold yields true, then negate it to answer the problem. \$\endgroup\$panadestein– panadestein2025年07月23日 08:39:43 +00:00Commented Jul 23 at 8:39
-
\$\begingroup\$ Yes, but how can you claim
¬∨´≡¨´is equivalent to{¬∨´≡¨´x}? Wouldn't it be a meaningless fork equivalent to{(¬x)∨´(≡¨´x)}? \$\endgroup\$Adám– Adám2025年07月23日 08:41:42 +00:00Commented Jul 23 at 8:41 -
\$\begingroup\$ Well, I am not considering that a train. This is a valid program,
⋄¬∨´≡¨´ arrayright? I made the block functions so I can test then in a single shot, I could run all tests independently and it would be correct. If that's not a valid assumption, then I am wrong again. \$\endgroup\$panadestein– panadestein2025年07月23日 08:43:56 +00:00Commented Jul 23 at 8:43
JavaScript (V8), 45 bytes
(a,/*!>t*/b,m=e=>e.sort()+[],r=m(b))=>m(a)==r
a=>b=>!a.some((r,t)=>r==b[t]/*()(),,=mmer+*/)
Try it online! (improved with +[] and expecting arrayified strings, from Arnauld's solution)
JavaScript (V8), 56 bytes
(a,/*!>t[]*/b,m=e=>[...e].sort().join``,r=m(b))=>m(a)==r
a=>b=>![...a].some((r,t)=>r==b[t]/*()()``,,=.jmmnieor*/)
It's a bit unclear to me whether single-line comments (// ...)should be accepted for arrow-functions because they don't seem to be generally parsed as part of the function. I've erred on the side of caution and not used them.
-
\$\begingroup\$ 44 with single-line comments, although you can use the
+[]trick and still get a 49 or something without them. personally I'd say that any code that evaluates to a function is the function's source \$\endgroup\$emanresu A– emanresu A2025年07月23日 07:35:01 +00:00Commented Jul 23 at 7:35 -
\$\begingroup\$ I like @Arnauld's approach of using strings instead of comments. I did try this, but couldn't figure out the right combination of operators (eg
^!+) to make it shorter. \$\endgroup\$Steve Bennett– Steve Bennett2025年07月23日 07:51:49 +00:00Commented Jul 23 at 7:51
PowerShell (Windows and Core), (削除) 41 (削除ここまで) 37 bytes
param ($a,$b)!(diff $a $b)#-s 0-i -e
param($a,$b)!(diff $a $b -s 0 -i -e)#
Input expected as two character arrays.
diff is an alias for Compare-Object.
Anagram uses diff's default mode, which will only show differences. The negation of the result turns the result to a boolean ($true if $null, $false otherwise). The hash starts a comment to the end of the line.
Differ uses -SyncWindow 0 (which forces diff to compare at the exact positions, not over the complete arrays), -IncludeSame to show same characters at the same position, and -ExcludeDifferent to suppress differences.
Edit: Updated the $null test for -4 bytes (d'oh)
Charcoal, 13 bytes
Anagram:
⬤θ=NoθιNoηι¬⊙§κ
Try it online! Outputs a Charcoal boolean, i.e. - for anagram, nothing if not. Explanation:
θ First input
⬤ All characters satisfy
Noθι Count in first input
= Equals
Noηι Count in second input
Implicitly print
¬⊙§κ Unparseable (ignored)
Different:
¬⊙θ=ι§ηκ⬤NoθNoι
Try it online! Outputs a Charcoal boolean, i.e. - for all different, nothing if not. Explanation:
θ First input
⊙ Any character matches
ι Current character
= Equals
§ηκ Matching character from second input
¬ Logical Not
Implicitly print
⬤NoθNoι Unparseable (ignored)
Uiua, 7 bytes
×ばつ≠#≍∩
Try it!
≍ match after ∩ both ⍆ sorted and ⍆ sort ×ばつ all ≠ not equal, respectively. The ⍆ sort in the second function is needed to offset the # comments starting positions.
Google Sheets, 104 + (0 * 1000) = 104
Anagram
+N("<>")+LET(s,SEQUENCE(LEN(A2)+0),0+SORT(AND(SORT(CODE(MID(A2,s+0,1)))=SORT(CODE(MID(B2,s+0,1))))))+(0)
Diff
=AND(SORT(LET(s,SEQUENCE(LEN(A2)),CODE(MID(A2,s+0,1))<>0+CODE(MID(B2,s+0,1)))))+0+N("+)SORT)()(SORT(")+0
Both expect the first string in A2 and the second string in B2.
Explore related questions
See similar questions with these tags.
"abc", "abc", I assume they are not anagrams? \$\endgroup\$