每个人都应该有一个朋友


11

孤立字符是没有相邻字符的相同类型的字符(除了换行符)。相邻字符可以在左侧,右侧上方或下方,但不能在对角线处。例如下面的文本H是孤立的:

Ybb
YH%
%%%%

所有其他字符不是孤立的,因为它们每个都至少有一个相邻的相同类型的其他字符。

您的任务是编写一个将字符串作为输入并确定独立字符数的程序。

计分

您的答案将由两个指标评分。第一个是程序中孤立字符的数量。您应该力图将其最小化。第二个将是程序中的字节数。您也应该最小化它。程序大小将成为第一个条件的决胜局。

附加规则

  • 您应该支持可打印的ascii范围的输入以及程序中使用的所有字符。

  • 您可能认为换行符是换行符或换行符后跟换行符。

  • 您可以采用任何合理的格式输入。这包括行列表。

测试用例

Ybb
YH%
%%%%

1


Aaaab
uuu
yyybbb

2


A

1


qqWWaaww

0


2
空字符串是有效输入吗?如果是,则得分为0?输入类型的灵活性也如何?行列表好吗?
Veskah

换行符可以隔离吗?
Jo King

1
@DimChtz,因为它Y下面有一个。
暴民埃里克(Erik the Outgolfer)

1
可以使用任何编程语言来规避第一个度量标准,并且每个答案现在的得分均为0。
GB

1
@GB的确如此。我想现在将它变成一个restricted-source挑战并完全禁止孤立的角色还为时不晚。
Arnauld

Answers:


7

Python 2中,0(350 344 314 309 301个 298个 291字节)

def f(tt):11
def f(tt):
 tt=tt.split('\n')
 r =0#.split('\n')
#r  0#
#for
 for  ii,ll in enumerate(tt):
  for jj,cc in enumerate(ll):
##for+=1-(
    r+=1-(cc in ll[(jj or 2)-1:jj+2:2]    +''.join(ll[jj:
 jj+1]for ll in tt[(ii or 2)-1:ii+2:2]))##+''.join(ll[jj:
#  +1]for 
#print r
 print r
 

在线尝试!

-7个字节,感谢Jo King



@JoKing谢谢!:)
TFeld

5

干净,0(439 ... 415字节)

-11感谢ØrjanJohansen

最后一个挑战,我可以用Clean得分0!
(通常在源代码布局挑战方面很糟糕!)

//module 
  module d
import StdEnv,ArgEnv,Data.List,Data.Maybe
import StdEnv,ArgEnv,Data.List,Data.Maybe
Start=sum[1\\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Start=sum[1\\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]] 
l=mklines[c\\c<-:getCommandLine.[1]]
l=mklines[c\\c<-:getCommandLine.[1]]
?x=mapMaybe(\k=k!?x)o(!?)l
?x=mapMaybe(\k=k!?x)o(!?)l

在线尝试!

TIO链接的使用是module main由于在TIO上实现了Clean的方式,但module d如果您命名文件d.icl而不是main.icl像TIO那样命名,它将可以使用。

解释了其中一条旧行(新版本是同一事物,但顺序不同):

Start                                       // entry point
 = let                                      // define locals
  l = mklines                               // `l` is argument split at newlines
   [c \\ c <-: getCommandLine.[1]];         // the second command-line arg turned into a [Char]
  ? x y                                     // function ? of coordinate (x,y)
   = mapMaybe                               // if the argument isn't Nothing
    (\k = k!?x)                             // try taking the `x`-th index
    (l!?y)                                  // of the `y`-th index of `l`
  in                                        // in the context of
   sum [                                    // the sum of
    1                                       // the integer one
    \\ i <- l & v <- [0..]                  // for every index in `l`
    , _ <- i & u <- [0..]                   // for every subindex in `l`
    | all (                                 // where all of the second argument
      (<>)(?u v)                            // doesn't equal the first argument
     ) [?(u-1)v, ?(u+1)v, ?u(v-1), ?u(v+1)] // over every adjacent element
   ]

1
不使用let将节省11个字节。
与Orjan约翰森

@ØrjanJohansen谢谢!我也更改了模块标头,因为我们有一个d方便的地方
乌鲁斯(

5

JavaScript(ES6),0(154字节)

@ØrjanJohansen 节省了2 4个字节

将输入作为字符串数组。

s  =>
s//=>(s,y,a
.map((s,y,a)=>[...s]
.map((c,x  )=>[...s]
&&//++c,x
 s[x+1]==c|
 s[x-1]==c|
(a[y-1]||0)[x]==c|
(a[y+1]||0)[x]==c||
  i++),i=0)
&&i//),i=

在线尝试!



4

果冻,0(41 27 25字节)

ŒĠạþ`€Ẏ§CẠ€S
ŒĠạþ`€Ẏ§CẠ€S

在线尝试!

将输入作为行列表。代码的第一行从不执行任何操作,仅在此处用于最小化孤立的字符。

ỴŒĠạþ`€Ẏ§1eⱮCS
Ỵ                 Split the text on newlines.
 ŒĠ               Group the multidimensional indices by their value.
      €           For each list of indices:
   ạ                Take the absolute difference...
    þ`              ...between each pair.
       Ẏ          Concatenate the lists of differences.
        §         Sum the x & y differences. This computes the Manhattan distance.
                  At this point we have a list for each character in the text of 
                  Manhattan distances between it and it's identical characters. 
         1eⱮ      Is there a 1 in each of the lists? None for isolated characters.
            C     Complement: 0 <-> 1.
             S    Sum. Counts the isolated characters


1

Python 3,0(323字节)

def f(s,e=enumerate):S={(x,y,c)for y,l in e(s.split("\n"))for x,c in e(l)};return-sum(~-any((x+u,y+v,c)in S for u,v in[(1,0),(~0,0),(0,~0),(0,1)])for x,y,c in S)
def f(s,e=enumerate):S={(x,y,c)for y,l in e(s.split("\n"))for x,c in e(l)};return-sum(~-any((x+u,y+v,c)in S for u,v in[(1,0),(~0,0),(0,~0),(0,1)])for x,y,c in S)

在线尝试!


1

05AB1E,0(101 字节

žGçU|€SXζζD"εγεDgDisëXи]"©.V˜sø®.V€Sø˜‚øʒË}ʒXå≠}gq
žGçU|€SXζζD"εγεDgDisëXи]"©.V˜sø®.V€Sø˜‚øʒË}ʒXå≠}gq

在线尝试。

这是我编写过的最丑陋和最长的05AB1E程序之一。>>>在05AB1E中,这一挑战令人难以置信。我毫不怀疑,通过使用不同的方法(甚至使用类似的方法),字节数至少可以减半,甚至可以减少三到四倍,但是我目前不知道如何。我只是很高兴它现在能正常工作。.如果有人用一些聪明的技巧发布了一个简短得多的05AB1E答案,我可能会出于羞耻而删除此答案... xD

说明:

žGç                # Character with unicode 32768 ('耀')
   U               # Pop and store it in variable `X`
                   # (This character is not part of the printable ASCII, nor of my 05AB1E code)
|                  # Take the multi-line input as list
                   #  i.e. "Ybb\nYH%\n%%%%" → ["Ybb","YH%","%%%%"]
 S                # Convert each string to a list of characters
                   #  i.e. ["Ybb","YH%","%%%%"] → [["Y","b","b"],["Y","H","%"],["%","%","%","%"]]
   Xζζ             # Zip with character `X` as filler twice to make the lines of equal length
                   #  i.e. [["Y","b","b"],["Y","H","%"],["%","%","%","%"]]
                   #   → [["Y","b","b","耀"],["Y","H","%","耀"],["%","%","%","%"]]
      D            # Duplicate this list
"             "    # Create a string
               ©   # Which we store in the register (without popping)
                .V # And execute that string as 05AB1E code
 ε                 #  Map each inner list to:
  γ                #   Split in chunks of the same characters
                   #    i.e. [["Y","b","b"],["Y","H","%"],["%","%","%","%"]]
                   #     → [[["Y"],["b","b"]],[["Y"],["H"],["%"]],[["%","%","%","%"]]]
   ε               #   Map each of those to:
    D              #    Duplicate the current inner list
     gDi           #    If its length is exactly 1:
        s          #     Swap so the mapping keeps the duplicated single character (as list)
       ë           #    Else:
        Xи         #     Take character `X` repeated the length amount of times
                   #      i.e. ["%","%","%","%"] (length 4) → ["耀","耀","耀","耀"]
          ]        #  Close the if-else and both maps
           ˜       #  Flatten the list to a single list of characters
                   #   i.e. [[["Y"],["耀","耀"],["耀"]],[["Y"],["H"],["%"],["耀"]],[["耀","耀","耀","耀"]]]
                   #    → ["Y","耀","耀","耀","Y","H","%","耀","耀","耀","耀","耀"]
s                  # Swap so the duplicate list is at the top of the stack
 ø                 # Swap its rows and columns
                   #  i.e. [["Y","b","b","耀"],["Y","H","%","耀"],["%","%","%","%"]]
                   #   → [["Y","Y","%"],["b","H","%"],["b","%","%"],["耀","耀","%"]]
  ®.V              # Execute the same piece of code again that we've stored in the register
     S            # Convert each to a list of characters
                   #  i.e. [[["耀","耀"],["%"]],[["b"],["H"],["%"]],[["b"],["耀","耀"]],[["耀","耀"],["%"]]]
                   #   → [["耀","耀","%"],["b","H","%"],["b","耀","耀"],["耀","耀","%"]]
       ø           # Swap its rows and columns back again
                   #  i.e. [["耀","b","b","耀"],["耀","H","耀","耀"],["%","%","耀","%"]]
        ˜          # Flatten this list as well
                  # Pair both lists together
                   #  i.e. [["Y","耀","耀","耀","Y","H","%","耀","耀","耀","耀","耀"],
                   #        ["耀","b","b","耀","耀","H","耀","耀","%","%","耀","%"]]
 ø                 # Swap its rows and columns to create pairs
                   #  i.e. [["Y","耀"],["耀","b"],["耀","b"],["耀","耀"],["Y","耀"],["H","H"],["%","耀"],["耀","耀"],["耀","%"],["耀","%"],["耀","耀"],["耀","%"]]
  ʒË}              # Filter out any inner lists where both characters are not equal
                   #  i.e. [["耀","耀"],["H","H"],["耀","耀"],["耀","耀"]]
     ʒXå≠}         # Filter out any inner lists that contain the character `X`
                   #  i.e. [["H","H"]]
g                  # Take the length as result
                   #  i.e. [["H","H"]] → 1
 q                 # Stop the program, making all other characters no-ops
                   # (and output the length above implicitly)

1

红宝石,评分0,237个 209字节

##->a{['',*a,''].each_cons(3).sum{|a,b,c|(0..b.size).count{|x|[[x>0&&b[x-1],a[x],b[x+1],c[x]]&[b[x
  ->a{['',*a,''].each_cons(3).sum{|a,b,c|(0..b.size).count{|x|[[x>0&&b[x-1],a[x],b[x+1],c[x]]&[b[x]]]==[[]]}}}

在线尝试!


0

JavaScript(Node.js),0(279字节)

  s=>(b=s.map(Buffer)).map((x,i)=>x.filter((y,j)=>y-(g=(x,y)=>~~(b[x]&&b[x][y]))(i,j-1)&&y-g(i,j+1)&&y-g(i-1,j)&&y-g(i+1,j))).join``.length
//s=>(b=s.map(Buffer)).map((x,i)=>x.filter((y,j)=>y-(g=(x,y)=>~~(b[x]&&b[x][y]))(i,j-1)&&y-g(i,j+1)&&y-g(i-1,j)&&y-g(i+1,j))).join``.length

在线尝试!

接收输入为线阵列。

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.