A sliding puzzleCS109 Programming ProjectsLCD display and clockIs this text readable?

Is this text readable?

The human brain has amazing abilities. Here is one interesting fact: If you take a text and randomly shuffle the internal letters of each word (that is, you keep the first and last letter intact, but completely mix up the other letters of the word), then one can almost always just read the text.

Our task is to write a script readable.kts that takes a filename as an argument, and prints out the file while shuffling the internal letters of each word. For example:

$ kts readable.kts input.txt 
In this cuosre we will use the Ktoiln priromangmg laguange.

In my exnepciree, sdentuts doevelp a sltye of pngarormmig in teihr
frist prmanrigmog lngaugae. Tehy find it diufciflt to laren a
dirnefeft slyte ltear, even if tehy stiwch to a dirnfeeft
lanaugge. Tihs manes taht suentdts sluohd strat ealry to use a mrdeon
lagaugne taht ecaruenogs writnig clean and elngaet code, and sptorpus
a good obcejt-oeritend and fnitoacunl sltye.

We will toeerfrhe use Kotiln in this csuroe. Ktolin is flluy
catbmploie with Jvaa, so you can use it for ainytnhg that can be done
in Jvaa, but it is a much mroe mdoren, much mroe rebldaae, and much
mroe sesnbile lnaaggue. We wlil aslo epnxeriemt wtih cpmionilg Kliton
suorce to Jvcsraipat, so you can run yuor pgrarmos idnsie a web
bsroewr, and will even wtrie slmal apps taht can run on Arnidod

Msot suttends will stlil have to lrean C++ and/or Jvaa ltaer driung
their suetids—but I bleveie that afetr gniettg prrmongimag prticcae in
Koitln, they wlil be albe to wtrie betetr cdoe in those leuagnags as

To make your life easier, start with the following skeleton (readable.kts):

// check if ch is a letter
fun isLetter(ch: Char) = ch in 'a'..'z' || ch in 'A'..'Z'

// shuffle(s) returns a randomly shuffled copy of the string s
fun shuffle(s: String): String {
  val l = s.toMutableList()
  return l.joinToString("")

fun reorderLetters(s: String) {
  var i = 0
  while (i < s.length) {
    // modify this space

if (args.size != 1)
  println("Usage: kts readable.kts <filename>")
  java.io.File(args[0]).forEachLine { reorderLetters(it) }

Don't change the existing code, except that you need to fill in the while-loop in the function reorderLetters. (It is okay to add additional functions.)

The skeleton already takes care of checking that there is exactly one parameter on the command line, opening a file with that name, and calling reorderLetters for each line of the file.

Inside reorderLetters, look at the letters of the string s one by one. If it is not a letter, print it out immediately using print(s[i]). If it is a letter, you will need a second loop that walks ahead to find the last letter of the word. Print the first letter, print the shuffled internal letters, and print the last letter of the word. Make sure you handle one-letter and two-letter words correctly.

You can test with the input file input.txt.

A sliding puzzleCS109 Programming ProjectsLCD display and clockIs this text readable?