我的表情符号干燥了吗?


17

这是我的宠物表情,比利:

-_-

表情符号不喜欢在雨中,所以比利很伤心……让我们给他戴上雨伞让他感觉好些!

  /\
 /  \
/    \

  -_-

很好,他完全被伞遮住了!这是仅覆盖他的一部分的示例:

  /\
 /  \
/    \

     -_-

在这种情况下,他身体的第2部分和第3部分暴露在雨中。

伞有多种形状和大小,但总是由一系列的斜杠/和一系列的反斜杠组成\。例如,这些都是有效的保护伞:

  /\
 /  \
/    \

/\

    /\
   /  \
  /    \
 /      \
/        \

这些不是:

/   \

\/

  \
 / \
/   \

 0\
/  \

//\\
/  \

您需要确定表情符号的哪些部分暴露在雨中。

澄清说明

  • 您的程序(或函数)将以2d字符串作为输入。对于您的语言,这可以是最方便或自然的任何格式。字符串数组,字符数组数组,带换行符的字符串等。

  • 您必须输出表情符号的哪些部分暴露在雨中。只要您明确指出,它可以是零索引或一索引。输出可以是任何合理的格式。如果整个表情符号都免受雨淋,则不输出任何内容(或输出空数组)。

  • 您可以假设所有输入都将具有有效的保护伞和相同的表情符号:-_-。表情符号将始终位于输入的最后一行,但是它们可能是伞形和表情符号之间的几行空行。

  • 不属于保护伞或表情符号的所有内容均为空格字符或换行符。

  • 输入将用空格填充,以使每行的长度相同。

存在标准漏洞,最短答案以字节为单位!

测试IO:

所有示例案例将使用一个索引。

  /\
 /  \
/    \

  -_-

Outputs: []

----------------

   /\
  /  \

     -_-

Outputs: [2, 3]

----------------

    /\
   -_-

Outputs: [1]

----------------

     /\
    /  \
   /    \
  /      \
 /        \
/          \




               -_-

Outputs: [1, 2, 3]

2
我们可以输出雨中的表情符号部分吗?即["_","-"]
Rɪᴋᴇʀ

如果我们的语言支持字符串,我们还可以接受二维字符数组吗?例如,JavaScript中的数组具有与String不同的可用功能。
Patrick Roberts

@PatrickRoberts是的,可以接受。
DJMcMayhem

@EᴀsᴛᴇʀʟʏIʀᴋ不,您应该输出数字。
DJMcMayhem

1
我认为您的意思是表情符号。干表情符号是🔥(或者我想是☂️)
NH。

Answers:


8

05AB1E18 17 15字节

码:

|…-_-123:)ø€J€ï

说明:

|                  # Take all input as a list of strings.
 …-_-              # 3-char string, which results into "-_-".
     123:)         # Replace "-_-" with 123.
          ø        # Zip, resulting into the columns of the 2D array.
           €J      # Join each of them.
             ە    # For each, convert to integer. If this is not possible, it will ignore
                     the result.
                   # Implicitly output the array.

使用CP-1252编码。在线尝试!(请确保在所有行上填充相同长度的空格。


5

JavaScript(ES6),95个字节

a=>[...a[n=0]].map((_,i)=>a.map(s=>(c=s[i])>"-"&c<"_"?p=1:n+=!++c,p=0)|p<!c&&o.push(n),o=[])&&o

输入应该是一个字符串数组,每行用空格填充以形成一个正方形。输出是一个由1索引的数字组成的数组。

说明

var solution =

a=>
  [...a[n=0]].map((_,i)=>  // n = current index of emoji, for each column i of input
    a.map(s=>              // for each line s
      (c=s[i])             // c = character in column
      >"-"&c<"_"?p=1       // p = 1 if column is protected from rain
      :n+=!++c,            // increment n if emoji character found, c = 1 if last line
                           // contained a space in the current column
      p=0
    )
    |p<!c&&o.push(n),      // if the emoji is not protected in the current column
    o=[]
  )
  &&o
<textarea id="input" rows="6" cols="40">   /\   
  /  \  
        
     -_-</textarea><br />
<button onclick="result.textContent=solution(input.value.split('\n'))">Go</button>
<pre id="result"></pre>


4

JavaScript(ES6),92个字节

a=>a.map(s=>s.replace(/\S/g,(c,i)=>c>'-'&c<'_'?u[i]=3:++n&u[i]||r.push(n)),n=0,u=[],r=[])&&r

接受参差不齐的行数组并返回1索引的结果。说明:

a=>a.map(               Loop through all lines
 s=>s.replace(/\S/g,    Loop through all non-whitepsace
  (c,i)=>c>'-'&c<'_'    If it's part of the umbrella
   ?u[i]=3              Mark that column as dry
   :++n&                Add 1 to the emoji index
     u[i]||             If the column is not dry
      r.push(n)         Add the emoji index to the result
  ),n=0,u=[],r=[]       Initialise variables
 )&&r                   Return result

3

爪哇8拉姆达,241 218 201 191 185 184(或161)个字符

因为您知道,Java也需要干表情符号。

import java.util.*;f->{int e,i=e=-1,c,l=f.length-1;while(++e<f[l].length&&f[l][e]!=45);List p=new Stack();l:for(;++i<3;){for(char[]r:f)if((c=r[e+i])==47|c==92)continue l;p.add(i);}return p;}

它返回一个ArrayList,一个HashSet,一个Stack,其中包含暴露在雨中的表情符号部分(索引从0开始)。整个东西都展开了:

import java.util.*;

public class Q82668 {
    static List isEmojiDryRevE(char[][] fieldToCheck) {
        int emojiStart, i = emojiStart = -1, j, rows = fieldToCheck.length - 1;

        while (++emojiStart < fieldToCheck[rows].length && fieldToCheck[rows][emojiStart] != 45)
            ;

        List parts = new Stack();
        emojiLoop: for (; ++i < 3;) {
            for (j = -1; ++j < rows;) {
                if (fieldToCheck[j][emojiStart + i] > 46) {
                    // umbrella part found
                    continue emojiLoop;
                }
            }
            // no umbrella part found
            parts.add(i);
        }
        return parts;
    }
}

更新

我做了一些基本的打高尔夫球。这包括将声明放在一起,与ascii值进行比较以节省一些字符并缩短循环。

感谢@ user902383指出我使用ArrayLists而不是List的转储错误。我用HashSets / Sets替换了ArrayLists / Lists,这节省了一些字符。也感谢他的技巧,在内部循环中使用foreach循环!通过该更改,我可以为最后一个网格行的索引创建一个变量,从而将其缩短一些。总共保存了17个字符!

@KevinCruijssen建议在初始化中删除泛型,我又走了一步:删除所有泛型。这样可以节省另外10个字符。

我从foreach循环切换回for循环。这使得跳过最后一行的比较成为可能,这反过来又使我可以缩短对ascii值的比较。在这种情况下,只有'/','\'和'_'的ascii值超过46。如果我们不检查最后一行,则可以使用a > 46 check代替检查实际值。

再次感谢@ user902383,告诉我使用lambda并可以使用List + Stack而不是Set + HashSet来剃除另一个字符。


字符串返回版本

@ user902383指出,我可以改用数字创建字符串。这听起来很骗人,但其他人似乎是用这种方式解决的,因此这是使用String返回的较短版本:

f->{int e,i=e=-1,c,r=f.length-1;while(++e<f[r].length&&f[r][e]!=45);String p="";l:for(;++i<3;){for(char[]o:f)if((c=o[e+i])==47|c ==92)continue l;p+=i;}return p;}

取消高尔夫:

public class Q82668 {
    public static String isEmojiDryRevD(char[][] fieldToCheck) {
        int emojiStart, i = emojiStart = -1, c, rows = fieldToCheck.length - 1;

        while (++emojiStart < fieldToCheck[rows].length && fieldToCheck[rows][emojiStart] != 45)
            ;

        String parts = "";
        emojiLoop: for (; ++i < 3;) {
            for (char[] row : fieldToCheck) {
                if ((c = row[emojiStart + i]) == 47 | c == 92) {
                    // umbrella part found
                    continue emojiLoop;
                }
            }
            // no umbrella part found
            parts += i;
        }
        return parts;
    }
}

2
您打破了规则编号1 always program to an interface,如果List改为使用则ArrayList可以保存5个字节
user902383

1
我认为内部for循环可以用foreach循环代替,它应该给您额外的几个字节
user902383

1
不确定,但=new HashSet<>();很可能会打高尔夫球=new HashSet();
凯文·克鲁伊森

1
@Frozn为什么不被允许?我知道编译器会产生警告,这在代码查询过程中经常发生。我不确定的是您的代码是否仍然可以正常工作,是否尚未对其进行测试。如果是这样,那么删除<>保存将为您节省2个字节。:)
Kevin Cruijssen

2
@Frozn在老式的旧Java中是正确的,但是现在我们有了lambda,并且在lambda表示法中您没有指定类型。因此,您有Set(3) HashSet(7)反对List(4)和Stack(5)的权利。
user902383 '16

3

V20 19字节(非竞争)

G^R123?/
f\GddHÍó

备用竞争版本(21字节):

G^R123?/
f\òjòddHÍó

在线尝试!(请注意,tryitonline.net使用的是V的稍旧版本。为弥补这一点,它使用了稍长的版本)

说明:

G^          "Move to the first non-whitespace character on the last column
  R123<esc> "Replace the emojii with '123'

?/          "Move backwards to the last '/' character
  <C-v>     "Start a blockwise selection
       f\G  "Move the selection to the next '\', and then to the last line
d           "Delete the block selection
 dH         "Delete the umbrella

仅此一项就可以在17个字节中产生正确的结果。但是,它还会创建一些额外的空格。我不介意,但我不想给自己带来不公平的优势,所以我要添加两个字节:

Íó          "Remove all whitespace

3

的JavaScript(ES6),117个 112字节

s=>s.map(r=>r.map((c,i)=>~'-_'[o='indexOf'](c)&&!s.some(a=>~'/\\'[o](a[i]))?i-r[o]('-'):-1)).pop().filter(n=>~n)

接受字符 数组的破烂的字符串数组,并返回0索引的结果。

s=>s.map(     // for each row
  r=>         // row
    r.map(    // for each character
      (c,i)=> // character, index
        ~'-_'[o='indexOf'](c) // if character is part of emoji
        &&                    // and
        !s.some(              // none of the rows have umbrella in this column
          a=>~'/\\'[o](a[i])
        )
        ? // then return 0-index of emoji
          i-r[o]('-')
        : // else return -1
          -1
  )
)
.pop()         // get last element of string array
.filter(n=>~n) // filter out -1s

演示版

f=s=>s.map(r=>r.map((c,i)=>~'-_'[x='indexOf'](c)&&!s.some(a=>~'/\\'[x](a[i]))?i-r[x]('-'):-1)).pop().filter(n=>~n)
i.oninput=()=>o.value=f(i.value.split`\n`.map(r=>r.split``))
i.oninput()
<textarea rows=6 cols=20 id=i>
   /\
  /  \

     -_-</textarea>
<br/>
<input readonly id=o>


我喜欢你的评论!
sintax

2

视网膜,56字节

字节数假定为ISO 8859-1编码。

m`(?<!(?(2)!)^(?<-2>.)*\S(.*¶)+(.)*).(?<=([-_]+))|\D
$.3

在线尝试!

这是一个替换阶段,其中正则表达式匹配一个表情符号字符,条件是在同一水平位置的上方某处有一个非空格字符(即a /\),然后我们捕获到该表情符号字符的数量点。这场比赛被最后一次捕获的长度所取代,这为我们提供了这个毫无表情的表情符号字符的索引。正则表达式还包含一个,|\D以匹配其他所有内容,而根本不替换任何内容,因此我们删除了所有其他字符。


您能否进一步解释此正则表达式在表情符号字符上方的外观?
sintax

1
@sintax它使用平衡组在自己的行上对前面的字符进行计数。这将测量其水平位置。我已经匹配之后再把/或者\,我是从该组再次弹出,同时匹配之前的东西,并确保我已经完全耗尽的组。这基本上可以确保表情符号的水平位置和屋顶字符相匹配。
Martin Ender

1

Pyth,27个 23字节

0索引。

-m.xsd;.T:R"-_-"s`M3.zd

在线尝试!

说明

-m.xsd;.T:R"-_-"s`M3.zd

                    .z   all lines of input, as a list
         :R"-_-"s`M3     replace "-_-" by "012" 
                         "012" is generated by s`M3
       .T                transpose, return all columns
                         The sample input becomes:
                           0
                           1
                          /2
                         / 
                         \ 
                         \
 m   d                   for each line:
  .xs                        attempt to convert to integer.
      ;                      if errors, replace to space
-                     d  remove all spaces

历史

27个字节:sM:#"^ *\d"0.T:R"-_-"s`M3.z在线试试吧!


1

Matlab,43个字节

@(x)find(sum((x(:,x(end,:)~=' '))~=' ')==1)

此代码查找输入最后一行中非空格字符的列位置,求和这些列中非空格字符的数量,并找到只有一个这样的字符的位置(表情符号字符,不受伞遮盖!) 。这段代码只会返回格式正确的伞的正确结果(假定表情符号上方的任何字符都是格式正确的伞的一部分)。

以下是一些实用程序代码,用于编写测试用例和检查我的工作:

ws = @(x) repmat(' ',1,x);  %  for making strings of spaces
% for writing three-storey umbrellas over an emoji located left-edge at position x
thrht = @(x) strvcat([ws(3) '/\' ws(3); ws(2) '/  \' ws(2); ws(1) '/' ws(4) '\' ws(1); ws(8)], [ws(x-1) '-_-']);
twht = @(x) strvcat([ws(3) '/\' ws(3); ws(2) '/  \' ws(2); ws(8)], [ws(x-1) '-_-']);

跑步x = thrht(7)

x =

   /\    
  /  \   
 /    \  

      -_-

x = twht(0)

x =

   /\   
  /  \  

 -_-     

0

APL,31个字节

{(⍳3)∩,(~∨⌿⍵∊'/\')/+\+\'-_-'⍷⍵}

这将字符矩阵作为输入。

测试:

      t1 t2 t3 t4
┌──────┬────────┬──────┬─────────────────┐
│  /\  │   /\   │    /\│     /\          │
│ /  \ │  /  \  │   -_-│    /  \         │
│/    \│        │      │   /    \        │
│      │     -_-│      │  /      \       │
│  -_- │        │      │ /        \      │
│      │        │      │/          \     │
│      │        │      │                 │
│      │        │      │                 │
│      │        │      │                 │
│      │        │      │                 │
│      │        │      │              -_-│
└──────┴────────┴──────┴─────────────────┘
      {(⍳3)∩,(~∨⌿⍵∊'/\')/+\+\'-_-'⍷⍵} ¨ t1 t2 t3 t4
┌┬───┬─┬─────┐
││2 3│1│1 2 3│
└┴───┴─┴─────┘

说明:

  • '-_-'⍷⍵:在输入大小为零的矩阵中,用1标记输入开始处的位置'-_-'
  • +\+\:行总和。第一个0 0 0 1 0 0 ...进入0 0 0 1 1 1 ...,第二个进入0 0 0 1 2 3 ...
  • ⍵∊'/\':用1s标记输入中所有出现的'/'和'\'。
  • ∨⌿or超过列。这将标记1表示伞所覆盖的最后一行的所有位置。
  • ~not,因为我们需要相反的东西
  • (...)/:从前面的运行总和矩阵中选择所有未发现的列
  • ,:获取结果矩阵中所有值的列表。
  • (⍳3)∩:和之间的交集1 2 3(这消除了所有选定的0或更高的值,该值将是空格)。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.