Teacup rim text: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{draft task|Teacup rim text}} On a set of coasters we have, there's a picture of a teacup. On the rim of the teacup the word "TEA" appears a number of times separated by bul...")
 
(Add Factor example)
Line 6: Line 6:


So here's the task: from a web accessible or locally accessible word source, iterate through each word. With each word, peel off the first letter and put it at the end. Check if the word exists. If it does, keep going with the next letter, repeating the process for as many letters as there are minus one. If all of the words exist store the original word. List the words that survive the process at the end. Optionally list all their variants.
So here's the task: from a web accessible or locally accessible word source, iterate through each word. With each word, peel off the first letter and put it at the end. Check if the word exists. If it does, keep going with the next letter, repeating the process for as many letters as there are minus one. If all of the words exist store the original word. List the words that survive the process at the end. Optionally list all their variants.

=={{header|Factor}}==
<lang factor>USING: combinators.short-circuit fry grouping hash-sets
http.client kernel make math prettyprint sequences
sequences.extras sets sorting splitting unicode ;

"https://raw.githubusercontent.com/dwyl/english-words/master/words.txt"
http-get nip "\n" split harvest
[ { [ length 2 > ] [ [ letter? ] all? ] [ all-equal? not ] } 1&& ] filter ! we want lowercase words with > 2
[ >hash-set ] [ ] bi ! letters which are not all the same.
[ [ all-rotations members swap dupd '[ _ in? ] all? [ , ] [ drop ] if ] with each ] { } make
[ natural-sort ] map members .</lang>
{{out}}
<pre>
{
{ "arar" "rara" }
{ "ary" "rya" "yar" }
{ "ate" "eat" "tea" }
{ "eth" "het" "the" }
}
</pre>


=={{header|Lychen}}==
=={{header|Lychen}}==

Revision as of 11:21, 4 August 2019

Teacup rim text is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

On a set of coasters we have, there's a picture of a teacup. On the rim of the teacup the word "TEA" appears a number of times separated by bullet characters. It occurred to me that if the bullet were removed and the words run together, you could start at any letter and still end up with a meaningful three-letter word. So start at the "T" and read "TEA". Start at the "E" and read "EAT", or start at the "A" and read "ATE".

That got me thinking that maybe there are other words that could be used rather that "TEA". And that's just English. What about Italian or Greek or ... um ... Telugu.

So here's the task: from a web accessible or locally accessible word source, iterate through each word. With each word, peel off the first letter and put it at the end. Check if the word exists. If it does, keep going with the next letter, repeating the process for as many letters as there are minus one. If all of the words exist store the original word. List the words that survive the process at the end. Optionally list all their variants.

Factor

<lang factor>USING: combinators.short-circuit fry grouping hash-sets http.client kernel make math prettyprint sequences sequences.extras sets sorting splitting unicode ;

"https://raw.githubusercontent.com/dwyl/english-words/master/words.txt" http-get nip "\n" split harvest [ { [ length 2 > ] [ [ letter? ] all? ] [ all-equal? not ] } 1&& ] filter  ! we want lowercase words with > 2 [ >hash-set ] [ ] bi  ! letters which are not all the same. [ [ all-rotations members swap dupd '[ _ in? ] all? [ , ] [ drop ] if ] with each ] { } make [ natural-sort ] map members .</lang>

Output:
{
    { "arar" "rara" }
    { "ary" "rya" "yar" }
    { "ate" "eat" "tea" }
    { "eth" "het" "the" }
}

Lychen

Lychen is V8 JavaScript wrapped in C#, exposing C# into JavaScript.

<lang javascript> const wc = new CS.System.Net.WebClient(); const lines = wc.DownloadString("https://raw.githubusercontent.com/dwyl/english-words/master/words.txt"); const words = lines.split(/\n/g); const collection = {}; words.filter(word => word.length > 2).forEach(word => {

 const theword = word.toLowerCase();
 let allok = true;
 let newword = theword;
 for (let i = 0; i < word.length - 1; i++) {
   newword = newword.substr(1) + newword.substr(0, 1);
   if (!words.includes(newword)) {
     allok = false;
     break;
   }
 }
 if (allok) {
   var key = theword.split("").sort().join("");
   if (!collection[key]) {
     collection[key] = [theword];
   } else {
     if (!collections[key].includes(theword)) {
       collection[key].push(theword);
     }
   }
 }

}); Object.keys(collection) .filter(key => collection[key].length > 1) .forEach(key => console.log("%s", collection[key].join(", "))); </lang>

arar, rara
ary, yar, rya
ate, eat, tea
eth, het, the, the