06.HighQualityMethodsHomework/RefactorCSharpPart2Exam/11. CSharp-Part-2-Exam/CSharp-Part-2-Exam/CatConcert/CatConcert.cs
/// <summary>
/// Solving problem using brute force (simulating all scenarios for the cats concert)
/// Using binary representation of the numbers from 1 to 2^n - 1 (for n=5 => 00001 -> 11111)
/// </summary>
/// <param name="catSongs">Boolean 2-dimensional array representing the cats' songs awareness</param>
/// <returns>Returns 32-bit-integer max value when there is a cat that will not sing</returns>
Solving problem using brute force (simulating all scenarios for the cats concert)
Using binary representation of the numbers from 1 to 2^n - 1 (for n=5 => 00001 -> 11111)
Solving problem using brute force ( simulating all scenarios for the cats concert ) Using binary representation of the numbers from 1 to 2^n - 1 ( for n = 5 = > 00001 - > 11111 )
private static int SolveUsingBitMasks(bool[,] catSongs)
        {
            var catsCount = catSongs.GetLength(0);
            var songsCount = catSongs.GetLength(1);
            int min = int.MaxValue;
            var maxCombination = (int)Math.Pow(2, songsCount) - 1;
            for (int combination = 1; combination <= maxCombination; combination++)
            {
                var songsToBeSing = new BitArray(new[] { combination });
                bool allCatsWillSing = true;
                for (int i = 0; i < catsCount; i++)
                {
                    bool catIWillSing = false;
                    for (int j = 0; j < songsCount; j++)
                    {
                        if (songsToBeSing[j] && catSongs[i, j])
                        {
                            catIWillSing = true;
                            break;
                        }
                    }
                    if (!catIWillSing)
                    {
                        allCatsWillSing = false;
                        break;
                    }
                }
                if (allCatsWillSing)
                {
                    int songs = 0;
                    for (int i = 0; i < songsCount; i++)
                    {
                        if (songsToBeSing[i])
                        {
                            songs++;
                        }
                    }
                    min = Math.Min(min, songs);
                }
            }
            return min;
        }
private static int SolveUsingBitMasks ( bool [ , ] catSongs ) { var catsCount = catSongs . GetLength ( 0 ) ; var songsCount = catSongs . GetLength ( 1 ) ; int min = int . MaxValue ; var maxCombination = ( int ) Math . Pow ( 2 , songsCount ) - 1 ; for ( int combination = 1 ; combination <= maxCombination ; combination ++ ) { var songsToBeSing = new BitArray ( new [ ] { combination } ) ; bool allCatsWillSing = true ; for ( int i = 0 ; i < catsCount ; i ++ ) { bool catIWillSing = false ; for ( int j = 0 ; j < songsCount ; j ++ ) { if ( songsToBeSing [ j ] && catSongs [ i , j ] ) { catIWillSing = true ; break ; } } if ( ! catIWillSing ) { allCatsWillSing = false ; break ; } } if ( allCatsWillSing ) { int songs = 0 ; for ( int i = 0 ; i < songsCount ; i ++ ) { if ( songsToBeSing [ i ] ) { songs ++ ; } } min = Math . Min ( min , songs ) ; } } return min ; }
Solving problem using brute force (simulating all scenarios for the cats concert)
Using binary representation of the numbers from 1 to 2^n - 1 (for n=5 => 00001 -> 11111)
Solving problem using brute force ( simulating all scenarios for the cats concert ) Using binary representation of the numbers from 1 to 2^n - 1 ( for n = 5 = > 00001 - > 11111 )
// Song j will be sing and the cat can sing it
Returns 32-bit-integer max value when there is a cat that will not sing
Returns 32 - bit - integer max value when there is a cat that will not sing
catSongs
bool[,]
Boolean 2-dimensional array representing the cats' songs awareness
Boolean 2 - dimensional array representing the cats ' songs awareness