This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
100 lines (91 loc) · 3.15 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace binning_demo
{
class Program
{
private static double _threshold = 0;
public static List<double> AddToBucket(List<double> numbersLeft, List<double> currentBucket)
{
if (numbersLeft.Count == 0)
{
return currentBucket;
}
List<List<double>> bucketsOfBuckets = new List<List<double>>();
int countNumsLeft = numbersLeft.Count;
for (int i = 0; i < countNumsLeft; i++)
{
double tempDouble = numbersLeft[i];
List<double> newList = new List<double>(numbersLeft);
newList.RemoveAt(i);
if ((currentBucket.Sum() + tempDouble) > 120)
{
return currentBucket;
}
List<double> newCurrentBucket = new List<double>(currentBucket);
newCurrentBucket.Add(tempDouble);
bucketsOfBuckets.Add(AddToBucket(newList, newCurrentBucket));
}
double max = 0;
int maxIndex = -1;
for (int j = 0; j < bucketsOfBuckets.Count; j++)
{
if (bucketsOfBuckets[j].Sum() > max)
{
max = bucketsOfBuckets[j].Sum();
maxIndex = j;
}
}
return bucketsOfBuckets[maxIndex];
}
static void Main(string[] args)
{
try {
StringBuilder sb = new StringBuilder();
// Try to load file.
String[] lines = File.ReadAllLines("input.txt");
if(!Double.TryParse(lines[0], out _threshold))
{
throw new Exception("Cannot parse the first line of input.txt. Should be your bin threshold.");
}
List<double> doubleList = new List<double>();
lines[1].Split(',').ToList().ForEach(v => doubleList.Add(Convert.ToDouble(v.Trim())));
double[] apps = doubleList.ToArray();
List<double> myList = new List<double>(apps);
int bucketCounter = 0;
while (myList.Count > 0)
{
bucketCounter++;
List<double> someBucket = AddToBucket(myList, new List<double>());
sb.AppendLine("Bucket number " + bucketCounter + ":");
for (int i = 0; i < someBucket.Count; i++)
{
if (i < (someBucket.Count - 1))
{
sb.Append(someBucket[i] + ", ");
}
else
{
sb.Append(someBucket[i]);
}
myList.Remove(someBucket[i]);
}
sb.AppendLine("\n");
}
string allText = sb.ToString();
Console.WriteLine(allText);
File.WriteAllText("output.txt", allText);
}
catch (Exception e)
{
Console.WriteLine("Binning failed due to: " + e.Message);
}
Console.WriteLine("Press any key to quit.");
Console.Read();
}
}
}