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
332 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
Stephen23
2017-5-30
编辑:Stephen23
2017-6-3
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 个评论
Stephen23
2017-6-5
编辑:Stephen23
2017-6-5
@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?
Deepak Sharma
2017-9-11
编辑:Deepak Sharma
2017-9-11
@Stephen Cobeldick: Thanks, this was a very elegent solution.
J Philps
2017-5-30
编辑:Walter Roberson
2017-6-3
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
0 个评论
Deepak Sharma
2017-9-11
@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 个评论
Stephen23
2017-11-7
编辑:Stephen23
2017-11-7
@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.
SULE SAHIN
2017-11-7
@Stephen Cobeldick I do anything, ı suppose that I will not solve this question Thank you
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!