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
조회 수: 382 (최근 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!