[关闭]
@aloyschen 2017-10-04T13:37:06.000000Z 字数 1267 阅读 76

CodeWars 刷题总结

CodeWars


Base Conversion

In this kata you have to implement a base converter, which converts positive integers between arbitrary bases / alphabets. Here are some pre-defined alphabets:

  1. bin = '01'
  2. oct = '01234567'
  3. dec = '0123456789'
  4. hex = '0123456789abcdef'
  5. allow = 'abcdefghijklmnopqrstuvwxyz'
  6. allup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  7. alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  8. alphanum = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

The function convert() should take an input (string), the source alphabet (string) and the target alphabet (string). You can assume that the input value always consists of characters from the source alphabet. You don't need to validate it.

Examples

  1. convert("15", dec, bin) ==> "1111"
  2. convert("15", dec, oct) ==> "17"
  3. convert("1010", bin, dec) ==> "10"
  4. convert("1010", bin, hex) ==> "a"
  5. convert("0", dec, alpha) ==> "a"
  6. convert("27", dec, allow) ==> "bb"
  7. convert("hello", allow, hex) ==> "320048"

Additional Notes:

The maximum input value can always be encoded in a number without loss of precision in JavaScript. In Haskell, intermediate results will probably be too large for Int.
The function must work for any arbitrary alphabets, not only the pre-defined ones
You don't have to consider negative numbers

知识点

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注