Wednesday, December 30, 2015

Today we review another coding question :
We have a grid with R rows and C columns in which every entry is either 0 or 1. We are going to perform N operations on the grid, each of which is one of the following:


  • Operation M: Change a number in one cell of the grid to 0 or 1
  • Operation Q: Determine the number of different connected regions of 1s.
    A connected region of 1s is a subset of cells that are all 1, in which any cell in the region can be reached from any other cell in the region by traveling between cells along edges (not corners).
This is the same as the problem I solved in the earlier post.

we solved it by identifying connected regions where a set of 1s appear together. The only variation here is that if two cells of 1 are adjacent only diagonally and not horizontally or vertically then they are not connected. There we identified the contiguous regions as extending the rightmost corner of the rectangle, here we bound the connected region by exploring top then right then bottom edges. A cell can be said to be in an already identified connected region if we can reach it from another point in the region horizontally or vertically where it shares one of the coordinates.

Now to continue with the AWS billing discussion, we were saying we could even prepare the bill based on allocations not usage. We get information on allocations as follows:
<?php
require 'vendor/autoload.php';
use Aws\CloudWatch\CloudWatchClient;

$key = "";
$secret = "";
$region = "us-west-2";
$version = "latest";
$interval = 15;

// Users/rajamani/Downloads/devserver/bill/vendor/aws/aws-sdk-php/src

// Use the us-west-2 region and latest version of each client.
$sharedConfig = [
    'region'  => $region,
    'version' => $version,
  'credentials' => array(
    'key' => $key,
    'secret'  => $secret,
  ),
    'key'    => $key,
    'secret' => $secret,
];

// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk($sharedConfig);
$client = $sdk->createEc2();
  date_default_timezone_set('America/Los_Angeles');
$result  = $client->describeInstances(array(
));
echo 'RESULT='.serialize($result);

$result = (array)$result;
foreach( $result as $reservations)
{
 echo 'RESER='.serialize($reservations)."\n";
 foreach($reservations['Reservations'] as $reservation)
 {
   echo 'RESERVATION='.serialize($reservation)."\n";
   foreach($reservation["Instances"] as $instance)
   {
        $attributes = $client->describeInstanceAttribute(array(
    'DryRun' => false,
    // InstanceId is required
    'InstanceId' => $instance["InstanceId"],
    // Attribute is required
    'Attribute' => 'instanceType',
));
       echo 'ATTRIBUTES='.serialize($attributes)."\n";
   }
 }

}

?>

No comments:

Post a Comment