Submit your work in the form of a multiple .m files. Please put your first initial and last initial (in lowercase letters) before each script or function name. If you are submitting a script file, start your file off with the command clear
and please clearly label and describe each problem with comments in your file.
Write a function called PrimeNumberSort
which asks the user to input ten different integers (individually) and returns these numbers sorted into two vectors. The first vector should be called $\mathbf{pr}$, which contains all prime numbers and the other should be called $\mathbf{npr}$, which contains all non-prime numbers.
Write another function called EvenNumberSort
which asks the user to input ten different integers (individually) and returns these numbers sorted into two vectors. The first vector should be called $\mathbf{even}$, which contains all even numbers the other should be called $\mathbf{odd}$, which contains all odd numbers.
Use the command strcmp
to ask the user if they would like to input another ten integers. What types of restrictions should you put on the inputs of these functions? How can you ensure that the user is inputting the correct type of information? Think carefully about how you structure these programs because it will help us in the future.
Since MATLAB© is a Linear Algebra software, it treats strings as specially flagged row vectors of character codes. Thus, you can treat them as any other vectors and thus certain operations, such as concatenation, still have similar dimension requirements.
For example, the code
str = 'Hello World';
str(1:5)
will return the first five elements of the string.
The command charcodes = double(str)
will convert the string into row vector $\mathbf{charcodes}$ containing its corresponding character codes (see this reference for the corresponding ASCII character codes) and the command char(charcodes)
will convert the character codes back into the string.
Write a short script called CharCodes
that translates your favorite quote from a string into a vector of its corresponding ASCII codes. Next, write me a brief message as a vector of ASCII characters and translate them into a string.
Additionally, one can create vectors (or matrices) of strings using the syntanx
['Hello',' ','world']
You can do much more with strings! Type help strfun
to see what other built-in commands MATLAB© has. The function sprintf
is particularly useful if you want to write data to a string.
Write a function called StringSort
which asks for a user to input a string of any length, identifies all the unique characters in the string using the command unique
and then counts the number of occurences of each unique character using the command count
. The function should return this information to the user as a message such as "The letter e appears in your text 3 times." for each unique character.
Test your funcion on your favorite quote. What was the output? What are the most common letters in the English language?
There are many variations of this theorem, but the most basic version goes something like this:
Assume that there is a monkey typing on a typewriter for an infinite amount of time. Then, almost surely, at some point the monkey will type any given text, such as your favorite novel or Shakespeare's Hamlet.
In fact, theoretically the monkey should type EVERY finite string of text an infinite number of times. The probability of this occuring however, is extremely low. One can show that if we had enough monkeys to fill the observable universe and that they were all typing, the probability that they would complete a work such as Hamlet is so tiny that the chance of it occuring during a period of time hundreds of thousands of orders of magnitude longer than the age of the universe, is extremely low (but not zero).
Unfortunately, we do not have a monkey, typerwriter, or an infinite amount of time to test this, but we can model a simplification using MATLAB©.
So, consider the following code.
function [monkey,attempts,elapsedtime] = monkeytype(str,iter)
alphbt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';
m = length(alphbt);
n = length(str);
t = iter;
k = zeros(1,t);
elapsedtime = zeros(1,t);
for i = 1:t
tic
monkey = 0;
while strcmp(monkey,str) == 0
monkey = alphbt(randi(m,1,n));
k(i) = k(i) + 1;
end
elapsedtime(i) = toc;
end
attempts = k;
Explain what each line of code is doing and describe why this is a simplified version of the theorem. Using $\mathbf{iter}=1$, examine the monkey's typing ability with the word banana by testing the sequence of strings; 'b', 'ba', 'ban', 'bana', ... separately. What do you notice about the number of attempts the monkey makes and the elapsed time it takes for each added letter? (hint: you may not want to try more than four letters unless you have a LONG TIME or a FAST computer to play with it.)
Next, modify this function so that the user can input the number of random characters the monkey types for each attempt and then use the command contains
to look for a given string in the collection of random characters.
Finally, if we wanted to give the monkey an even better chance of typing English words, we could consider the occurrence frequency of letters in the English language. How might we modify our code to modify the probability that the monkey is more likely to type letters based on their frequency?