6 kyu
intToBits(int, length)
181wthit56
Loading description...
Bits
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.
absurd test cases, lack of definitions for valid arguments.
Nice kata, except, IMO, the need to check for absurd values passed to length (NaN or such...).
It would be helpful to see some sample inputs with expected outputs. My code seems to fullfill the task but I get
Test didn't pass: Unknown error
.HELP!
Good idea. I've copied a few of the basic tests into the "Example Tests" section; you should be able to use those to test out your initial code...
I've also added those examples to the description with a brief explanation. Does that help?
This comment has been hidden.
I've now fixed the unsigned problem, and also the negative problem (same issue).
I still need the bits truncation, as that is the
expected
value. So to leave it untruncated would make it incorrect for the given generated int.This comment has been hidden.
!
This comment has been hidden.
I've added a note that the
length
should be between 1 and 32 (inlcusive). And if the number is outside that range, or otherwise invalid, the description already sayslength
should default to 32.I've tweaked the note about JavaScript ints to include the signed 32 bit description.
And I've added a float test, too.
Thanks for your help!
Great. I'll mark this one resolved.
This comment has been hidden.
Thanks for that. I've hopefully fixed all the cited problems?
Yep, the descriptions on the tests look accurate now.
it dosen't check the length correctly
Which tests are you referring to?
And it's still unknown to me, what should return for example
intToBits(-5, 2)
? How much bits it should capture from '1...11111011' ?Effectively, the specified
length
is the minimum number of bits that should be returned. So if the int has more data (as in, there's a 1 beyond the limit oflength
bits), then the bits up to the that point should be returned.For negative numbers it always will 32 bits length?
Yes. The way a negative number is represented in the bits is to have the first bit be a 1. That means there will always be 31 bits after the negative indicator. So in total, there will always be 32 bits returned.
Add some tests with negative numbers and lengths together
Good idea. Added.
It's a little confusing at the end of your description, because you state:
However, with the optional length argument, this isn't true. You can have an arbitrary number of characters.
In the description, you state that 0 or greater is valid for the optional length parameter. Should that be 1 or greater? Or should the function allow the integer
0
to return an empty string''
?On a related note, you don't have any tests for validity of length, maybe you should make it clear that the function doesn't have to check for a valid length parameter.
Also, your setup function doesn't declare the
length
parameter, making it potentially confusing. Since dealing with optional parameters isn't really a critical part of this kata, I recommend changing your setup code to be:This makes it clear that length is optional (that's standard JS format for optional params), as well as making it clear you don't have to worry about testing it for validity.
First off, thanks for your awesome, detailed notes. Much appreciated. As you've probably noticed, I added the whole
length
argument thing last-minute, so a lot of changes slipped through the cracks. ;/I didn't know that, no. Pretty cool. I guess they're going for the UInt32 stuff, now.
OK, looks better. (I kinda preferred not having to validate length, but this is more realistic. :-)
Cool. Thanks for your help!
I felt this was a great kata. Description was clear and concise, and I liked the technical aspect of it.
Thanks, Joey. I appreciate it.
You also should be explicitly testing for -1 and 0.
Added.
Marked as ready.
Cheers.
You should specify that you're expecting a 32-bit width and a two's complement representation in the description even though it's easy enough to figure out with the test cases for people familiar with binary representations. However, it would be nice for the people who have no idea to give them that bit of info.
Okay; I've made it clear that ints are 32 bits, so the string should be 32 characters long. I already have it in there that the string should be made up of ones and zeros. Two's complement is a bit technical and hard to understand if you don't know what it means. Is that okay?
Yea, but if you don't say two's complement then a novice won't have any idea where to start looking on how to implement negative numbers in binary.
Okay. Interestingly, I didn't know what the whole "two's complement" thing was about until I looked it up just now. So it's not necessary to understand it for this kata (see my solution). Even so, I've made a "notes" section with things that might be good to know. I've added the two's complement note here, and linked to the wikipedia article.