45
4
Don't ask me how or why but while coding on a project I noticed that the characters of a couple of words had a particular pattern referred to the alphabet, I connected each character of the words with the pencil on the alphabet and I obtained two spirals, then I noticed that the first spiral was clockwise and the other was counterclockwise, and other characteristics... so I named them Swirling Words!
A Swirling Word can be:
- clockwise or counterclockwise
- centripetal or centrifugal
Here there are some examples of Swirling Words:
Task 1:
Write a full program or function that will take a word from standard input and will output if is a Swirling Word and it's characteristics, in a readable format, extended text, 3 characters, flags, etc.
Test cases and example outputs for different words (but you can decide how to represent the results):
EARTH, GROUP > NO // NOT A SWIRLING WORD
OPERA, STAY, IRIS > SW,CF,CW // SWIRLING WORD, CENTRIFUGAL, CLOCKWISE
MINER, TAX, PLUG, META > SW,CF,CC // SWIRLING WORD, CENTRIFUGAL, COUNTERCLOCKWISE
AXIOM, AXIS, COOK > SW,CP,CW // SWIRLING WORD, CENTRIPETAL, CLOCKWISE
WATCH, YETI, PILL > SW,CP,CC // SWIRLING WORD, CENTRIPETAL, COUNTERCLOCKWISE
MORE EXAMPLES OF FALSE TEST CASES (NOT SWIRLING WORDS):
EARTH, GROUP, OUTPUT, WORD, CONNECTION, ODD, MOM, DAD,
CHARACTER, EXAMPLE, QUESTION, NEWSLETTER, OTHER
Rules:
- The connection between the first two characters must be up (like in the graphics), all the even connection must be down, all the odd connections must be up.
- You can ignore upper/lowercase or consider/convert all to upper case or all to lower case.
- The input words are only characters in the alphabet range of A-Z, no spaces, no punctuation, etc.
- If a word has double characters, like "GROOVE", you must collapse the doubles to one character: "GROOVE" > "GROVE".
- The input words will contain at least 3 distinct characters. Words like "MOM", "DAD", "LOL" are not valid words.
- It's possible to pass multiple times in the same character, like "IRIS".
- Shortest code wins.
Task 2:
To obtain more reputation, find the longest Swirling Words, and it's characteristics, that you can find in the english dictionary, following the above rules. You can take for example as reference the complete list of the english words here.
Happy coding!
16Nice diagrams! :) (And nice challenge, too. ;)) – Martin Ender – 2016-10-06T20:11:14.237
Would omitting "Swirling" be a valid output format, since it's implied when the input isn't "not swirling"? – Martin Ender – 2016-10-06T20:12:12.893
@MartinEnder Yes, since it's understandable when it's swirling or not, can be empty for "not" and "1" for "yes", etc. (Glad you liked the diagrams and challenge! :)) – Mario – 2016-10-06T20:14:26.187
Related -- all the swirling example words except
COOK
are also Bumpy Words. – AdmBorkBork – 2016-10-06T20:15:46.7871@TimmyD But not all bumpy words are swirling. :) – Martin Ender – 2016-10-06T20:17:44.777
@MartinEnder Very true. Hence "Related" and not "Close as dupe." -- This is a very nice challenge. – AdmBorkBork – 2016-10-06T20:18:17.147
1I wonder how much shorter answers would be if it was simply Truthy or Falsey output for swirling or not swirling. – mbomb007 – 2016-10-06T20:28:26.957
It appears to me that rule 1 has no effect for the challenge, does it? – Luis Mendo – 2016-10-06T21:33:19.810
@LuisMendo it determines clockwise vs counterclockwise. – Martin Ender – 2016-10-06T21:37:55.097
@Martin Oh, I see now. Thanks! – Luis Mendo – 2016-10-06T21:47:07.600
Are words with only two letters like
MOM
andDAD
either centripetal or centrifugal? Are they spiralling? – Linus – 2016-10-07T01:44:27.063@Linus According to Martin Ender proper suggestion, I updated the rules to avoid such "circular" case and don't mess up other working answers. – Mario – 2016-10-07T12:37:11.480
1Tip for future challenges: your challenge includes a step that detracts from the “main idea”, namely removing duplicate letter runs (
PILL
→PIL
). That kind of thing is generally Not Fun, and I think this challenge would have been better overall if there were fewer tricky edge cases to deal with (maybe a guarantee that there will be no duplicate letters in the input at all, not even words likeIRIS
, would have been nice.) – Lynn – 2016-10-07T15:43:40.217That said, this challenge (and its diagrams!) are wonderful. Maybe throw in some long test cases (like
AZYBXC…
)? – Lynn – 2016-10-07T15:44:36.2602@Lynn Thanks for the appreciation and suggestions, I'll try to improve for the future. I added the "remove double" rule to anticipate people asking me things like "what do we do when there are doubles?" > you can consider doubles as 1 single character because from "L" going to "L" is zero distance :) Is not that I wanted to add tricky difficulties for it's own sake. – Mario – 2016-10-07T16:05:40.790
this function works correctly for the given test cases
function f($z){$z=preg_replace("#(.)\\1#","$1",$z);for($i=0;++$i<strlen($z);){$a[]=abs($n=ord($z[$i-1])-ord($z[$i]));$i%2?$e[]=$n:$o[]=$n;}$c=$a[0]>end($a)?1:2;if(count(array_diff($e,range(0,-26)))==0)$c+=2;$c*=min(end($a),$a[0])==min($a)&&max(end($a),$a[0])==max($a)?1:0;return $c;}$z="test";echo ["N","F-","P-","F+","P+"][f($z,1)];
but if I try the word "test" if gives me a wrong result. And swirling words like bananas , banana or ananas looks great – Jörg Hülsermann – 2016-10-08T23:19:42.907@JörgHülsermann I tryed your function and it gives positive results for words which are not swirling words, like
test
orabcdefg
so I think there is something wrong somewhere. – Mario – 2016-10-10T07:33:22.463@Mario I think adding more false test cases is better to decide if a program do this job – Jörg Hülsermann – 2016-10-10T10:21:51.027
@JörgHülsermann I added more false test cases – Mario – 2016-10-10T12:04:38.553
@Mario What did you use to make the diagrams? – mbomb007 – 2016-10-17T19:47:31.670
@mbomb007 I used Illustrator because I use it in my job, but is possible to do that using Inkscape or Gimp for example – Mario – 2016-10-18T06:46:47.390