Each number on telephone keypads, except 0 and 1, corresponds to a set of uppercase letters as shown in this list: 2 ABC, 3 DEF, 4 GHI, 5 JKL, 6 MNO, 7 PQRS, 8 TUV, 9 WXYZ Hence, a phone-number specification can include uppercase letters and

REMAINING QUESTION: digits. Write a function called dial that takes as its input argument a char vector of length 16 or less that includes only these characters and returns as its output argument the telephone number as a uint64. Here is the input and output for one example of a call of the function: Input: '1FUNDOG4YOU' Output: 13863644968 You can assume that a phone number never starts with 0. If the input contains any illegal characters, the function returns 0. You are not allowed to use the built-in function strrep You can see code below. Do you know how one can add number 7 'PQRS' and 9 'WXYZ' and 8 'TUV'. THANKS

3 Comments

@Wasi von Deutschland: this is your homework, not ours.
My code that's incorrect. Could you tell me where am I going wrong *
function out = dial(in_char)
abc = ('A':'Z');
d = ['#*','0':'9',' '];
if all(ismember(in_char,[abc,d]))
ii = [' ',sprintf('%d',kron(2:9,[1 1 1]))];
[lo,idx] = ismember(in_char,abc);
out = in_char;
out(lo) = ii(idx(lo));
else
out = [];
end
end*
Stephen I know but I have problem solving it that's why I need your help, feedback and of course recommendations.

Sign in to comment.

Answers (3)

A much simpler solution (based on my comment to an earlier question):
function out = dial(inp)
dig = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
vec = '012345678922233344455566677778889999';
[~,idb] = ismember(inp,dig);
out = sscanf(vec(idb),'%lu');
end
(to which it is easy to add input checks), and tested:
>> dial('1FUNDOG4YOU')
ans =
13863644968

14 Comments

Could you please explain, what is ''ida'' in the above mentioned code? There is no value assigned to this variable and is being used.
Lia = ismember(A,B,'rows') treats each row of A and each row of B as single entities and returns a column vector containing logical 1 (true) where the rows of A are also rows of B. Elsewhere, the array contains logical 0 (false).
[Lia,Locb] = ismember(A,B) also returns an array, Locb.
Generally, Locb contains the lowest index in B for each value in A that is a member of B. Values of 0 indicate where A is not a member of B.
====
In other words, ida contains a logical vector telling you whether each character in inp occurs at all in dig
"Could you please explain, what is ''ida'' in the above mentioned code? There is no value assigned to this variable and is being used."
Actually there was a value being assigned to ida but it was not being used. So I removed ida.
stephen actually it gives me vector unit64 but I need number not a vector. I tried str2double and str2num but it isn't correct. thanks
Feedback: Your function performed correctly for argument(s) '1FUNDOG4YOU'
Feedback: Your function performed correctly for argument(s) '12AQZ2'
Feedback: Your function performed correctly for argument(s) '1234567890'
Feedback: Your function performed correctly for argument(s) 'ABCDEFGHIJKLMNOP'
Feedback: Your function performed correctly for argument(s) 'QRSTUVWXYZ'
Feedback: Your program made an error for argument(s) '(615)123-4567'
Your solution is _not_ correct.
@Wasi von Deutschland: thank you for your comment. As you do not ask any question, I guess you made this comment just to provide some feedback that my function works correctly. Thank you.
I notice that your function failed for an input which includes illegal characters that you did not mention at all in your original question. Do you have a complete specification for the accepted input strings?
I thank you again for providing that feedback.
"If the input contains any illegal characters, the function returns 0"
So the function Stephen provided needs to be enhanced slightly to reject illegal characters.
Hint: instead of throwing away the first output of ismember(), examine it.
Should I use ''if statement'' such as
if inp ~= '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
out=0;
or can you give me a hint how it can be solved
That test would almost certainly fail. If inp is a character vector, then ~= would be comparing each inp(K) to the K'th character of '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', and it would be an error for inp to not be the same length as '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' unless inp happened to be exactly 1 character long. If inp is a string object (R2016b or newer) then the test would check whether the string object was exactly the string '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', which it would not be. If your intent was to compare entire strings at a time, you should be using strcmp().
But your intent should not be to compare entire strings.
I already gave you a big hint on how to solve the problem:
"instead of throwing away the first output of ismember(), examine it."
[look_at_this_output, idb] = ismember(inp,dig);
@Wasi von Deutschland: the specification in the assignment clearly states "...Hence, a phone-number specification can include uppercase letters and digits. Write a function called dial that takes as its input argument ... that includes only these characters ...", yet the example you have shown contains other characters: do they want parentheses accepted or not? You should request clarity from the person who wrote the assignment.
Stephen I'm sorry but if you have a look at 'REMAINING QUESTION' it is clearly stated that If the input contains any illegal characters, the function returns 0. You are not allowed to use the built-in function strrep. In a nutshell They do not want accept parentheses accepted. Thanks
@Wasi von Deutschland: I'm sorry but if you have a look at MY ANSWER it is clearly stated "to which it is easy to add input checks": In a nutshell that was to remind you to put in some effort and figure out the remaining 1% of this task. Have you made any effort to do this yet? Walter Roberson has already given you a big hint on how to do this. Twice, in fact. Did you put in any effort yet?
The tone of your last comment is "do this for me!" (you do not ask anything, and imply that I should finish your homework for you. But why do I have to do your homework for you?)
Please put in some effort. Look at Walter Roberson's comment. This is the easiest part of the task. Will you give it a try?
@Stephen Cobeldick: Thanks, this was a very elegent solution.

Sign in to comment.

Here is a sample:
function asNumbers = convertPhoneNum(asLetters)
% Make a cell array where each cell represents the letters for a number on the keypad.
lettersByIndex = {'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ'};
% We will build up this final string as the output.
asNumbers = ''; % Preallocate enough space to put the answer in.
% Iterate through the input
for i=1:length(asLetters)
% Check if this character is already a number.
if isstrprop(asLetters(i), 'digit')
asNumbers(i) = asLetters(i);
% If this character is not a number, find the corresponding
% alphabetical representation in the cell array.
else
% Sample Code:
%{
for j=1:length(lettersByIndex)
% Check if the character can be found in that cell
if ~isempty(strfind(lettersByIndex{j}, asLetters(i))) % If letter is found in that group of letters
asNumbers(i) = num2str(j + 1);
end
end
%}
end
end
@Stephen, Thanks for your answer. That's a very simple and beautiful solution. I am very new to Matlab (a few weeks) and I had solved the problem in a cumbersome way. You solution is very elegant.
@Wasi von Deutschland I think the answer would be a bit late for you, but might help someone in future. The answer (edited on 3rd June) by Stephen is almost 99% there. From the questions, there is only one thing remaining that needs to be handled: If there is any special character, return 0. Note that this will be uint64 (and not just say out = 0)
% Check if there are any invalid characters. If yes, return 0.
if sum(~ismember(inp,dig))>0
out = uint64(0);
return;
end
Once you plug it in, it should work. Hope this helps.

5 Comments

In your answer;
function out = dial(inp) dig = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; vec = '012345678922233344455566677778889999'; [~,idb] = ismember(inp,dig); out = sscanf(vec(idb),'%lu'); if sum(~ismember(inp,dig))>0 out = uint64(0); return; end end but Feedback: Your program made an error for argument(s) '(615)123-4567'
Your solution is _not_ correct.
In order to solve the above problem, what do I do?
@SULE SAHIN: first read and understand the requirements, then read, understand and test the code to find why it does not meet those requirements, and then the solution will usually be clear.
@Stephen Cobeldick I find anything please give some hints
@SULE SAHIN: The string '(615)123-4567' contains some non-digits characters. What are the requirements for handling these characters: '()-' ? What does your homework tell you to do if there are non-digit characters?
Once you answer that, you can decide how to change the code to suit.
@Stephen Cobeldick I do anything, ı suppose that I will not solve this question Thank you

Sign in to comment.

Asked:

on 27 May 2017

Commented:

on 7 Nov 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!