35
\$\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\$
  • 8
    \$\begingroup\$ Related but different (only letters, no case, no spaces) \$\endgroup\$ – Luis Mendo Feb 25 '16 at 16:09
  • 4
    \$\begingroup\$ This question's title is very perplexing to someone who's not had enough coffee. +1 :D \$\endgroup\$ – cat Feb 25 '16 at 17:36
  • 1
    \$\begingroup\$ @DonMuesli I would argue that this is still a dupe. The slight changes are very trivial. \$\endgroup\$ – Mego Feb 25 '16 at 17:59
  • 15
    \$\begingroup\$ Manage Trash So, Those anagrams. Nice. \$\endgroup\$ – mbomb007 Feb 25 '16 at 20:13
  • 3
    \$\begingroup\$ So, the anagrams... \$\endgroup\$ – Leaky Nun May 21 '16 at 17:23

32 Answers 32

0
\$\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\$
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\$
  • 1
    \$\begingroup\$ Did you forget a ) at the end of your lambda? \$\endgroup\$ – Zacharý Nov 8 '16 at 20:55
  • 1
    \$\begingroup\$ Can strip(a[0].toLower) be replaced by a[0].toLower.strip, likewise with a[1]? \$\endgroup\$ – Zacharý Nov 8 '16 at 21:03
  • \$\begingroup\$ @ZacharyT Probably, yes \$\endgroup\$ – cat Nov 8 '16 at 21:37

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.