Organizing Containers of Balls

Sort by

recency

|

604 Discussions

|

  • + 0 comments

    These sorting are used in CMS especially WordPress . I design a website in WordPress( https://goldrateinfo.ae/سوق-الذهب-الشارقة ) and used it internally in func.php

    PHP Solution

    function sortBalls( $containers ) { 
        $ballTypes = [];
        $swaps = 0;
    
        // Group the balls by type
        foreach ($containers as $container) {
            $ballType = $container[0];
            if (!isset($ballTypes[$ballType])) {
                $ballTypes[$ballType] = [];
            }
            $ballTypes[$ballType] = array_merge($ballTypes[$ballType], $container);
        }
    
        // Assign the balls back to the containers
        $index = 0;
        foreach ($ballTypes as $type => $balls) {
            foreach ($balls as $ball) {
                if ($containers[$index][0] != $ball) {
                    // Swap the ball to its correct container
                    $swaps++;
                    $temp = $containers[$index][0];
                    $containers[$index][0] = $ball;
                    $containers[$index + 1][0] = $temp;
                }
                $index++;
            }
        }
    
        return $swaps;
    }
    
    // Example usage
    $containers = [
        [1, 1, 2, 2, 3, 3],
        [1, 1, 2, 2, 3, 3],
        [1, 2, 3, 1, 2, 3]
    ];
    
    $swaps = sortBalls($containers);
    echo "Minimum number of swaps required: " . $swaps;
    
  • + 0 comments

    My idea consists of verifying if the number of balls of each type is equal to the number of spaces in each box.

    Python Code

    def organizingContainers(container):
        
        total_spaces = sorted(list(map(sum, container)))
        total_balls = sorted(list(map(sum, zip(*container))))
        
        if total_spaces == total_balls:
            return 'Possible'
        else:
            return 'Impossible'
    
  • + 0 comments
    def organizingContainers(container):
      dt=set()
      a=[0]*len(container)
      for i in  range(len(container)):
        dt.add(sum(container[i]))
        for j in range(len(container)):
            a[j]+=container[i][j]
      a.sort()
      if set(a)==dt:
        return 'Possible'
      else:
        return 'Impossible
    
  • + 0 comments
    #include <math.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <limits.h>
    #include <stdbool.h>
    
    int main(){
        int q; 
        scanf("%d",&q);
        for(int a = 0; a < q; a++){
            int n,f=0,d=0; 
            scanf("%d",&n);
            int M[n][n],x[n],s,y=0,z[n],l;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++)
                scanf("%d ",&M[i][j]);
           
            }
           for(int i=0;i<n;i++){ 
               s=0;
               l=0;
                for(int j=0;j<n;j++)
                         s=s+M[j][i];                               
                      x[i]=s;      
               for(int j=0;j<n;j++)
                         l=l+M[i][j];       
                   z[i]=l; 
           }
            for(int i=0;i<n;i++) {
                d=0;
                  for(int j=0;j<n;j++)
                    if(x[i]==z[j]) d=1;
                      
                     f=f+d;   
            }
            if(f==n && d==1) printf("Possible\n");
            else printf("Impossible\n");
            
            
        }
        return 0;
    }
    
  • + 1 comment
    def organizingContainers(container):
        container_sizes = [sum(row) for row in container]
        ball_counts = [sum(i) for i in zip(*container)]
        return "Possible" if sorted(container_sizes) == sorted(ball_counts) else "Impossible"
    

    Python