I have a queue of elements, sorted by date. I need to extract the first n elements, which have the same date and add them to a temporary ArrayList, from which I choose one of them and scrap the others. After that I need to continue doing the same thing for the next n elements of the queue with the same date (extract them to the temp list and so on) until I have no more items in the queue.
// some notes to help you understand the code
PriorityQueue<Results> r, size(4), elementsEqualByTime(1=2,3=4);
List<Comments> c, size(2);
ArrayList temp;
if (c.size() != r.size() && resultIter.hasNext()) {
//first iteration will compare element 0 to itself -> 100% true
ResultObject r2 = resultIter.next();
ResultObject r1 = r2;
while (resultIter.hasNext() && r1.getTime().equals(r2.getTime())) {
temp.add(r1);
//we add the matching elements before we continue
r1 = r2;
temp.add(r1);
if (resultIter.hasNext()) {
//after we add the 2 matching elements we continue
r2 = resultIter.next();
}
}
//use the items in temp
temp.clear();
}
Right now it works for the 1st set of elements, but on the 2nd iteration it adds no elements to the temp ArrayList. I'd appreciate help with this solution, but am also open to different suggestions.
boolean Check (List<Element> elements,Element element)
{
for(Element element1:elements)
if(element1.equals(element))
return true;
return false;
}
void Stuff()
{
// some notes to help you understand the code
PriorityQueue<Element> r = new PriorityQueue<Element>();
List<Element> c;
List<Element> temp = new ArrayList<Element>();
for(Element element:r)
{
if(!Check(temp, element))
{
// do stuff with temp
temp = new ArrayList<Element>();
}
temp.add(element);
}
}
while (commentIter.hasNext()) {
Comment c1 = null;
temp.add(arrayQueue[0]);
for (int i = 1; i < arrayQueue.length; i++) {
if (!arrayQueue[i].getTime().equals(arrayQueue[i - 1].getTime())) {
c1 = commentIter.next();
//do stuff with the results
temp = new HashSet<ResultObject>();
}
temp.add(arrayQueue[i]);
}
if (!temp.isEmpty()) {
c1 = commentIter.next();
//do stuff with the results
}
temp = new HashSet<ResultObject>();
}
That's a tested solution which works.
Related
I have a list declared outside a for loop and then I assign some values to this list inside that for loop and its value is updated when printed inside the loop but when I print it after the loop, it gives me an empty list.
List<List<String>> chunkSizeCollection(List<String> followedList) {
int counter = 0;
int ongoingCounter = 0;
bool isLessThanTen = false;
List<List<String>> returnAbleChunkedList = [];
List<String> midList = [];
log("in followedlist");
followedList.forEach((element) {
if (counter == 0) {
int difference = followedList.length - ongoingCounter;
if (difference < 10) {
// log("in difference if: $difference");
isLessThanTen = true;
}
}
midList.add(element);
counter++;
ongoingCounter++;
if (counter == 10 || (isLessThanTen && ongoingCounter == followedList.length)) {
returnAbleChunkedList.add(midList);
log("returnAbleChunkedList in counter 10 after adding new val is: $returnAbleChunkedList");
//above log works properly and prints the updated list
midList.clear();
counter = 0;
}
});
//this log on the other hand, returns an empty list
log("returnAbleChunkedList: $returnAbleChunkedList before return");
return returnAbleChunkedList;
}
The output:
[log] returnAbleChunkedList in counter 10 after adding new val is: [[KQTuEPllbmRrlBNvYgZ7oUXwtA63, OZUZOE10IzT8quUFoZbNxZOynU32, fCYIlYemCvbLTc7SpNHw6fCHrcm1, CbcLrtDNOdYZyC23FzEehOrJbKx2, FFvvVHCpPGNKUiXPQD34QdoPqH32, Gk09y59vSVXa1HNhzYvc6Atqnt53, JDO356z8urYQvuktJmc6eNUYqSm2, YesvvNI43gUVYPMfqhG4uRO5t6K2]]
[log] returnAbleChunkedList: [[]] before return
In this line:
returnAbleChunkedList.add(midList);
you add a reference to midList to your output list. If your input is longer than 10, you'll end up adding more than one reference to midList to the output list (i.e. you might now have 2). Subsequently, you clear midList so you now have an output list that contains 2 references to a list that you've now cleared, so you end up with a list containing 2 empty lists.
If, instead, you changed this to:
returnAbleChunkedList.add(midList.toList());
you'd add a copy of midList to your output list and your end up with:
[[a, b, c, d, e, f, g, h, i, j], [k]]
as you expected.
The problem is this:
returnAbleChunkedList.add(midList);
midList.clear();
You add the object midlist to the list and then you clear it. So the object you added will be emptied. You have to create a copy or add the elements of the list.
returnAbleChunkedList.add(midList.toList());
// or
returnAbleChunkedList.add(List.of(midList));
// or
returnAbleChunkedList.add([...midList]);
working with an ADT message template in Mirth, having issues with the IN1 and IN2 segments, the IN2 specifically.
Here's a sample message that I'm working with, removed almost all the segments.
MSH|^~&|EPIC|AMB||99|20220403165344|RELEASEAUTO|ADT^A04|367476|T|2.5|||AL|NE
IN1|1|10500201|105^Test|BCBS NC BLUE CARE^Test1|PO BOX 35^^DURHAM^NC^27702^||
IN2|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||first iteration IN1-62||
IN1|1|10500201|106^Test|BCBS NC BLUE CARE^Test1|PO BOX 35^^DURHAM^NC^27702^||
IN2|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||second iteration IN1-62||
So far I've built the following to iterate over the IN1 segment:
//IN1 Segments
var IN1count=0;
for each (seg in msg.IN1) {
createSegment('IN1',output,IN1count);
output.IN1[IN1count]['IN1.2'] = seg['IN1.2'];
output.IN1[IN1count]['IN1.3'] = seg['IN1.3'];
output.IN1[IN1count]['IN1.4'] = seg['IN1.4'];
output.IN1[IN1count]['IN1.8'] = seg['IN1.8'];
output.IN1[IN1count]['IN1.9'] = seg['IN1.9'];
output.IN1[IN1count]['IN1.10'] = seg['IN1.10'];
output.IN1[IN1count]['IN1.12'] = seg['IN1.12'];
output.IN1[IN1count]['IN1.13'] = seg['IN1.13'];
output.IN1[IN1count]['IN1.22'] = seg['IN1.22'];
output.IN1[IN1count]['IN1.36'] = seg['IN1.36'];
IN1count++;
}
I'm struggling to map the IN2 segment correctly on the outbound, I've read about getSegmentsAfter but I can't find that function online... how do I write the correct JS code to look for whether the given IN1 has an IN2 after, specifically if it has IN2-61, and only then create the IN2 segment?
Hope that makes sense :)
You'll find code for createSegmentAfter here; it is JavaScript inserted before your transformer: https://github.com/nextgenhealthcare/connect/blob/2f3740db44c8d42aa6fafffd991b9c1fde940ea0/server/src/com/mirth/connect/server/builders/JavaScriptBuilder.java
One approach to keeping track of whether you just passed an IN1 would be to loop over all segments with something like this:
var was_in1 = false;
var last_in1;
for each ( seg in msg.children() ){
if (was_in1) {
if (seg.name() == "IN2") {
// existing IN2 follows IN1
} else {
// insert new IN2
}
if (seg.name() == "IN1") {
was_in1 = true;
last_in1 = seg;
} else {
was_in1 = false;
}
}
It'd be nice if there was a "nextSibling()" method for messages, but there isn't. Luckily, we can simulate it by:
Getting the current object's childIndex
Getting all children from the current object's parent
Access the next child with the index childIndex + 1
It'd look like this:
for each (seg in msg.IN1) {
// Do your IN1 stuff ...
var nextSeg = seg.parent().children()[ seg.childIndex() + 1];
if(nextSeg != null && nextSeg.name() == 'IN2' && nextSeg['IN2-61'].toString() != '') {
// Do yourIN2 stuff ...
}
}
Trying to split one row into many based on string in two cells. it is similar to the question
LINQ to separate column value of a row to different rows in .net
but i need to split based on Product & Cost Columns rather than product column only
SNo.
Product
Cost
1
colgate,closeup,pepsodent
50,100,150
2
rin,surf
100
into
SNo.
Product
Cost
1
colgate
50
1
closeup
100
1
pepsodent
150
2
rin
100
2
surf
100
I'm using Linq to Object with Entity Framework
Try the following. Since you have not presented any model it can be inaccurate in names.
var loaded = ctx.Products.ToList();
var query =
from p in loaded
from sp in p.Product.Split(',').Zip(p.Cost.Split(','), (p, c) => (p, c))
select new
{
Sno = p.Sno,
Product = sp.p,
Cost = sp.c
};
var splitted = query.ToList();
Using #SvyatoslavDanyliv naming, here is an answer:
var loaded = ctx.Products.ToList();
var query =
from p in loaded
from sp in p.Product.Split(',').Zip(p.Cost.Split(','), (p, c) => (p, c))
select new
{
Sno = p.Sno,
Product = sp.p,
Cost = sp.c
};
var splitted = query.ToList();
It feels a bit complicated to me. I would prefer using an extension method to create a variant of Zip that repeats the last element of a shorter sequence to match the longer sequence:
public static class EnumerableExt {
public static IEnumerable<(T1 First,T2 Second)> ZipExtend<T1,T2>(this IEnumerable<T1> s1, IEnumerable<T2> s2) {
var s1e = s1.GetEnumerator();
var s2e = s2.GetEnumerator();
T1 s1eLast = default;
T2 s2eLast = default;
bool has_s2 = false;
if (s1e.MoveNext()) {
do {
s1eLast = s1e.Current;
if (s2e.MoveNext()) {
s2eLast = s2e.Current;
has_s2 = true;
}
else if (!has_s2)
yield break;
yield return (s1eLast, s2eLast);
} while (s1e.MoveNext());
if (has_s2)
while (s2e.MoveNext())
yield return (s1eLast, s2e.Current);
}
yield break;
}
}
Then the answer is:
var query =
from p in loaded
from pr in p.Product.Split(',').ZipExtend(p.Cost.Split(','))
select new
{
Sno = p.Sno,
Product = pr.First,
Cost = pr.Second
};
var splitted = query.ToList();
I want to update the information contained in my "var" variable in my "PrincipalProduit__c" object,
using this function
public void DMLOperation () {
List <PrincipalProduit__c> letter = getValo ();
List <PrincipalProduit__c> updateList = new List <PrincipalProduit__c> ();
system.debug (letter);
for (PrincipalProduit__c opps: letter) {
updateListe.add (opps);
system.debug (opps);
}
update (sObject []) updateList;
}
But when I make a debug of my valriable "letter" I do not see any value.
what I would like to do is to be able to update the data in my object "principalProduct__c" because in this table the data are incomplete
Here is the code that returns the data that I want to update in my "PrincipalProduit__c" object
public List<PrincipalProduit__c> getValo(){
List<PrincipalProduit__c> searchList = new List<PrincipalProduit__c>();
List<PrincipalProduit__c> var = new List<PrincipalProduit__c>();
for (integer l = 0; l < data.size(); l++) {
searchList=[SELECT id From PrincipalProduit__c where Email__c=:data[l][0]];
system.debug(searchList);
for (PrincipalProduit__c principalProd :searchList) {
principalProd.CodeAlliance__c = data[l][1];
var.add(principalProd);
}
}
system.debug(var);
return var;
}
First of all,
In
List <PrincipalProduit__c> updateList = new List <PrincipalProduit__c> ();
the variable is updateList, but you are using updateListe in the inner part.
updateListe.add (opps);
I tried to do a Parameterized constructor for a linked list my program is about to implement a queue by using a liked list so i want to do a parameterized constructor like Queue(int value , int size) and it dose not run or doing a list
this is my code for this problem
Queue(int value,int _size)
{
for(int i = 0; i < _size; ++i)
{
Node* temp = new Node;
temp->data = value;
temp->next = nullptr;
if(head == nullptr)
{
head = tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
}
i expected that the result is to fill the lest by value times size like if i run this function Queue x(20,3) the linked list should be
20 20 20
Since that this is a constructor, The head and tail are not properly initialized to use them. I would suggest adding head = tail = nullptr just before the loop and see what happens.
Follow this code after your node creation. I hope this will work. And do use i++ instead of ++i, as the later will make the loop for size-1 times.
if(head == NULL)
head = temp;
else{
Node *x;
x= head;
while(x->next != NULL)
x = x->next;
x->next = temp;
}