进行查找索引


12

给定一个字符串,返回一个表,其中第一列按出现的顺序具有该字符串的唯一字母,随后的列使用零或基于一的索引列出该字母在字符串中的索引。只要最左边的列垂直对齐,水平空格就没有关系。索引必须按从左到右的升序排列。

例子

使用从零开始的索引并给定“ abracadabra”,返回

a 0 3 5 7 10
b 1 8       
r 2 9       
c 4         
d 6   

使用基于一个的索引并给定“ 3141592653589793238462643383279503”,返回:

3  1 10 16 18 25 26 28 34
1  2  4                  
4  3 20 24               
5  5  9 11 32            
9  6 13 15 31            
2  7 17 22 29            
6  8 21 23               
8 12 19 27               
7 14 30                  
0 33                     

输出中可以有空格吗?
暴民埃里克(Erik the Outgolfer)'17年

输出格式必须严格吗?
Leaky Nun

@EriktheOutgolfer是的。添加。
亚当

@LeakyNun号。
亚当

2
是否需要处理不可打印的ASCII字符?我们可以假设字符串中不会包含任何字符(尤其是空格)吗?
FryAmTheEggman

Answers:


6

APL(Dyalog),4个字节

,⌸

在线尝试!

是3个字节)

使用基于1的索引。

是关键运营商。在这里,它表现为单子运算符。它将函数,(将左参数与右参数连接起来)应用于其右参数中的每个唯一元素以及原始参数中该唯一元素的索引。


嗯,这需要一个解释:-)
亚当

4
@Adám我怀疑这个挑战是专为APL量身定制的
Uriel

@Uriel反过来。我一直在研究APL可以巧妙地完成的工作,并认为这将是一个很好的挑战。
亚当

我在那里看到4个字节。
亚当

2

Haskell,86个字节

import Data.List
f s=unlines[x:concat[' ':show i|i<-[0..length s-1],s!!i==x]|x<-nub s]

定义一个函数,f该函数返回一个包含此输出的String。

在线尝试!

怎么样?

import Data.List                                                            -- Imports Data.List. This contains the nub method which is necessary
f s =                                                                       -- Define a function, f, which takes one argument, s
            [                                               |x<-nub s]      -- Loop through nub s with the variable x. Nub removes duplicates from a list, in this case the input.
                     [          |i<-[0..length s-1]        ]                -- Go thourgh the list [0,1,2...] until the length of the input - 1. Basically a indexed for-loop
                                                   ,s!!i==x                 -- Filter out everything where the char at this index isn't x
                      ' ':show i                                            -- i as a string with a space before it
               concat                                                       -- Flatten the whole list
             x:                                                             -- Append x before it
     unlines                                                                -- Insert newlines between every element in the list and flatten it, effectively putting every element on it's own line

2
我认为我从未见过Haskell使用的注释格式。
亚当

您的内部列表理解可以缩短为[' ':show i|(i,c)<-zip[0..]s,c==x]
Laikoni

2

kdb + / q,5个字节

group

内置工厂

q)group"abracadabra"
a| 0 3 5 7 10
b| 1 8
r| 2 9
c| ,4
d| ,6

我通常打k,但是2字节的k版本(=:)不能很好地格式化输出

k)=:"abracadabra"
"abrcd"!(0 3 5 7 10;1 8;2 9;,4;,6)

结果完全相同,但是格式丢失。为了格式化和删除返回对象,我们实际上选择了比q版本更多的字节

k)f:{1@.Q.s x;} //11 bytes!
k)f"abracadabra"
a| 0 3 5 7 10
b| 1 8
r| 2 9
c| ,4
d| ,6

嗯,它返回一个字典。有趣。
2015年





0

05AB1E,14个字节

ÙvSyQƶ0Ky¸ìðý,

在线尝试!

说明

Ùv              # for each unique char y in input
  S             # split input into a list of chars
   yQ           # compare each to y for equality
     ƶ          # multiply each by its 1-based index
      0K        # remove zeroes
        y¸ì     # prepend y to the list of indices
           ðý,  # join by spaces and print

0

Mathematica,85个字节

使用基于一的索引

(t=#;j=First/@t~StringPosition~#&/@(s=First/@Tally@Characters@t);Row[Column/@{s,j}])&

0

JavaScript(ES草案),77字节

s=>s.replace(/./g,(c,i)=>a[c]=(a[c]||c)+` `+i,a={})&&Object.values(a).join`
`

在旧版浏览器中为88个字节:

f=
s=>s.replace(/./g,(c,i)=>a[c]=(a[c]||c)+` `+i,a={})&&Object.keys(a).map(c=>a[c]).join`
`
<input oninput=o.textContent=f(this.value)><pre id=o>



0

QBIC,95个字节

dim X(126)[_l;||i=asc(_sA,a,1|)┘X(i)=X(i)+@ `+!a$][_lA||_SA,b,1|i=asc(C)~X(i)<>D|?C,X(i)┘X(i)=@

说明

这复制了我对这一挑战的回答的重要部分:

dim x(126)      Create an array of 126 elements (one for each ASCII element)
[_l;||          Read cmd line input, loop over its length
i=asc(_sA,a,1|) Read the next char's ascii value
┘               (Syntactic linebreak)
X(i)=           Set the value for this ascii-codepoint to
 X(i)             everything we've put in before
 +@ `             and a literal space
 +!a$             and the current (1-based) index cast as string
 ]              close the FOR loop
 [_lA||         Loop over the original string again (to preserve order of appearance)
 _SA,b,1|       Read 1 char, assign to C$ (note the capital S in the function call,
                this auto-creates C$ abd assigns it the value of the substring-call)
 i=asc(C)       Get the index in our storage array from C$'s ascii value
 ~X(i)<>D       IF the storage array holds data for i (<> D$, which we'll set to "" in a second), 
 |?C,X(i)       THEN PRINT the character, followed by the indices saved for this char
 ┘              (Syntactic linebreak)
 X(i)=@         Clear out the data stored for C$
                @ declares a string lit, ` would close it and that gets auto-added at EOF, 
                creating the literal @`, which gets assigned to D$

样品运行:

Command line: abracadabra
a              1 4 6 8 11
b              2 9
r              3 10
c              5
d              7


0

Python 3中,111个 106字节

def q(f):
 d={a:[]for a in f}
 for a,b in enumerate(f):d[b]+=[a]
 [print(p,*d.pop(p))for p in f if p in d]

代表


0

C#,138字节

using System.Linq;s=>Console.Write(string.Join("\n",s.Distinct().Select(c=>c+string.Join("",s.Select((d,i)=>d==c?i:-1).Where(i=>i>-1)))));

0

F#,120字节

let f s=s|>Seq.indexed|>Seq.groupBy(fun(a,b)->b)|>Seq.iter(fun(a,b)->
 printf"\n%c"a 
 Seq.iter(fun(c,_)->printf"%i"c)b)

更具可读性的版本:

let f s =
    s
    |> Seq.indexed
    |> Seq.groupBy (fun (idx, char) -> char)
    |> Seq.iter (fun (char, charIdxSeq) ->
         printf "\n%c" char 
         Seq.iter (fun (idx, _) -> printf "%i" idx) charIdxSeq)

Seq.indexed 创建一个包含元组的新序列,该元组由原始元素及其原始序列中从0开始的索引组成。

其余的都是自我解释,这对高尔夫从来都不是一件好事!


0

R80 77字节

function(s)for(i in (z=unique(s<-strsplit(s,'')[[1]])))cat(i,which(s==i),"
")

在线尝试!

匿名函数;打印以尾随换行符为1索引的结果。



0

外壳,10字节

§mȯ§:;ms¥u

在线尝试!

注意:外壳(或至少是command ¥)比此挑战要新。

说明

§mȯ§:;ms¥u  Implicit input, say S = "ababbc".
         u  Remove duplicates: "abc"
§m          Map over this string:
             Argument is a character, say 'b'.
        ¥    1-based indices in S: [2,4,5]
      ms     Convert each to string: ["2","4","5"]
     ;       Wrap argument in a string: "b"
  ȯ§:        Prepend to list of index strings: ["b","2","4","5"]
            Result is a list of lists of strings
             [["a","1","3"],["b","2","4","5"],["c","6"]]
            Implicitly print, separating strings by spaces and lists by newlines.
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.