6 kyu
Base Conversion
8,090 of 10,776BattleRattle
Loading description...
Strings
Mathematics
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.
should handle solutions like this in python.
See issue by Madjosz below.
Something is broken?
I only get this error on codewars, not on my local python3 or other online python3 runners:
The code:
NO random tests in C#
I did skip it until now. But I finally passed this kata. I am very satisfied because of I solved.
Is that really 6 kyu kata ? Looks really hard with all these "Alphabets"...
here we go again...
I don't understand. on what principle to convert. example : convert("hello", Alphabet.ALPHA_LOWER, Alphabet.HEXA_DECIMAL); // should return "320048" how from h --7 character Alphabet.ALPHA_LOWER , it turned out 4 character Alphabet.HEXA_DECIMAL ----3?? what are the rules/principles?
p.s. 4 days later. i'm crying , because i did it. thanks BattleRattle -- kata is beautifull!!!!
Could you please provide a hint about the logic in this case?
https://habr.com/ru/post/124395/ - this help me
Thanks! Really useful article. It helped to undestand.
Python: parameter
input
shadows a built-inThis comment has been hidden.
спустя от 4-6,5 часа(примерно) от начала каты, я ее решил. можно было пива попить с копчеными ребрышками. Вот сейчас и займемся
the deepl-translation doesn't make any sense:
"I haven't decided this yet, but these people who praise kats that almost got divorced or spent 4+ hours on it are total freaks)))))"
what do you mean?
C: missing random tests (only random input for source = target)
Python: missing random tests (only random input for source = target)
JS: missing random tests (only random input for source = target)
Every hour I have spent on this kata worth it!
am break my head while solving this kata!!!!(4 hour +)
Is this kata should have 6 kyu? I think it should be 4 kyu :(
I solved it in 5 minutes, once you know the theorie of numeral systems it becomes trivial
Some advices pls
ranks can't be changed
I almost got divorced because of this kata!
This comment has been hidden.
convert(x, y, z)
converts the numberx
from the systemy
toz
more detailed answers will spoil the kata
learning new things while practicing kata is what makes the site fun to use
As i wrote, i understand general task. But looking at solutions, where people count systems length, making Math.pow, and don't use systems keys and values at all...
really good kata, you have to know how numeral systems work
In Ruby syntax, you must have 'end' keyword for the method to end. Add to the template for ruby.
def convert (input, source, target) #your code here end
Ruby 3.0 should be enabled.
Read about Horner's method https://en.wikipedia.org/wiki/Binary_number
In Visual Studio everyting works fine, I tested every test, but here, the error "error CS0119: 'Kata.Convert(string, string, string)' is a method, which is not valid in the given context" comes up. pls help
Function name is Convert so if you need to use Convert.ToString then use System.Convert.ToString
Something is wrong with this kata. Let's try one of the accepted solutions: Convert("AAAA", Alphabet.ALPHA_UPPER, Alphabet.ALPHA_UPPER); Output should be "AAAA" and is "" because char a in ALPHAs has index 0 :-(
Ruby initial solution is wrong
This comment has been hidden.
Given two set of digits (source and destination) and one string containg a "number" (this "number" can contain alphanumerical "digits"). Change the base or radix of this given "number" to other bases using the digits of the destination base.
Example (Converting the number "hello" in base "allow" to base 10):
In base "allow" we have the digits "a,b,c,d,...,z"; the "a" is the number 0 in base 10, meanwhile z is number 25 in base 10.
We take the 'h' and multiply it decimal value by pow(25, 4). 25 is the base number (We only have 25 digits, and 4 is the "position" of the digit in the number) now with e, and l, and so on. Now we sum all the values of each digits and now we have a number in base 10, which is: 3276872
Remember that each base uses different digits.
Oh sweet. Thankns =)
I spent some time trying to figure out an example from your answer. I came to the conclusion that it has a mistake. "Allow" has base 26, but not 25.
Great Kata!
It should be a 4kyu, not 6kyu data i think
the kyu values are all over the place, I've seen pretty hard 7 and 6 kyu and really easy 4 kyu.
This comment has been hidden.
Think about how you did the other conversions, using the bases given (or use the bases given for the first examples). Hint: You'll need to do a string lookup at some point to get the correct value in the target base.
This comment has been hidden.
Look up "base conversion"?
This comment has been hidden.
nvm figured it out debugged the coder
This comment has been hidden.
I can get the answer right via print, but even assigning it to a variable and then returning it results in this error. Is that a bug or something I'm not getting? Thanks!
The error:
Traceback (most recent call last): File "main.py", line 15, in test.assert_equals(convert("CodeWars", alpha, bin), '110100110111011111011110001100111111110000110', '"' + input + '" alpha -> bin'); TypeError: Can't convert 'builtin_function_or_method' object to str implicitly
TypeError: Can't convert 'builtin_function_or_method' object to str implicitly
. Did you even read the log? You're converting afunction
to astring
somewhere.Yeah... I read it. The problem wasn't in the function itself, but I messed up one of the test cases. Never mind, my bad.
convert("27", dec, allow) ==> "bb" Is this example right? why?
That is right。
The base of "27" is decimal, what is the base of "bb"?
This comment has been hidden.
They're passed in as arguments.
Right! I get it now, thanks!
The reason this is 4kyu is that you have to be able to debug the sample tests ;)
I see that the Python sample tests are still broken exactly the way people have been saying :(
Fixed now.
This comment has been hidden.
After you understand it... this is a very nice kata! Congrats :)
This comment has been hidden.
Fixed now.
Python:
Submitted solution that was accepted, cannot 'RUN SAMPLE TESTS' though because of error:
Traceback: in AttributeError: 'dict' object has no attribute 'BINARY'
Look way down the comments, you need to subtitute in test area case: bin = Alphabet['BINARY']; oct = Alphabet['OCTAL']; dec = Alphabet['DECIMAL']; hex = Alphabet['HEXA_DECIMAL']; allow = Alphabet['ALPHA_LOWER']; alup = Alphabet['ALPHA_UPPER']; alpha = Alphabet['ALPHA']; alnum = Alphabet['ALPHA_NUMERIC'];
Fixed now.
Had fun with this kata while learning C#. After looking at the other solutions looks like I have a long way to go. Should be fun!
This comment has been hidden.
0
is the first number of dec, anda
is first letter in alphadec is base 10, so
27
is27 = 2 * 10 + 7
. allow has 26 chars, is base 26.a
is0
,b
is1
,c
is2
... andz
is25
. sobb = b * 26 + b = 1 * 26 + 1
.Do you understand better now?
I think that I understand it better now. For those who are still uncertain I think the pattern is as follows: 27 -> base26 = 11. printf("%c%c", allow[1], allow[1]);
Whenever another 26 gets added (for base26) we add a digit to the second column. Similar to how hexadecimal works.
52 following this pattern would equal 20 so "ca".
Typo in the description. Should be "too large" instead of "to large".
Fixed.
Maybe it's because I've done a lot of base conversions in the past but I don't really get why this is a 4 kyu. It was pretty easy.
This comment has been hidden.
Replace
with
in the test code.
Fixed now.
Brain-melting kata, but now when I've finished it I think I'm loving it :D
Strange I can run it fine in repl.it Python 3.5 but get timeout error in here
great kata, BattleRattle. At first it was tempting to convert to decimal then to the target alphabet, but a direct conversion seemed a bit better.
This comment has been hidden.
Might have been a CW issue, nothing to do with the kata.
Enjoyed this Kata a great deal. I certainly learned how to convert between bases in a mathematical sense, however the crux of my solution involved converting to decimal as middle-step, which was probably not needed. Will probably refactor at some point to make the solution smaller.
This comment has been hidden.
This comment has been hidden.
Typo - s/to large/too large/
Fixed.
I was using python. Something wrong with the code in test cases area. Need to change "Alphabet.BINARY" to "Alphabet['BINARY']", etc. Otherwise it is inconvenient for other challengers to test their codes. Thank you.
You get around it if you just submit your code but yeah had the same problem
Yep, this problem is still here. First two lines of tests should be replaced to: bin = Alphabet['BINARY']; oct = Alphabet['OCTAL']; dec = Alphabet['DECIMAL']; hex = Alphabet['HEXA_DECIMAL']; allow = Alphabet['ALPHA_LOWER']; alup = Alphabet['ALPHA_UPPER']; alpha = Alphabet['ALPHA']; alnum = Alphabet['ALPHA_NUMERIC'];
this problem is still there
〃
Fixed now.
This comment has been hidden.
The rot13 just means an alphabet with 13 letters in it. This test tests your code for the following note in the description:
-The function must work for any arbitrary alphabets, not only the pre-defined ones.
No, the ROT13 alphabet just displaces all characters in the regular alphabet by 13 places: "NOPQRSTUVWXYZABCDEFGHIJKLM"
Don't use
input
as paramenter name in Python - it's a builtin function to read keyboard inputs! Please rename it todata
or something like that.Bad naming, true. But it's not an issue (perhaps a suggestion).
It was an issue and remains one 5 years later.
Tests use bin, hex, oct as variable names...
This comment has been hidden.
in link you gave a === 1, in this kata a === 0
It appears that the alphabets are indexed from 0, so z would be 25, a, 26, and b would be 27 if iterating past the last element restarts at the first element.
@stealthguy Don't worry I felt the same way particularly when I hit convert("hello", allow, hex) # 320048
Solved
Enjoyed this kata a lot! Thank you, BattleRattle! Please take a look at my solution in Ruby :)
Really nice kata. A lil tricky and challenging but enjoyable at the same time!
You may want to review the description in Python, as the listed alphabets are just not listed (I looked at them in the js version, though).
Great Kata! I enjoyed it a lot. Thanks BattleRattle!
This comment has been hidden.
Fixed now.
This comment has been hidden.
Oh. Wow. Well, I guess it's fine, since a solution that doesn't use that module is around the same length. I can't change my translation anymore, but if BattleRattle is against those functions he can disable the module with
hiding [Module "…"]
.Lack of BaseConversion.Normalize (normalize) annoying.
See issue by @fozworth. That's the preloaded-code-doesn't-get-loaded-on-example-test-cases bug, sorry. That hasn't been an issue when I first translated the kata to Haskell. See my post below for a temporary fix.
Traceback: in NameError: name 'Alphabet' is not defined
For python, I cannot run the tests.
Seconding this. In the default python tests, Alphabet is a dict but it's treated like an object. Alphabet.TYPE should be changed to Alphabet['TYPE'].
Fixed
I cannot run the tests.
That's a known problem with preloaded code. That happened shortly after I translated the kata, sorry. However, you can simply remove the import and add
normalize
, which should be something likeWorks fine now.
I could not understand this conversion:
convert("27", Alphabet.DECIMAL, Alphabet.ALPHA_LOWER); // should return "bb" convert("hello", Alphabet.ALPHA_LOWER, Alphabet.HEXA_DECIMAL); // should return "320048"
Great little kata. Not too taxing, but a nice little brain teaser to keep you occupied for a few minutes. Well done.
This comment has been hidden.
You're right. This is fixed now :)
This comment has been hidden.
Thanks for your feedback. I added the point about negative numbers to the description and also changed it to be a 4 kyu.
Just to be pedantic, I'd mention that you don't have to deal with numbers with decimal points too. Otherwise, great kata!
This comment has been hidden.