Convert alphanumeric (base-36) sentences into hexadecimal (base-16) sentences

-2

I use "sentence" here loosely to mean "any sequence of words separated by spaces" and "words" to mean "sequences of alphanumeric (hexatrigesimal) characters unseparated by spaces".


Conversion is from base-36 (0123456789abcdefg...) or (0123456789ABCDEFG...)

Conversion to base-16 (0123456789abcdef) or (0123456789ABCDEF)

Bonus -1 off your byte score if you are case-insensitive on input

Bonus -1 off your byte score if you are insensitive to non-alphanumeric characters (ignore them, except space)


Conversion of sentences

E.g. if your input sentence is "The meaning of life is 31313" then your out put should convert the following base-36 "words" into base-16 numbers (the, meaning, of, life, is, 31313) and then output them in order.

Bonus -1 off your byte score for retaining spaces in output sentence

Test Cases

You may convert these to your preferred single-case form if you choose not to opt for the case-insensitive bonus, strings end after spaces. Assume input will never start with a space.


Input text:

"The meaning of life is 31313"

Output: ("9542 B59F1602C 36F F508A 2A4 4DA897") - with or without spaces, with lower or upper case letters

Bonus -1 off your byte score for removing leading zeroes i.e. "9542" not "000009542".


Input text: 

"Jet fuel can't melt steel beams"

Output: ("6245 B47AD 8C0F9 FF371 2E28A2D 1241854")

Input text: 

"fhs8230982t90u20's9sfd902 %2'+13jos32*'ej eos"

Output: ("74510A686B453A2B1933C7AADA25DD2 BB8E18A11AEB 4A5C")

The Winner

This is , so the fewest bytes wins (including bonus -1 bytes). :)

Can accept the inputs in any way that works, as long as your code can solve arbitrary problems like the test cases.

DrQuarius

Posted 2019-05-25T00:46:20.053

Reputation: 562

8bonus -1 bytes only one byte? Also, bonuses in code-golf are generally discouraged – MilkyWay90 – 2019-05-25T01:22:25.107

Yes, I had a feeling it might be like that, but I wanted to give a minor incentive. All four bonuses add up nicely for any language that implicitly does them, but still definitely not worth adding characters to solve them. – DrQuarius – 2019-05-25T01:48:15.420

7@DrQuarius The bonuses don't add anything interesting for the golfer -- either the language base-conversion has that feature and it's a free -1 byte, or it doesn't and it's not worth adding anything to their code to achieve it. The bonuses are just clutter in the challenge and in scoring. – xnor – 2019-05-25T02:23:59.597

2@xnor hmm, thanks for the explanation. I will avoid them in future, but don't want to 'move the goalposts' now, so to speak. – DrQuarius – 2019-05-25T02:37:51.450

By removing leading zeros do you mean ignoring them in input or not outputting them? – Embodiment of Ignorance – 2019-05-25T03:25:58.973

@EmbodimentofIgnorance in the output: so that 000009542 becomes 9542. – DrQuarius – 2019-05-25T03:40:05.740

5Very tempted to -1 this for getting the mean of life so very wrong! – Shaggy – 2019-05-25T11:42:54.277

Answers

2

Japt -S, 6 - 3 = 3 bytes

¸®n36G

Saved two bytes thanks to @Shaggy

Try it

Embodiment of Ignorance

Posted 2019-05-25T00:46:20.053

Reputation: 7 014

Very nice, and you do indeed remove leading zeros. so yeah 8-3=5 (but you said 8-2=5 in current edit). You also ignore apostrophes but get NaN for the %2'+13jos32*'ej string in the third test case, so -3 out of -4 bonus. – DrQuarius – 2019-05-25T03:45:03.887

I think yours will probably be the winner. :) Your Japt golfing is marvellous! – DrQuarius – 2019-05-25T03:46:49.033

n36G saves you 2 bytes. – Shaggy – 2019-05-25T07:35:47.677

1@DrQuarius, that "bonus" would cost at least 4 bytes. – Shaggy – 2019-05-25T11:43:46.023

2

Perl 6, 28 bytes - 2 = 26

{S:g{\S+}=:36(~$/).base(16)}

Try it online!

Jo King

Posted 2019-05-25T00:46:20.053

Reputation: 38 234

So fast! and very nice! – DrQuarius – 2019-05-25T01:04:28.367

2

MATL array, 9 - 1 = 8 bytes

Yb36ZA1YA

Try it online!

Case-insensitive, but turns spaces into line breaks.

MATL inline, 20-2 = 18 bytes (inelligible)

Yb36ZA1YAO10Z(!999e!

Try it online!

Takes case-insensitive and preserve-spaces bonuses, (last test case fails due to implicit size assumptions = inelligible submission)

Yb     % Split the input at spaces, place the results in a cell array
36ZA   % convert from base-36
1YA    % Convert to hexadecimal. Gives a char array 

O10Z(  % Assign char 0 to 10th column. Adds "Spaces"
!999e! % Reshape in row-major order as a 999-column char array
       % Implicitly display. Display 0 as space.

(I hope Luis Mendo comes and improves on this, because I got most of this from his answers here, here, here and sundar's answer here.)

DrQuarius

Posted 2019-05-25T00:46:20.053

Reputation: 562

Good job. I came but I don't see how to improve it :-D – Luis Mendo – 2019-05-27T09:36:16.663

1

Ruby -p, 23-1 = 22 bytes

Only uses the case-insensitive bonus.

$_=$_.to_i(36).to_s(16)

Try it online!

Ruby -p, 33-2 = 31 bytes

Takes case-insensitive and preserve-spaces bonuses.

gsub(/\w+/){$&.to_i(36).to_s(16)}

Try it online!

Ruby -p, 46-3 = 43 bytes

Takes all 3 bonuses.

gsub(/\S+/){$&.gsub(/\W/){}.to_i(36).to_s(16)}

Try it online!

Value Ink

Posted 2019-05-25T00:46:20.053

Reputation: 10 608

No need for parentheses around the last method's argument. – manatwork – 2019-05-25T13:43:41.873

1

05AB1E, score 3 (4 bytes - 1 bonus)

#₆öh

Bonus -1: no leading 0s, since this is done implicitly.
Outputs as a list of converted words.

Try it online.


05AB1E, score 8 (12 bytes - 4 bonus)

u#εžKÃ}₆öhðý

All four bonuses:
-1 for no leading 0s, since this is done implicitly;
-1 case-insensitivity by first converting to uppercase with u;
-1 for retaining spaces in the output by joining the list by spaces with ðý;
-1 for ignoring every character apart from [A-Za-z0-9 ], by keeping [A-Za-z0-9] after the split_on_spaces with εžKÃ}

Try it online.

Explanation:

#             # Split the (implicit) input-string on spaces
 ₆ö           # Convert each string from base-36 to integers
   h          # Convert those integers to hexadecimal
              # (and output the list implicitly as result)

u             # Convert the (implicit) input to uppercase
 #            # Split it on spaces
  ε           # Map each string to:
   žK         #  Push builtin string "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
     Ã        #  And only keep those characters in the mapped string
      }₆ö     # After the map: convert each string from base-36 to integer
         h    # Convert those integer to hexadecimal
          ðý  # And join them by spaces
              # (after which the result is output implicitly)

Kevin Cruijssen

Posted 2019-05-25T00:46:20.053

Reputation: 67 575

0

Python 2, 47 - 2 = 45 bytes

lambda S:[hex(int(s,36))[2:]for s in S.split()]

Try it online!

Because case insensitive. And also leading 0s.

Chas Brown

Posted 2019-05-25T00:46:20.053

Reputation: 8 959

0

Perl 5 -Mbigint -MList::Util=reduce -p, 92 - 4 = 88 bytes

y/_/*/;s|\S+|Math::BigInt::as_hex reduce{$a*36+$b}map{7x/\d/+ord(uc)-55}$&=~/\w/g|ge;s/0x//g

Try it online!

Qualifies for all 4 bonuses (case insensitive, keeps spaces, ignores punctuation, no leading 0s). Handles even large test cases properly, including the last one.

Xcali

Posted 2019-05-25T00:46:20.053

Reputation: 7 671

0

Charcoal, 14 bytes - 1, score 13

⪫E⪪S ⍘⍘ι³⁶¦¹⁶ 

Try it online! Link is to verbose version of code. Only qualifies for the leading zeros bonus. Explanation:

   S            Input string
  ⪪             Split on spaces
 E              Map over words
       ι        Current word
      ⍘ ³⁶      Convert from base 36
     ⍘     ¹⁶   Convert to base 16
⪫               Join on spaces
                Implicitly print
  • If output of the hex numbers on separate lines is acceptable, then the first and last characters can be removed for a 2-byte saving.
  • Case insensitive input is possible at a cost of 1 byte, which would not affect the score.
  • Preservation of multiple spaces is possible at a cost of 2 bytes, unless the current misbehaviour is a Charcoal bug, in which case it would work once the bug is fixed.
  • Skipping of punctuation is possible at a cost of 4 bytes. (Currently the only supported case is a single trailing .)

Neil

Posted 2019-05-25T00:46:20.053

Reputation: 95 035

0

Java, 94-2 = 92 bytes

These "bonuses" are too expensive to intentionally claim. I get case insensitive and leading zeros for free though.

s->{for(var w:s.split(" "))System.out.print(new java.math.BigInteger(w,36).toString(16)+" ");}

TIO

Benjamin Urquhart

Posted 2019-05-25T00:46:20.053

Reputation: 1 262

0

Gema, 20 characters

<A>=@radix{36;16;$1}

Sample run:

bash-4.4$ gema '<A>=@radix{36;16;$1}' <<< 'The meaning of life is 31313'
9542 B59F1602C 36F F508A 2A4 4DA897

Try it online!

manatwork

Posted 2019-05-25T00:46:20.053

Reputation: 17 865

0

Ecmascript 2020, score 90 - 3 = 87

Likely it would be there, already works in Firefox 68+ and Chrome 67+.

s=>s.replace(/\w+/g,x=>[...x].reduce((r,x)=>36n*r+BigInt(parseInt(x,36)),0n).toString(16))

Bonuses:

  • Bonus -1 off your byte score if you are case-insensitive on input
  • Bonus -1 off your byte score for retaining spaces in output sentence
  • Bonus -1 off your byte score for removing leading zeroes i.e. "9542" not "000009542".

Test:

f=s=>s.replace(/\w+/g,x=>[...x].reduce((r,x)=>36n*r+BigInt(parseInt(x,36)),0n).toString(16))

console.log([
["The meaning of life is 31313", "9542 B59F1602C 36F F508A 2A4 4DA897"],
["Jet fuel can't melt steel beams", "6245 B47AD 8C0F9 FF371 2E28A2D 1241854"],
["fhs8230982t90u20's9sfd902 %2'+13jos32*'ej eos", "74510A686B453A2B1933C7AADA25DD2 BB8E18A11AEB 4A5C"],
["  123   1234   tyu ", "  55B   C0D0   97B6 "],
].map(([t,k])=>f(t.replace(/(?!\s|_)\W/g,"")).toUpperCase()==k))

Qwertiy

Posted 2019-05-25T00:46:20.053

Reputation: 2 697