7 kyu
Square Every Digit
122,100 of 324,573MysteriousMagenta
Loading description...
Mathematics
Fundamentals
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Hi, I'm unable to test my code. Getting importing error in the testing script : Traceback (most recent call last): File "/workspace/default/tests.py", line 2, in from solution import square_digits ImportError: cannot import name 'square_digits' from 'solution' (/workspace/default/solution.py)
OP has figured it out. This is not an issue.
Sorry, I had this issue in a different Kata and posted it here by mistake.
testing number 1 should be added .. it might failed with some solution based on log to find the number of digit (since log(1) = o)
This comment has been hidden.
I'm just gonna leave this here. Translation Dart
im having a problem where my code passed all the tests but in the attempt section each time i run the tests it shows different number of passed and failed test each time. any reason or solution for that?
Your code only passes all the sample tests.
On running
ATTEMPT
your code is tested by multiple random tests, and it does not pass all of them.and by the way I un-flagged your comment as a spoiler because it did not discuss / reveal any solution concepts / code
yes but the random tests passing is absurd, like a test in ATTEMPT at once is failing but if i run ATTEMPT again, the same test is now passed,why is that?
It's not absurd because random tests are never the same test ... because, well, they are random. That's why your code passes a different amount of them on each run of the
ATTEMPT
.yep got it.thanks
This comment has been hidden.
Добрый день. Имею решение, но не имею возможности правильно его вставить для тесьтрования. Подскажите как это сделать. Good day. I have a solution, but I have no way to insert it correctly for testing. Tell me how to do this.
test your solution on all multiple randoms tests until you get it.
I am having problems with this kata in C language.
Example- My test will fail with the following message:
"for n = 2494392752, expected 4168116981449254, but got 0"
Despite showing n as a positive integer, this is seemingly not the case as I get a different, non-zero output when changing n to the absolute value of n. This would only make sense if n was somehow negative.
Additionally, for cases such as this, nothing is printing to the console, allowing me to debug the issue. There are no errors, and I have tried printing both the unsigned long n (unmutated from the function argument) as well as its necessary cast to int.
the input is an
unsigned
(32-bit) so it logically cannot be negative; the output is anunsigned long long
(64-bit). the tests do show you the input, expected and actual values. your code does not take care of avoiding integer overflow.Groovy Translation
Approved
The Mods here are so wonderful!
I have finished the code, but I cant get the tester to accept the number = input() command. Any help on how to get this to run?
This is a
question
, not anissue
. Please only post as anissue
if there is a problem with the kata code, not your code.Your code has several problems, one critical one is that you do not
print
the final value, but instead shouldreturn
it from the function.And using
input()
does not actually apply here; your input is already given as a paramenter within the function declaration itself.My fault, thanks for the help
no problem
welcome to the site
happy coding
This comment has been hidden.
But your code returns a string
You're right lol thank you
Note: I did say there was a 0.01% chance i was wrong though. lol I am very new to coding, and shouldn't be sure of anything!
Hello to everyone! I'm new here. Could you please explain me where and how to put code to analyze it with the others?
If you get an answer, let me know. I would like to know this too!
Unfortunately, I didn't get an answer.
You may look at the solution tab to see others' solutions. Other than that, you can only subscribe to Codewars RED to be able to compare 1v1 stats with certain users.
this is 8kyu :D
This is too hard. Why this is 7 kyu? :(
Nope. This one is just right. Some 7kyu ranked most recent are slightly harder.You should solve more 7kyus to have a bigger sample space for rank comparison and assessment.
don't give up. You just need more practice. I feel like I am the slowest one here. But with consistancy, I am getting better and I felt this one should have been 8kyu.
Have you tried doing a lot of 'most completed' 8kyus before you came to this one?
If this is too hard, just stick to only 8kyus for a while.
my humble suggestion.
I had stuck to 8kyus for a while, once I got good, I gravitated to seven kyus and have been completing a lot of them as well
This comment has been hidden.
In Java: When using the sample test, it keeps failing but failing on a string as input? The kata says only accepts and returns an integer. Do I need to be cleaning the input? Text from the error here: Test Results: SquareDigitTest test For input string: "813611416183156161"
I am pretty new to CodeWars, I am returning an integer via return, not print.
It is unfortunately JUnit giving not very clear feedback in some situations, but the root cause of the error is in your code. Look at the stack trace:
in the line 4th from the bottom you can see that the error comes from the
Imteger.parseInt
, which is un turn called fromSquareDigit.squareDigits
, i.e. your solution. You have a bug in your code, which paired up with the poor test feedback.I changed the tests a bit in an attempt to make the failure messages clearer. They look better now, but I wonder if clear enough.
I'll take another look, thanks so much for the speedy reply. It shows my naivete that I didn't know the stack trace was that helpful in debugging. Thank you!
This comment has been hidden.
Nice Kata :)
I've forked the Haskell translation here to add random cases and clean a few things up.
So many solutions invalidated because they either handle
0
wrong, or rewrite the function signature to(Integral a) -> a -> a
..This comment has been hidden.
The function accepts an integer and returns an integer.
returns
0/10
I have tried for hours, and, for some reason, when the code reaches one of the tests, in the middle of the execution of do_test() function, the numbers reset to 0 without any reason. And i have not even reached the numbers over the "long long int" size
This comment has been hidden.
Nevermind. i just need to convert it into int XD
please remember to use the spoiler flag if posting code. (I have added the flag to your comment)
This one has kicked my butt in C. I don't know if I'm just overthinking it or what, but I've gotta come back another time
me too ;)
W's in the chatt!!!!
my solution works fine. there are some optimization needed but im happy with it.
Why doesn't my code work correctly?
OP solved it, closing
This comment has been hidden.
That's because in VSCode you pass a string as input and here a number is passed.
Don't forget to close the question if you got your answer.
Two differnt attempts, each with the correct output, both failed. Had to unlock solution to find convuluted answers. I hope I never get this as an inteview question.
Read the error message:
And this part of the description:
Your function is returning a string, not a number.
yh same thing here
This comment has been hidden.
i passed basic tests but attempt is failed wondering what mistake did i
Your current code doesn't work, this is the error I get with it:
This comment has been hidden.
the code you just posted is wrong because of how you assign to
t
. look carefully to see which digits are squared and which are notThis comment has been hidden.
print(t)
for each iterationI get the following message with test and attempt, I have not been able to solve any kata training because of the same issue, please help
Basic Test Cases Log 811181 None should equal 811181
The
ISSUE
label is for provable kata issues. Please use theQUESTION
label when asking for help. See https://docs.codewars.com/training/troubleshooting/#post-discourse.This was already explained to you by another member of the community. You need to return a value instead of printing it. See https://docs.codewars.com/training/troubleshooting/#.print-vs-return.
Seeing you're new to codewars, I highly recommend reading this article.
Same here. Using JavaScript by the way.
Python. Hint for you: data type str can be used in loops, for example, with 'for'. Then, when you iterate, proceed processing your data.
This comment has been hidden.
Read the error message:
Your code is returning a wrong data type. Not a kata issue.
I think there is an issue with this kata. It says "expected +0 to equal [correct number]," every time, but when I copied my exact code into vscode and tested it myself, it gives the correct answer. Every time.
No, but there are a lot of issues with your code. Both lines of
num.toString...
andNumber...
do absolutely nothing, because you don't assign those values anywhere. Those lines on their own change nothing. As a result, your loop never runs, because "num" is a number, not a string (and doesn't have.length
property).This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
2 ms
Really enjoyed solving this Kata in JS.
This comment has been hidden.
little bit tough
This comment has been hidden.
This comment has been hidden.
I would say that you need to add .to_i to your result.join() line. This will convert the string you have created into an integer
This comment has been hidden.
Because you return the wrong variable and you have to check its type before returning it.
(I flag your code as a spoiler since it's almost right.)
halp mahhh😭😭😭
Typo at the end of the second example in the description - should say "(49-36-25)"
Fixed.
I'm receiving the following error when running 'test' or 'attempt':
Traceback (most recent call last): File "/workspace/default/tests.py", line 2, in from solution import square_digits ImportError: cannot import name 'square_digits' from 'solution' (/workspace/default/solution.py)
Correct me if I'm wrong, but this issue has nothing to do with my code, and seems to prevent my solving. Any advise??
Issue has everything to do with your code, because the name of the solution function you submit does not match the name of the solution function expected by tests. At some point, you changed the name of solution function to
square_digits1
.This comment has been hidden.
Your solution does not return numbers, it returns strings.
I think it works incorrectly also on your machine, you just dont test it thoroughly enough.
Your solution is buggy. Bugs in your solution are not a kata issue.
This comment has been hidden.
Did you test your code in your VSCode with
n=0
?Cheers! Again, I'm nonplussed at the fact that this solution is only worth 2 points! Crazy, seems pretty damn hard in C.
This is unfortunately a disadvantage of the fact that every Codewars kata can have only a single rank, shared among all languages. Sometimes it happens that a kata which is very easy in JS or Python is translated into C and is significantly more difficult in a language of lower level. Unfortunately, we have to live with this. But hey, choosing C to solve a kata we know what we signed up for, right? :)
Having said that, I would recommend taking a look at other solutions after completing the kata. While indeed it might be not totally easy in C, the solution could be much simpler than your current code. Study other solutions, learn something new!
Cheers!
Yes, I always look after I've finished! Someone always has a much more elegant solution! My solution is a product of me just wanting to get the job done and see what everyone else did. On another note, I thought 7kyu kata were 3 points apiece, but I only got 2 points for this one. What's the reason for that?
Same here, all the tests went well except one. It says "3 should be 4" with the values (1000,2.0,50,1214) but it's wrong, because after 3 years p = 1214.228. What am I missing here?
well, somehow in Java one of the input strings is "3249240124013249" which is too big for an integer. and since the method takes in the int value idk what to do in this situation. changing the input type sounds too complicated for a 7 kyu kata.
This comment has been hidden.
Нет возврата значения из функции, ошибка в типе выходной переменной
This comment has been hidden.
Because of this: https://docs.codewars.com/training/troubleshooting/#expected-the-same Not a kata issue.
About how to post code in Discourse, read this: https://docs.codewars.com/training/troubleshooting/#post-discourse
I get : Basic Test Cases '811181' should equal 811181 '0' should equal 0
but isn't this wrong??
Your result returns a string while the actual answer should be an integer
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Your code is wrong, not a kata issue. Please read this: https://docs.codewars.com/training/troubleshooting
It says Test Case: 164 should equal 811181 when it should be 53. This is only in Python.
Not a kata issue, why are you using sum in your code? Please read the description again, that exact test is explained there.
Because it says concatenate all the squared digits together. So i summed all the digits.
simple math
This comment has been hidden.
you are returning only the first value of the list
y
. you need to find some other way to concatenate all the results ofy
and print them out togetherWhen I use std::stoi it catches an exception in the unitttests. Is it illegal to use stoi?
Wondering the same thing I used stoi too. had a string variable add each sqaured number onto the string and return a stoi of that string. but I get error from an exception being caught. Not sure why this isn't allowed.
Exception occurs when the formatted string is not in the correct format (i.e, having leading or trailing spaces or non-digit characters)
OP solved it, closing...
This comment has been hidden.
Your code returns a string and the tests expect a number - your code is wrong.
A good indication that it's your code that is wrong and not the kata is the number of times the kata has been completed in your language.
NunoOliveira, thank you, I had same problem as OlaniyanEniola. Solve it.
This comment has been hidden.
Not a kata suggestion, please don't post solutions in Discourse.
closing
Doing in Java. Why descripton says inputs and returns int but testcase says input of String?
That's the Java's initial code and the sample tests, both use ints, where do you see strings?
only you shoud know how to convert string to intiger and othrwise!
This comment has been hidden.
Please mark your post as having spoiler content next time.
Your function returns a string.
ty
Python ValueError: invalid literal for int() with base 10: '' why is this happening help me pls
Your approach needs some debugging as it's not 100% correct yet, but this current mistake is probably due to the fact that you are not returning anything - replace
print(number)
byreturn(number)
at the end of your solution.when solving in python, an error occurs related to the importer of the solutions module "ImportError: cannot import name 'square_digits' from 'solution'" help me please
Don't rename the function. This is the initial code:
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting#post-discourse
And the function's name should be
squareDigits
, notsquareDigit
C# only tests for numbers between 0 - 1000
I could easily extend the range of the random tests to 1-9999, extending to more than that, could surpass the max-limit of integers and a change in the output value would break all valid solutions. Is that enough?
Given that the description says:
I've done what I said before, the range is now from 1 to 9999 in random tests.
Thanks, closing!
These languages only test for numbers between 1000 - 9999
This comment has been hidden.
Please don't post solutions in Discourse, this is not a kata suggestion (that would be some suggestion on how to improve the kata itself). Use Solutions section to discuss solutions and you can see there better solutions from other people and learn from it.
Apologize, I'm new, will take note and thanks
Interesting task and a variety of solutions
Nice!
This comment has been hidden.
Read the error message:
Your function returns strings, not numbers.
This comment has been hidden.
Because
replaceAll
requires Node v15+. Not a kata issue. Read the error message next time to help you find out what the problem is.Thank you for your help. My bad. I will try another way :)
This comment has been hidden.
This comment has been hidden.
This seems to be the case for about half the translations?
Fixed for Python, which other languages should be fixed?
Reraised above with more detail
D translation
Approved!
This comment has been hidden.
Your code is wrong, try again.
Please can you kindly help me out.
Scala Translation.
looks good to me
Approved by someone
(In Python) ImportError: cannot import name 'square_digits' from 'solution' (/workspace/default/solution.py) Why is this happening why I try to run the attempt?
Because you changed the function's name, it should be
square_digits
notsquared_num
Got it, thanks for the assist!
Lua translation!
(In Haskell) why are some cases in the tests and when submitting expecting a negative number ? squares are never negative.
An issue about that was raised 10 months ago, one is enough.
This comment has been hidden.
The function's name should be
squareDigits
notsquareDigit
, not a kata issue.This comment has been hidden.
Because
str(num)
returns a string, it doesn't change num var value in place.Why does it want us to double the negative input?
Why not?
I am using Java, Some Problem my code works fine on IDE. its complacing when I declare String str =""; with empty value
The same problem.
Hi @LipskyiAlex - welcome to Codewars;
I don't use Java, so I can't help specifically with your code, but you should checkout the following Troubleshooting document especially the section on "It works fine on my IDE".
Maybe there is some advice there that applies to your situation.
I was having the same issue, for me it worked to validate when the input is 0.
This comment has been hidden.
Please don't post solutions in Discourse, and if you post code somewhere (to ask for help for example), please use a spoiler flag (I put the flag for you this time).
Thank you, I am new here
This comment has been hidden.
Which error message?
This comment has been hidden.
A problem with your code is not a kata issue. Try using a new string.
This comment has been hidden.
sorry guys. Very silly but I think i was missing the line below:
return int(mystring)
This comment has been hidden.
Here you don't read the input from keyboard, you use the argument of the function, and also, your function should return the result, not print it to the console, read this: https://docs.codewars.com/training/troubleshooting/#.print-vs-return
Your function doesn't do what the kata asks either.
This comment has been hidden.
OP left the building, closing
This comment has been hidden.
The return value should be a number. You are returning a string. The rest is correct though.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
That's a problem with your code, not a kata issue, read this: https://docs.codewars.com/training/troubleshooting/
Find the sample test your code fails, fix your code.
Doesn't work with your solution either.
Test Results: Square_Every_Digit Sample_tests Caught std::exception, what(): stoi
Doesn't work with your solution either.
Test Results: Square_Every_Digit Sample_tests Caught std::exception, what(): stoi
What happens if
n
= 0?This comment has been hidden.
Please mark your post as having spoiler content next time. Also, read this: https://docs.codewars.com/training/troubleshooting
About your problem, read the note at the end of the description:
Then the error message you get:
You're not returning a number there.
Int -> String -> Int -> String -> Int
This comment has been hidden.
Its Math.pow not Math.Pow
maybe its cause pow gives double?
This comment has been hidden.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting#.print-vs-return
used return. thanks Chrono
This comment has been hidden.
why is my code not authorized ??
Read this: https://docs.codewars.com/training/troubleshooting/#works-but-no also I get this error message from your code:
Move it inside your function.
This comment has been hidden.
Read the error message:
You can't chain
parseInt
This comment has been hidden.
This is one of my first kata in haskell, I am not sure if this is a problem with the way the test is written or it is a limitation of the language, but each test should be it's own. I have spent several hours trying to understand why my code was failing in the first test when actually it was passing it and failing the second(-1)
This comment has been hidden.
Please use a spoiler flag.
This comment has been hidden.
Start by commenting out lines from bottom to top, and make sure each step really does what the comment says.
ok thanks, I will try that
This comment has been hidden.
This comment has been hidden.
Please use a spoiler flag next time. The code you provide is incomplete, there's an unclosed bracket.
COBOL translation (author inactive).
approved
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Do you know the difference between
'9414'
and9414
?one is a string and the other is an integer. I didn't pay attention to it >.<'
This comment has been hidden.
This comment has been hidden.
Not a kata suggestion, please, don't post solutions in Discourse.
This comment has been hidden.
Closing since C# tests are working fine! Read this
Please, NASM Translation (the author is inactive)
Approved
This comment has been hidden.
How does
None
(which is returned by your function) match expected value of 9414?Please read this paragraph, it contains answer to your problem: https://docs.codewars.com/training/troubleshooting#expected-the-same
OK, thanks. I searched for 'None', and I did find a section that looked like it might've been the part you're talking about. So, I tried to 'return' the value I was printing, but it did no good. Am I on the right track with using return, instead of print?
Did you do this:
return print(...)
?print
returnsNone
, return the number instead (removingprint
).In Haskell we may be given a negative number, this is out of the description and apparently there's nothing like that in other languages.
C translation (author gone)
Thanks :)
Hobovsky made a suggestion on your translation page just when I approved it, you may have a look.
done ;-)
This comment has been hidden.
Several issues that you could work on ;-) :
CONST
at each function call, so later calls will keep adding digits to the same stringCONST
in the wrong order : 123 becomes 321grand thank you very muuuch.
This comment has been hidden.
Your code is returning a string.
It seems that what being asked and the answer from test cases are different. Is the expectation from the instruction different from the test cases? I skipped this one
Not an issue, please post your code in spoiler and use question label ~~
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
why does it give me an error when i use list comprehension?
This comment has been hidden.
Because you forgot some parentheses and integers are not iterables.
so i wrote my code in JS and its working. It takes a number and than every single number squares.And returns a number with all squared. But i get an error.Error says that expected output not achived, wich is not true.what i run that input it gets correct result. and it is not letting me throught.IDK what i have to do HELP!
This kata has been completed more than 56000 times in JS. It's very unlikely that the kata has a problem, I'd rather bet your code has. You should presume on the contrary that your code is not working, though you don't see where it isn't. It's impossible to help you if you don't give more details. See if this can help you: Troubleshooting Your Solution.
when code exectued it dosen't accept normal values and ask for value to be equal to 9114
What's a "normal value"? Please read this: https://docs.codewars.com/training/troubleshooting
Javascript tests are fine:
This comment has been hidden.
Your code changes the order of the digits.
Ohh, ok. Thank you.
i keep getting "FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory"
It means you use an array or some container and you fill it so much that Javascript gets out of memory. There's probably an infinite loop in your code.
This comment has been hidden.
Which language? Hard to help you without knowing what you do exactly. You may post your code to receive more help (don't forget to add a spoiler tag to your comment if you do so).
This comment has been hidden.
Print the input and
res
.I don't quite get you.
here is what i'm getting
Your code fails with 0, fix it.
Thank you!
Elixir translation revised.
This comment has been hidden.
No, you changed the method name. It should be 'squareDigits', not 'squareDigit'. The issue comes from your code, not kata itself.
This comment has been hidden.
Your function returns a string instead.
It should be specified in the description what the ranges for n are, everyone is just assuming it will never have an input that could overflow.
This comment has been hidden.
I don't quite understand how this works. Why isn't 'numb' being overwritten every time you iterate through the for loop?
This comment has been hidden.
OP left the building, closing...
This comment has been hidden.
This comment has been hidden.
You shouldn't use
input
here and the function's name should besquare_digits
Ok, Thank you so much
This comment has been hidden.
Terrific. But the discourse isn't the place to post solutions. That's what the solutions page is for. I marked your post as a
spoiler
.This comment has been hidden.
Please don't post your solution in the discourse. If you must, use the
spoiler
flag.The tests should generally guide you when they fail; you'll see what your result was and what the expected output was supposed to be. It usually helps to
Console.Log
the input so you see what input made your solution fail.looks like u solved it
I had to go back to find the Kata I was working on is there a way to go back to your Kata if you returned to your home page?
In your profile, go to solution and unfinished, you should be able to see it if you submit once but haven't pass the test
I am very new to programming, but I am trying to figure it out. My code is really rudimentary, but I do not see an array of numbers to loop through or go much off of. So I wrote a basic function that should square any number that is called as an argument. However, it obviously is not correct. I am lost and need help.
function squareDigits(num){
let total = num * num; return total; }
squareDigits(9);
You read the instructions wrong. You need to square every digit of the number and combine them (no summing involved). Look at examples for more details.
Ok Thanks I see what you are saying.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting#post-discourse
Thanks for the Heads up
Test passed . however give strange result when number start with 0 ( 017 gives 125) or (012 gives 10)
Numbers with a leading zero are (in some programming languages) interpreted as octal (base 8). 17 in the octal number system is 15 in the decimal numer system (base 10). squareDitigts(15) gives 125, since 1² = 1, and 5² = 25.
You can use the hexadecimal system by prefixing the number with 0x, and binary with 0b. At least in JavaScript. 017 = 0xf = 0b1111 = 15.
Thanks . really good
This comment has been hidden.
And please mark your post as having spoiler content next time.
Thank you for your respsonse. And yes, I will keep it in mind for all future posts.
This comment has been hidden.
Note the quotes around the first number, your code is returning a string instead of a number.
Ah! Tricky tricky, thanks, this goes to show I really need to slow down and read haha.
I completed the task, it works in my IDE and on the tests but when it's an attempt it says it's not equal even though the log shows the correct number(output is an int). Anyone know why this is?
https://docs.codewars.com/training/troubleshooting/
Not an issue to the kata.
I solved it in the end, it's because I was printing my solution rather than returning it
Coffeescript translation
R translation
(author inactive)
Thanks :)
This comment has been hidden.
f
Also known as the story of the ambiguously typed value. Now you see me as a number, now you don't!
I did it
This comment has been hidden.
Square negative number equals square of this number without sign. Jesus Christ.
This comment has been hidden.
wow
Same
This comment has been hidden.
What does this means? i have tested my code in my terminal and it works perfectly but whenever i submit or test anything(even a simple return) i get this error message
Traceback (most recent call last): File "main.py", line 2, in import codewars_test as test ModuleNotFoundError: No module named 'codewars_test'
Try again, make sure 3.8 is the selected Python's version.
Thx! it now works. but shouldn't other vesions of python be disabled if they dont work properly?
They should be disabled, but for whatever reason sometimes that fails and a manual edit, like the one I did, has to force that.
This comment has been hidden.
Hi, just to mention: please do not post code without the spoiler tag, as others can see it in the main discussion (I have added rthe tag for you).
let sum = '';
is outside of the function, so put that statement inside the funciton instead.Sorry im new here didnt know about the red flag.
Thank you very much for your help!
No problem, you're welcome, happy coding!
I am trying in haskell. Why does the test require -1 to return 1? Isn't -1^2 = 1?
This comment has been hidden.
did almost the same thing failed all the tests,why?
Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Return the result not print it.
This comment has been hidden.
Please do not post spoilers in kata discussion pages.
Generally, solutions should be discussed on the solution page, but I can see that your solution for this kata is not visible due to a bug :(
Swift translation kumited. Please review and approve :)
Approved.
This comment has been hidden.
248 / 5000 Your logic is very good! But instead of using foreach, use for, as foreach does not allow you to work with other variables.
Another thing, you should use "return" instead of putting the information in console.log.
I'm sorry by my English, I'm Brazilian.
This comment has been hidden.
This comment has been hidden.
Discuss your solutions in Solutions section please, and remember marking your posts as having spoiler content if they have, even there. Thank you.
This comment has been hidden.
You don't need eval, other than that you're not supposed to post solution in the discourse.
Discuss your solutions in Solutions section please, and remember marking your posts as having spoiler content if they have, even there. Thank you.
great!
This comment has been hidden.
How are you using it? Are you using it inside your function? There shouldn't be any problems to use it.
If you mean Javascript, then yes,
console.log
andcosnole.info
should work here. If you cannot get it to work, you might read this paragraph of FAQ for some hints, and if it still does not work, then share your code for us to see.This comment has been hidden.
There's a few problems:
thanks a lot, you are a blessing. so I should assign everything to a value. I got a fairly weird solution now, based on my earlier code, but at least it works ^^ so happy and yes, I will never name my variables like that ever again ( at least in spyder it shows me when I do that, which is nice....)
I'm glad you got it working :> Don't forget to check solutions from others to see what you could improve ;-)
Getting the answer right but with a NaN at the end. Somebody help
You must've done some mathematically invalid operation somewhere in your code.
This comment has been hidden.
Yeet! I can't believe I didn't think of streaming it through the ES6+ syntax!
Perhaps not the prettiest to read, but a damn sight smaller than the typical for loop, nice work!
I feel so dumb writing 15 lines of code and then I saw someone with just 5 lines of code. Lol, good one though.
That's the whole reason we are here; to learn from people who write more optimized code. All the best
take a step each day your path will grow as you walk learning never ends
What is preferable solution? To have a code that is executed fast? Or to have the least lines of code? I think the first is more preferable than the second. If that is the case then a math solution is better than a solution that converts number to string and works with strings operations.
You should try this kata, and read its comments.
... and what exactly do you suggest?
Not a kata suggestion.
Thanks hobovsky. My suggestion would be that every solution should have an execution time value to compare with other solutions. But yes you are right Chrono79 this is not a kata suggestion. My mistake. I am new here.
This comment has been hidden.
No, read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution and your code returns a list, not a number.
Yep, they are.
This doesn't tell us anything, other than you're failing tests. What were inputs for these results?
Either way, there are a LOT of completions, which means the tests are correct, it's not a kata issue.
ok yeah im just dumb i didnt notice my bad
The square of 9119 should be equal to 83156161.
No, read the instructions again. It's square every digit, not square the whole number.
You are not squaring the whole number, you are squaring each indivdual number in the string. 9 1 1 9 9^2 = 81 1^2 = 1 1^2 = 1 9^2 = 81
Thus the correct answer is 811181
I think I don't get it right. Maybe my math is wrong, I just don't get it.
9641681 should equal 8116649 (shouldn't equal 813616136641?) 11125 should equal 25111 (shouldn't equal 111425?..) 1493625 should equal 2536491 368114 should equal 418136 (I can't pass every test except the basic one)
I can pass only the basic test where 9119 should equal 811181.
In the error message, the first value is what your function returns, not the input.
I understand (or maybe not), but I still don't get it. Input 1 1 1 2 5 Output 1 1 1 4 25 (What I think is correct)
Can you help me understand, please? (Idk if that's important but I'm training in python)
No, you don't. If you want to see what the input is, print it, read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#print-input When you see
11125 should equal 25111
it means your function returned11125
instead of the expected value of25111
and the input was5111
It seems your code is reversing the order of the squares for no reason.Then how do I get the sample test right? 9119 should equal 811181, my program returns 811181, the inputs and outputs are not the problem, I need help with the explanation of math.
and how 25111 equals 11125 or vice versa?
Let's start over again, this is the third time I told you the error message doesn't contain the input, right? If you want to see it, print it. when your code processed
9119
and you returned811181
it was because of this:Knowing the input value is important when you try to debug your code, otherwise you're working blindly. The error message says this:
11125 (you returned this value and was wrong, the correct value) should equal 25111
You can always edit sample tests, and see what your code does, try adding this one:
Now I got it, thanks a lot, and sorry :)
Now I finnaly solved that with your help, Ty, and sorry, I'm a dummie :)
This comment has been hidden.
There seems to be an error in the test case for '9118':
Log num: 9118 '811164' should equal 811181
There is no such fixed test, and the random tests seem to be fine. Also, note your function should return a number, not a string.
This comment has been hidden.
This comment has been hidden.
Please don't post solutions in Discourse, it's not for that. You have Solutions section to see and discuss solutions and even there, remember marking your post as having spoiler content.
This comment has been hidden.
This comment has been hidden.
Please don't post solutions in Discourse, and you can see all the solutions in Solutions section.
sorry! first time here!
A very well constructed kata for beginners imo!
This comment has been hidden.
https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#when-i-print-my-answer-it-looks-exactly-the-same-as-the-expected-output-yet-tests-fail
'Looks like you've solved it'
src/main/java/SquareDigit.java:6: error: cannot find symbol Stack store = new Stack<>(); ^
Stack is not allowed?
It is allowed, but:
Anyone have some idea, why Array.from() doesn't work on TypeScript?
Because you need to select the right version for it to work
2.4/ES6
No random test in Haskell
Latest Haskell version added random tests.
This comment has been hidden.
I suggest to extend the test case pool.
There is no test cases for:
And how would you make that work?
I see your point, but in the vast majority of answers here, the problem is that you can easily provide Int.MaxValue as an initial value. Regardless of the code's assumption(I mean that you explicitly want to return integer), the problem will occur during convertion between string and integer. In other word you will receive an exception from your code, not integer no matter what.
The test case you mentioned could not be added to languages which have built-in unbounded integer types (for example Python, Ruby). I think it was not added for sake of consistency between translations, but still it could be mentioned in the description, that result will not overflow.
Ok thanks, good to know that the test case is the same for each language. My suggestion was in regards to C# language and its specifics. However I think we should always provide a safe solution here. I see where the fun part is and obviously it is extremely fun and educational to see some clever one-liners, even though they are not 100% correct for all edge cases.
This comment has been hidden.
You are supposed
return
the answer, you're logging it in console.The Crystal (v0.34.x) version's initial solution has a syntax error, and requires parentheses around
num
.Fixed.
I've been trying to figure this one out for a while, and only just realised that in the description the note states that the function takes in and outputs an integer. Yet the test itself is casting the test input as a string......
Edit: running this kata in Python
I am not sure what you mean by "the test itself is casting the test input as a string" . As much as I see, tests always pass an instance of
int
to your function, and your function receives instances ofint
to work with. If you want to extract digits by stringification, you can do this, but it's not the only possible way. Tests also expect you to return an instance ofint
.Could you elaborate on what you think is the issue here?
This comment has been hidden.
Okay so it seems we are not on the same page here, for some reason. The test case you pasted, it's the one you see in your trainer in "Sample tests" section, right? If it is, then I see it defferently on my side. For me, it looks correctly:
Maybe kata indeed had some issue in the past which got corrected and you still see old version, or maye you got sample tests modified somehow? Please hit
RESET
button on the trainer page (back up your solution first!), and see if it changes to ints.Thanks very much. I 'reset' and the test changed to what you've shown. Strangely, when I refresh the page it switches back, but thank you. I'll work on it from there.
Please remember to mark your issue as resolved when you think everything is clarified.
This comment has been hidden.
Have you tested your code with -1?
I was trying this in Haskell, my function doesn't pass the Haskell test suite but the python version works. I think something is up with the Haskell test suite.
@squirrelpatrick it can be fixed if you can show how this is. However, it is far more likely a problem with your code.
This comment has been hidden.
This comment has been hidden.
return the value, not print. (also don't post code without the spoiler tag.)
oh sorry
This comment has been hidden.
Please don't post solutions in Discourse.
I don't get it . I have just joined and have solved 3 problems so far. But each solution is not acceptable here. When i try to run the same on my VSCode, it works perfectly fine :( specially the input command. Each input command ends with an error. EOF error
You don't need to use any inputs, you're getting them through function arguments. Try reading this for basic troubleshooting, or post your code here if you still can't figure it out. (mark as spoiler)
This comment has been hidden.
From FAQ:
Also from another point of the same FAQ:
Your function does not return anything. It always returns
None
.Oooh. i get it... so we need to return the value instead of printing it... thanks a lot
only when you are defining the function
The math is wrong 9119*9119 does not equal 811181. The math is wrong both in the program and the description;
No, the kata is ok and you didn't understand the instructions, read them again:
you must square digit by digit not the complete entry
This comment has been hidden.
Where are you squaring anything? And next time, please, mark your post as having spoiler content. You also have a double dot there.
Great, it's now working and I have marked it as spoiler. Thank you.
This comment has been hidden.
Don't post solutions in Discourse, it's forbidden.
This comment has been hidden.
Your function should return the result instead of printing it. Not a kata issue.
I did it recursive way. https://www.codewars.com/kata/reviews/5990b32035fd2f187600001a/groups/5edfbcadac16670001231620
why python version is old 3.6.why not latest 3.8
Right you are! I think there is a there is an open issue here:
https://github.com/Codewars/codewars-runner-cli/issues/771
This comment has been hidden.
Not a kata suggestion.
In the haskell test suit it says "Should double a positive integer:" so I thought at first we should leave the negative numbers as it is. Maybe remove that statement for clearity.
Reraised as issue by akar