Load Excel data set in processing and use it to draw sphere - processing

I'm a beginner and would like to know how I can generate a sphere in processing using a small data set. Sort of like a pie chart but in a sphere. I was able to load the dataset. Either create the sphere using product so you'll have 10 distributions or using category where you sum up the categories and create 3 distro. Thanks in advance.
Data:
Product Sales Category
1 7353784 1
2 745551 1
3 145702 1
4 547905 2
5 201991 3
6 88669 2
7 48909 2
8 36361 2
9 141726 2
10 1877020 3
11 374020 3
Table table;
void setup(){
table = loadTable("HW 1 Data.csv", "header");
println(table.getRowCount() + " total rows in table");
for (TableRow row : table.rows()) {
int product = row.getInt("Product");
int sales = row.getInt("Sales");
int category = row.getInt("Category");
println(product + " (category " + category + ") has total sales of " + sales);
}
}

Related

How can I improve my Countdown Numbers Solver algorithm to find more solutions?

As my school Project, I need to build a solver for Countdown Numbers & Letters rounds. I wanted to develop a structure which I can use to build both solvers, and I first developed a Numbers solver. However, before using this solution for Letters, I need to improve my current algorithm. I think I'm wrong somewhere, because I don't get the same results with other tools I am using to compare my program. Here is program for my solver;
/// numbers_game_solver.dart
import 'dart:collection';
import 'package:trotter/trotter.dart';
/* Import statements was package-based, I turned them into relative paths for question. */
import 'number_generator.dart';
import 'operation.dart';
import 'solution.dart';
import 'solutions.dart';
/* Will try to combine numbers with operations, as shown below;
* List<List<Operation>> operations = <Operations>[a, ,b, ,c, ,d, ,e, ,f
* + - + * / ]
* Then if last operations result is equal to target, will result it.
* If not will show closest result.
*/
const List<String> kOperators = const <String>[kOpAdd, kOpDiv, kOpMul, kOpSub];
class NumbersGameSolver {
NumbersGameSolver()
: this.solutions = Solutions(_expectedResult);
/* TODO: Do tests with smaller numbers and targets. */
final List<int> _numbers = const <int>[1, 2, 3, 4]; // NumberGenerator.numbers;
static final int _expectedResult = 15; //NumberGenerator.expectedResult;
final Solutions solutions;
void solve() {
/* All permutations of operators with replacement, which will be inserted between numbers. */
final Set<List<String>> amalgamsOperators = Amalgams<String>(_numbers.length - 1, kOperators)().toSet();
/* There may duplicates occur in numbers list, because of this, numbers will be mapped
using permutations of indices. */
final List<int> indices = List<int>.generate(_numbers.length, (int index) => index);
final Iterable<List<int>> permutationsIndices = Permutations<int>(indices.length, indices)();
final Set<List<int>>
permutationsNumbers = permutationsIndices.map(
(List<int> listPerm) => listPerm.map(
(int index) => _numbers[index]
).toList()
).toSet();
for (final List<int> numbers in permutationsNumbers) {
for (final List<String> operators in amalgamsOperators) {
Queue<int> stackNums = Queue<int>.from(numbers);
Queue<String> stackOprts = Queue<String>.from(operators);
Solution tempSolution = Solution(_expectedResult);
do {
int left = stackNums.removeFirst(), right = stackNums.removeFirst();
Operation tempOperation = Operation(stackOprts.removeFirst(), left, right);
/* Record solutions current state. */
SolutionState solutionState = tempSolution.addOperation(tempOperation);
if (solutionState == SolutionState.currentlyValid) {
/* If valid, add result to the current numbers stack. */
stackNums.addFirst(tempOperation.result);
} else if (solutionState == SolutionState.lastOperationRedundant) {
/* If operation is redundant, dispose it and continue. */
continue;
} else if (solutionState == SolutionState.lastResultInvalid) {
/* If results is invalid at any stage, dispose whole solution. */
break;
}
if (solutions.addSolution(tempSolution) == true) break;
} while (stackNums.length > 1);
}
}
/* Will show only accurate solutions.
* If there is no accurate solutions, will show solutions which results
* are closest to the expected result.
*/
solutions.showSolutions();
}
}
There are 5 classes, to shorten the question I added them in this Gist.
My algorithm is as follows;
Rules for this Project are; program must randomly generate 5 single digit number and 1 two digit number where twoDigitNumber % 10 == 0 and a three digit number as target.
I get permutations of 4 operators and numbers that will be used in operations (Using trotter package.)
For each permutation of numbers, I apply each permutation of operators; using Operation class and add them into a Solution instance for each permutation.
I pass some redundant operations in each iteration, and if there is an invalid result at any stage, I dispose that solution and continue. (I'm taking this DataGenetics blog about this topic as a reference.)
To test my algorithm I used numbers 1, 2, 3, 4 and set target as 15. The results from dcode.fr Solver are as is;
15 (2 op.)
4 + 1 = 5
5 x 3 = 15
15 (3 op.)
4 + 3 = 7
7 x 2 = 14
14 + 1 = 15
15 (3 op.)
4 x 3 = 12
12 + 2 = 14
14 + 1 = 15
15 (3 op.)
4 x 3 = 12
2 + 1 = 3
12 + 3 = 15
15 (3 op.)
3 + 2 = 5
4 - 1 = 3
5 x 3 = 15
15 (3 op.)
4 x 3 = 12
12 + 1 = 13
13 + 2 = 15
15 (3 op.)
4 - 1 = 3
3 + 2 = 5
5 x 3 = 15
15 (3 op.)
4 + 2 = 6
6 - 1 = 5
5 x 3 = 15
15 (3 op.)
2 + 1 = 3
4 x 3 = 12
12 + 3 = 15
15 (3 op.)
2 - 1 = 1
4 + 1 = 5
5 x 3 = 15
(A total of 10 solutions.)
and the solutions my program found are as is;
> SOLUTION 1 ~
4 - 1 = 3
3 + 2 = 5
5 x 3 = 15
> SOLUTION 2 ~
4 + 1 = 5
5 x 3 = 15
(A total of 2 solutions.)
Can you tell me what am I thinking wrongly; Why can't I find all solutions? What are alternative approaches I can take to solve this problem? Is there anything I'm missing?
TY for taking time.

linq query to list that adds position of result

I have the linq query that produces the result below:
var result = from x in model.SITEs
where x.SiteId == homeSite
select new { x.SiteId,
x.SiteAlloc1,
x.SiteAlloc2,
x.SiteAlloc3,
x.SiteAlloc4 });
SiteId SiteAlloc1 SiteAlloc2 SiteAlloc3 SiteAlloc4
======================================================
1 5 3 2 4
But what I need is something more like this, where the Rank is the position of the SiteId in the result.
SiteId Rank
==================
1 1
2 4
3 3
4 5
5 2
var result = from x in model.SITEs
where x.SiteId == homeSite
select new { x.SiteId,
x.SiteAlloc1,
x.SiteAlloc2,
x.SiteAlloc3,
x.SiteAlloc4 }).
Select((t,u) => new {
SiteId = t.SiteId,
SiteAlloc1 = t.SiteAlloc1,
SiteAlloc2 = t.SiteAlloc2,
SiteAlloc3 = t.SiteAlloc3,
SiteAlloc4 = t.SiteAlloc4,
Rank = u + 1));
Where u is the index (0 based, that's why I added 1), or in your case the rank and t is the selected object

How to increment by group

There is a table and now add a new column -- sort_num int default 0
id level sort_num
1 1 0
2 1 0
3 2 0
4 2 0
5 2 0
6 3 0
7 3 0
8 3 0
9 3 0
Now I want to set sort_num values like below
id level sort_num
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
6 3 1
7 3 2
8 3 3
9 3 4
The Java code implement above requirement is
int sortNum = 0;
int currentLevel = fooList.get(0).getLevel();
for (RuleConf foo : fooList) {
if(currentLevel != foo.getLevel()){
sortNum = 0;
currentLevel = foo.getLevel();
}
foo.setSortNum(++sortNum);
}
I want to know if Java8 could simplify above code?
PS. Use mysql to implement this requirement
set #index:=0; update t set sort_num = (#index:=#index+1) where level = 1 order by id;
set #index:=0; update t set sort_num = (#index:=#index+1) where level = 2 order by id;
set #index:=0; update t set sort_num = (#index:=#index+1) where level = 3 order by id;
The best approach is to stick to your plain enhanced for loop. I don't think it is possible to come up with a single Stream solution, since you need to have intermediate values. Like:
Map<Integer, List<RuleConf>> levels = fooList.stream()
.collect(Collectors.groupingBy(RuleConf::getLevel));
levels.values().forEach(v ->
IntStream.range(0, v.size()).forEach(i -> v.get(i).setSortNum(i + 1))
);
If you keep track of the next order numbers yourself, you may do it with one stream. This solution is thread safe as well, hence should work with parallel streams:
Map<Integer, AtomicInteger> orders = new ConcurrentHashMap<>();
fooList.stream().forEachOrdered(foo -> {
orders.putIfAbsent(foo.getLevel(), new AtomicInteger());
foo.setOrder(orders.get(foo.getLevel()).incrementAndGet());
});
It should outperform the other stream-solutions, because it requires to iterate over the list only ones.

How to sort a vector of structure belonging to same category/set in c++

While solving a competitive coding problem I got stuck with the the following sorting scenario.
I have a vector of following structure
struct Data{
int p;
int val;
int ll;
};
Defined as :
vector<Data> a(N);
Now p field in the structure tells the set number to which val belong.
e.g if values are 1 2 3 4 5 6 7 8 9
(1,4,7) belong to group/set 3 i.e p is 3 , (2,5,8) belong to group/set 4 i.e p is 4 and (3,6,9) belong to group/set 5 i.e p is 5
i have p and val field in structure as
p as 3 4 5 3 4 5 3 4 5
val as 1 2 3 4 5 6 7 8 9
Now the problem is I have to sort the vector set wise in descending order
i.e 7 8 9 4 5 6 1 2 3
here 1 4 and 7 belong to set 3 so they are sorted in their respective places.
I tried with the selection sort as below which worked fine but it gave Time limit exceeded because of O(N^2) complexity.
for(int i=0;i<N;i++)
{
int mi=i;
Data max=a[i];
for(int j=i+1;j<N;j++)
{
if((a[i].p==a[j].p)&&(a[j].val>max.val))
{
max=a[j];
mi=j;
}
}
a[mi]=a[i];
a[i]=max;
}
Please help me find the best (time complexity) way to sort this scenario (if possible using STL sort).
Thanks in advance.
Modifying http://www.cplusplus.com/reference/algorithm/sort/, the key bit is:
#include <algorithm>
...
bool mycomparison (Data i, Data j) {
if (i.p != j.p)
return j.p < i.p;
else
return j.val < i.val;
}
...
// Sort vector a
a.std::sort( a.begin(), a.end(), mycomparison );
...
Note that reversing i and j in the return lines causes things to be in descending order.

Accessing pixel pointer

Can you explain to me how the accessing pixel has been done in this double loop ? how do they change rows and cols and pixel's value?
for(int i=0;i<cv_ptr->image.rows;i++)
{
float* ptr_img_A = cv_ptr->image.ptr<float>(i);
for(int j=0;j<cv_ptr->image.cols;j++)
{
*ptr_img_B=255*(*ptr_img_A)/3.5;
ptr_img_A++;
}
}
Thank you.
First of, is this the complete code or is ptr_img_B actually ptr_img_A in the first row of the inner loop?
Secondly I would recommend naming your variables by the content it has.
for(int i=0;i<cv_ptr->image.rows;i++)
{
float* row_ptr = cv_ptr->image.ptr<float>(i);
for(int j=0;j<cv_ptr->image.cols;j++)
{
*row_ptr=255*(*row_ptr)/3.5;
row_ptr++;
}
}
So basically it scales every pixel in the image with the factor of 255/3.5
Regarding the access of the pixel, it is stored in memory like this
COL
ROW 1 2 3 4
1 0 1 2 3
2 4 5 6 7
3 8 9 10 11
4 12 13 14 15
Then in order to access, for example pixel on row 3, col 2
float* row_ptr = cv_ptr->image.ptr<float>(row-1);
float value = row_ptr[col-1];
Finally I hope you will enjoy your time with openCV, it is a really nice framework :)

Resources