Sort by

recency

|

1274 Discussions

|

  • + 0 comments

    This constant sum is known as the magic constant or magic sum. Magic squares have fascinated mathematicians for centuries and often appear in puzzles, artwork, and historical texts and Start Your Voiceover Project. The process of creating one can vary depending on whether the square has an odd, even, or doubly even order (such as 3x3, 4x4, or 6x6).

  • + 1 comment

    my solution is based on the fact of 9 distinct digits with total sum of 45 (1-9) meaning sum of each row/colum being 15.

    This solution fails few tests on summission without any ability to debug. Also the expected output is incorrect coming from the HackerRank in my opinion. So what's going on here?

    def formingMagicSquare(s):

      r1=sum(s[0])
    r2=sum(s[1])
    r3=sum(s[2])
    column_sums=[sum (column) for column in zip(*s)]
    c1=column_sums[0]
    c2=column_sums[1]
    c3=column_sums[2]
    
    row_correction = abs(r1-15)+abs(r2-15)+abs(r3-15)
    column_correction = abs(c1-15)+abs(c2-15)+abs(c3-15)
    #print(s)
    
    
      #print(f"row_correction:{row_correction} && column_correction:{column_correction}")
    if row_correction>=column_correction:
        return row_correction
    else:
            return column_correction
    
  • + 0 comments

    // c#

        List<int> linear_s = [];
        List<List<int>> ideal_s =
         [
              [2, 7, 6, 9, 5, 1, 4, 3, 8]
            , [6, 7, 2, 1, 5, 9, 8, 3, 4]
            , [4, 3, 8, 9, 5, 1, 2, 7, 6]
            , [8, 3, 4, 1, 5, 9, 6, 7, 2]
            , [2, 9, 4, 7, 5, 3, 6, 1, 8]
            , [4, 9, 2, 3, 5, 7, 8, 1, 6]
            , [6, 1, 8, 7, 5, 3, 2, 9, 4]
            , [8, 1, 6, 3, 5, 7, 4, 9, 2]
         ];
        int calc_cost = 0, cost = 99;
    
        foreach(var s1 in s)
        {
            foreach(int s2 in s1)
            {
                linear_s.Add(s2);
            }
        }      
    
        foreach (List<int> si in ideal_s)
        {
            calc_cost = 0;
            for (int i=0; i<si.Count; i++)
            {
                calc_cost += Math.Abs(si[i] - linear_s[i]);
            }
    
            if (calc_cost < cost)
            {
                cost = calc_cost;
            }
        }
    
        return cost;
    }
    
  • + 0 comments

    Solution without hardcoding any of the possible 3x3 magic sqaures. Magic squares are generated for odd sized matrices using the Siamese method by placing "1" in any of the positions matrix(0,1), matrix(1,0), matrix (1,2) or matrix(2,1) and then rotating at 90 or 180 or 270 deg or reflecting.

        /*
         * Complete the 'formingMagicSquare' function below.
         *
         * The function is expected to return an INTEGER.
         * The function accepts 2D_INTEGER_ARRAY s as parameter.
         */
    
        public static int formingMagicSquare(List<List<Integer>> s) {
            Moves[] moveList = new Moves[]{
                new Moves(0,1,1,1,true),
                new Moves(0,1,1,-1,true),
                new Moves(2,1,-1,1,true),
                new Moves(2,1,-1,-1,true),
                new Moves(1,0,-1,-1,false),
                new Moves(1,0,1,-1,false),
                new Moves(1,2,-1,1,false),
                new Moves(1,2,1,1,false),
            };
            
            Integer minCost = null;
            for(Moves move: moveList) {
                int[][] magicSquare = new int[3][3];
                magicSquare[move.startX][move.startY] = 1;
                int posX = move.startX;
                int posY = move.startY;
                int cost = Math.abs(s.get(posX).get(posY) - 1);
    
                for (int i = 2; i < 10; i++) {
                    int newX = posX - move.vert;
                    if (newX < 0) newX = 2;
                    if (newX > 2) newX = 0;
    
                    int newY = posY + move.hz;
                    if (newY > 2) newY = 0;
                    if (newY < 0) newY = 2;
    
                    if (magicSquare[newX][newY] != 0) {
                        if (move.isVertFirst) {
                            newX = posX  + move.vert;
                            newY = posY;
                        }
                        else {
                            newY = posY + (-move.hz);
                            newX = posX;
                        }
                    }
    
    
                    magicSquare[newX][newY] = i;
                    cost += Math.abs(s.get(newX).get(newY) - i);
    								if (minCost != null && cost > minCost) break;
                    posX = newX;
                    posY = newY;
    
                }
    
                if (minCost == null || minCost > cost) minCost = cost;
            }
            
            return minCost;
        }
    }
    
    class Moves {
        int startX;
        int startY;
        int vert;
        int hz;
    
        boolean isVertFirst;
    
        public Moves(int startX, int startY, int vert, int hz, boolean isVertFirst) {
            this.startX = startX; this.startY = startY; this.vert = vert; this.hz = hz; this.isVertFirst = isVertFirst;
        }
    }
    
  • + 0 comments

    Should we make the code using the fact that there are a total of 8 possible magic squares, as noted on the Official Website of Dj Hochzeit Bodensee, or should we make it assuming there are more?