Need help for Cody Problem 42409: Divisible by 7

3 vues (au cours des 30 derniers jours)
Nafees Ul Haque Akhand
Nafees Ul Haque Akhand le 4 Mai 2021
Hi! I am new in Matlab programing. I try to learn this language through cody problem. But I couldn't solve this problem for a long time. It'll be a great help for me if you give me some solutions!
I have been trying this problem to solve for a several time but everytime it shows the same error. When I run this code in the Scratch Pad, I get the desirable result, but when i submit my code, it shows an error: "The server timed out while running and assessing your solution. Use the Run button to verify your code. If successful, submit again.If the error persists, contact the instructor.".
**For your information, in this problem I can not use direct functions to get the answer. I have to do everything manually. Please see the problem question first in the link below.
I am submitting my written code here and also the link of the Cody Probelm. Thanks in advance for your suggesitions!
x = num2str(n_str) - '0';
seven = [0 7 14 21 28 35 42 49 56 63 70 77 84 91 98];
l = length(x);
if l > 2
while l > 2
p = x(1:end-1);
p = num2str(p);
p(p == ' ') = [];
p = str2num(p);
s = p - x(end)*2;
x = num2str(s) - '0';
l = length(x);
end
else
s = str2num(n_str);
end
s
if any(seven(:) == s) == 1
tf = 1
else
tf = 0
end
Please tell me if there is anything wrong in my code or the arrangements.

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 4 Mai 2021
You are getting the error because your code took to long to process than the allowed time limit.
1 - The input is in the form of string so, use x = n_str-'0';
2 - You can get rid of the first set of if/else statements, and just use the while loop. Your code will work the same.
3 - You can improve the speed of your while loop by substituting statements which do the work quickly.
x=n_str-'0';
while length(x) > 2 %Instead of defining 'l' as a variable, directly use length(x).
p = polyval(x(1:end-1),10);
s = p - x(end)*2;
x = num2str(s) - '0';
end
%defining variable takes memory to be stored and needs memory to be calculated at every loop iteration
If you want to learn MATLAB, Cody is not the best place to do so. Cody is meant for practicing/applying the skills you learnt. I would suggest you start with MATLAB Onramp course.
  3 commentaires
Dyuman Joshi
Dyuman Joshi le 4 Mai 2021
While your updated code is faster than the previous one, it still takes a significant time to process for large inputs such as in the Test case 16. Your code still takes more time that the time limit. Also, polyval() is not accurate for large numbers (the limit of double precision).
I suggest you take another approach to the problem as mentioned in the problem statement. Feel free to ask any queries you might have.
Nafees Ul Haque Akhand
Nafees Ul Haque Akhand le 4 Mai 2021
Sure, I will try some other methods. Thanks for your help and sure will take the Onramp course you suggested.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by