{"id":18652,"date":"2023-12-28T22:41:32","date_gmt":"2023-12-29T06:41:32","guid":{"rendered":"https:\/\/www.kith.org\/words\/?page_id=18652"},"modified":"2023-12-28T22:47:26","modified_gmt":"2023-12-29T06:47:26","slug":"switchback-finder","status":"publish","type":"page","link":"https:\/\/www.kith.org\/words\/code\/switchback-finder\/","title":{"rendered":"Switchback finder"},"content":{"rendered":"\r\n\r\n<p>For information, see <a href=\"https:\/\/www.kith.org\/words\/1997\/07\/13\/backward\/\">Column B<\/a>.<\/p>\r\n<pre style=\"overflow: scroll;\">\r\n\r\n#! \/bin\/sh\r\n\/bin\/cat <<'%%%'\r\nContent-type: text\/plain\r\n#! \/usr\/sbin\/perl\r\n# switchback.pl: Program to determine whether a given word is a \"switchback.\"\r\n#\r\n# Version 1.0, copyright 1997 by Jed Hartman.\r\n# Last modification: 10 July 1997.\r\n#\r\n# Permission to use, copy, modify, and distribute this software and its\r\n# documentation, in whole or in part, for any purpose and without fee is\r\n# hereby granted, provided that: (a) the above copyright notice appear in\r\n# all copies; (b) both the copyright notice and this permission notice\r\n# appear in supporting documentation; and (c) no fee is charged for further\r\n# redistribution of the software. This software is provided \"as is\" without\r\n# express or implied warranty.\r\n#\r\n# Thanks to Jef Poskanzer for the permission-to-use notice.\r\n#\r\n#\r\n# Notes:\r\n#\r\n# Takes a word as input. Creates a list of all strings which consist of reversing\r\n# the given word and changing exactly one letter (to another letter). Then checks\r\n# every string in the created list against a given file containing a list of valid\r\n# words. The result is a list of all words (in the given word file) which can be\r\n# obtained by spelling the original word backward *and* changing exactly one letter.\r\n#\r\n# If you have comments or suggestions, write to me at logos@kith.org. I'm a\r\n# novice perl hacker and would be delighted to receive style comments.\r\n$alphabet = \"abcdefghijklmnopqrstuvwxyz\";\r\n$wordfilename = \"\/usr\/dict\/words\";\r\n# Some UNIX systems use \"\/usr\/share\/dict\/words\" instead.\r\n$stop = 0;\r\nwhile ($stop == 0)\r\n{\r\n  $wordcount = 0;\r\n  @foundwords = ();\r\n  @wordlist = ();\r\n  print \"Enter the word to check, or xxx to stop: \";\r\n  $word = <>;\r\n  chop($word);\r\n  if ($word eq \"xxx\")\r\n  {\r\n    $stop = 1;\r\n    print \"\\nGoodbye.\\n\";\r\n    last;\r\n  }\r\n  $rev = reverse($word);\r\n  $rev =~ tr\/A-Z\/a-z\/;\r\n  $wordlength = length($rev);\r\n  for ($cur_let_index = 0; $cur_let_index < $wordlength; $cur_let_index++)\r\n  {\r\n    $cur_let = substr($rev, $cur_let_index, 1);\r\n    for ($i = 0; $i < 26; $i++)\r\n    {\r\n      $new_let = substr($alphabet, $i, 1);\r\n      if ($cur_let eq $new_let)\r\n      {\r\n         next;\r\n      }\r\n      substr($rev, $cur_let_index, 1) = $new_let;\r\n      $wordcount++;\r\n      $wordlist[$wordcount] = $rev;\r\n      substr($rev, $cur_let_index, 1) = $cur_let;\r\n    }\r\n  }\r\n  # Now @wordlist contains a list of 25 * length($word) words, each a reversal-\r\n  # with-one-letter-changed from $word. So go through each line of the dictionary\r\n  # file and compare with each of those words...\r\n  @foundwords = (do lookup(@wordlist));\r\n  foreach $word (@foundwords)\r\n  {\r\n    print \"$word\\n\";\r\n  }\r\n}\r\n# The 'lookup' function takes a list of word candidates and returns a list of\r\n# those candidates which are found in the given word file.\r\n#\r\n# Assumption is that the word-list file contains one word per line, each line\r\n# (including final line) terminated by a return. Words need not be in any\r\n# particular order, though if we assume alphabetical, routine could be smarter.\r\n#\r\n# There's probably a nicer way to do this in Perl, but I didn't see it offhand.\r\nsub lookup\r\n{\r\n  @found = ();\r\n  $foundwordcount = 0;\r\n  @words = @_;  # passed parameter list\r\n  open(WORDFILE, \"$wordfilename\");\r\n  while ($nextword = <WORDFILE>) # get the next line from the word file\r\n  {\r\n    chop ($nextword);\r\n    foreach $to_find (@words) # iterate over all candidate words\r\n    {\r\n      if ($to_find eq $nextword)\r\n      {\r\n        $found[$foundwordcount++] = $nextword;  # add to list of actual words\r\n      }\r\n    }\r\n  }\r\n  close(WORDFILE);\r\n  return @found;\r\n}\r\n%%%\r\n<\/pre>\r\n\r\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":5,"featured_media":0,"parent":18647,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-18652","page","type-page","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.kith.org\/words\/wp-json\/wp\/v2\/pages\/18652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kith.org\/words\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.kith.org\/words\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.kith.org\/words\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kith.org\/words\/wp-json\/wp\/v2\/comments?post=18652"}],"version-history":[{"count":5,"href":"https:\/\/www.kith.org\/words\/wp-json\/wp\/v2\/pages\/18652\/revisions"}],"predecessor-version":[{"id":18657,"href":"https:\/\/www.kith.org\/words\/wp-json\/wp\/v2\/pages\/18652\/revisions\/18657"}],"up":[{"embeddable":true,"href":"https:\/\/www.kith.org\/words\/wp-json\/wp\/v2\/pages\/18647"}],"wp:attachment":[{"href":"https:\/\/www.kith.org\/words\/wp-json\/wp\/v2\/media?parent=18652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}