スペースで区切られたバイナリ文字の文字列を取得し、ASCII文字列に変換します。
例えば...
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
に変換します...
Hello World
バイナリ文字列はと呼ばれる変数に保存されますs。
これはコードゴルフの挑戦なので、最短のソリューションが勝ちます。
ZX81ここで考えています。
スペースで区切られたバイナリ文字の文字列を取得し、ASCII文字列に変換します。
例えば...
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
に変換します...
Hello World
バイナリ文字列はと呼ばれる変数に保存されますs。
これはコードゴルフの挑戦なので、最短のソリューションが勝ちます。
ZX81ここで考えています。
回答:
s.split.map{|x|x.to_i(2).chr}*""
s.gsub(/\w+ ?/){$&.to_i(2).chr}
Chronによる最適化
.join""に*""数文字を保存できます。また、split + map + joinの代わりにgsubを使用して、s.gsub(/\w+ ?/){$&.to_i(2).chr}(31文字)のようにすることもできます。
s.gsub(/\d+./){$&.to_i(2).chr}動作し、それは30文字ですが、なぜそれが動作するのか分かりません。.最後の時間と一致していないはずですが、それはありません。
s.replace(/\d+./g,x=>String.fromCharCode('0b'+x))
編集 @bebe提案の受け入れ-感謝
Edit2バイナリ数値リテラルについて知らなかった-感謝@kapep
Edit3 Wow 6 4バイトは保存されません thx @ETHproductions
コメントで要求された説明
String.replace 2つの引数を取ることができます。
/\d+./g:1つ以上の数字の後に1つの異なる文字が続く-gフラグは、パターンを複数回検索することを指定します文字列の最後にある正規表現は、末尾のスペースなしで数字のシーケンスに一致することに注意してください。この場合、ドットは最後の数字に一致します。「123」.match(/(\ d +)./)を試して確認してください。
(それでも)これまでで最も冗長なJavaScriptの1つです...
(stringへの割り当てはカウントされません)
var s='1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'
var x=
s.replace(/\d+./g,x=>String.fromCharCode('0b'+x))
console.log(x)
s.replace(/\d+./g,x=>String.fromCharCode(parseInt(x,2)))56
s.replace(/\d+./g,x=>String.fromCharCode(eval("0b"+x)))55
/\d+./g,x=>は何をするのか説明してもらえますか?
eval('0b'+x)する'0b'+x-0ように変更できます。
dc<<<2i$s[Pz0\<m]dsmx|rev
mを次のように定義します。
mスタックが空でない場合、マクロを比較して呼び出すm登録してマクロをポップして保存する最初にバイナリ文字列全体をスタックにプッシュするため、各値をポップすると、文字列が逆になります。そのため、revユーティリティを使用して修正します。
$ s="1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
$ dc<<<2i$s[Pz0\<m]dsmx|rev
Hello World
$
38バイトバージョン:
for(int*x=s;putchar(strtol(x,&x,2)););
または、sがポインターの場合は31バイトのみです。
while(putchar(strtol(s,&s,2)));
私はこれがforループとwhileループが正確にどのように使用されることになっているとは思わない...しかしそれは動作します。
smCv+"0b"dPZ
sはPythで有効な変数ではないため、代わりにZを使用したことに注意してください。
説明:
print(
s sum(
m map(lambda d:
C chr(
v eval(
+"0b"d "0b"+d)),
P split(
Z Z))))
例:
=Z"<the binary string from above>"smCv+"0b"dPZ
Hello World
id2、代わりに使用する4文字を保存しませんv+"0b"dか?いずれにせよ、これは読みにくくてクールです。
00000000 30 d2 b4 08 cd 21 2c 30 72 06 d0 e2 08 c2 eb f2 |0....!,0r.......|
00000010 b4 02 cd 21 eb ea |...!..|
マシンコードには実際の文字列変数がないため(特に、 " s" という名前の変数はありません)、入力としてstdinを使用しました。
NASM入力:
org 100h
section .text
start:
xor dl,dl
loop:
mov ah,8
int 21h
sub al,'0'
jb print
shl dl,1
or dl,al
jmp loop
print:
mov ah,2
int 21h
jmp start
-join(-split$s|%{[char][convert]::ToInt16($_,2)})
$ sのバイナリ文字列の単純なループ。[変換]を含めると、スコアが失われます。
編集:Powershellでこれを実行する方法は本当に1つしかありません。ジョーイと私はどちらもほぼ同じ答えを独立して得ました!
入力:
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
出力:
Hello World
ああ、Mathematicaの素敵な関数名
f=FromCharacterCode[#~FromDigits~2&/@StringSplit@#]&
編集:ソリューションを更新、32。
say$s=~s/\d+ ?/chr oct"0b$&"/rge
以前のソリューション(33):
$_=$s;say map{chr oct"0b$_"}split
または
say map{chr oct"0b$_"}split/ /,$s
テスト:
perl -E '$s="1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100";$_=$s;say map{chr oct"0b$_"}split'
u:;(#.@:("."0))&.>cut s
テスト:
s=:'1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'
u:;(#.@:("."0))&.>cut s
Hello World
説明:
cut s NB. split S on spaces
( )&.> NB. for each element
("."0) NB. evaluate each character
@: NB. and
#. NB. convert bitstring to number
; NB. unbox each number
u: NB. convert to ASCII
⎕UCS{2⊥⍎¨⍕⍵}¨⍎s
テスト:
s←'1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'
⎕UCS{2⊥⍎¨⍕⍵}¨⍎s
Hello World
説明:
⍎s:評価しs、整数の配列に変換します。配列はスペースで区切られた数値として書き込まれるため、これは分割されますs。{... }¨:各要素に対して:
⍕⍵:数値を文字列に戻す⍎¨:個々の数字を評価し、ビット文字列を与えます2⊥:base-2デコード、数字を与える⎕UCS:各番号の文字を取得<?=join(array_map('chr',array_map('bindec',explode(' ',$s))))
foreach(str_split($s,8)as$v)echo chr(bindec($v));
str_split($s,8)ません。foreach(explode(' ',$s)as$v)echo chr(bindec($v));有効ですが、とにかく明らかにゴルフではないことが明らかになったPPGCの最初の回答を編集する予定はありません。ともあれ、ありがとう!
Cには標準ライブラリにベース2コンバータがないため、ここでテストしてください
編集:あります、私はそれについて知るにはあまりにも愚かです
r;f(char*s){for(;*s;(*s|32)-32||(putchar(r),r=0))r=2*r|*s++&1;}
Brainfuckには変数がないため、代わりに標準の入力と出力を使用しました。
コード32+は+、インタープリターによって32 秒として解釈される必要があります。インタープリターがRLEをサポートしていない場合は、手動で置き換えてください。
>,[32->+<[16-<[>++<-]>[<+>-]>-<]>[<<.[-]>>-]<,]<.
拡張(非RLE)バージョン:(91バイト)
>,[-------------------------------->+<[----------------<[>++<-]>[<+>-]>-<]>[<<.[-]>>-]<,]<.
コードは、EOFが0としてエンコードされることを前提としています。
次のレイアウトが使用されます。
+---+---+------+
| x | a | flag |
+---+---+------+
どこxASCIIバイトが印刷され、a標準入力からの文字があるとflagあれば1であるaスペースでした。
>, Read a character a into the second cell
[ While not EOF:
32- Decrease a by 32 (a -= ' ')
>+< Set the flag to 1
[ If a was not a space:
16- Decrease by 16 more ('0' == 32+16)
<[>++<-] a += 2*x
>[<+>-] Move it back (x = a)
>-< Reset the flag, it was not a space.
]>
[ If a was a space (flag == 1):
<<.[-] Print and reset x
>>- Reset the flag
]
<, Read the next caracter a
]
<. Print the last character x
Java 8でラムダを使用(75バイト):
Arrays.stream(s.split(" ")).reduce("",(a,b)->a+(char)Byte.parseByte(b,2));
そして、静的インポート(ここで使用されているものもあります)を許可する場合は(61バイト)です:
stream(s.split(" ")).reduce("",(a,b)->a+(char)parseInt(b,2))
forループを使用した短いバージョン(60バイト):
for(String n:s.split(" ")){out.print((char)parseInt(n,2));}
All-Clojure impl:
(apply str(map #(char(read-string(str"2r"%)))(re-seq #"\d+"s)))
Java相互運用機能を使用する場合:
(apply str(map #(char(Long/parseLong % 2))(.split s" ")))
REPLセッション:
golf> (def s "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100")
#'golf/s
golf> (apply str(map #(char(read-string(str"2r"%)))(re-seq #"\d+"s)))
"Hello World"
golf> (apply str(map #(char(Long/parseLong % 2))(.split s" ")))
"Hello World"
NodeJS – 62
Buffer(s.split(' ').map(function(a){return parseInt(a,2)}))+''
PHP – 75
array_reduce(explode(' ', $b),function($a,$b){return $a.chr(bindec($b));});
これは、parseIntまたはevalを使用せずに数値変換を行います。文字列を逆読みし、設定されているビットをカウントします(ビットxが1の場合)。スペースが見つかると、数値が文字に変換され、ビットを設定するために新しい0の数値が開始されます。
x=n=0,w='',s=' '+s
for(i=s.length;i--;){m=s[i]
if(m==1)n|=1<<x
x++
if(m==' ')w=String.fromCharCode(n)+w,n=x=0
}
NS/{:~2bc}/
sはCJamで有効な変数名ではないため、代わりにNを選択しました。
$ cjam <(echo '
> "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
> :N;
> NS/{:~2bc}/
> '); echo
Hello World
NS/ " Split N at spaces. ";
{ }/ " For each chunk: ";
:~ " Evaluate each character ('0' ↦ 0, '1' ↦ 1). ";
2b " Convert from base 2 array to integer. ";
c " Cast to character. ";
Haskellでの私の最初のゴルフの試みです。
map(chr.foldl1((+).(*2)).map digitToInt)$words s
Data.Charをインポートする必要があります
使用法(ghci単位):
Prelude> :m +Data.Char
Prelude Data.Char> let s = "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
Prelude Data.Char> map(chr.foldl1((+).(*2)).map digitToInt)$words s
"Hello World"
説明:
map(chr.foldl1((+).(*2)).map digitToInt)$words s
$words s -- split s on spaces into a list
map digitToInt -- convert each digit in input string to int
((+).(*2)) -- a function that multiplies its first
-- argument by 2, then adds the second argument
foldl1((+).(*2)).map digitToInt -- fold the above over the list of ints:
-- in other words this is a function that reads strings as binary and gives the value as int
(chr.foldl1((+).(*2)).map digitToInt) -- cast to character
map(chr.foldl1((+).(*2)).map digitToInt)$words s -- map our function over the list of words
#CçJ動作します。このバージョンとバージョンの両方で、入力が1文字のみð¡である#場合の代わりに必要です。