-
-
Save cdrnet/8005386 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System; | |
using System.Linq; | |
public class Anagram | |
{ | |
// I am aware about, but have no intention on using | |
// `string.Equals(a, b, StringComparison.OrdinalIgnoreCase)` | |
// as that is longer | |
public static string[] Detect(string subject, string[] Candidates) | |
{ | |
return Candidates | |
.Where(x => subject.ToLower() != x.ToLower() | |
&& Normalize(subject) == Normalize(x)) | |
.ToArray(); | |
} | |
static string Normalize(string input) { | |
var chars = input.ToLower().ToCharArray(); | |
Array.Sort(chars); | |
return new string(chars); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Anagram do | |
def match(subject, candidates) do | |
candidates | |
|> Enum.filter(fn x -> normalize_word(subject) == normalize_word(x) end) | |
|> Enum.filter(fn x -> String.downcase(subject) != String.downcase(x)end) | |
end | |
defp normalize_word(word) do | |
word |> String.downcase |> String.codepoints |> Enum.sort | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
let anagram s cs = | |
let isAnagram x = | |
let upCase = Array.map Char.ToUpper | |
let normalize = upCase >> Array.sort | |
normalize s = normalize x && upCase s <> upCase x | |
Seq.filter isAnagram cs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package anagram | |
import ( | |
"sort" | |
"strings" | |
) | |
func Detect(subject string, candidates []string) []string { | |
result := []string{} | |
for _, candidate := range candidates { | |
if normalize(subject) == normalize(candidate) && | |
strings.ToLower(subject) != strings.ToLower(candidate) { | |
result = append(result, strings.ToLower(candidate)) | |
} | |
} | |
return result | |
} | |
func normalize(subject string) string { | |
runes := []string{} | |
for _, Rune := range subject { | |
runes = append(runes, strings.ToLower(string(Rune))) | |
} | |
sort.Strings(runes) | |
return strings.Join(runes, "") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List (sort) | |
import Data.Char (toUpper) | |
anagram :: [Char] -> [[Char]] -> [[Char]] | |
anagram s cs = | |
let upCase = map toUpper | |
normalize = sort . upCase | |
is_anagram x = normalize s == normalize x && upCase s /= upCase x | |
in filter is_anagram cs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment