8 kyu
Convert a String to a Number!
1,755 of 284,102bkaes
Loading description...
Parsing
Strings
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.
I need help with Kata Convert a String to a Number. My code won't compile and I can't find the reason why. I compared my code to Instructio and Sample Test again and again, debugged it in IntellyJ, where it runs without problems. It's driving me crazy, not to find the problem. If anybody could help me,I'ld be very grateful. The language is Java.
July1975
Your current code looks like you've copied/pasted the sample tests to Solution panel. Click reset and start over. Or post your code following these guidelines: https://docs.codewars.com/training/troubleshooting#post-discourse
I didn't copy and paste it. I adjusted it step by step, because I thought the code has to look very similar to the example. Thank you for the tip, I will delete my code and restart.
This is the initial code for Java:
Your code should be below or replacing that comment line.
Allright, thank you. I'm glad to read that. It's more fun to write an individual code and it's better practice, too. :)
On Kotlin it was supposed to be easy, but I tried more than 6 times and it didn't work.
You need to return the values, not print them. You also don't need to provide the inputs, the test suite will run your function with the appropriste inputs, you just need your function to return the correct answer.
Finally, an 8kyu that is actually easy (in C# at least)
This comment has been hidden.
Not a kata suggestion. See https://docs.codewars.com/training/troubleshooting/#post-discourse.
This comment has been hidden.
Not a kata issue. You should convert the string to number, not an array of numbers. Also you do not need the
main
function except if you're using to test in your local machineThis was my easiest program this week
i dont know why everyone is convering it into integr. i mean if the user have given a number with decimals. then! thats why i prefer float whenevr there is a number in a program
easy
I think it would be better to have a function that returns
int
anderror
. In this case, you could have added a failed test.This comment has been hidden.
Your solution is not written in a way Codewars could understand it. See this article for some pointers, and if it does not clear things up let us know, or even better visit the
#help-solve
channel on Codewars Discord to get help faster.Your solution is not right, it's not a kata issue.
flames!!!
good one
very good practice
Simple
I saw someone say that you shouldn’t use a built-in function to solve it. So I tried to dig into the Java source code to see what happened behind it. When I understood most of it, I realized I made things complicated for myself.
very easy XD. and we have 3 options for solving this task (in C). in python this task very fuuny
Nice and useful.
nice
This comment has been hidden.
This comment has been hidden.
You can use TryParse when you don't know the format that will come. But if you have sure about the format can be simple just use Parse.
...also, there's this awesome thing called
google
where you can find out a lot of stuff 'n junk, for example, if you jam nothing more thanparse vs tryparse
into it you can easily find awesome things like this as found on this other awesome thing calledstackoverflow
This comment has been hidden.
it aasked you to convert not replace. you have to convert it to an integer or float. float is the best solution as it support decimal.
why most of the people do it using built-in conversion functions? there is clearly stated that you're expected to write your own solution!
actually pretty nice kata!
This comment has been hidden.
I can't find the submit button. Can anyone help me out!
See if this helps: https://docs.codewars.com/training/training-example#completing-a-kata
i suck at challenges
me too :(
mistake improves us
In TypeScript tests use assert.strictEqual insted assert.equal
Do you mind explaining why is that an issue? It seems to work fine.
The error exist in TypeScript test.
assert.equal
usenon-strict equality (==)
andassert.equal(3, '3')
does not throw error.assert.strictEqual
usestrict equality (===)
andassert.strictEqual(3, '3')
throw error.Current test:
My porposal:
Doc assert.equal: https://www.chaijs.com/api/assert/#method_equal
Doc assert.strictEqual https://www.chaijs.com/api/assert/#method_strictequal
Example of wrong solution:
Ok, thanks, fixed. I was aware of the diffeence between one assertion and the other, I didn't know how you could write a wrong solution.
This is a good start for beginners like me.
That's really a good practice for the beginners. I like this one.
This comment has been hidden.
This comment has been hidden.
That's a problem with your code, not a kata issue, please read this: https://docs.codewars.com/training/troubleshooting#works-but-no
This comment has been hidden.
Great first kata. Took a minute to realize how it was wanting the answer as I am coming from codecademy.
Congrats!
Im coming from codecademy too. They dont explain conversions values. This is very sad. I'd became lost because my last experience knologe is in Python, but in Kotlin many thing is in Functions and Methods.
love this
Go translation
This is really strange beaucause the first time i tested it the code did not work but the second time with the same code there is no problem, there is an issue with this challenge
Can someone enlighten me about => function? hwo it works?
Hi, you use it exactly as a function,
I took the examples from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions Hope it helps you!
This could help you too:
https://www.youtube.com/watch?v=h33Srr5J9nY
Thank you ill try that
It seems the tests for this are broken, because input are not strings, but numbers. Language - Python.
I had to add conversion to str to make sure I got a string input (s = str(s)).
Fixed in this fork (also added random tests for negative numbers): https://www.codewars.com/kumite/62920ec01bd1bb8c8adaefb6?sel=62920ec01bd1bb8c8adaefb6
Approved!
This comment has been hidden.
The output console is RED and does not show anything. It will not show whether my code works or not, only a RED background.
Very easy
COBOL translation ready for review :)
Approved
Im so confused only 1 test passed i have no idea why i did it correctly and only 1 passed these questions ar so weird
Crystal translation kumitied :)
R translation
ok for R but I could not approve Crystal's (see translation page).
ok :)
This comment has been hidden.
Converted language-specific blocks into language-agnostic description
No random tests in Rust
Hi. I've added some random tests in Rust. Since I didn't add the original Rust translation, I'd be happy about some feedback on the additional tests. Thanks.
Thanks :)
easy cata
cool kata,
I think that this was the easyest kata ever
No if you use Dart.
This comment has been hidden.
Always mark your code as containing spoiler content, and don't post your working solution in the discourse -- it's forbidden.
Racket translation
Julia translation
Dart translation kumited.
ty!
Groovy translation kumited. Please review and approve. Thanks, suic
New fork of the Lua translaton with Auto Tests and Description. Review and approve, please.
Approved
This comment has been hidden.
It rounds a number, the same as
Math.round
. But it also works with strings, but Math.round does it too I believe.Note: next time, when you ask a question, mark it as a 'question'.
TY for the reply
PHP translation kumited 🙂
Approved.
Thanks. That was fast 🙂
This comment has been hidden.
I run CodeBlocks 17.12 and fixed tests passed, however the codewars gives me an error as follows "Caught unknown exception"
Please help.
This comment has been hidden.
Haskell: This kata is lame after already completed https://www.codewars.com/kata/square-every-digit https://www.codewars.com/kata/noobcode-01-supersize-me-dot-dot-dot-or-rather-this-integer https://www.codewars.com/kata/alan-partridge-ii-apple-turnover https://www.codewars.com/kata/parse-float
This comment has been hidden.
So?
This is a 8kyu kata, so this is to be expected for every language :P
Description is kind of broken. Examples for different languages are messed up together.
Please be specific. Which language is not right?
C translation kumited :)
.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
What do you get? A string. What do you return? A string. That's not what you're asked for, the kata asks for a number. The types don't match.
Hint: save or return the result of your function call.
@ZozoFouchtra: Marked your comment as spoiler.
Thank you for your help @bkaes and @ZozoFouchtra, what a silly thing to miss!
This comment has been hidden.
It is a string, but you try to access a property that's not part of the
String
interface; you have a typo inlength
(remember,h
comes aftert
inlength
,width
,heigth
). Thus node falls back to the underlyingObject
prototype. And lastly,length
isn't a function, but a value, so you need to drop()
.'Looks like you've solved it'
It's better to add in JavaScript tests a test with a huge number (e.g. "12345678912345"), because some solutions that use bitwise operations:
var stringToNumber = function(str){ return str|0; // or ~~str (or str^0) }
may work incorrectlly on huge numbers.
For example:
str = "12345678912345" str|0 // 1942903641
This happens because in JavaScript bitwise operations convert it arguments to 32-bit integers. In this example binary representation of number in str is more than 32 bit, therefore it is truncated.
This comment has been hidden.
Which language? That being said, please mark comments containing spoiler messages as having spoiler content. If you're not content with a solution please add a comment there.
It should be stated that the ideal solution does this by hand and not by simply calling (i.e., delegating to) a single API method for this purpose.
It's a 8kyu kata. The community has chosen a single API method in your preferred language (Java). Feel free to vote a by-hand-version as best practise if you consider it to be more ideal.
The tests trivially pass as written. They should look like this (note I've added an =)
(deftest string-to-number-test (is (= (string-to-number "1234") 1234)) (is (= (string-to-number "605") 605)) (is (= (string-to-number "1405") 1405)) (is (= (string-to-number "-7") -7)) )
Didn't write the Clojure variant, but at this point one cannot change the test cases anymore.
This comment has been hidden.
This isn't really a suggestion, but a solution.
sorry, I thought I can put it here as a solution
Your solution is listed among the other solutions, don't worry.
oh..alright, thanks
In the Java solution template, it should be "public static int", not "public int static". The latter won't compile.
Please post an issue next time. Somehow, I didn't get a notification for your comment :/. Fixed.
This comment has been hidden.
string_to_number
takes exactly one string, not an array of strings. It's meant to be called like this:Also,
to_f
is meant for floats, while the description indicates that we're just checking integral numbers:'Looks like you've solved it'
But your solution will be invalidated if there were random tests
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Changed the text, since it's actually about integral numbers (see the Haskell version:
String -> Int
). Sorry for the inconvenience.