Sort by

recency

|

1297 Discussions

|

  • + 0 comments

    Saft batteries are widely used in industrial and aerospace applications due to their high energy density and long life. For long-term reliability, a Saft batteries solution provides stable performance even in extreme temperatures and harsh environments. They are especially suitable for backup power systems, remote monitoring, and critical equipment. Proper storage in a cool, dry place helps maintain their efficiency over time. Always follow safety guidelines to avoid short circuits or damage, and ensure disposal follows local recycling regulations to protect the environment.

  • + 0 comments

    Python

    def formingMagicSquare(s):
        all_magic_squares = [
            [[8, 1, 6], [3, 5, 7], [4, 9, 2]],
            [[6, 1, 8], [7, 5, 3], [2, 9, 4]],
            [[4, 9, 2], [3, 5, 7], [8, 1, 6]],
            [[2, 9, 4], [7, 5, 3], [6, 1, 8]],
            [[8, 3, 4], [1, 5, 9], [6, 7, 2]],
            [[4, 3, 8], [9, 5, 1], [2, 7, 6]],
            [[6, 7, 2], [1, 5, 9], [8, 3, 4]],
            [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
        ]
        min_cost = float('inf')
        
        for magic in all_magic_squares:
            current_cost = 0
            for i in range(3):
                for j in range(3):
                    current_cost += abs(s[i][j] - magic[i][j])
            if current_cost < min_cost:
                min_cost = current_cost
        return min_cost
    
  • + 0 comments

    Mi solución en Python 3:

    def formingMagicSquare(s):
        # Write your code here
        count = [0,0,0,0,0,0,0,0]
    
        magics = [
            [[8, 1, 6], [3, 5, 7], [4, 9, 2]],
            [[6, 1, 8], [7, 5, 3], [2, 9, 4]],
            [[4, 9, 2], [3, 5, 7], [8, 1, 6]],
            [[2, 9, 4], [7, 5, 3], [6, 1, 8]],
            [[8, 3, 4], [1, 5, 9], [6, 7, 2]],
            [[4, 3, 8], [9, 5, 1], [2, 7, 6]],
            [[6, 7, 2], [1, 5, 9], [8, 3, 4]],
            [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
            ]
    
        for x in range(0, 8):
            for i in range(0, 3):
                for j in range(0, 3):
                    count[x] += abs(s[i][j]-magics[x][i][j])
    
        return min(count)
    
  • + 0 comments

    Python 3

    I have created an additional function to calculate all possible combinations for the Magic Square. This challenge should worth more thatn 20 pts.

    from itertools import permutations, chain
    
    # function to list all magic square valid options
    def validMagicOptions() -> list:
        seq_valid = []
        for seq in permutations(range(1,10), 9):
            sum3 = sum(seq[0:3])
            # only add records if the sum of all rows, columns and diagonals match
            if     (
                # rows
                    sum(seq[3:6])==sum3
                and sum(seq[6:9])==sum3
                # cols
                and sum(seq[0::3])==sum3
                and sum(seq[1::3])==sum3
                and sum(seq[2::3])==sum3
                # diagonals
                and sum(seq[0::4])==sum3
                and sum(seq[2:7:2])==sum3
                ):
                seq_valid.append(list(seq))
    
        return seq_valid
    
    def formingMagicSquare(s):
        # Write your code here
        
        valid = validMagicOptions()
        sflat = list(chain(*s))
                
        lowest_cost = float('inf')
        for v in valid:
            cost_abs = sum(abs(x-y) for x,y in zip(v, sflat))
            lowest_cost=min(lowest_cost, cost_abs)
            
        return lowest_cost
    
  • + 0 comments

    Python3 Solution:

    def formingMagicSquare(s):
        magicCombination = [
            [2, 7, 6, 9, 5, 1, 4, 3, 8],
            [2, 9, 4, 7, 5, 3, 6, 1, 8],
            [4, 3, 8, 9, 5, 1, 2, 7, 6],
            [4, 9, 2, 3, 5, 7, 8, 1, 6],
            [6, 1, 8, 7, 5, 3, 2, 9, 4],
            [6, 7, 2, 1, 5, 9, 8, 3, 4],
            [8, 1, 6, 3, 5, 7, 4, 9, 2],
            [8, 3, 4, 1, 5, 9, 6, 7, 2]
        ]
        
        res = float("inf")
        
        for magic in magicCombination:
            currRes = 0
            pointer = 0
            for i in range(3):
                for j in range(3):
                    val = s[i][j]
                    currRes += abs(val - magic[pointer])
                    pointer += 1
                
            if currRes < res:
                res = currRes
        
        return res