Assignment 4. Strings


Exercise 1

Read the documentation of the string functions at https://docs.julialang.org/en/stable/stdlib/strings/. You might want to experiment with some of them to make sure you understand how they work. strip and replace are particularly useful.

Exercise 2

The following functions are all intended to check whether a string contains any lowercase letters, but at least some of them are wrong. For each function, describe what the function actually does (assuming that the parameter is a string).


In [3]:
function any_lowercase1(s)
    for c in s
        if islower(c)
            return true
        else
            return false
        end
    end
end


Out[3]:
any_lowercase1 (generic function with 1 method)

In [4]:
function any_lowercase2(s)
    for c in s
        if islower('c')
            return "true"
        else
            return "false"
        end
    end
end


Out[4]:
any_lowercase2 (generic function with 1 method)

In [5]:
function any_lowercase3(s)
    for c in s
        flag = islower(c)
    end
    flag
end


Out[5]:
any_lowercase3 (generic function with 1 method)

In [6]:
function any_lowercase4(s)
    flag = false
    for c in s
        flag = flag | islower(c)
    end
    flag
end


Out[6]:
any_lowercase4 (generic function with 1 method)

In [7]:
function any_lowercase5(s)
    for c in s
        if !islower(c)
            return false
        end
    end
    true
end


Out[7]:
any_lowercase5 (generic function with 1 method)

Exercise 3

A Caesar cypher is a weak form of encryption that involves “rotating” each letter by a fixed number of places. To rotate a letter means to shift it through the alphabet, wrapping around to the beginning if necessary, so ’A’ rotated by 3 is ’D’ and ’Z’ rotated by 1 is ’A’.

To rotate a word, rotate each letter by the same amount. For example, "cheer" rotated by 7 is "jolly" and "melon" rotated by -10 is "cubed". In the movie 2001: A Space Odyssey, the ship computer is called HAL, which is IBM rotated by -1.

Write a function called rotate_word that takes a string and an integer as parameters, and returns a new string that contains the letters from the original string rotated by the given amount.

But beware: the numeric codes for upper case letters are different.

Exercise 4*

This question is based on a Puzzler that was broadcast on the radio program Car Talk (http://www.cartalk.com/content/puzzlers):

Give me a word with three consecutive double letters. I’ll give you a couple of words that almost qualify, but don’t. For example, the word committee, c-o-m-m-i-t-t-e-e. It would be great except for the 'i' that sneaks in there. Or Mississippi: M-i-s-s-i-s-s-i-p-p-i. If you could take out those i’s it would work. But there is a word that has three consecutive pairs of letters and to the best of my knowledge this may be the only word. Of course there are probably 500 more but I can only think of one. What is the word?

Exercise 5*

Here’s another Car Talk Puzzler (http://www.cartalk.com/content/puzzlers):

“I was driving on the highway the other day and I happened to notice my odometer. Like most odometers, it shows six digits, in whole miles only. So, if my car had 300,000 miles, for example, I’d see 3-0-0-0-0-0. “Now, what I saw that day was very interesting. I noticed that the last 4 digits were palindromic; that is, they read the same forward as backward. For example, 5-4-4-5 is a palindrome, so my odometer could have read 3-1-5-4-4-5. “One mile later, the last 5 numbers were palindromic. For example, it could have read 3-6-5-4-5-6. One mile after that, the middle 4 out of 6 numbers were palindromic. And you ready for this? One mile later, all 6 were palindromic!

“The question is, what was on the odometer when I first looked?”

Write a Julia program that tests all the six-digit numbers and prints any numbers that satisfy these requirements.

Exercise 6*

Here’s another Car Talk Puzzler you can solve with a search (http://www.cartalk.com/content/puzzlers):

“Recently I had a visit with my mom and we realized that the two digits that make up my age when reversed resulted in her age. For example, if she’s 73, I’m 37. We wondered how often this has happened over the years but we got sidetracked with other topics and we never came up with an answer. “When I got home I figured out that the digits of our ages have been reversible six times so far. I also figured out that if we’re lucky it would happen again in a few years, and if we’re really lucky it would happen one more time after that. In other words, it would have happened 8 times over all. So the question is, how old am I now?”