ASCII Character Jumble


24

Tulis sebuah program yang memasukkan string yang terdiri dari karakter yang dapat dicetak (ASCII 20-7E) dan integer ndalam [2,16] dan lakukan modifikasi pada string tersebut.

  • Setiap karakter dalam string dikonversi ke kode ASCII-nya (contoh yang diberikan dalam heksadesimal, meskipun basis 10 juga dapat diterima).
  • Kode ASCII dikonversi menjadi markas ndan digabung menjadi satu.
  • String baru dibagi setiap karakter lainnya. Jika ada jumlah karakter ganjil, maka karakter terakhir dihapus seluruhnya.
  • Mencetak kode ASCII (pada basis 16) dikonversi kembali ke dalam karakter mereka, sedangkan kode ASCII yang tidak dicetak dihapus.
  • String yang dihasilkan dicetak.

Kasus cobaan

Memasukkan

Hello, World!
6

Tangga

Hello, World!
48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21
2002453003003031125222330331030024453
20 02 45 30 03 00 30 31 12 52 22 33 03 31 03 00 24 45

Output dari program ini adalah E001R"31$E.


Ini kode golf, jadi aturan standar berlaku. Kode terpendek dalam byte menang.


Algoritma pengodean ini dapat bermanfaat untuk mengirim pesan rahasia!
Kritixi Lithos

Harus melakukan ini kapan-kapan!
anOKsquirrel

@ ΚριτικσιΛίθος selagi program bekerja untuk setiap string input yang mungkin, tidak setiap string output unik. Misalnya, dalam basis 7, string Jakan melalui langkah J-> 50-> 101-> 10-> (no output), seperti halnya string Katau L.
Arcturus

Like @Eridan said, this is a lossy encryption since odd sequences get the last character lopped off. Though I'm sure to the ignorant observer it could be a snarky way of communicating :)
DoctorHeckle

1
Step 1 is confusing - no need to convert chars to hexadecimal - in the example: H is ASCII 72 (decimal) or 48 (hex), but what I need is 200 (base 6). All the row 2 in the example is useless and confusing in my opinion
edc65

Jawaban:




3

Bash + common linux utils, 118

printf %s "$1"|xxd -p|sed -r "s/../\U& /g;y/ /n/;s/^/dc -e$2o16i/e;s/../& /g;s/ .$//;"|xxd -rp|sed 's/[^[:print:]]//g'

I think you could shorten printf %s "$1" into echo -n "$1" to save 2 bytes
Aaron

@Aaron That works until the input string is -e. Try echo -n "-e"
Digital Trauma

Damn, nicely spotted !
Aaron

2

CJam, 24 bytes

l:irifbe_2/{Gbc',32>&}/

Note that there is a DEL character (0x7F) between ' and ,. Try it online in the CJam interpreter.

How it works

l:i                     Read a line from STDIN and cast each char to integer. 
   ri                   Read another integer (base) from STDIN.
     fb                 Convert each integer from line 1 to that base.
       e_2/             Flatten and split into chunks of length 2.
                        If the last chunk has only one element, it will get
                        converted into a control character, which will be
                        removed later.
          {         }/  For each digit pair:
           Gb             Convert the pair from base 16 to integer.
             c            Cast to character.
              ',          Push the string of ASCII characters up to '~'.
                32>       Remove the first 32 (control characters).
                   &      Intersect.

What about the DEL character...? It looks like you've worked around that, but I can't see it in your explanation!
wizzwizz4

StackExchange filters unprintable characters. The DEL character is only present in thr permalink, and invisible even there.
Dennis

I mean... Delete is a control character, but is not one of the first 32 characters. It is character number 127, 0x7F, for people who are not familiar with ASCII.
wizzwizz4

I'm not sure I understood your question. Are you wondering how I filter it from the output?
Dennis

Yes. Your code explanation doesn't seem to say how you filter the DEL character from the output.
wizzwizz4

2

JavaScript (ES6), 137 147

Using the most verbose functions available in JavaScript

f=(s,b)=>alert(s.replace(/./g,x=>x.charCodeAt().toString(b)).match(/../g).map(x=>(x=String.fromCharCode('0x'+x))<='~'&x>' '?x:'').join``)

// Just for test purpose, redefine alert()
alert=x=>document.write('<pre>'+x+'</pre>')

f('Hello, World!',6)
f('PORK',3)


+1 for x=>x>=
Ypnypn

I think you can save some bytes by using [for(z of ...)if(...)...] instead of map(...).filter(...)
Ypnypn

@Ypnypn I did not found a way to use your hint (apart from using array comprehension that is ES7) but you pressed me to rethink it all. Thx. I hope you'll keep your +1 even if x=>x>= has gone
edc65

1
What's wrong with using ES7?
Ypnypn

1
@Ypnypn I prefer an answer that can work even with subpar javascript engines <troll on>like Chrome</troll off>
edc65

1

Julia, 118 bytes

f(s,n)=join(map(i->(c=string(Char(parse(Int,i,16))))^isprint(c),matchall(r"..",join(map(i->base(n,Int(i)),[s...])))))

Ungolfed:

function f(s::AbstractString, n::Integer)
    # Construct an array of ASCII codes in base n
    v = map(i -> base(n, Int(i)), [s...])

    # Join into a string and get all pairs, truncating
    # to an even length
    m = matchall(r"..", join(v))

    # Parse each pair as an integer in base 16, get the
    # character associated with that code point, convert
    # to a string, and include if it's printable
    x = map(i -> (c = string(Char(parse(Int, i, 16)))^isprint(c), m)

    # Join as a string and return
    return join(x)
end

1

Mathematica, 134 bytes

Print@FromCharacterCode@Select[#~FromDigits~16&/@StringPartition[""<>ToCharacterCode@InputString[]~IntegerString~Input[],2],31<#<127&]

If a function is allowed:

Mathematica, 112 bytes

FromCharacterCode@Select[#~FromDigits~16&/@StringPartition[""<>ToCharacterCode@#~IntegerString~#2,2],31<#<127&]&

1

TeaScript, 23 bytes

TeaScript is JavaScript for golfing

£lc¡T(y©K(2)ßC(P(l,16±µ

Relatively straight-forward but delightfully short. I can probably golf down a few more characters with some more operators. A few other new features might also be able to be used to cut down some bytes.

Ungolfed && Explanation

x.l(#
    l.c().T(y)
).K(2)
.m(#
    C(
      P(l,16)
    )
).j``

1
I believe this is 23 chars (29 bytes) long.
Cristian Lupascu

@w0lf That would be with UTF-8 encoding but because all characters are less then 256 we can safely count them as one byte
Downgoat

1

Ruby 92

->s,n{o=''
s.chars.map{|x|x.ord.to_s n}.join.scan(/../).map{|x|x>?2&&x<?8&&o<<x.to_i(16)}
o}

Online test here.


1

Python 2, 174 bytes

def J(a,b,i=0):
 h=r=''
 B=lambda n,b:n*'x'and B(n/b,b)+chr(48+n%b+7*(n%b>9))
 for c in a:h+=B(ord(c),b)
 while i<len(h):v=int(h[i:i+2],16);r+=chr(v)*(31<v<127);i+=2
 print r

Try it here

Not really the best tool for the job. Since Python has no convert-to-arbitrary-base function, I had to implement my own. That was fun, at least--particularly finding a [marginally] shorter expression for the digits than "0123456789ABCDEF"[n%b]. For iterating over two characters at a time, I found a while loop was slightly shorter than a functional approach.

181 bytes as a full program:

B=lambda n,b:n*'x'and B(n/b,b)+chr(48+n%b+7*(n%b>9))
a=raw_input()
b=input()
h=r=''
for c in a:h+=B(ord(c),b)
i=0
while i<len(h):v=int(h[i:i+2],16);r+=chr(v)*(31<v<127);i+=2
print r

0

MATLAB, 103 bytes

function k(s,n),b=dec2base(s,n)';b(~cumsum(b-'0',1))='';c=base2dec(textscan(b,'%2c'),16)';char(c(c>31))

I've written a function k that takes a string s and an integer n as input. e.g.:

k('Hello, World!',6)

gives

 E001R"31$E

Most annoying thing I had to work around is leading zeros showing up when converting to base n. Getting these out of the array that was to be split after every 2nd character cost quite a lot of bytes. Not sure if it is possible to save any more bytes using this approach.


0

PHP - 286 bytes

Put the string in $s and the integer in $b.

<?php $s=$_GET["s"];$b;$m="array_map";echo implode($m(function($v){return ctype_print($v)?$v:"";},$m("chr",$m("hexdec",str_split(strlen(implode($a=$m(function($v){global$b;return base_convert($v,16,$b);},$m("dechex",$m("ord",str_split($s))))))%2==1?substr(implode($a),0,-1):$a,2)))));?>

Pass the value to GET["s"].

Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.