Find the alphabetic word product
1 view (last 30 days)
Show older comments
If the input string s is a word like 'hello', then the output word product p is a number based on the correspondence a=1, b=2, ... z=26. Assume the input will be a single word, although it may mixed case. Note that A=a=1 and B=b=2.
23 Comments
Walter Roberson
on 22 Dec 2018
Recursion.
Create a function factorize that accepts a number N and is responsible for outputing a cell array of all of the unique ways of factoring the number with factors between 2 and 26. It can do this by selecting looping through all of those possible factors L, and at each step testing whether the factor L divides the number N, and if so then recursively call factorize to factor the N/L and then to take the cell array that is returned and put L at the beginning of each entry; continue on for all of the possible factors L, then return the augmented entries.
At any one point, the function is only responsible for finding one more factor, and calls itself on the smaller value N/L . By induction, as each step involves smaller values, then you will eventually get down to things simple exact factors. Note, though, that that induction does not work if you permit 1 as a value (corresponding to 'a'): you can put any number of 'a' in without changing the product: tttt has the same product as atataaaaaataaaaaaaaaaaaa .
You will find plenty of combinations that multiply out to 1E6. That isn't the hard part. The hard part is identifying whether each of the combinations is a valid English word. Like why isn't jetbedy a valid solution?
Accepted Answer
Rik
on 18 Dec 2018
See the code below for some more guided hints in an almost finished form. You don't need to call the input function btw. You can call the function by using e.g.
word_product('hello')
If you see a function you don't know, read its documentation. For most common tasks there exists a Matlab function. As an example: the sum function will calculate the sum of an array. Generally a combination of Google, the documentation, and guesses will get you there.
function p=word_product(s)
%convert a word to a ceasar cypher product
%(i.e. convert letters to numbers (a=A=1, b=B=2, etc), and calculate the
%product of these numbers)
%convert all letters to lowercase so we don't need to worry about uppercase
s=SomeFunction(s);
%Now convert the letters to numbers
%This can be done with a tedious lookup for all 26 cases, but this is
%Matlab, there is probably an easier way; and luckily there is.
s=double(s);
%This is not yet correct (nor technically even needed for the next step),
%so you will have to do an extra edit.
%Check out what double() does to char arrays (run in command window):
%double('a')
%double('ab')
%Now we can calculate the product:
p=SomeOtherFunction(s);
end
0 Comments
More Answers (1)
kushwantkumar chimmiti
on 2 Mar 2020
function product = word_product(string)
small_alphabet = char(97:122);
large_alphabet = char(65:90);
product = 1;
alphabet_value = strings(length(small_alphabet),2);
for i = 1:length(small_alphabet)
alphabet_value{i,1} = small_alphabet(i);
alphabet_value{i,2} = large_alphabet(i);
end
for k = 1:length(string)
[order,~] = find(strcmp(alphabet_value, string(k)));
product = product*order;
end
end
0 Comments
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!