37
\$\begingroup\$

It's become somewhat of a tradition in PPCG that some users temporarily change their names by an anagram (a new name formed by reordering the letters of the old).

Sometimes it gets difficult to find out who is who. I could use a program or function to tell if two phrases are anagrams of each other.

The challenge

The program or function should take two strings and produce a truthy result if they are anagrams of each other, and falsy otherwise.

Rules

  • Input will only contain letters (ASCII 65 to 90 and 97 to 122), digits (ASCII 48 to 57) or space (ASCII 32).
  • The anagram relation is independendent of case. So "Arm" and "RAM" are anagrams.
  • Spaces don't count either. So "keyboard" and "Barked Yo" are anagrams
  • All builtins allowed
  • Input format is flexible (two strings, an array of two strings, a string containing both phrases with a suitable separator ...)

Code golf. Fewest bytes wins.

Test cases

Truthy:

Lynn, Nyl N
Digital Trauma, Tau Digital Arm
Sp3000, P S 3000
Manage Trash So, Those anagrams

Falsy

Calvins Hobbies, Helka Homba
Android, rains odd
In between days, bayed entwine
Code golf, cod elf got
\$\endgroup\$
7
  • 8
    \$\begingroup\$ Related but different (only letters, no case, no spaces) \$\endgroup\$
    – Luis Mendo
    Commented Feb 25, 2016 at 16:09
  • 5
    \$\begingroup\$ This question's title is very perplexing to someone who's not had enough coffee. +1 :D \$\endgroup\$
    – cat
    Commented Feb 25, 2016 at 17:36
  • 1
    \$\begingroup\$ @DonMuesli I would argue that this is still a dupe. The slight changes are very trivial. \$\endgroup\$
    – user45941
    Commented Feb 25, 2016 at 17:59
  • 16
    \$\begingroup\$ Manage Trash So, Those anagrams. Nice. \$\endgroup\$
    – mbomb007
    Commented Feb 25, 2016 at 20:13
  • 3
    \$\begingroup\$ So, the anagrams... \$\endgroup\$
    – Leaky Nun
    Commented May 21, 2016 at 17:23

36 Answers 36

1
2
1
\$\begingroup\$

Factor, 68 bytes

[ [ 32 swap remove >lower natural-sort ] map duplicates length 1 = ]

An anyonymous function. Call it like { "array" "of" "strings" } ~quotation~ call.

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

Q, 25 Bytes

f:{~/{x@<x:x@&~^x:_x}'x}

NOTE.- counting include function name f: to facilitate tests (as lambda we can decrement 2 Bytes)

Readable version

match over {ascending not null lower x} each x

{.. x ..} is an anonymous function with arg x
_x        lowers string x
&~^x      where not null x (space is considered null)
x@..      selects elements of x according to indexes .. 
<x        ascending indexes of x (not values). Ex <"cab" is 1 2 0
x@<x      ascending values of x (x at ascending indexes of x)
~         match (diad function). Ex "one"~"one" is true
f'..      applies function f for each argument ..
f/..      applies function f over elements of sequence (fold)

Test

f("Lynn";"Nyl N")                       
f("Digital Trauma";"Tau Digital Arm")   
f("Sp3000";"P S 3000")                  
f("Manage Trash So";"Those anagrams")   
f("Calvins Hobbies";"Helka Homba")      
f("Android";"rains odd")                
f("In between days";"bayed entwine")    
f("Code golf";"cod elf got")    

generates (1b = true, 0b = false)

1b
1b
1b
1b
0b
0b
0b
0b

About Q

General-purpose language (APL derivative, specialized in data processing) developed by kx.com. Free full functional evaluation version for Windows/Linux/MacOS.

\$\endgroup\$
4
  • \$\begingroup\$ What do you mean, other languages are not serious? :-P \$\endgroup\$
    – Luis Mendo
    Commented May 21, 2016 at 8:59
  • \$\begingroup\$ If the f is required for the code to evaluate properly, then it must be counted. Otherwise, just leave it off of your submission code, and only use it in examples to show how to assign the function. \$\endgroup\$
    – user45941
    Commented May 21, 2016 at 9:28
  • \$\begingroup\$ Of course, other languages are as serious as Q. I beg my poor english. But some languages sacrifices readability or are equiped with libraries ad-hoc for this type of contests. Q is a 'general purpose language', in spite of the fact that code is not very readable. \$\endgroup\$
    – J. Sendra
    Commented May 21, 2016 at 16:59
  • \$\begingroup\$ You only need to assign x once if you lower later, thus k)~/{x@<x:_x@&~^x}' for 17 bytes.. but I'd say it's 19 as you need the k) bracket as this is K code rather than Q... \$\endgroup\$
    – mkst
    Commented Sep 27, 2017 at 14:15
1
\$\begingroup\$

PHP, 89 bytes

for(;$i++<2;)$r[]=count_chars(join(explode(" ",strtolower($argv[$i]))));echo$r[0]==$r[1];

Try it online!

PHP, 94 bytes

for(;$i++<2;sort($x),$r[]=trim(join($x)))$x=str_split(strtolower($argv[$i]));echo$r[0]==$r[1];

Try it online!

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

Excel VBA, 122 Bytes

Anonymous VBE immediate window Function that takes input from range [A1:B1] and outputs to the VBE immediate window

a=Replace([A1]," ",""):b=Replace([B1]," ",""):For i=1To Len(a):b=Replace(b,Mid(a,i,1),"|",,1,1):Next:?b=String(len(a),"|")
\$\endgroup\$
0
\$\begingroup\$

C#, 378 bytes

I need a handicap!!

https://dotnetfiddle.net/FNDt0E

using System;
using System.Linq;
using System.Text;

public class Program
{

    public static void Main()
    {
        var l = "Hello World";

        var r = "Red Who Loll";

        var y = new Func<string,string>(s => new String(s.ToLower().Replace(" ","").OrderBy(v => v).ToArray()));
        var z = new Func<string,string,Func<string,string>,bool>((w,x,f) => f(w) == f(x));
        var o = z(l, r, y);


        Console.WriteLine("{0} & {1} are anagram: {2}",l, r, o);


                Console.WriteLine("C#, {0} bytes", Encoding.Unicode.GetByteCount(@"var y = new Func<string,string>(s => new String(s.ToLower().Replace("" "","""").OrderBy(v => v).ToArray()));
    var z = new Func<string,string,Func<string,string>,bool>((w,x,f) => f(w) == f(x));"));

    }

}
\$\endgroup\$
3
  • 5
    \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! General rule is to put your language together with your byte count in the headline of your post. You can do that by adding a leading # to the first line. Also for code-golf questions it is required to golf your program. For the start you should remove unecessary whitespaces and use single-character variable names. Also you can always use a function instead of a full program (unless explicitly forbidden) to save some more bytes. \$\endgroup\$
    – Denker
    Commented Feb 28, 2016 at 0:40
  • \$\begingroup\$ @DenkerAffe you ninja'd me :) \$\endgroup\$
    – cat
    Commented Feb 28, 2016 at 0:40
  • 2
    \$\begingroup\$ Why all the whitespace?? \$\endgroup\$ Commented Mar 6, 2016 at 20:32
0
\$\begingroup\$

D, 99 107 103 102 99 131 116 117 bytes

Edit: forgot about Dre case insensitivity imports lambdas

I don't like this language :D

import std.string,std.algorithm;(string[]a)=>sort(split(strip(a[0].toLower),""))==sort(split(strip(a[1].toLower),""))

To use it, assign it:

import std.string,std.algorithm;

void main () {
    auto x = (string[]a) => sort(split(strip(a[0].toLower),"")) == sort(split(strip(a[1].toLower),""))
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Did you forget a ) at the end of your lambda? \$\endgroup\$
    – Adalynn
    Commented Nov 8, 2016 at 20:55
  • 1
    \$\begingroup\$ Can strip(a[0].toLower) be replaced by a[0].toLower.strip, likewise with a[1]? \$\endgroup\$
    – Adalynn
    Commented Nov 8, 2016 at 21:03
  • \$\begingroup\$ @ZacharyT Probably, yes \$\endgroup\$
    – cat
    Commented Nov 8, 2016 at 21:37
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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