March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -1,17 +1,31 @@
|
|||
MsgBox % anagrams("able")
|
||||
|
||||
anagrams(word) {
|
||||
Static dict
|
||||
IfEqual dict,, FileRead dict, unixdict.txt ; file in the script directory
|
||||
w := sort(word)
|
||||
Loop Parse, dict, `n, `r
|
||||
If (w = sort(A_LoopField))
|
||||
t .= A_LoopField "`n"
|
||||
Return t
|
||||
}
|
||||
|
||||
sort(word) {
|
||||
a := RegExReplace(word,".","$0`n")
|
||||
Sort a
|
||||
Return a
|
||||
FileRead, Contents, unixdict.txt
|
||||
Loop, Parse, Contents, % "`n", % "`r"
|
||||
{ ; parsing each line of the file we just read
|
||||
Loop, Parse, A_LoopField ; parsing each letter/character of the current word
|
||||
Dummy .= "," A_LoopField
|
||||
Sort, Dummy, % "D," ; sorting those letters before removing the delimiters (comma)
|
||||
StringReplace, Dummy, Dummy, % ",", % "", All
|
||||
List .= "`n" Dummy " " A_LoopField , Dummy := ""
|
||||
} ; at this point, we have a list where each line looks like <LETTERS><SPACE><WORD>
|
||||
Count := 0, Contents := "", List := SubStr(List,2)
|
||||
Sort, List
|
||||
Loop, Parse, List, % "`n", % "`r"
|
||||
{ ; now the list is sorted, parse it counting the consecutive lines with the same set of <LETTERS>
|
||||
Max := (Count > Max) ? Count : Max
|
||||
StringSplit, LinePart, A_LoopField, % " " ; (LinePart1 are the letters, LinePart2 is the word)
|
||||
If ( PreviousLinePart1 = LinePart1 )
|
||||
Count++ , WordList .= "," LinePart2
|
||||
Else
|
||||
var_Result .= ( Count <> Max ) ? "" ; don't append if the number of common words is too low
|
||||
: "`n" Count "`t" PreviousLinePart1 "`t" SubStr(WordList,2)
|
||||
, WordList := "" , Count := 0
|
||||
PreviousLinePart1 := LinePart1
|
||||
}
|
||||
List := "", var_Result := SubStr(var_Result,2)
|
||||
Sort, var_Result, R N ; make the higher scores appear first
|
||||
Loop, Parse, var_Result, % "`n", % "`r"
|
||||
If ( 1 == InStr(A_LoopField,Max) )
|
||||
var_Output .= "`n" A_LoopField
|
||||
Else ; output only those sets of letters that scored the maximum amount of common words
|
||||
Break
|
||||
MsgBox, % ClipBoard := SubStr(var_Output,2) ; the result is also copied to the clipboard
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
#!/usr/bin/env js
|
||||
var fs = require('fs');
|
||||
|
||||
var anas = {};
|
||||
var words = read('unixdict.txt').split(/\n/g);
|
||||
var words = fs.readFileSync('unixdict.txt', 'UTF-8').split('\n');
|
||||
var max = 0;
|
||||
|
||||
for (var w in words) {
|
||||
var key = words[w].split("").sort().join('');
|
||||
var key = words[w].split('').sort().join('');
|
||||
if (!(key in anas)) {
|
||||
anas[key] = [];
|
||||
}
|
||||
anas[key].push(words[w]);
|
||||
var count = anas[key].push(words[w]);
|
||||
max = Math.max(count, max);
|
||||
}
|
||||
|
||||
for (var a in anas) {
|
||||
if (anas[a].length >= 2) {
|
||||
print(anas[a]);
|
||||
if (anas[a].length === max) {
|
||||
console.log(anas[a]);
|
||||
}
|
||||
}
|
||||
|
||||
quit();
|
||||
|
|
|
|||
|
|
@ -19,6 +19,16 @@ Procedure.s sortWord(word$)
|
|||
ProcedureReturn word$
|
||||
EndProcedure
|
||||
|
||||
;for a faster and more advanced alternative replace the previous procedure with this code
|
||||
; Procedure.s sortWord(word$) ;returns a string with the letters of the word sorted
|
||||
; Protected wordLength = Len(word$)
|
||||
; Protected Dim letters.c(wordLength)
|
||||
;
|
||||
; PokeS(@letters(), word$) ;overwrite the array with the strings contents
|
||||
; SortArray(letters(), #PB_Sort_Ascending, 0, wordLength - 1)
|
||||
; ProcedureReturn PeekS(@letters(), wordLength) ;return the arrays contents
|
||||
; EndProcedure
|
||||
|
||||
|
||||
tmpdir$ = GetTemporaryDirectory()
|
||||
filename$ = tmpdir$ + "unixdict.txt"
|
||||
|
|
@ -43,15 +53,20 @@ If ReceiveHTTPFile("http://www.puzzlers.org/pub/wordlists/unixdict.txt", filenam
|
|||
Else
|
||||
; if no
|
||||
anaMap(key$)\anas = word$ ; applying a new record
|
||||
anaMap()\isana + 1
|
||||
anaMap()\isana = 1
|
||||
EndIf
|
||||
|
||||
If anaMap()\isana > maxAnagrams ;make note of maximum anagram count
|
||||
maxAnagrams = anaMap()\isana
|
||||
EndIf
|
||||
|
||||
Until Eof(1)
|
||||
CloseFile(1)
|
||||
DeleteFile(filename$)
|
||||
|
||||
;----- output -----
|
||||
ForEach anaMap()
|
||||
If anaMap()\isana >= 4 ; only emit what had 4 or more hits.
|
||||
If anaMap()\isana = maxAnagrams ; only emit elements that have the most hits
|
||||
PrintN(anaMap()\anas)
|
||||
EndIf
|
||||
Next
|
||||
|
|
|
|||
39
Task/Anagrams/Rust/anagrams.rust
Normal file
39
Task/Anagrams/Rust/anagrams.rust
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
extern crate collections;
|
||||
|
||||
use std::str;
|
||||
use collections::HashMap;
|
||||
use std::io::File;
|
||||
use std::io::BufferedReader;
|
||||
use std::cmp;
|
||||
|
||||
fn sort_string(string: &str) -> ~str {
|
||||
let mut chars = string.chars().to_owned_vec();
|
||||
chars.sort();
|
||||
str::from_chars(chars)
|
||||
}
|
||||
|
||||
fn main () {
|
||||
let path = Path::new("unixdict.txt");
|
||||
let mut file = BufferedReader::new(File::open(&path));
|
||||
|
||||
let mut map: HashMap<~str, ~[~str]> = HashMap::new();
|
||||
|
||||
for line in file.lines() {
|
||||
let s = line.trim().to_owned();
|
||||
map.mangle(sort_string(s.clone()), s,
|
||||
|_k, v| ~[v],
|
||||
|_k, v, string| v.push(string)
|
||||
);
|
||||
}
|
||||
|
||||
let max_length = map.iter().fold(0, |s, (_k, v)| cmp::max(s, v.len()));
|
||||
|
||||
for (_k, v) in map.iter() {
|
||||
if v.len() == max_length {
|
||||
for s in v.iter() {
|
||||
print!("{} ", *s)
|
||||
}
|
||||
println!("")
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue