locked
any hints how I can solve this exercism challenge RRS feed

  • Question

  • Hello, 

    I try to solve this annoying exercism challenge for 2 / 3 days without luck. 

    The challenge can be found here : https://github.com/exercism/csharp/tree/master/exercises/saddle-points

    The idea that I had is first to find the min and max of a row/column. 

    But what is the best way to make this work. 

    It looks lik I cannot use a foreach or a for loop. 

    With the foreach loop I never known what column or row I am and with the for loop I cannot specify that the max or min is the first item. 

    Anyone any tips. 

    Roelof

    Thursday, October 5, 2017 7:57 AM

All replies

  • I have now this : 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class SaddlePoints
    {
        private int[,] Values;
        private int[][] original;
        private Tuple<int, int> tuple;
        private int maxRow = int.MinValue;
    
        public SaddlePoints(int[,] values)
        {
            {
                int n = values.GetUpperBound(0);
                int m = values.GetUpperBound(1);
    
                this.original = new int[n][];
    
                for (int i = 0; i < n; ++i)
                {
                    this.original[i] = new int[m];
                    for (int j = 0; j < m; ++j)
                    {
                        this.original[i][j] = values[i, j];
                    }
                }
            }
        }
    
    
        public IEnumerable<Tuple<int, int>> Calculate()
        {
            // find max in a row 
    
            for (int row = 0; row < Values.Length; row++)
            {
                if (original[1] [row] > maxRow)
                {
                    maxRow = original[1][row]; 
                }
    
                Console.WriteLine(maxRow); 
                
            }
    
    
    
            Console.ReadLine(); 
            yield return tuple;    
        }
    
        public static void Main()
        {
            var values = new[,]
            {
                { 9, 8, 7 },
                { 5, 3, 2 },
                { 6, 6, 7 }
            };
            var actual = new SaddlePoints(values).Calculate();
    
            Console.ReadLine(); 
        }
    }

    but now maxRow gives no output
    Thursday, October 5, 2017 9:16 AM
  • In order to start executing Calculate, try, for example, this:

       var actual = new SaddlePoints(values).Calculate();
       var count = actual.Count();

    Thursday, October 5, 2017 9:24 AM
  • Thanks, Still something not right about my code. I was expecting to see as output 9, 5 . 7 where I see 9 8 5 3 any tips ? Roelof
    Thursday, October 5, 2017 9:54 AM
  • Maybe try an alternative:

    var values = new int[,]
    	{
    		{ 9, 8, 7 },
    		{ 5, 3, 2 },
    		{ 6, 6, 7 },
    	};
    
    int row_count = values.GetLength( 0 );
    int col_count = values.GetLength( 1 );
    
    Func<int, int, bool> is_max_in_row = ( r, c ) => Enumerable.Range( 0, col_count ).All( k => values[r, c] >= values[r, k] );
    Func<int, int, bool> is_min_in_col = ( r, c ) => Enumerable.Range( 0, row_count ).All( k => values[r, c] <= values[k, c] );
    
    for( int r = 0; r < row_count; ++r )
    {
    	for( int c = 0; c < col_count; ++c )
    	{
    		if( is_max_in_row( r, c ) && is_min_in_col( r, c ) )
    		{
    			Console.WriteLine( "Saddle point at ({0}, {1}), value: {2}", r, c, values[r, c] );
    		}
    	}
    
    }
    

    Thursday, October 5, 2017 11:05 AM
  • oke, 

    can you explain your func where the max and min are explained ? 

    and can someone explain why my code  gives the wrong answers so I can learn from it 

    • Edited by Roelof1967 Thursday, October 5, 2017 11:21 AM
    Thursday, October 5, 2017 11:08 AM
  • The suggested solution does not find max and min but just check if the current value at (r,c) satisfies the required conditions, where r is row index and c is column index. This is done by local functions is_max_in_row and is_min_in_col. The local functions iterate the ranges (0, 1, 2, …) generated by Enumerable.Range and uses LINQ and lambda expressions.

    Hire is one more approach:

    var values = new int[,]
    	{
    		{ 9, 8, 7 },
    		{ 5, 3, 2 },
    		{ 6, 6, 7 },
    	};
    
    int row_count = values.GetLength( 0 );
    int col_count = values.GetLength( 1 );
    
    var row_enum = Enumerable.Range( 0, row_count );
    var col_enum = Enumerable.Range( 0, col_count );
    
    Func<int, int, bool> is_max_in_row = ( r, c ) => col_enum.All( k => values[r, c] >= values[r, k] );
    Func<int, int, bool> is_min_in_col = ( r, c ) => row_enum.All( k => values[r, c] <= values[k, c] );
    
    var results = row_enum
    		.SelectMany( r => col_enum, ( r, c ) => new { r, c } )
    		.Where( rc => is_max_in_row( rc.r, rc.c ) && is_min_in_col( rc.r, rc.c ) );
    
    foreach( var rc in results )
    {
    	Console.WriteLine( "Saddle point at ({0}, {1}), value: {2}", rc.r, rc.c, values[rc.r, rc.c] );
    }


    • Edited by Viorel_MVP Thursday, October 5, 2017 11:24 AM
    • Proposed as answer by Fei Hu Wednesday, October 11, 2017 11:31 AM
    Thursday, October 5, 2017 11:22 AM
  • Thanks,

    Can you also help me to find out why my code is not working so I can learn from it 

    Thursday, October 5, 2017 11:40 AM
  • Hi Roelof1967,

    According to your question is more related to Saddle Points, you could post in GitHub.

    https://github.com/exercism/csharp/tree/master/exercises/saddle-points

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 26, 2017 5:54 AM