C # , 136 байт
Данные
- Input
Int32[]
i
Массив целых
- Выходные данные
Int32[,]
Двумерный массив.
Golfed
i=>{int m=System.Linq.Enumerable.Max(i),l=i.Length,x,y;var o=new int[m,l];for(y=0;y<m;y++)for(x=0;x<l;)o[y,x]=i[x++]>y?y+1:0;return o;};
Ungolfed
i => {
int
m = System.Linq.Enumerable.Max( i ),
l = i.Length,
x, y;
var o = new int[ m, l ];
for( y = 0; y < m; y++ )
for( x = 0; x < l; )
o[ y, x ] = i[ x++ ] > y ? y + 1 : 0;
return o;
};
Ungolfed читабельный
// Take an array of Int32
i => {
// Store the max value of the array, the length and declare some vars to save some bytes
int
m = System.Linq.Enumerable.Max( i ),
l = i.Length,
x, y;
// Create the bidimensional array to output
var o = new int[ m, l ];
// Cycle line by line...
for( y = 0; y < m; y++ )
// ... and column by column...
for( x = 0; x < l; )
// And set the value of the line in the array if it's lower than the the value at the index of the input array
o[ y, x ] = i[ x++ ] > y ? y + 1 : 0;
// Return the bidimentional array.
return o;
};
Полный код
using System;
using System.Collections.Generic;
namespace TestBench {
public class Program {
// Methods
static void Main( string[] args ) {
Func<Int32[], Int32[,]> f = i => {
int
m = System.Linq.Enumerable.Max( i ),
l = i.Length,
x, y;
var o = new int[ m, l ];
for( y = 0; y < m; y++ )
for( x = 0; x < l; )
o[ y, x ] = i[ x++ ] > y ? y + 1 : 0;
return o;
};
List<Int32[]>
testCases = new List<Int32[]>() {
new[] { 1, 2, 5, 6, 4 },
new[] { 3, 5, 2, 1, 6 },
new[] { 1, 2, 3, 4, 3, 2, 1 },
};
foreach( Int32[] testCase in testCases ) {
Console.WriteLine( " INPUT: " );
PrintArray( testCase );
Console.WriteLine( "OUTPUT: " );
PrintMatrix( f( testCase ) );
}
Console.ReadLine();
}
public static void PrintArray<TSource>( TSource[] array ) {
PrintArray( array, o => o.ToString() );
}
public static void PrintArray<TSource>( TSource[] array, Func<TSource, String> valueFetcher ) {
List<String>
output = new List<String>();
for( Int32 index = 0; index < array.Length; index++ ) {
output.Add( valueFetcher( array[ index ] ) );
}
Console.WriteLine( $"[ {String.Join( ", ", output )} ]" );
}
public static void PrintMatrix<TSource>( TSource[,] array ) {
PrintMatrix( array, o => o.ToString() );
}
public static void PrintMatrix<TSource>( TSource[,] array, Func<TSource, String> valueFetcher ) {
List<String>
output = new List<String>();
for( Int32 xIndex = 0; xIndex < array.GetLength( 0 ); xIndex++ ) {
List<String>
inner = new List<String>();
for( Int32 yIndex = 0; yIndex < array.GetLength( 1 ); yIndex++ ) {
inner.Add( valueFetcher( array[ xIndex, yIndex ] ) );
}
output.Add( $"[ {String.Join( ", ", inner )} ]" );
}
Console.WriteLine( $"[\n {String.Join( ",\n ", output )}\n]" );
}
}
}
релизы
- v1.0 -
136 bytes
- Исходное решение.
Примечания