2

指定された配列の連続するサブセットをすべて出力する次のメソッドがあります。for ループに織り込まれた醜い print ステートメントを新しい関数に分離できるようにしたいと考えています。これは実行可能ですか?

    // Example:  
    // Input: char[] input = new char[] { 'a', 'b', 'c' };
    // Output: 
    //        (a) (a b) (a b c)
    //        (b) (b c)
    //        (c)  

    static void  PrintContSubArrays(char[] arr)
    {
        int len = arr.Length;
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();

        for (int i = 0; i < len; i++)
        {
            for (int j = 1; j <= len; j++)
            {
                sb2.AppendFormat("(");

                for (int k = i; k < j; k++)
                {
                    sb2.AppendFormat("{0} ", arr[k]);
                }

                sb2.Remove(sb2.Length - 1, 1);
                sb2.Append(") ");

                if (sb2.Length > 3) sb1.Append(sb2);
                sb2.Clear();
            }
            sb1.Append(System.Environment.NewLine);
        }

        Console.WriteLine(sb1.ToString());
        Console.ReadLine();
    }
4

4 に答える 4

2

Implemented this based on suggestion by @usr

Instead of doing the string processing inline extract the subarrays as char[] and return them using yield return. That way the caller receives a stream of subarrays. He can process, format and print them as he wants.

This is what I have. But this does not translate to your exact output format. Some formatting is lost due to loss of context. If you are willing to pass in the original array length into the print method, you could get exactly what you need by tweaking the for-loop in print method.

    // Input: int[] input = new int[] { 1, 2, 3};
    // Call: Console.WriteLine(PrintContSubArrays(GetContSubArrays(input)));
    // Output: 
    //       (1)
    //       (1 2)
    //       (1 2 3)
    //       (2)
    //       (2 3)
    //       (3)

    // Generate subsets
    static IEnumerable<int[]> GetContSubArrays(int[] arr)
    {
        int len = arr.Length;

        for (int i = 0; i < len; i++)
        {
            for (int j = 1; j <= len; j++)
            {
                int[] placeholder = new int[j - i < 0 ? 0 : j - i];
                bool isPlaceholderEmpty = true;

                for (int k = i; k < j; k++)
                {
                    placeholder[k - i] = arr[k];
                    isPlaceholderEmpty = false;
                }
                if (!isPlaceholderEmpty) yield return placeholder;
            }
        }
    }

    // Print
    static string PrintContSubArrays(IEnumerable<int[]> input)
    {
        StringBuilder sb1 = new StringBuilder();

        foreach (int[] intarr in input)
        {
            if (intarr != null)
            {
                sb1.Append("(");
                foreach (int intsingle in intarr)
                {
                    sb1.AppendFormat("{0} ", intsingle);
                }
                sb1.Remove(sb1.Length - 1, 1);
                sb1.Append(")");
            }
            sb1.AppendFormat(Environment.NewLine);
        }

        return sb1.ToString();
    }
于 2015-02-04T23:25:05.447 に答える