5 kyu
int32 to IPv4
1,050 of 27,492sahglie
Loading description...
Networks
Bits
Algorithms
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.
This comment has been hidden.
I have error "Execution Timed Out (12000 ms)". If anybody knows why? It's interesting that this error appears when kata return correct answer. When kata return wrong answer there in no error, only message about "expected.."
You've created an infinite loop. It only triggers when your code reaches the second sample test, so when your code returns the correct answer for the first test, your code will time out.
Thanks a lot! You help me to find the most stupid bug in my code! Of course it was infinite loop! Thank you!
i have "for int32 = 1111595428: expected '132.131.51.36' to equal '66.65.153.164'" Why this can be?
This comment has been hidden.
Nice one, had a lot of fun with it :)
Is it possible to implement this in Kotlin? When attempting a logical AND operation, garbage is returned. When I look at the code that compiles in Java, there is a completely wrong idea. UPD:Implemented via string. But the test fails. Error with ip: 89.118.64.172. In 10th representation it is 1500922028. In the 2nd representation - 10110010111011001000000101011000. The first 8 bits 10110010 = 178. Am I missing something or is there an error in the tests?
it is your code that is wrong: you are adding trailing zeroes instead of leading zeroes to the binary string. please ask a
question
in the future instead of raising anissue
;-)I guess there is some problem with a sample test in C#. If I return
128.114.17.10
,128.114.17.105
or128.114.17.104.
it says that128.114.17.104
is expected. But if I return the correct string or just literally typereturn "128.114.17.104";
I get anArgumentOutOfRangeException
.no, the problem comes from your code. you are assuming that the binary string you create is always 32 chars long, this is not true.
Please ask a
question
(not anissue
) to ask for help in the future ;-)Thanks, you helped!
I will next time. I was confused and really thought there was an issue :/
This comment has been hidden.
Hello, your code does not guarantee that you get 4 full octets.
Nice one, pretty challenging, well done, compliments for the kata, I really enjoyed it.
nice kata
There is a problem. My solution is very bad and my solution isn't working good. I touched several times the sumbit button and I took the errors. After that my solution passed this kata when I touched last times the sumbit button.
So there is a problem. Randrom test algorithm isn't controlling all situations. This is a big problems for kata.
Random tests must control all situation and It should not allow like bad code.
My language is C.
fixed. your solution fails both the random and fixed tests consistently now
Definitelly to easy for 5kyu
WhAT Should i do when length < 30?
Especially in Java translation, it's confusing to see the method
String longToIP(long ip)
when the kata name clearly states that the IP address is represented as a 32-bit integer. From my point of view it would be more appropriate to haveString numToIP(int ip)
.but it's explained in the initial code:
Java has no
unsigned
, so you have to use along
to keep everything positiveHeh :) somehow it slipped out of my view. It makes perfect sense now. Thank you for pointing that out!
This comment has been hidden.
This comment has been hidden.
toString(2)
will not necessarily have a length of32
, so you are splitting the string at incorrect indicesBecause input length maybe <32, we should convert binary to decimal started from end not from start. Maybe someone can say why, we should start from end?
It does not matter whether it is from start or end, because the binary representation of a 32 bit number must be divided into 4 octets (each having 8 bits / 1 byte)
My solution for C, sets the IPv4 to the right value with a null value to end it. However when the test is run the intput is shown as a load of random sysmbols such as '@@@@@@@@@@@@@@@@!@@@' etc
In your code, you attempt to allocate memory for the provided
char *IPv4
within the function, but it has already been pre-allocated prior to being passed to the function.But if it I don't allocate the memory, it causes a memory access error
that remains seperately as a different problem with your code because it doesn't handle every case properly
i too am a integer division // modulo enjoyer
Scala translation
Fun kata! Unique premise and well-explained.
Please, help me. In the Eclipse IDE my code solves this task correctly. But when i attempt my solution i see another result which is mistake. For example, when i input 2154959208 in my IDE i get the "128.114.17.104" which is right. Meanwhile, in CodeWars i get a mistake - "128.0.0.0". How can i solve the problem? I have written my program on c++.
Your solution has a very subtle bug which leads to UB and might be difficult to reproduce on other compiler.
Bug in your solution is not a kata issue.
This comment has been hidden.
As I understand it, it's just how BitConverter works. It seems that whatever architecture CodeWars uses to run our code happens to be little-endian, and thus GetBytes() acts accordingly. See the documentation:
The order of bytes in the array returned by the GetBytes method depends on whether the computer architecture is little-endian or big-endian.
Do note that the Kata does implicitly describe the first octet being the left-most one (and, correspondingly, the most significant one).
This comment has been hidden.
How is this a kata issue? You have to do with the implementation available. If you are not happy with a compiler behaviour, fill a bug to their developpers.
Also, you must use a spoiler flag as soon as you post code somewhere. I've put the flag on for you.
Sorry, I didn't know about the flag.
There is an error in the 3. basic test in c.
Test:
Error:
Other random tests also error out randomly:
Not sure, but it is possible that it comes from your code writing out of the buffer allocated for the output. As you can see, this is not what the test is supposed to write according to the code.
Thanks for the comment. Yes sprintf was writing one extra character to the buffer. Fixed it now.
D translation
I'm wish to have this IPv4 adress: 0.1.15.44
Lua translation!
Approved
Thanks!
Returning the correct answer in Java throws an index out of bounds error: "begin 0, end 8, length 1".
There is no issue with the tests, you should re-evaluate your solution.
(little hint: is the string you're going over always has the length your code seems to expect?)
Passed testcases, failed on all randoms in python
Go translation (author is inactive).
The description doesn't provide enough data. Once you find out how the 32 bits number represent the IP, the solution is pretty easy.
This kata was really interesting to solve. Had a lot of fun :)
Omg, felt proud that i had a nice solution for this Kata of around 25 lines of code in JS, until i saw solutions of just 1 line. Still lots of this for me to learn here. I guess I'll be starting to review my code and try to refactor.
Groovy translation kumite. Please review and approve. Regards, suic
Approved :)
COMPLETED IT. M feeling so proud.
COBOL translation.
approved
JavaScript needs way more than 3 test cases!
My first attempt was completely wrong and I only noticed, because I tried to use the code in another kata.
added
Can someone please explain to me why 10000000.00100000.00001010.00000001 is suddenly 2149583361? I cannot find sufficient information about this and I am lost.
10000000.00100000.00001010.00000001
is how you'd write the IP address128.32.10.1
using the binary representation of numbers instead of decimal.128
,32
,10
, and1
are simply the individual bytes of the number2149583361
- explaining this further may somewhat spoil the task, so if you still don't understand how it works, you really should look up the relation between 32-bit integers and IPv4 yourself.IEEE 754
https://en.wikipedia.org/wiki/IPv4
This comment has been hidden.
Just discard a dots and convert this binary number to decimal int32 number
While reading the C++ solutions I can't help but think that they're all really ugly because the standard library lacks a concise and intuitive string formatter. It will be nice when C++20 is available everywhere and we can get our hands on the new <format> library.
It would be good if the website accepts C++20. It only accepts C++17 as of now (10/2022).
Is there consistent compiler support for C++20 outside of MSVC? But yeah, I agree it would be nice to have.
I'm using c and I get a weird error:
Your solution has memory leak, expected max length 15 + 1 '\0' = 16.
. I tried printing the length of the problematic result on my pc and it is15
, meaning16
with the null character. What is the problem?your solution writes past the end of the
output
buffer: you are printing a trailing'.'
, and then erase it. this is not robust: the worst-case output size is16
i.e.strlen("100.100.100.100") + 1
, so the caller of the function can expect that16
bytes should be enough, but your solution needs more bytes than that. as a result of writing out-of-bounds, you corrupt the tests suiteThis comment has been hidden.
I just came here to say the same thing.
Easy while using a certain Lib, but try doing it without it
This comment has been hidden.
Nice Kata, thanks!
i dont understand !
Caught std::exception, what(): stoi
Wtf is this?
i think stoi only converts string to int (aka 32 bit numbers)
Nice kata. I am new to programming and not proficient in bitwise operations/operators so I solved this using regular math. Questions from novice in programming: how important/usefull are bitwise operations/operators in C? Is that something every programmer should know? Are there any other katas where I could practice bitwise stuff? Thank you in advance!
This comment has been hidden.
added
i dont understand this,
Testing 138368321, expecting: 8.63.85.65 binary of 138368321 is 1000001111110101010101000001 but all the binary numbers of 8.63.85.65 are 1000 111111 1010101 1000001 8 63 85 65 but if we divide up the bin of the original argument in pieces, we get 131.bla bla bla.
In problem definition it is mentioned that each number in the ip address is converted in to 8 bit binary by adding 0s at the beginning if not enough digit are present. In such scenario 8.63.85.65 becomes 00001000 00111111 01010101 01000001 which will be converted to 138368321.
The user can modify the input in C++.
that cannot be true: the input is an integer passed by value
Errors logs in C reverse actual and expected:
fixed
This comment has been hidden.
"100.246.254.253" is the right answer indeed. That's weird. I've just solved this kata in c++ and had no problem. Is it a permanent problem?
The reference solution returns
"100.246.254.253"
. Unless you're modifying the input, your code is wrong.This comment has been hidden.
1457763694 -> 1010110111000111011010101101110
10101101->'173'
11000111->'199'
01101010->'106'
1101110->'110'
Expected: equal to "86.227.181.110" Actual: "173.199.106.110"
???????????????????????????????????????????
10101101->'173' but 01010110->'86' you need to fill it with a 0 at the beginn
My solution required using bitset<>bits>::bitset() on the belief that the solution comprises 4 x 8 bit octets producing the 32 bit solution. I am failing, is this because the solution does not allow bitsets (i.e. it appears not allow the bitset names 'undeclared variable') or because I am a chump and completely misreading the question?
This comment has been hidden.
My mistake - I said honor instead of score points. The fact remains that I received only 8 score points instead of 21. So, the issue remains. Could you do something about it?
Are you sure you're looking at score points and not Honor? You can't see your score points on your profile, you can check your score in the API here.
Thank you, @mrLeriaz for the clarification.
nice kata
where is sample tests for numbers < 32 and > 0?
I doubt that such sample tests are needed as it does not represent any special case or edge case
Maybe I'm making a mistake, but I notice only 1 Coffeescript solution, so maybe not :) I pass all sample tests, but not a single random test. Might there be an issue in the random tests?
Oh, I thought I was doing something wrong, but it turns out reference solution is wrong.
Can you please check whether it is fixed now?
It's been fixed, thanks.
This comment has been hidden.
Small numbers cases are not tested. An uncomplete solution might pass ...
Is Java version borken? Test case just says "begin 0, end 8, length 1" and I return "128.114.17.104" what seems to be the correct answer
No, the java version is working fine I think. There is the case that the binary is smaller than 32, for this case you have to fill up with leading zeros, this isn't mentioned anywhere. Hope this helps.
My bad, yes it is working fine. I just misread the error message on test case, adding the zeros did the trick.
OP solved it, closing
I really don't undersand it said the IPv4 is 32 bits that's pretty clear and the random tests are not 32 bits lol so weird.I have no idea how to solve,someone can answer my question?Appreciate so much.
What language?
java 11 . expected:<[6.21.181.189]> but was:<[48.86.107.61]> This is one of the test and below are each number's bits [6.21.181.189]. 110 10101 10110101 10111101 I don't know how i can count 3+5+8*2 = 32 if my brain is not washed...
well... After looking at others' solution I realized taht I brute forced the heck out of this kata haha.
Rust translation
This comment has been hidden.
This comment has been hidden.
OP left the building, closing
I think there is some problem is this kata for Python 3. Sometimes it shows 60 test cases passed, sometimes 50 and it keeps changing every time I run my code(without changing my code). Please tell me if this is problem with my code or it's related to kata or it's test cases.
I just ran my code. There are 103 test cases - same number every time. Does your code actually pass all tests and have you submitted?
No, not yet. I mean to say that each time I attempt with same code I get different number for test cases passed out of 103. This is first time I am coming across this type of problem, I have no idea what to do.
I guess the short answer is that your solution doesn't work - some random test cases are generated and you don't pass all of them, but because they're generated randomly, you don't necessarily always fail on the same exact test case.
This comment has been hidden.
added
How is it possible to convert an int to an unsigned int in python or get the unsigned int binary representation? My current solution works for only half of the tests, which should be caused by this problem.
This comment has been hidden.
This comment has been hidden.
There is a problem with String index out of range: when u solve the problem, in Java
There is a problem with String index out of range: when you solve the problem, in Java. There is no problem with String index out of range: when I solve the problem, in Java.
Well thank you for your replay but when i printed out my solution it was matching with his answer. I can try to solve it again. which version of java did u use?
I tried both, Java 8 and Java 11. If you think your solution is OK and tests are incorrect, you can post your solution here (with spoiler flag!) and we will check it.
This comment has been hidden.
Sorry I ve never shared my code before in this platform i dont know why it looks so weird
Use markdown formatting
There's an error in your code. Recreate this test case locally and see how it fails:
assertEquals("0.0.0.0", Kata.longToIP(0));
i was going to add if statement at the beginning of the code for that. my problem is that i cannot even run program for solving this:
assertEquals("128.114.17.104", Kata.longToIP(2154959208L)
So I have changed my solution to more static/brute force method to test.
Aaa now I understood what you mean. it wasn't stacking in the first test it was stacking in 0 :D. sorry that I have waste your time. and thank you for your help.
Swift 5 translation kumited. Please review and approve if acceptable.
this should be a 7 kata to be honest, its very easy. not 5
Hey i am using C language. can anyone suggest me how can i print on output console for debugging purpose? i already use printf but ocassionally it skips over some data or do not print at all!
printf("%u\n", input_value);
should work fine.It prints everything, but it is not guaranteed that the log will appear exactly before the corresponding test. In that case try submitting again.
Sometimes it's in the same line with testing framework messages, so I recommend
\n
in the beginning too.Also try
fprintf(stderr, "%u\n", input_value);
to see your output after the tests and not to mix with them.Julia translation
Kotlin translation, would someone mind reviewing this please.
Please, Forked C Translation Please, NASM Translation
This was fun, but still too easy for 5kyu.
This was fun. However, out of all katas I have seen so far, this one seriously needs some random tests. Also, this felt more like a 6 kyu rather than a 5 kyu.
C++ translation added, please review :)
Approved (hope it's good ;-)
Updated Python translation kumited, including random tests.
Oh, and it's rather overrated, but what the heck...
Thanks
I am able to create the correct string with my implementation, which I know because I can return a substring of the function's result and it will be displayed in output. However when I return the entirety of the result, this throws an out of range error. Does anyone know what might be going on? (I am writing in Java)
This is just broken now. Attempt throws warnings, which are treated as error:
/tmp/haskell117915-5-e9kr75.ud1rc9dx6r/IPv4/Test.hs:13:17: Warning: Literal 2154959208 is out of the Int32 range -2147483648..2147483647
/tmp/haskell117915-5-e9kr75.ud1rc9dx6r/IPv4/Test.hs:15:17: Warning: Literal 2149583361 is out of the Int```
The type signature in Haskell is wrong. It should use Word32 instead of Int32
Still not fixed. Input datatype "Int" would also work.
Given that the author hasn't logged in for 2 years, and first I reported this 4 years ago I decided to fix the types myself.
Need random tests
Added random tests to Ruby, other languages are OK, except JS
Yeah, JS is missing random tests: https://www.codewars.com/kata/reviews/544696aed6aed37c1a0001d2/groups/5cf7ffff9bde340001473524
added
Java translation kumited!
This kata is awesome. I really enjoyed it.
Hey Haskellers!
Use the type signature
(Show a, Integral a) => a -> IPString
!According to @DasBrain
Thank you! I still think the correct signature should be int32ToIP :: Word32 -> IPString, but with the help of your hack, the test went green.
To explain why this type signature is nessencary:
The random tests use
Int32
, but if you useInt32
in your signature, you get an error because two of the static test cases uses 2154959208, which is outside of the range ofInt32
.So we have to write a function that works with
Int32
, but also withInt
.So now for the constraints:
Show a
andIntegral a
:Integral a
is self-explaintory: it gives usdiv
,mod
and so on. But why do we needShow a
? Because we should return a string and use the result ofdiv
andmod
there.I suppose you could also use a typeclass with two instances, one for
Int
and one forInt32
.Given that the author hasn't logged in for 2 years, and first I reported this 4 years ago I decided to fix the types myself.
As others have noted, this Kata is (still!) unsolvable for Haskell due to the wrong datatype.
CoffeeScript translation Kumited! Please accept :D
if anyone needs it, this helped me to make the binary numbers http://www.mathcs.emory.edu/~cheung/Courses/255/others/BinNumReps.html
It is not clear what should be returned in the wrong cases, for example 0
what do you mean 'in the wrong cases'? int32_to_ip(0) => "0.0.0.0"
Example added to description
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Please fix the data type to Word32, or the kata is unsolvable.
The word "integer" is incorrectly spelled in the first test case: "wrong ip for interger: 2154959208".
fixed
Please fix the data type as @phaul has noted. Both of the test case, eg 2149583361 are > maxBound :: Int32. Word32 is the correct type for unsigned Ints. Otherwise the test cases fail as it is trying to parse a negative number. This kata is currently unsolvable as is. Thanks.
For reference, maxBound :: Int32 == 2147483647 and minBound :: Int32 == -2147483648 while maxBound :: Word32 == 4294967295 and minBound :: Word32 == 0
Use the signature
int32ToIP :: (Show a, Integral a) => a -> IPString
Given that the author hasn't logged in for 2 years, and first I reported this 4 years ago I decided to fix the types myself.
Int32 is the wrong data type for this. It should be Word32. The number in the example 2149583361 is not representable in Int32. Haskell has a type sytem to enforce good coding not work around it. I would say this kata is non-solvable with the Int32 interface.
Use the signature
int32ToIP :: (Show a, Integral a) => a -> IPString
This comment has been hidden.
Most probably not a kata issue.
Maybe try to randomize values in tests. I suggest to add this test:
Random tests added to Ruby
Nice idea, I would have really liked to see more tests.
Why? What kind of tests? Can you give examples...
Just more than three test cases, you know ? Something fun, like 255.255.255.255, or 127.0.0.1 :P Again, just for kicks, that's all, otherwise your kata is pretty straightforward and for this kind of algorithm a few tests are indeed enough.
This comment has been hidden.
This one lacks test cases. What should be output for 0?
Why not in Javascript!!? :)
translation "kumited"
This comment has been hidden.
Already submitted the random test cases. Waiting for author's answer.