Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1 +1,2 @@
|
|||
Two or more words can be composed of the same characters, but in a different order. Using the word list at http://www.puzzlers.org/pub/wordlists/unixdict.txt, find the sets of words that share the same characters that contain the most words in them.
|
||||
Two or more words can be composed of the same characters, but in a different order.
|
||||
Using the word list at http://www.puzzlers.org/pub/wordlists/unixdict.txt, find the sets of words that share the same characters that contain the most words in them.
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
import std.stdio, std.algorithm, std.file, std.string;
|
||||
|
||||
void main() {
|
||||
auto keys = cast(char[])"unixdict.txt".read;
|
||||
import std.stdio, std.algorithm, std.file, std.string;
|
||||
|
||||
auto keys = "unixdict.txt".readText!(char[]);
|
||||
immutable vals = keys.idup;
|
||||
string[][string] anags;
|
||||
foreach (w; keys.splitter) {
|
||||
immutable k = cast(string)w.representation.sort().release;
|
||||
anags[k] ~= vals[k.ptr-keys.ptr .. k.ptr-keys.ptr + k.length];
|
||||
immutable k = w.representation.sort().release.assumeUTF;
|
||||
anags[k] ~= vals[k.ptr - keys.ptr .. k.ptr - keys.ptr + k.length];
|
||||
}
|
||||
//immutable m = anags.byValue.max!q{ a.length };
|
||||
//immutable m = anags.byValue.maxs!q{ a.length };
|
||||
immutable m = anags.byValue.map!q{ a.length }.reduce!max;
|
||||
writefln("%(%s\n%)", anags.byValue.filter!(ws => ws.length == m));
|
||||
writefln("%(%-(%s %)\n%)", anags.byValue.filter!(ws => ws.length == m));
|
||||
}
|
||||
|
|
|
|||
23
Task/Anagrams/JavaScript/anagrams-1.js
Normal file
23
Task/Anagrams/JavaScript/anagrams-1.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
var fs = require('fs');
|
||||
var words = fs.readFileSync('unixdict.txt', 'UTF-8').split('\n');
|
||||
|
||||
var i, item, max = 0,
|
||||
anagrams = {};
|
||||
|
||||
for (i = 0; i < words.length; i += 1) {
|
||||
var key = words[i].split('').sort().join('');
|
||||
if (!anagrams.hasOwnProperty(key)) {//check if property exists on current obj only
|
||||
anagrams[key] = [];
|
||||
}
|
||||
var count = anagrams[key].push(words[i]); //push returns new array length
|
||||
max = Math.max(count, max);
|
||||
}
|
||||
|
||||
//note, this returns all arrays that match the maximum length
|
||||
for (item in anagrams) {
|
||||
if (anagrams.hasOwnProperty(item)) {//check if property exists on current obj only
|
||||
if (anagrams[item].length === max) {
|
||||
console.log(anagrams[item].join(' '));
|
||||
}
|
||||
}
|
||||
}
|
||||
22
Task/Anagrams/JavaScript/anagrams-2.js
Normal file
22
Task/Anagrams/JavaScript/anagrams-2.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
var fs = require('fs');
|
||||
var dictionary = fs.readFileSync('unixdict.txt', 'UTF-8').split('\n');
|
||||
|
||||
//group anagrams
|
||||
var sortedDict = dictionary.reduce(function (acc, word) {
|
||||
var sortedLetters = word.split('').sort().join('');
|
||||
if (acc[sortedLetters] === undefined) { acc[sortedLetters] = []; }
|
||||
acc[sortedLetters].push(word);
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
//sort list by frequency
|
||||
var keysSortedByFrequency = Object.keys(sortedDict).sort(function (keyA, keyB) {
|
||||
if (sortedDict[keyA].length < sortedDict[keyB].length) { return 1; }
|
||||
if (sortedDict[keyA].length > sortedDict[keyB].length) { return -1; }
|
||||
return 0;
|
||||
});
|
||||
|
||||
//print first 10 anagrams by frequency
|
||||
keysSortedByFrequency.slice(0, 10).forEach(function (key) {
|
||||
console.log(sortedDict[key].join(' '));
|
||||
});
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
var fs = require('fs');
|
||||
|
||||
var anas = {};
|
||||
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('');
|
||||
if (!(key in anas)) {
|
||||
anas[key] = [];
|
||||
}
|
||||
var count = anas[key].push(words[w]);
|
||||
max = Math.max(count, max);
|
||||
}
|
||||
|
||||
for (var a in anas) {
|
||||
if (anas[a].length === max) {
|
||||
console.log(anas[a]);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,11 +4,11 @@ use Collection;
|
|||
class Anagrams {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
lines := HttpClient->New()->Get("http://www.puzzlers.org/pub/wordlists/unixdict.txt");
|
||||
anagrams := StringMap->New();
|
||||
count := 0;
|
||||
if(lines->Size() = 1) {
|
||||
line := lines->Get(0)->As(String);
|
||||
words := line->Split("\n");
|
||||
anagrams := StringMap->New();
|
||||
words->Size()->PrintLine();
|
||||
each(i : words) {
|
||||
word := words[i]->Trim();
|
||||
key := String->New(word->ToCharArray()->Sort());
|
||||
|
|
@ -18,12 +18,13 @@ class Anagrams {
|
|||
anagrams->Insert(key, list);
|
||||
};
|
||||
list->AddBack(word);
|
||||
count := count->Max(list->Size());
|
||||
};
|
||||
|
||||
lists := anagrams->GetValues();
|
||||
each(i : lists) {
|
||||
list := lists->Get(i)->As(Vector);
|
||||
if(list->Size() > 1) {
|
||||
if(list->Size() >= count) {
|
||||
'['->Print();
|
||||
each(j : list) {
|
||||
list->Get(j)->As(String)->Print();
|
||||
|
|
|
|||
|
|
@ -1,14 +1,25 @@
|
|||
import urllib.request, itertools
|
||||
import time
|
||||
words = urllib.request.urlopen('http://www.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
print('Words ready')
|
||||
t0 = time.clock()
|
||||
anagrams = [list(g) for k,g in itertools.groupby(sorted(words, key=sorted), key=sorted)]
|
||||
anagrams.sort(key=len, reverse=True)
|
||||
count = len(anagrams[0])
|
||||
for ana in anagrams:
|
||||
if len(ana) < count:
|
||||
break
|
||||
print(ana)
|
||||
t0 -= time.clock()
|
||||
print('Finished in %f s' % -t0)
|
||||
>>> import urllib
|
||||
>>> from collections import defaultdict
|
||||
>>> words = urllib.urlopen('http://www.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
>>> len(words)
|
||||
25104
|
||||
>>> anagram = defaultdict(list) # map sorted chars to anagrams
|
||||
>>> for word in words:
|
||||
anagram[tuple(sorted(word))].append( word )
|
||||
|
||||
|
||||
>>> count = max(len(ana) for ana in anagram.itervalues())
|
||||
>>> for ana in anagram.itervalues():
|
||||
if len(ana) >= count:
|
||||
print ana
|
||||
|
||||
|
||||
['angel', 'angle', 'galen', 'glean', 'lange']
|
||||
['alger', 'glare', 'lager', 'large', 'regal']
|
||||
['caret', 'carte', 'cater', 'crate', 'trace']
|
||||
['evil', 'levi', 'live', 'veil', 'vile']
|
||||
['elan', 'lane', 'lean', 'lena', 'neal']
|
||||
['abel', 'able', 'bale', 'bela', 'elba']
|
||||
>>> count
|
||||
5
|
||||
>>>
|
||||
|
|
|
|||
|
|
@ -1,25 +1,22 @@
|
|||
>>> import urllib
|
||||
>>> from collections import defaultdict
|
||||
>>> import urllib, itertools
|
||||
>>> words = urllib.urlopen('http://www.puzzlers.org/pub/wordlists/unixdict.txt').read().split()
|
||||
>>> len(words)
|
||||
25104
|
||||
>>> anagram = defaultdict(list) # map sorted chars to anagrams
|
||||
>>> for word in words:
|
||||
anagram[tuple(sorted(word))].append( word )
|
||||
>>> anagrams = [list(g) for k,g in itertools.groupby(sorted(words, key=sorted), key=sorted)]
|
||||
|
||||
|
||||
>>> count = max(len(ana) for ana in anagram.itervalues())
|
||||
>>> for ana in anagram.itervalues():
|
||||
|
||||
>>> count = max(len(ana) for ana in anagrams)
|
||||
>>> for ana in anagrams:
|
||||
if len(ana) >= count:
|
||||
print ana
|
||||
|
||||
|
||||
['abel', 'able', 'bale', 'bela', 'elba']
|
||||
['caret', 'carte', 'cater', 'crate', 'trace']
|
||||
['angel', 'angle', 'galen', 'glean', 'lange']
|
||||
['alger', 'glare', 'lager', 'large', 'regal']
|
||||
['caret', 'carte', 'cater', 'crate', 'trace']
|
||||
['evil', 'levi', 'live', 'veil', 'vile']
|
||||
['elan', 'lane', 'lean', 'lena', 'neal']
|
||||
['abel', 'able', 'bale', 'bela', 'elba']
|
||||
['evil', 'levi', 'live', 'veil', 'vile']
|
||||
>>> count
|
||||
5
|
||||
>>>
|
||||
|
|
|
|||
63
Task/Anagrams/RapidQ/anagrams.rapidq
Normal file
63
Task/Anagrams/RapidQ/anagrams.rapidq
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
dim x as integer, y as integer
|
||||
dim SortX as integer
|
||||
dim StrOutPut as string
|
||||
dim Count as integer
|
||||
dim MaxCount as integer
|
||||
|
||||
dim AnaList as QStringlist
|
||||
dim wordlist as QStringlist
|
||||
dim Templist as QStringlist
|
||||
dim Charlist as Qstringlist
|
||||
|
||||
|
||||
function sortChars(expr as string) as string
|
||||
Charlist.clear
|
||||
for SortX = 1 to len(expr)
|
||||
Charlist.AddItems expr[SortX]
|
||||
next
|
||||
charlist.sort
|
||||
result = Charlist.text - chr$(10) - chr$(13)
|
||||
end function
|
||||
|
||||
'--- Start main code
|
||||
wordlist.loadfromfile ("unixdict.txt")
|
||||
|
||||
'create anagram list
|
||||
for x = 0 to wordlist.itemcount-1
|
||||
AnaList.AddItems sortChars(wordlist.item(x))
|
||||
next
|
||||
|
||||
'Filter largest anagram lists
|
||||
analist.sort
|
||||
MaxCount = 0
|
||||
|
||||
for x = 0 to AnaList.Itemcount-1
|
||||
Count = 0
|
||||
for y = x+1 to AnaList.Itemcount-1
|
||||
if AnaList.item(y) = AnaList.item(x) then
|
||||
inc(count)
|
||||
else
|
||||
if count > MaxCount then
|
||||
Templist.clear
|
||||
MaxCount = Count
|
||||
Templist.AddItems AnaList.item(x)
|
||||
elseif count = MaxCount then
|
||||
Templist.AddItems AnaList.item(x)
|
||||
end if
|
||||
exit for
|
||||
end if
|
||||
next
|
||||
next
|
||||
|
||||
'Now get the words
|
||||
for x = 0 to Templist.Itemcount-1
|
||||
for y = 0 to wordlist.Itemcount-1
|
||||
if Templist.item(x) = sortChars(wordlist.item(y)) then
|
||||
StrOutPut = StrOutPut + wordlist.item(y) + " "
|
||||
end if
|
||||
next
|
||||
StrOutPut = StrOutPut + chr$(13) + chr$(10)
|
||||
next
|
||||
|
||||
ShowMessage StrOutPut
|
||||
End
|
||||
|
|
@ -1,39 +1,27 @@
|
|||
extern crate collections;
|
||||
|
||||
use std::str;
|
||||
use collections::HashMap;
|
||||
use std::collections::hashmap::{HashMap, Occupied, Vacant};
|
||||
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();
|
||||
fn sort_string(string: &str) -> String {
|
||||
let mut chars = string.chars().collect::<Vec<char>>();
|
||||
chars.sort();
|
||||
str::from_chars(chars)
|
||||
String::from_chars(chars.as_slice())
|
||||
}
|
||||
|
||||
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!("")
|
||||
let mut map = HashMap::new();
|
||||
for line in file.lines().map(|s| s.unwrap()) {
|
||||
let s = line.as_slice().trim();
|
||||
match map.entry(sort_string(s)) {
|
||||
Vacant(entry) => { entry.set(vec![s.into_string()]); },
|
||||
Occupied(mut entry) => { entry.get_mut().push(s.into_string()); }
|
||||
}
|
||||
}
|
||||
let max_length = map.values().fold(0, |s, v| cmp::max(s, v.len()));
|
||||
for v in map.values().filter(|&v| v.len() == max_length) {
|
||||
println!("{}", v.connect(" "))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
36
Task/Anagrams/UNIX-Shell/anagrams.sh
Normal file
36
Task/Anagrams/UNIX-Shell/anagrams.sh
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
http_get_body() {
|
||||
local host=$1
|
||||
local uri=$2
|
||||
exec 5<> /dev/tcp/$host/80
|
||||
printf >&5 "%s\r\n" "GET $uri HTTP/1.1" "Host: $host" "Connection: close" ""
|
||||
mapfile -t -u5
|
||||
local lines=( "${MAPFILE[@]//$'\r'}" )
|
||||
local i=0 found=0
|
||||
for (( ; found == 0; i++ )); do
|
||||
[[ -z ${lines[i]} ]] && (( found++ ))
|
||||
done
|
||||
printf "%s\n" "${lines[@]:i}"
|
||||
exec 5>&-
|
||||
}
|
||||
|
||||
declare -A wordlist
|
||||
|
||||
while read -r word; do
|
||||
uniq_letters=( $(for ((i=0; i<${#word}; i++)); do echo "${word:i:1}"; done | sort) )
|
||||
wordlist["${uniq_letters[*]}"]+="$word "
|
||||
done < <( http_get_body www.puzzlers.org /pub/wordlists/unixdict.txt )
|
||||
|
||||
maxlen=0
|
||||
maxwords=()
|
||||
|
||||
for key in "${!wordlist[@]}"; do
|
||||
words=( ${wordlist[$key]} )
|
||||
if (( ${#words[@]} > maxlen )); then
|
||||
maxlen=${#words[@]}
|
||||
maxwords=( "${wordlist["$key"]}" )
|
||||
elif (( ${#words[@]} == maxlen )); then
|
||||
maxwords+=( "${wordlist[$key]}" )
|
||||
fi
|
||||
done
|
||||
|
||||
printf "%s\n" "${maxwords[@]}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue