พิมพ์ตัวอักษรที่หายไป


18

ความท้าทายง่ายแรงบันดาลใจจากความนิยมของฉันก่อนหน้าข้อความที่มองไม่เห็นการพิมพ์และการพิมพ์ข้อความที่มองไม่เห็นจริงความท้าทายและความยาวสตริงที่แตกต่างกันความท้าทาย

รับสตริงที่ประกอบด้วยอักขระที่พิมพ์ได้เท่านั้น ( 0x20 to 0x7E) พิมพ์อักขระที่พิมพ์ได้ทุกตัวที่ไม่มีอยู่ในสตริง

อินพุต

สตริงหรืออาร์เรย์ของอักขระประกอบด้วยอักขระ ASCII ที่พิมพ์ได้เท่านั้น

เอาท์พุต

อักขระ ASCII ที่พิมพ์ได้ทุกตัวไม่มีอยู่ในอินพุตสตริงในลำดับใด ๆ

Testcases

Input:  "Hello, World!"
Output: ""#$%&'()*+-./0123456789:;<=>?@ABCDEFGIJKLMNOPQRSTUVXYZ[\]^_`abcfghijkmnpqstuvwxyz{|}~"
========
Input:  "Hi!"
Output: " "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghjklmnopqrstuvwxyz{|}~"
========
Input:  ""
Output: " !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
========
Input:  " !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
Output: ""

เกณฑ์การให้คะแนน

นี่คือโค้ดกอล์ฟที่มีจำนวนน้อยที่สุดในแต่ละภาษาที่ชนะ


หากส่งคืนอาร์เรย์เราสามารถรวมองค์ประกอบที่ว่างเปล่าแทนอักขระที่ใช้แล้วได้หรือไม่?
Shaggy

@Shaggy แน่ใจว่าใช้ได้
Skidsdev

@Rod ไม่รั่วไหลแผนของฉัน D:
Skidsdev

เอาท์พุทสามารถเป็นวัตถุชุดของสตริงของตัวละคร? set( 'a', 'b', 'c' )
Brad Gilbert b2gills

1
@MikhailV เฉพาะในกรณีที่ภาษาของคุณไม่สามารถส่งออกอักขระ ASCII ได้
Skidsdev

คำตอบ:



6

Perl 6 , 29 ไบต์

{[~] keys (' '..'~')∖.comb}

โปรดทราบว่าผลลัพธ์เป็นแบบสุ่มเนื่องจากSet s ไม่ได้เรียงลำดับ

ทดสอบมัน

ขยาย:

{
  [~]        # reduce using string concatenation
             # (shorter than 「join '',」)

  keys       # get the keys from the Set object resulting from the following

  (' '..'~') # Range of printable characters
            # Set minus (this is not \ )
  .comb      # split the input into individual characters
}

นอกจากนี้ยังมีรุ่น ASCII (-)ด้วย แต่จะต้องมีช่องว่างก่อนจึงจะไม่แยกวิเคราะห์เป็นการเรียกรูทีนย่อย



5

Japt , 14 ไบต์

Ho#_dÃf@bX ¥J

ลองออนไลน์!

บันทึกแล้ว 4 ไบต์ขอบคุณ Shaggy และ obarakon


1
ไม่จำเป็นต้องมีการตั้งค่าสถานะ (ดูคำตอบสำหรับความคิดเห็นของฉันในคำถาม) แทนที่127ด้วย#เพื่อบันทึกไบต์และลบการUบันทึกอื่น
Shaggy

1
คุณสามารถใช้¦และจัดเรียงอาร์กิวเมนต์ของคุณใหม่เพื่อบันทึกสองสามไบต์ นอกจากนี้ 127 สามารถย่อTIO
Oliver

1
รับลงไปที่ 14 ไบต์: ethproductions.github.io/japt/…
Shaggy

1
ไม่คุณทำงานกับมันทอม - อย่างที่คุณพูดไปก่อนหน้านี้ : D
Shaggy

1
รุ่น 10 ไบต์ แต่น่าเสียดายที่ไม่ใช่การแข่งขัน: ethproductions.github.io/japt/…
Shaggy


4

MATL , 5 ไบต์

6Y2X~

ลองออนไลน์!

ขอบคุณ Luis Mendo สำหรับ เล่นกอล์ฟ 8 ไบต์!

คำอธิบาย:

   X~   % The symmetric set difference
6Y2     % Between all printable ASCII
        % And the input string (implicit)
        % Implicitly display

ความแตกต่างของชุดสมมาตรจะให้ทุกองค์ประกอบที่มีอยู่ในหนึ่งในสองชุดอินพุต (แต่ไม่ใช่ทั้งคู่) นี่จะให้คำตอบที่ถูกต้องเสมอเนื่องจากชุดอินพุตจะเป็นชุดย่อยของชุดที่สองเสมอ (ASCII ที่พิมพ์ได้ทั้งหมด)

รุ่นเดิม:

32:126tGom~)c

คำอธิบาย:

32:126          % Push the range 32-126
      t         % Duplicate it on the stack
       G        % Push the input
        o       % Convert it to character points
         m      % Is member (0 for each char that isn't in input, 1 for each char that is)
          ~     % Logical NOT
           )    % Take the truthy elements of this array from the previous array (All Printable ASCII)
            c   % Display as a string


3

JavaScript (ES6), 74 ไบต์

ฉันแน่ใจว่ามีวิธีที่สั้นกว่านี้!

s=>[...Array(95)].map((_,y)=>s.includes(c=String.fromCharCode(y+32))?"":c)

ลองมัน

let f=
s=>[...Array(95)].map((_,y)=>s.includes(c=String.fromCharCode(y+32))?"":c)
oninput=_=>o.innerText=f(i.value).join``
o.innerText=f(i.value="Hello, World!").join``
<input id=i><pre id=o>


1
ควรจะArray(95)รวมถึงการหายไป~
Malivil

ที่นั่นเสมอ @Malivil? ได้สาบานว่าตัวละครตัวสุดท้ายคือ}ตอนที่ฉันเขียนมันขึ้นมา แก้ไขแล้วขอบคุณ
Shaggy

ไม่อยากเชื่อเลยว่า C # จะสั้นกว่า JavaScript สำหรับสิ่งนี้โดยเฉพาะเมื่อฉันต้องรวมการใช้งานของฉันด้วย
TheLethalCoder

1
@TheLethalCoder String.fromCharCodeเป็นคอมไพล์นั่นเป็นเหตุผล! : D
Shaggy

@Shaggy ฉันไม่ได้ดูรหัสของคุณเพื่อดูว่ามันทำงานอย่างไรและฉันพิมพ์สัญลักษณ์ทุกตัวบนคีย์บอร์ดของฉันและสังเกตุเห็น~ว่าไม่ได้เปลี่ยนอะไรเลย นอกจากนี้จำเป็นต้องอัปเดตส่วน "ลองใช้" ด้วย
Malivil


3

อ็อกเทฟ22 22ไบต์

ขอบคุณ @Luis Mendo บันทึก 2 ไบต์

@(s)setxor(32:'~',s)

ลองออนไลน์!

คำตอบอื่น ๆ :

@(s)setdiff(' ':'~',s)

ลองออนไลน์!


1
@(s)setxor(' ':'~',s)ประหยัด 1 ไบต์
Luis Mendo

@LuisMendo ดีมาก! แต่ฉันคิดว่านั่นเป็นสิ่งที่แตกต่าง ฉันขอแนะนำให้คุณโพสต์คำตอบใหม่ :)
rahnema1

1
ไม่มันเป็นเพียงการปรับปรุงเล็กน้อย ฉันจะดีใจที่คุณโพสต์ถ้าคุณต้องการ BTW @(s)setxor(32:'~',s)ดูเหมือนว่าจะทำงานด้วย --- และความคิดเห็นแบบเดียวกันกับที่ :-)
Luis Mendo

1
@ LuisMendo ขอบคุณฉันเห็นด้วยเพราะ (Luis) กล่าว
rahnema1

2

PHP, 42 ไบต์

อินพุตเป็นอาร์เรย์

เอาต์พุตเป็นสตริง

<?=join(array_diff(range(" ","~"),$_GET));

ลองออนไลน์!

PHP, 53 ไบต์

อินพุตเป็นสตริง

เอาต์พุตเป็นสตริง

<?=join(array_diff(range(" ","~"),str_split($argn)));

แทนที่<?=joinด้วยprint_rสำหรับผลลัพธ์เป็นอาร์เรย์

ลองออนไลน์!


บางทีคุณควรสร้าง PHP เวอร์ชันกอล์ฟ: P
CalculatorFeline

@CalculatorFeline ฉันแน่ใจว่ามีอยู่จริง แต่มันไม่ดีจริงๆ
JörgHülsermann

บางทีคุณควรทำให้ดี ขั้นตอนที่ 1: แท็กเริ่มต้นอัตโนมัติ
CalculatorFeline

@CalculatorFeline ฉันมีการค้นหาลิงก์สำหรับคุณ github.com/barkermn01/PGP-php-CodeGolfฉันไม่มีความสนใจที่จะเข้าร่วม
JörgHülsermann

1
@CalculatorFeline การทำให้ PHP เป็นภาษากอล์ฟทำลายสิ่งที่สนุกในการเล่นกอล์ฟด้วย PHP (อย่างน้อยสำหรับฉัน): คุณต้องสมดุลระหว่างฟังก์ชั่นการโทรอย่างต่อเนื่อง (ซึ่งมักจะมีชื่อยาว) โดยใช้ลูป Step 1: automatic starting tagดีphp -r... แต่เช่นในตัวอย่างนี้มันไม่ได้จ่ายเงินเพราะมีความยาวมากกว่าecho <?=
Christoph


2

Perl, 39 ไบต์

s!.*!"pack(c95,32..126)=~y/$_//dr"!ee

perl -peทำงานด้วย


ฉันได้รับข้อความแสดงข้อผิดพลาด 'Bareword พบว่าผู้ควบคุมคาดว่าจะอยู่ที่ (eval 1) บรรทัดที่ 2 ใกล้กับ "y / Hello World! // dr"' เมื่อฉันเรียกใช้ ...
Chris

บรรทัดคำสั่งแบบเต็ม: echo 'Hello World!' | perl -pe 's!.*!"pack(c95,32..126)=~y/$_//dr"!ee'. สิ่งนี้ใช้ได้กับฉันทั้ง Perl v5.14 และ v5.24
Grimmy

มันคือ Perl v5.10 ที่ใช้งานไม่ได้ ... จะต้องมีการเปลี่ยนแปลงระหว่าง 5.10 และ 5.14
Chris

2

brainfuck , 120 ไบต์

+[+[>+<+<]>]>-[[>>]+[<<]>>-],[<+++++[>------<-]>-[>[>>]+[<<]>-]>[>>]<[-]<[-<<]>,]++++++++[->++++<]>[>[-<.>]<[->>+<<]>>+]

ลองออนไลน์!

ห่อ:

+[+[>+<+<]>]>-[[>>]+[<<]>>-],[<+++++[>--
----<-]>-[>[>>]+[<<]>-]>[>>]<[-]<[-<<]>,
]++++++++[->++++<]>[>[-<.>]<[->>+<<]>>+]

อธิบาย:

+[+[>+<+<]>]>-         initialize what we will now consider cell 0 to 95
[[>>]+[<<]>>-]         initialize cells 2 4 etc 95*2 to 1; end on cell 0 at 0
,[                     main input loop (for each char of input)
  <+++++[>------<-]>-  subtract 31 from the input
  [>[>>]+[<<]>-]       lay a trail of (input minus 31) ones in the empty spaces
  >[>>]<[-]<[-<<]>     use the trail to clear the appropriate "print" flag
,]                     keep reading input until it ends
++++++++[->++++<]>     initialize the cell directly before flag 1 to 32
[                      we'll let the accumulator overflow; no harm done
  >[-<.>]              print the accumulator if the flag is still set
  <[->>+<<]>>+         shift over the accumulator and increment it
]


2

Ruby, 23 18 17 bytes

->s{[*' '..?~]-s}

Uses a lambda function as per @sethrin's comments.

Previous versions:

[*' '..?~]-s.chars

(' '..'~').to_a-s.chars

Doesn't s have to be either read from STDIN or supplied as a function argument? The challenge also specifies that the input may be given as an array of characters. Converting to a stabby lambda and dropping chars gives a 16-byte solution.
canhascodez

I wasn't really sure how the input was to be addressed, given that it wasn't explicitly specified. There are a few other answers that assume the existence of the input in a variable. Is there a codegolf convention? I don't do this much.
Mark Thomas

@sethrin With a stabby lambda wouldn't it be 20 chars? ->(s){[*' '..?~]-s)}
Mark Thomas

All of the parentheses in your lambda are optional. But I may have miscounted a byte. Other languages either accept input implicitly or have stdin bound to a global variable. In Ruby $< is a shortcut for stdin but lambdas tend to be shorter. The conventions on input and output are here. I also don't do this much, so if the rules aren't what I think do let me know.
canhascodez

2

APL, 13 bytes

⍞~⍨⎕UCS31+⍳95

Straightforward:

       31+⍳95  ⍝ A vector 32 .. 126
   ⎕UCS        ⍝ as characters
 ~⍨            ⍝ without
⍞              ⍝ those read from character input.

1

R, 50 bytes

function(s)intToUtf8(setdiff(32:126,utf8ToInt(s)))

returns an anonymous function. Converts the input string to integers, computes the set difference between the printable range and the input values, and then converts them back to a string and returns it.

Try it online!


1

PHP, 53 bytes

for($k=31;$k++<126;)~strstr($argn,$k)?:print chr($k);
# or
for($k=31;$k++<126;)echo~strstr($argn,$k)?"":chr($k);

Run as pipe with -r.


I have left no more playground
Jörg Hülsermann

@JörgHülsermann You do. You just have to share it.
Titus

1

C#, 74 71 bytes

using System.Linq;s=>new int[95].Select((n,i)=>(char)(i+32)).Except(s);

Old version with creating a range for 74 bytes:

using System.Linq;s=>Enumerable.Range(32,95).Select(n=>(char)n).Except(s);




1

Jelly, 8 bytes

Really, 8 bytes? Please, tell me I missed something!

32r126Ọḟ

Try it online!

How?

32r126Ọḟ - Main link: list of characters s
32r126   - inclusive range from 32 to 126 = [32,33,...,125,126]
      Ọ  - cast ordinals to characters = list of printable characters
       ḟ - filter discard if in s

Alternatively

“ ~‘r/Ọḟ - Main link
“ ~‘     - code-page indexes = [32,126]
    r/   - reduce by inclusive range = [32,33,...,125,126]
      Ọ  - cast from ordinals to characters = list of printable characters
       ḟ - filter discard if in s

Since this challenge a new atom which yields all printable ASCII characters, ØṖ, has been introduced making the following work for 3 bytes:

ØṖḟ

No you didn't miss anything.
Erik the Outgolfer

1

Charcoal, 18 15 10 8 bytes

Fγ¿¬№θιι

Try it online! Link is to verbose version of code. Edit: Saved 3 bytes by ranging over characters instead of integers. Saved a further 5 bytes when I discovered the undocumented γ variable which holds the printable ASCII characters. Saved a further 2 bytes when @ASCII-only fixed predefined inputs in verbose mode (the answer is still valid as it stands, it's only the try it online link that wouldn't have worked at the time).


8 bytes (unless preinitialized inputs weren't working back then)
ASCII-only

@ASCII-only They weren't working in verbose mode... they would probably have worked in succinct mode, but I like the verbose links.
Neil



0

shortC, 33 bytes

i;AOi=31;++i<'~';strchr(*@,i)?:Pi

Conversions made in this program:

  • A -> int main(int argc, char **argv) {
  • O -> for(
  • @ -> argv
  • P -> putchar
  • Auto-inserted closing ));}

The resulting program looks like:

i;int main(int argc, char **argv){for(i=31;++i<'~';strchr(*argv,i)?:putchar(i));}

Try it online!


0

Pyth, 17 bytes

Vr32 127I!}CNzpCN

The naive approach.

Explanation:

Vr32 127I!}CNzpCN
Vr32 127             For N in [32, 127[
           CN        Get the ASCII character for the code N
        I!}  z       If it is in the input string...
              pCN    ...then print it

Test it online!


0

Clojure, 60 or 49 bytes

#(apply str(sort(apply disj(set(map char(range 32 127)))%)))

These "apply"s are killing me :/ Oh, if returning a list is fine then this is a bit shorter.

#(sort(apply disj(set(map char(range 32 127)))%))
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.