I am doing some shipping calculations. I need some help trying to figure this out.
Basically, I have a generic list of products with Length, Width, and Height properties.
I would like to EASILY look at the products and find the largest values of all three properties.
From here, I can do some math and figure out the box size based on the # of products.
My initial thought would be to make 3 arrays and find the max of each. Just wanted to see if there was a simpler or cooler way I didnt' know.
Sounds like an array of arrays. As you read each element (box) from your data source (SQL Server, XML, etc), create an 3-member array and insert the attributes in order of size. Then, add the three-member array to an array of arrays. You can then sort the array of arrays by the first, second or third member using LINQ or some other function.
Box1,2,2,3
Box2,5,10,1
Box3,8,4,7
Becomes:
{ {10,5,1}, {8,7,4}, {3,2,2} } // First
or
{ {8,7,4}, {10,5,1}, {3,2,2} } // Second
or
{ {8,7,4}, {3,2,2}, {10,5,1} } // Third
Then, you can sort the array by the first element, second element, etc.
You could easily build an array of arrays in a single statement using LINQ, but exactly how you would do so depends on the data source. Assuming you have an class named Box with three parameters, Length, Width and Height, and that you have created a strongly-typed collection containing instances of this class:
class BoxSorter {
public IEnumerable<Box> Boxes {
get;
private set;
}
class Box {
public double Height {
get;
set;
}
public double Width {
get;
set;
}
public double Length {
get;
set;
}
}
public void Initialize() {
this.Boxes = new List<Box>( new Box[] {
new Box() { Height = 2, Length = 2, Width = 3 },
new Box() { Height = 5, Length = 10, Width = 1 },
new Box() { Height = 8, Length = 4, Width = 7 }
} );
}
public void Sort() {
var l_arrayOfArrays =
this.Boxes.Select(
// Create an array of the Height, Length and Width, then sort the array elements (largest to smallest)
b => new double[] { b.Height, b.Length, b.Width }.OrderByDescending( v => v ).ToArray()
);
var l_dimension1 =
l_arrayOfArrays.OrderByDescending(
// Sort the array of arrays by the first (and largest) dimension
a => a[0]
);
var l_dimension2 =
l_arrayOfArrays.OrderByDescending(
// Sort the array of arrays by the second (and middle) dimension
a => a[1]
);
var l_dimension3 =
l_arrayOfArrays.OrderByDescending(
// Sort the array of arrays by the third (and smallest) dimension
a => a[2]
);
}
}
What you probably need to do is to have a set of box sizes and THEN try packing them optimally in one or more boxes of that size.
This is a simple packer for the 2D case, you can extend this to 3D.
Your algorithm will sorta look like
foreach box in boxes (ordered by decreasing volume)
while there are unpacked items
if box has space
pack item
else
box = another box of the same size
Now you can decide what to do with unused space in the last box - either pick them all out and try a smaller box, or try packing all items in all size boxes and then pick the combination that results in least number of boxes.
You are really having a problem finding the min, max, and middle of three numbers?
It does not make sense to take the smallest of each column as if you had just two
4 x 4 x 4
8 x 8 x 2
You would incorrectly conclude the smallest is 4 x 4 x 2 and the largest is 8 x 8 x 4.
double[] dimensions;
dimensions = new double[] {8,7,7};
Array.Sort(dimensions);
System.Diagnostics.Debug.WriteLine(dimensions[0]);
System.Diagnostics.Debug.WriteLine(dimensions[1]);
System.Diagnostics.Debug.WriteLine(dimensions[2]);
dimensions = new double[] { 7, 9, 8 };
Array.Sort(dimensions);
System.Diagnostics.Debug.WriteLine(dimensions[0]);
System.Diagnostics.Debug.WriteLine(dimensions[1]);
System.Diagnostics.Debug.WriteLine(dimensions[2]);
P.S. I agree with anathonline that it is way more complex than just simple math if you want an optimal box size and how to pack the items.
Related
This is my first question on this site. I am practicing on a problem on Hackerrank that asks to find numbers "Between two Sets". Given two arrays of integers, I must find the number(s) that fit the following two criteria:
1) The elements in the first array must all be factors of the number(s)
2) The number(s) must factor into all elements of the second array
I know that I need to find all common multiples of every element in the first array, but those multiples need to be less than or equal to the minimum value of the second array. I first sort the first array then find all the multiples of ONLY the largest number in the first array (again, up to a max of the second array's minimum) and store those multiples in a list. Then, I move on to the second largest element in the first array and test it against the array of existing multiples. All elements in the list of existing multiples that isn't also a multiple of the second largest element of the first array is removed. I then test the third largest value of the first array, all the way to the minimum value. The list of existing multiples should be getting trimmed as I iterate through the first array in descending order. I've written a solution which passes only 5 out of the 9 test cases on the site, see code below. My task was to edit the getTotalX function and I created the getCommonMultiples function myself as a helper. I did not create nor edit the main function. I am not sure why I am not passing the other 4 test cases as I can't see what any of the test cases are.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
/*
* Complete the getTotalX function below.
*/
static int getTotalX(int[] a, int[] b) {
//get minimum value of second array
int b_min = b.Min();
//create List to hold multiples
List<int> multiples = getCommonMultiples(a, b_min);
//create List to hold number of ints which are in solution
List<int> solutions = new List<int>();
foreach(int x in multiples)
{
foreach(int y in b)
{
if (y % x == 0 && !solutions.Contains(x))
{
solutions.Add(x);
}
else
{
break;
}
}
}
return solutions.Count;
}
static List<int> getCommonMultiples(int[] array, int max)
{
//make sure array is sorted
Array.Sort(array);
int x = array.Length - 1; //x will be the last # in array -- the max
int y = 1;
//find all multiples of largest number first and store in a list
int z = array[x] * y;
List<int> commonMultiples = new List<int>();
while(z <= max)
{
commonMultiples.Add(z);
y++;
z = array[x] * y;
}
//all multiples of largest number are now added to the list
//go through the smaller numbers in query array
//only keep elements in list if they are also multiples of smaller
//numbers
int xx = array.Length - 2;
for(int a = array[xx]; xx >= 0; xx--)
{
foreach(int b in commonMultiples.ToList())
{
if (b % a != 0)
{
commonMultiples.Remove(b);
}
else
{
continue;
}
}
}
return commonMultiples;
}
static void Main(string[] args) {
TextWriter tw = new StreamWriter(#System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] nm = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(nm[0]);
int m = Convert.ToInt32(nm[1]);
int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), aTemp => Convert.ToInt32(aTemp))
;
int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), bTemp => Convert.ToInt32(bTemp))
;
int total = getTotalX(a, b);
tw.WriteLine(total);
tw.Flush();
tw.Close();
}
}
Again, I can't see the test cases so I do not know what exactly the issue is. I went through the code line by line and can't find any OutOfBoundExceptions or things of that sort so it has to be a logic issue. Thanks for the help!
A typical sample involves 3 lines of input. The first line has 2 integers which gives the length of the first array and the second array, respectively. The second line will give the integers in the first array. The third line will give the integers in the second array. The output needs to be the total number of integers "in between" the two arrays. It will looks like this:
Sample Input
2 3
2 4
16 32 96
Sample Output
3
Explanation: 2 and 4 divide evenly into 4, 8, 12 and 16.
4, 8 and 16 divide evenly into 16, 32, 96.
4, 8 and 16 are the only three numbers for which each element of the first array is a factor and each is a factor of all elements of the second array.
I see two issues with the code you posted.
Firstly, as #Hans Kesting pointed out, a = array[xx] is not being updated each time in the for loop. Since the variable a is only used in one spot, I recommend just replacing that use with array[xx] and be done with it as follows:
for(int xx = array.Length - 2; xx >= 0; xx--)
{
foreach(int b in commonMultiples.ToList())
{
if (b % array[xx] != 0)
{
commonMultiples.Remove(b);
For your understanding of for loops: to properly increment a each time you'd write the for loop like this:
for(int xx = array.Length - 2, a = array[xx]; xx >= 0; xx--, a = array[xx])
The first part of the for loop (up until ;) is the initialization stage which is only called before the entering the loop the first time. The second part is the while condition that is checked before each time through loop (including the first) and if at any time it evaluates to false, the loop is broken (stopped). The third part is the increment stage that is called only after each successful loop.
Because of that in order to keep a up to date in the for loop head, it must appear twice.
Secondly, your solutions in getTotalX is additive, meaning that each multiple that works for each value in array b is added as a solution even if it doesn't fit the other values in b. To get it to work the way that you want, we have to use a Remove loop, rather than an Add loop.
List<int> multiples = getCommonMultiples(a, b_min);
//create List to hold number of ints which are in solution
List<int> solutions = multiples.ToList();
foreach(int x in multiples)
{
foreach(int y in b)
{
if (y % x != 0)
{
solutions.Remove(x);
break;
}
}
}
You could also use LINQ to perform an additive solution where it takes into account All members of b:
//create List to hold number of ints which are in solution
List<int> solutions = multiples.Where((x) => b.All((y) => y % x == 0)).ToList();
This seems extremely simple, yet I can't seem to find applicable documentation anywhere. In C#, how do you create a 'double[,]'? Specifically, the data I'm trying to represent is something like this:
[0,0] = 0
[0,1] = 1
[1,0] = 1
[1,1] = 2
I have tried [[0,1],[1,2]] and the equivalent with {{}{}} and {[][]} and various other things, but cannot seem to figure out the syntax. It seems that a simple [0,1] alone is a 'double[,]' but I would like to find a way to represent the above data (more than just 2 numbers).
What am I missing? If anyone can point me to some simple documentation, that would be great.
See Array initializers:
For a multi-dimensional array, the array initializer must have as many levels of nesting as there are dimensions in the array. The outermost nesting level corresponds to the leftmost dimension and the innermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array is determined by the number of elements at the corresponding nesting level in the array initializer. For each nested array initializer, the number of elements must be the same as the other array initializers at the same level.
In our case:
double[,] a = { { 0, 1 }, { 1, 2 } };
A multi-dimensional double array:
Double[,] newdouble = new Double[2,2];
or
Double[,] newdouble = { { 0, 0 }, { 1, 1 } };
In order to create a two dimensional array that you can assign to, you are going to need to first allocate the correct size. In this case, you have 2 rows and 2 columns, so that will be a [2,2].
double[,] twod = new double[2,2];
Next you simply assign to it like this
twod[0,0] = 0;
twod[0,1] = 1;
twod[1,0] = 1;
twod[1,1] = 2;
And then work with it however you wish.
There is three ways to initialized your array:
double[,] twoDemn = { { 0 , 1 }, { 1 , 2 } };
or:
double[,] twoDemn = new double[,] { { 0 , 1 }, { 1 , 2 } };
or:
double[,] twoDemn = new double[2,2];
twoDemn[0,0] = 0;
twoDemn[0,1] = 1;
twoDemn[1,0] = 1;
twoDemn[1,1] = 2;
I currently have this function:
public double Max(double[] x, double[] y)
{
//Get min and max of x array as integer
int xMin = Convert.ToInt32(x.Min());
int xMax = Convert.ToInt32(x.Max());
// Generate a list of x values for input to Lagrange
double i = 2;
double xOld = Lagrange(xMin,x,y);
double xNew = xMax;
do
{
xOld = xNew;
xNew = Lagrange(i,x,y);
i = i + 0.01;
} while (xOld > xNew);
return i;
}
This will find the minimum value on a curve with decreasing slope...however, given this curve, I need to find three minima.
How can I find the three minima and output them as an array or individual variables? This curve is just an example--it could be inverted--regardless, I need to find multiple variables. So once the first min is found it needs to know how to get over the point of inflection and find the next... :/
*The Lagrange function can be found here.** For all practical purposes, the Lagrange function will give me f(x) when I input x...visually, it means the curve supplied by wolfram alpha.
*The math-side of this conundrum can be found here.**
Possible solution?
Generate an array of input, say x[1,1.1,1.2,1.3,1.4...], get an array back from the Lagrange function. Then find the three lowest values of this function? Then get the keys corresponding to the values? How would I do this?
It's been a while since I've taken a numerical methods class, so bear with me. In short there are a number of ways to search for the root(s) of a function, and depending on what your your function is (continuous? differentiable?), you need to choose one that is appropriate.
For your problem, I'd probably start by trying to use Newton's Method to find the roots of the second degree Lagrange polynomial for your function. I haven't tested out this library, but there is a C# based numerical methods package on CodePlex that implements Newton's Method that is open source. If you wanted to dig through the code you could.
The majority of root finding methods have cousins in the broader CS topic of 'search'. If you want a really quick and dirty approach, or you have a very large search space, consider something like Simulated Annealing. Finding all of your minima isn't guaranteed but it's fast and easy to code.
Assuming you're just trying to "brute force" calculate this to a certain level of prcision, you need your algorithm to basically find any value where both neighbors are greater than the current value of your loop.
To simplify this, let's just say you have an array of numbers, and you want to find the indices of the three local minima. Here's a simple algorithm to do it:
public void Test()
{
var ys = new[] { 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 4, 5, 4 };
var indices = GetMinIndices(ys);
}
public List<int> GetMinIndices(int[] ys)
{
var minIndices = new List<int>();
for (var index = 1; index < ys.Length; index++)
{
var currentY = ys[index];
var previousY = ys[index - 1];
if (index < ys.Length - 1)
{
var neytY = ys[index + 1];
if (previousY > currentY && neytY > currentY) // neighbors are greater
minIndices.Add(index); // add the index to the list
}
else // we're at the last index
{
if (previousY > currentY) // previous is greater
minIndices.Add(index);
}
}
return minIndices;
}
So, basically, you pass in your array of function results (ys) that you calculated for an array of inputs (xs) (not shown). What you get back from this function is the minimum indices. So, in this example, you get back 8, 14, and 17.
I do have a 3-dimensional matrix
private int[][][] Matrix
but I dont know how to fill this.
the first dimension is for my slices of a picture
the second for my x-values of one slice ad the 3rd slice for my y-values.
so das anybody know how to fill this arrays with some data for testing?
thanks
You can do something like this:
Matrix = new int[5][][]; // 5 slices
Matrix[0] = new int[3][]; // 3 x values for the first slice
Matrix[0][0] = new int[2]; // 2 y values for the first x value in the first slice
But I don't think that you should use something like this. It is very error prone.
I suggest something like this:
class Slice
{
public IList<XValue> XValues {get; set; }
}
class XValue
{
public IList<YValue> YValues {get; set; }
}
class YValue
{
// ...
}
var slices = new List<Slice>();
You can use array literals to create arrays of arrays of arrays:
private int[][][] Matrix = {
{
{1,2,3},
{4,5}
},
{
{1,2},
{3},
{4,5,6,7,8},
{9,10}
},
{
{1,2,3}
}
};
Note that this is a jagged array, so different subarrays can have different number of items. If you want a three dimensional matrix you might want the three dimensional array int[,,] instead of nested one dimensional arrays.
I have an array such as
string[,] SSISVariableNameValue = new string[,]
{
{"DestinationConfigDB","dest db"},
{"DestinationServer","dest server"},
{"SourceConfigDB","source db"},
{"SourceServer","source server"},
{"SSISConfigFilter","filter"}
};
The .length property is listed as 10. How can i get the 'x' axis of this array..aside from dividing by 2?
An array has a Rank property to tell you the number of dimensions and a GetLength(int d) function to give you the size in a specific dimension. d = 0 .. Rank-1
So you want SSISVariableNameValue.GetLength(0);