Given n bulbs, initially, all bulbs are off and m switches, each switch controls a range of light bulbs l to r (both inclusive)
We neeed to determine if it is possible to switch on all the bulbs using any of m switches any no of time.
The answer can be yes if one can or can be no if one cant.
E.g first given n then m and then next m lines give the range which the switch controls. In the following example n = 5 and m = 2.
5 2
1 2
3 5
here answer is yes because 2 switches can control all the bulbs. and can be turned on.
Second example
5 2
1 2
3 4
here the answer is no because one of the bulbs is not controlled by any switch.
What I did is declare an array count with size m and for each switch I counted how many bulbs it control by (r-l +1) if the sum of count is equal to n then yes else no.
But only sample test cases are passed rest all fails.
Note: Ranges might intersect
If ranges intersect for example
1 3
3 5
The answer is no because when one turn the second switch on the 3 rd bulb flip from on to off
So the answer will be no.
We can see this problem as an instance XOR-SAT problem, though it is more general than the problem posed here, that's one way to go.
Just to gain some intuition I provide a very simple example. Suppose you have systems of three switches and three bulbs like this:
S B
1 1, 3 // toggles bulbs 1 and 3
2 1, 2
3 1, 2, 3
It is equivalent to have the following formula, which we want to satisfy:
(x1^x2^x3)&(x1^x2)&(x1^x3).
And now we want to satisfy this formula. We start with writing it as system of boolean equations modulo 2:
|1 1 1| |x_1| |1|
|0 1 1| * |x_2| = |1| mod 2
|1 0 1| |x_3| |1|
Now solve it with Gaussian elimination.
First, add the first and the second rows to the third:
1 1 1 1 1 1 1 1
0 1 1 1 -> 0 1 1 1
1 0 1 1 0 0 1 1 // for RHS, 1+1+1 = 1 mod 2
Second, back-substitute: x1 = 0, x2 = 0, x3 = 1, which is obviously the answer.
So, the main complexity here is to program Gaussian elimination process.
I have a dataset of transactions where each transactions represent a purchase of a single item. So, each order is recorded as 3 transactions if the order contained 3 items.
Example dataset:
User Order, ItemCount, ItemPrice
1 1 1 10
1 1 1 10
1 2 1 30
1 2 1 30
2 3 1 20
2 3 1 20
3 4 1 15
3 4 1 15
3 4 1 15
To reduce the dataset I have grouped by order and user and aggregated ItemCount and ItemPrice to get a dataset like this:
User Order, ItemCount, OrderAmount
1 1 2 20
1 2 2 60
2 3 2 40
3 4 3 45
Now I want to group the orders by user and do some analysis on the orders for each user. Is there a way in Spark to group the orders by user and end with a pair of > where User is the user id and the Dataset contains the orders?
The only solution I see at the moment is to convert the dataset to rdd and do groupbykey to get rddpair> and then write some code to do my analysis on the list of rows.
I would prefer a solution where I can work with the orders as a Dataset and do my analysis using Dataset functionality. Can anyone point me into the right direction here? Is this possible?
I am new to spark and have been using Spark with Java so far as I have very limited experience with Scala, but examples in Scala would help.
Just group by user and order and aggregate columns itemcount and itemprice. Then group by user and run all the aggregations in the appropriate columns.
df.groupBy($"User", $"Order").agg(sum($"ItemCount").as("count"),
sum($"ItemPrice").as("total"))
.groupBy($"User").agg(avg($"total").as("avg_amount"),
avg($"count").as("avg_count"),
count($"count").as("total_purchases"))
I need your help with F-W algorithm. My question is: is it possible to find shortest path between two vertexes? Down below I will give you an example of what I want.
1 -----50(w)----- 5
| \__ / |
| \5(w) /20(w)|
10(w) \__3__/ |25(w)
| \___ |
| 18(w)\ |
2--------8(w)------4
If my picture isn't possible to see, here is a picture of it: http://prntscr.com/b6n43d
This is what I got with final cycle with weights:
0 10 5 18 25
10 0 15 8 33
5 15 0 18 20
18 8 18 0 25
25 33 20 25 0
And this is what I got with vertex'es:
0 1 2 1 2
0 0 0 3 3
0 0 0 3 4
1 1 2 0 4
2 3 2 3 0
And I need to find that vertex, that furthermost vertex be as most as possible near that first vertex. What I mean, that vertex 1 have 50 weight with 5 vertex (that mean they are furthermost from each other) and I need to find shortest path between 1 and 5. Any ideas how I can do that ?
The whole code were write'd from https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm pseuudo code.
This is a largely conceptual question so i dont have any code to show. I'll try to explain this as best i can. I am writing a program that is supposed to find common sequences of numbers found in a large table of random combinations.
So for example take this data:
1 5 3 9 6 3 8 8 3 3
6 7 5 5 5 4 9 2 0 1
6 4 4 3 7 8 3 9 5 6
2 4 2 4 5 5 3 4 7 7
1 5 6 3 4 9 9 3 3 2
0 2 7 9 4 5 3 9 8 3
These are random combinatinos of the numbers 1-9. For every 3 digit (or more) sequence found more than once i need to put that into another database. So the first row contains "5 3 9" and the 6th row also contains "5 3 9". I would put that sequence in a separate table with the number of times it was found.
I'm still working out the algorithm for actually making these comparisons but i figure i'll have to start with "1 5 3", compare that to every single 3 number trio found, then move on to "5 3 9" then "3 9 6" etc....
MY MAIN PROBLEM RIGHT NOW is that i dont know how to do this if these numbers are stored in a database. My database table has 11 columns. One column for each individual number, and one column for the 10 digit sequence as a whole. Columns are called Sequence, 1stNum, 2ndNum, 3rdNum...10thNum.
Visual: first row in my database for the data above would be this :
| 1 5 3 9 6 3 8 8 3 3 | 1 | 5 | 3 | 9 | 6 | 3 | 8 | 8 | 3 | 3 |
("|" divide columns)
How do i make comparisons efficiently with Java? I'm iterating over every row in the table many times. Once for the initial sequence to be compared, and for every one of those sequences i go through each row. Basically a for loop in a for loop. This sounds like its going to take a ton of queries and could take forever if the table gets to be massive (which it will).
Is it more computationally efficient if i iterate through a database using queries or if i dump the database and iterate through a file?
I tried to explain this as best as i could, its a very confusing process for me. I can clarify anything you need me to. I just need guidance on what the best course of action for this would be.
Here is what I would do, assuming you have retrieved the sequences in a list :
List<String> sequences = Arrays.asList("1539638833","6755549201","6443783956","2424553477","1563499332","0279453983");
Map<String,Integer> count = new HashMap<>();
for (String seq : sequences) {
int length = seq.length();
for (int i=0 ; i<length - 2 ; i++) {
String sub = seq.substring(i,i + 3);
count.put(sub,count.containsKey(sub) ? count.get(sub) + 1 : 1);
}
}
System.out.println(count);
Ouput :
{920=1, 783=1, 945=1, 332=1, 963=1, 644=1, 156=1, 983=1, 453=1, 153=1, 388=1, 534=1,
455=1, 245=1, 539=2, 554=1, 242=1, 555=1, 553=1, 437=1, 883=1, 349=1, 755=1, 675=1,
638=1, 395=1, 201=1, 956=1, 933=1, 499=1, 634=1, 839=1, 794=1, 027=1, 477=1, 833=1,
347=1, 492=1, 378=1, 279=1, 993=1, 443=1, 396=1, 398=1, 549=1, 563=1, 424=1}
You can then store these values in the database from the Map.
You can do it in sql with a union clause:
select sum(c), sequence
from
(
select
count(*) as c, concat(col1 ,col2 , col3) as sequence
from t
group by col1, col2, col3
union
select
count(*) as c, concat(col2 ,col3 , col4) as sequence
from t
group by col2, col3, col4
union (... and so on enumerating through the column combinations)
) as tt
group by sequence
I would imagine a pure java implementation would be quicker and have less
memory overhead. But if you already have it in the database it may be quick
enough.
How to group rows in htmldatatable?
I am using JSF.
A short example :
TransNum TransAmount InvoiceNum InvoiceAmount
1 50 1 10
1 50 2 15
1 50 3 30
2 10 1 6
2 10 2 5
If I select Grouping column as "InvoiceNum" then the table should look like:-
(i.e Grouping is done on InvoiceNum):
TransNum TransAmount InvoiceNum InvoiceAmount
1
1 50 1 10
2 10 1 6
2
1 50 2 15
2 10 2 5
3
1 50 3 30
TransNum TransAmount InvoiceNum InvoiceAmount
Similarly, grouping can be done based on multiple columns values too.
Thanks in advance.
JSF h:dataTable has no built-in grouping.
Either you find a component that fits your needs in one of the component libraries, such as Primefaces, Richfaces or Icefaces.
Or you have to implement it yourself in the backing bean by sorting the list in the way you want.