インデックスのリストの反転リスト


14

この StackOverflowの投稿に触発されました。

前書き

ボブの仕事は、スプレッドシートを作成して整理することです。彼がそれらを整理する方法は、ボブを除いてほとんど知られていないが、同じグループに属する各スプレッドシートのリストを作成する。彼が作成したスプレッドシートには大量のデータがありますが、現在確認しているデータは1つだけです。このジョブを開始してからスプレッドシートを作成した日までの日数です。初日、彼は2つのスプレッドシートを作成し、両方のメモを書き、0適切な場所に分類しました。

現在、彼の上司は毎日どのような種類のスプレッドシートが発生したかをレビューするよう求めています。ボブにとってそれを把握するコードを書くのはあなたの仕事です。彼はスプレッドシートが多すぎて手作業ではできません。

入力

彼があなたに提供するボブの情報は、各データがの形式である(0または1のインデックス付き)ギザギザの配列の形式で提供されx = a[i][j]ます。 aは、私がジャグ配列自体と呼んでいるものでiあり、スプレッドシートのタイプでありx、配列が作成された日付です。 j重要ではありません。

タスク

タイプごとに整理されたスプレッドシート作成日のぎざぎざの配列がある場合、スプレッドシート作成日ごとに整理されたスプレッドシートの種類のぎざぎざの配列を返します。

ボブは、この抽象的なデータをただ残すだけではありません。彼は、スプレッドシートの一部のサブセットを提供してくれて、すべてがどうあるべきかを理解するのを助けてくれます。

入力例(0から始まる):

a = [
[3,2,5,0], # Bob doesn't necessarily sort his lists
[1,3],
[2,1,0,4],
[4,5,3],
[6,6]
]

出力例(コメントはもちろん必須ではありません):

output = [
[0,2] # On day 0, Bob made one type 0 and one type 2 spreadsheet
[1,2] # On day 1, Bob made one type 1 and one type 2 spreadsheet
[0,2] # On day 2, Bob made one type 0 and one type 2 spreadsheet
[0,1,3] # On day 3, Bob made one type 0, one type 1, and one type 3 spreadsheet
[2,3] # On day 4, Bob made one type 2 and one type 3 spreadsheet
[0,3] # On day 5, Bob made one type 0 and one type 3 spreadsheet   
[4,4] # On day 6, Bob made two type 4 spreadsheets
]

ボブは毎日2つのスプレッドシートを作成するわけではないため、出力もギザギザになる場合があります。しかし、彼は毎日少なくとも1つのスプレッドシートを作成しているため、出力に空の配列を含める必要はありません。ただし、出力に最後に空の配列がある場合は、削除する必要はありません。

その他のテストケース:

[[3,5,6,2],[0,0,0],[1,0,3,4]] -> [[1,1,1,2],[2],[0],[0,2],[2],[0],[0]]
[[-1]] -> Undefined behavior, as all input numbers will be non-negative integers. 
[[0],[0],[],[0]] -> [[0,1,3]]

出力の内部リストをソートする必要はありません。

いつものように、標準的な抜け穴はなく、もちろん最短のコードが勝ちます。

(これが私の最初の質問なので、改善するためにできることは何でも教えてください。)


ボブは、何らかの種類のスプレッドシートを作成しませんか?
-xnor

1
@xnorいいえ、ボブは毎日スプレッドシートを作成します。これらのスプレッドシートは会社の運営にとって非常に重要であるため、ボブが病気で電話をかけなければならない場合、別の人がその日のスプレッドシートを作成するために一時的に投稿され、ボブは翌朝スプレッドシートを作成する前に整理します。
スティーブンH.

1
@Dennisその例をまとめると少し疲れましたが、それが示されたと思います。:P修正(両方)!
スティーブンH.

6
よさそうだ。これは非常に良い挑戦であり、特にそれが最初であることを考えると。:)ところで、あなたが気付いていない場合のために、「ライブ」になる前にコミュニティがフィードバックを提供できるサンドボックスがあります。
デニス

1
いいえ、ボブは毎日スプレッドシートを作成します」というコメントに基づいて声明を編集しましたが、自分の答えを最適化しようとしたので、意図よりも制限が厳しい可能性があることに気付きました。末尾の空の配列は許可されますか?例えば[[0 0]]、出力を与えることができます[[0 0] []]か?
ピーターテイラー

回答:





4

Brachylog、28バイト

:1f.
cdo:Im:?:2f.
t:.m:Im~h?

説明

  • メイン述語、入力(?)=リストのリスト

    :1f.              Find all valid outputs of predicate 1 with ? as input
    
  • 述語1:

    c                 Concatenate the list of lists into a single list
     do               Remove duplicates and sort
       :Im            Take the Ith element of that sorted list
          :?:2f.      Find all valid outputs of predicate 2 with [Element:?] as input
    
  • 述語2:

    t:.m              Take the (Output)th element of the list of lists
        :Im           Take the Ith element of that list
           ~h?        This element is the element of the input [Element:List of lists]
    


3

JavaScript(ES6)、58バイト

a=>a.map((b,i)=>b.map(j=>(r[j]=r[j]||[]).push(i)),r=[])&&r

3

CJam(30 29バイト)

Mq~{W):W;{:X)Me]_X=W+X\t}/}/`

オンラインデモ

不思議なことに、主にインデックスを変数にしたいので、W使用するよりもハックする方が短いeeです。e]最初に最大要素を見つけて2バイト節約しましたmm+1空の配列の配列を初期化する。

に値を保存して1バイト節約したMartinに感謝しますXスタックをジャグリングする代わりにます。

注意:末尾の空の配列が許可されている場合、代わりにスプレッドシートと同じ数の空の日の配列を初期化することによる代替の29バイトのアプローチがあります。

q~_e_,Ma*\{W):W;{_2$=W+t}/}/`

3

CJam、20バイト

Peter Taylorに、このコードを彼のソリューションに基づいて、3バイトを節約させてくれてありがとう。

{ee::f{S*\+S/}:~:.+}

スタックの最上部に入力を期待し、それを出力に置き換える名前のないブロック。

ここでテストしてください。

説明

ee    e# Enumerate the input. E.g. if the input is 
      e#   [[3 5 6 2] [0 0 0] [1 0 3 4]]
      e# this gives:
      e#   [[0 [3 5 6 2]] [1 [0 0 0]] [2 [1 0 3 4]]]
::f{  e# The exact details of how this works are a bit tricky, but in effect
      e# this calls the subsequent block once for every spreadsheet and
      e# its correspond index, so the first time we'll have 0 and 3 on the
      e# stack, the next time 0 5, and at the last iteration 2 and 4.
      e# Note that this is a map operation, so we'll end up with an array
      e# on the stack.
  S*  e#   So the stack holds [... index date] now. We start by creating
      e#   a string of 'date' spaces, so "   " on the first iteration.
  \+  e#   We swap this with the index and append the index.
  S/  e#   Now we split this thing on spaces, which gives us 'date' empty
      e#   lists and a list containing the index, e.g. [[] [] [] [0]].
}
:~    e# This flattens the first level of the result, so that we get a list
      e# of all those lists we just created. This is simply one list for
      e# every spreadsheet with its type in the last element.
:.+   e# Finally we fold pairwise concatenation over this list. All the 
      e# empty lists won't affect the result so we'll just end up with all
      e# the types in lists for the correct date.

2

Python 2、82バイト

r=[];i=0
for t in input():
 for v in t:r+=[()]*-(~v+len(r));r[v]+=i,
 i+=1
print r

Ideoneでテストします。


2

Mathematica、35バイト

Table[#&@@@#~Position~i,{i,Max@#}]&

不規則なリストを受け入れて返す名前のない関数。1ベースのインデックスを使用します。

ありがたいことにMax、すべての入力が自動的にフラット化されるため、入力が不規則なリストであっても最終日のインデックスが取得されます。次に#&@@@#~Position~i、すべての日のインデックスのリストを単純に計算しますi。この式自体は、i不規則なリスト内の位置を見つけます(連続する深さでインデックスの配列として返されます)。したがって、必要な値は、それらのリストのそれぞれの最初の値です。#&@@@適用することにより、すべてのサブリストから最初の要素を取得する標準的なゴルフのトリックです#&それらの各です。これは、最初の引数を返す名前のない関数です。

または、同じバイトカウントに対して単項演算子を定義できます(ISO 8859-1エンコードされたソースファイルを想定)。

±n_:=#&@@@n~Position~#&~Array~Max@n

2

Java、314バイト

int[][]f(int[][]n){int w=0;Map<Integer,List<Integer>>m=new TreeMap<>();for(int i=0;i<n.length;i++)for(Integer x:n[i]){if(m.get(x)==null)m.put(x,new ArrayList<>());m.get(x).add(i);w=x>w?x:w;}int[][]z=new int[w+1][];for(int i=0,j;i<w+1;i++){z[i]=new int[m.get(i).size()];j=0;for(int x:m.get(i))z[i][j++]=x;}return z;

詳細

public static Integer[][] f(Integer[][]n)
{
    int w=0;
    Map<Integer,List<Integer>>m=new TreeMap<>();

    for(int i=0;i<n.length;i++)
    {
        for(Integer x : n[i])
        {
            if(m.get(x)==null) m.put(x,new ArrayList<Integer>());
            m.get(x).add(i);
            w=x>w?x:w;
        }
    }

    Integer[][]z=new Integer[w+1][];
    for(int i=0,j; i<w+1; i++)
    {
        z[i]=new Integer[m.get(i).size()];
        j=0;for(Integer x : m.get(i))z[i][j++]=x;
    }

    return z;
}

1
本当に必要Mapですか?
リーキー修道女

0

Perl 5、48バイト

サブルーチン:

{for$i(0..@_){map{push@{$b[$_]},$i}@{$_[$i]}}@b}

次のような動作をご覧ください。

perl -e'print "@$_$/" for sub{for$i(0..@_){map{push@{$b[$_]},$i}@{$_[$i]}}@b}->([3,2,5,0],[1,3],[2,1,0,4],[4,5,3],[6,6])'
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.