Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -7,7 +7,7 @@ reverse(text s)
|
|||
i = length(s);
|
||||
while (i) {
|
||||
i -= 1;
|
||||
b_insert(b, -1, character(s, i));
|
||||
b_insert(b, -1, s[i]);
|
||||
}
|
||||
|
||||
return b_string(b);
|
||||
|
|
@ -50,9 +50,7 @@ main(void)
|
|||
} while (r_greater(r, s, s));
|
||||
}
|
||||
|
||||
o_text("Semordnilap pairs: ");
|
||||
o_integer(p);
|
||||
o_text("\n");
|
||||
o_form("Semordnilap pairs: ~\n", p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,24 @@
|
|||
(use 'clojure.java.io)
|
||||
(require '[clojure.string :as string])
|
||||
(ns rosettacode.semordnilaps
|
||||
(:require [clojure.string :as str])
|
||||
[clojure.java.io :as io ]))
|
||||
|
||||
(def dict-file (or (first *command-line-args*) "unixdict.txt"))
|
||||
(def dict-file
|
||||
(or (first *command-line-args*) "unixdict.txt"))
|
||||
|
||||
(def dict (set (line-seq (reader dict-file))))
|
||||
(def dict (-> dict-file io/reader line-seq set))
|
||||
|
||||
(defn semordnilap? [word]
|
||||
(let [rev (string/reverse word)]
|
||||
(and (not (= word rev)) (dict rev))))
|
||||
(let [rev (str/reverse word)]
|
||||
(and (not= word rev) (dict rev))))
|
||||
|
||||
(def semordnilaps
|
||||
(filter (fn [[x y]] (<= (compare x y) 0))
|
||||
(map (fn [word] [word (string/reverse word)])
|
||||
(filter semordnilap? dict))))
|
||||
(->> dict
|
||||
(filter semordnilap?)
|
||||
(map #([% (str/reverse %)]))
|
||||
(filter (fn [[x y]] (<= (compare x y) 0)))))
|
||||
|
||||
(printf "There are %d semordnilaps in %s. Here are 5:\n"
|
||||
(count semordnilaps)
|
||||
dict-file)
|
||||
|
||||
(dorun (map println (sort (take 5 (shuffle semordnilaps)))))
|
||||
(dorun (->> semordnilaps shuffle (take 5) sort (map println)))
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
import std.stdio, std.file, std.string, std.algorithm;
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.file, std.string, std.algorithm;
|
||||
|
||||
bool[string] seenWords;
|
||||
size_t pairCount = 0;
|
||||
|
||||
foreach (const word; readText("unixdict.txt").toLower.splitter) {
|
||||
const drow = word.dup.reverse; // Deprecated.
|
||||
foreach (const word; "unixdict.txt".readText.toLower.splitter) {
|
||||
//const drow = word.dup.reverse();
|
||||
auto drow = word.dup;
|
||||
drow.reverse();
|
||||
if (drow in seenWords) {
|
||||
if (pairCount++ < 5)
|
||||
writeln(word, " ", drow);
|
||||
|
|
|
|||
80
Task/Semordnilap/Eiffel/semordnilap-1.e
Normal file
80
Task/Semordnilap/Eiffel/semordnilap-1.e
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
note
|
||||
description: "Summary description for {SEMORDNILAP}."
|
||||
author: ""
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
SEMORDNILAP
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
make
|
||||
--read wordlist "unixdict.txt", search across wordlist with binary_search
|
||||
local
|
||||
count,i,j, middle, upper, lower: INTEGER
|
||||
reverse: STRING
|
||||
do
|
||||
read_wordlist
|
||||
create solution.make_empty
|
||||
from
|
||||
i:= 1
|
||||
until
|
||||
i> word_array.count
|
||||
loop
|
||||
word_array[i].mirror
|
||||
reverse:=word_array[i]
|
||||
from
|
||||
lower:= i+1
|
||||
upper:= word_array.count
|
||||
until
|
||||
lower>=upper
|
||||
loop
|
||||
|
||||
middle:= (upper-lower)//2+lower
|
||||
if reverse.is_case_insensitive_equal (word_array[middle]) then
|
||||
count:= count+1
|
||||
upper:= 0
|
||||
lower:= 1
|
||||
solution.force (word_array[middle],count)
|
||||
elseif reverse.is_less (word_array[middle]) then
|
||||
upper:= middle-1
|
||||
else
|
||||
lower:= middle+1
|
||||
end
|
||||
end
|
||||
if lower < word_array.count and then reverse.is_case_insensitive_equal (word_array[lower]) then
|
||||
count:= count+1
|
||||
upper:= 0
|
||||
lower:= 1
|
||||
solution.force (word_array[middle],count)
|
||||
end
|
||||
i:= i+1
|
||||
end
|
||||
end
|
||||
|
||||
solution: ARRAY[STRING]
|
||||
|
||||
feature {NONE}
|
||||
read_wordlist
|
||||
local
|
||||
l_file: PLAIN_TEXT_FILE
|
||||
wordlist: LIST[STRING]
|
||||
i: INTEGER
|
||||
do
|
||||
create l_file.make_open_read_write ("unixdict.txt")
|
||||
l_file.read_stream (l_file.count)
|
||||
wordlist:=l_file.last_string.split ('%N')
|
||||
create word_array.make_empty
|
||||
from
|
||||
i:= 1
|
||||
until
|
||||
i> wordlist.count
|
||||
loop
|
||||
word_array.force( wordlist.at (i),i)
|
||||
i:= i+1
|
||||
end
|
||||
end
|
||||
word_array: ARRAY[STRING]
|
||||
end
|
||||
16
Task/Semordnilap/Eiffel/semordnilap-2.e
Normal file
16
Task/Semordnilap/Eiffel/semordnilap-2.e
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class
|
||||
APPLICATION
|
||||
inherit
|
||||
ARGUMENTS
|
||||
create
|
||||
make
|
||||
feature
|
||||
make
|
||||
do
|
||||
create se.make
|
||||
across se.solution.subarray (27, 32)as s loop io.put_string (s.item.out+"%T"); s.item.mirror; io.put_string(s.item.out+"%N") end
|
||||
io.put_string ("There are "+se.solution.count.out+" pairs.")
|
||||
end
|
||||
|
||||
se: SEMORDNILAP
|
||||
end
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Semordnilaps {
|
||||
public class Semordnilap {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
List<String> lst = readLines("unixdict.txt");
|
||||
public static void main(String[] args) throws Exception {
|
||||
List<String> lst = Files.readAllLines(Paths.get("unixdict.txt"));
|
||||
Set<String> seen = new HashSet<>();
|
||||
int count = 0;
|
||||
for (String w : lst) {
|
||||
w = w.toLowerCase();
|
||||
String r = new StringBuilder(w).reverse().toString();
|
||||
if (seen.contains(r)) {
|
||||
if (count++ < 5)
|
||||
|
|
@ -16,15 +17,4 @@ public class Semordnilaps {
|
|||
}
|
||||
System.out.println("\nSemordnilap pairs found: " + count);
|
||||
}
|
||||
|
||||
private static List<String> readLines(String fn) throws IOException {
|
||||
List<String> lines;
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fn))) {
|
||||
lines = new ArrayList<>();
|
||||
String line;
|
||||
while ((line = br.readLine()) != null)
|
||||
lines.add(line.trim().toLowerCase());
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
data = Import["http://www.puzzlers.org/pub/wordlists/unixdict.txt", "List"];
|
||||
result = DeleteDuplicates[ Select[data, MemberQ[data, StringReverse[#]] && # =!= StringReverse[#] &], (# ===StringReverse[#2]) &];
|
||||
result = DeleteDuplicates[ Select[data, MemberQ[data, StringReverse[#]]
|
||||
&& # =!= StringReverse[#] &], (# ===StringReverse[#2]) &];
|
||||
Print[Length[result], Take[result, 5]]
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
/*REXX program finds palindrome pairs using a dictionary (UNIXDICT.TXT)*/
|
||||
#=0 /*number of palindromes (so far).*/
|
||||
parse arg iFID .; if iFID=='' then iFID='UNIXDICT.TXT' /*use default?*/
|
||||
@.= /*caseless non-duplicated words. */
|
||||
do while lines(ifid)\==0; _=linein(iFID); u=translate(space(_,0))
|
||||
if length(u)<2 | @.u\=='' then iterate /*can't be a unique pal.*/
|
||||
r=reverse(u)
|
||||
if @.r\=='' then do; #=#+1 /*found palindrome pair.*/
|
||||
if #<6 then say @.r',' _ /*only list first 5 pals*/
|
||||
end
|
||||
@.u=_
|
||||
end /*while*/
|
||||
say /*a unique palindrome pair = a semordnilap*/
|
||||
say "There're" # 'unique palindrome pairs in the dictionary file:' iFID
|
||||
/*REXX program finds palindrome pairs using a dictionary (UNIXDICT.TXT).*/
|
||||
#=0 /*# palindromes (so far)*/
|
||||
parse arg iFID .; if iFID=='' then iFID='UNIXDICT.TXT' /*use default?*/
|
||||
@.= /*caseless no-duped word*/
|
||||
do while lines(iFID)\==0; _=space(linein(iFID),0); parse upper var _ u
|
||||
if length(_)<2 | @.u\=='' then iterate /*can't be a unique pal.*/
|
||||
r=reverse(u) /*get the reverse of U. */
|
||||
if @.r\=='' then do; #=#+1 /*found palindrome pair?*/
|
||||
if #<6 then say @.r',' _ /*only list first 5 pals*/
|
||||
end /* [↑] bump count, show*/
|
||||
@.u=_ /*define palindromic pal*/
|
||||
end /*while*/ /* [↑] read dictionary.*/
|
||||
say
|
||||
say "There're" # 'unique palindrome pairs in the dictionary file: ' iFID
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
DICT=File.readlines("unixdict.txt").collect &:chomp
|
||||
dict = File.readlines("unixdict.txt").collect(&:strip)
|
||||
i = 0
|
||||
res = DICT.collect(&:reverse).sort.select {|z|
|
||||
i+=1 while z > DICT[i] and i < DICT.length-1
|
||||
z == DICT[i] and z < z.reverse
|
||||
}
|
||||
res = dict.collect(&:reverse).sort.select do |z|
|
||||
i += 1 while z > dict[i] and i < dict.length-1
|
||||
z == dict[i] and z < z.reverse
|
||||
end
|
||||
puts "There are #{res.length} semordnilaps, of which the following are 5:"
|
||||
res.sample(5).each {|z| puts "#{z} #{z.reverse}"}
|
||||
res.take(5).each {|z| puts "#{z} #{z.reverse}"}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue