LINQ to extract duplicate data more than 3 - list

class Year
{
public int YearNumber;
public List<Month> Months = new List<Month>();
}
class Month
{
public int MonthNumber;
public List<Day> Days = new List<Day>();
}
class Day
{
public int DayNumber;
public string Event;
}
So I have a list of Years(list<year> years). How do I get the list (another list) which have the result that has duplicates event on the same day? I mean events can be happen on multiple dates, does not matter, what matters is, to find out if this any of date happens the same event from different year. . Lastly, (filter) only if its occurs more than 3 times. Example, 5 July 2014, 5 July 2017 and 5 July 2019 is 'Abc Festival', which occurs more than 3 times. So u get the date, the event, and the number of counts.

Using just the classes you show we can only group dates, where a "date" is a day in a month:
var query = from y in years
from m in y.Months
from d in m.Days
select new { m.MonthNumber, d.DayNumber }
into date
group date by date
into dateGroup
where dateGroup.Count() > 2
select dateGroup;
select dateGroup;
As you see, the core solution is to build new { m.MonthNumber, d.DayNumber } objects and group them.

Related

How do I link an int in a list to a string in a list in dart?

So I have created a list with the names of apps inside it. I sorted it in alphabetical order, printing the result. These apps have won the app of the year award for different years. How would I assign the year, an int, to a string in a list, for example, 2012 to FNB Banking, and print the result by calling for the year? So I've tried to put the years in a list but have no idea of how to link the two lists so backed out from that idea.
Did you try using a map<String,String>, you can set the map item key as the year and its value as the app.
it can be something like this:
List<String> apps = ["app_one","app_two"];
List<int> years = [2020,2021];
Map<int, String> appPerYear = {};
for (var i = 0; i < apps.length; i++) {
appPerYear[years[i]] = apps[i];
}
Then to print any app of a specific year use
print(appPerYear[2020]);

DynamoDB - Get all items which overlap a search time interval

My application manages bookings of a user. These bookings are composed by a start_date and end_date, and their current partition in dynamodb is the following:
PK SK DATA
USER#1#BOOKINGS BOOKING#1 {s: '20190601', e: '20190801'}
[GOAL] I would query all reservations which overlap a search time interval as the following:
I tried to find a solution for this issue but I found only a way to query all items inside a search time interval, which solves only this problem:
I decided to make an implementation of it to try to make some change to solve my problem but I didn't found a solution, following you can find my implementation of "query inside interval" (this is not a dynamodb implementation, but I will replace isBetween function with BETWEEN operand):
import { zip } from 'lodash';
const bookings = [
{ s: '20190601', e: '20190801', i: '' },
{ s: '20180702', e: '20190102', i: '' }
];
const search_start = '20190602'.split('');
const search_end = '20190630'.split('');
// s:20190601 e:20190801 -> i:2200119900680011
for (const b of bookings) {
b['i'] = zip(b.s.split(''), b.e.split(''))
.reduce((p, c) => p + c.join(''), '');
}
// (start_search: 20190502, end_search: 20190905) => 22001199005
const start_clause: string[] = [];
for (let i = 0; i < search_start.length; i += 1) {
if (search_start[i] === search_end[i]) {
start_clause.push(search_start[i] + search_end[i]);
} else {
start_clause.push(search_start[i]);
break;
}
}
const s_index = start_clause.join('');
// (end_search: 20190905, start_search: 20190502) => 22001199009
const end_clause: string[] = [];
for (let i = 0; i < search_end.length; i += 1) {
if (search_end[i] === search_start[i]) {
end_clause.push(search_end[i] + search_start[i]);
} else {
end_clause.push(search_end[i]);
break;
}
}
const e_index = (parseInt(end_clause.join('')) + 1).toString();
const isBetween = (s: string, e: string, v: string) => {
const sorted = [s,e,v].sort();
console.info(`sorted: ${sorted}`)
return sorted[1] === v;
}
const filtered_bookings = bookings
.filter(b => isBetween(s_index, e_index, b.i));
console.info(`filtered_bookings: ${JSON.stringify(filtered_bookings)}`)
There’s not going to be a beautiful and simple yet generic answer.
Probably the best approach is to pre-define your time period size (days, hours, minutes, seconds, whatever) and use the value of that as the PK so for each day (or hour or whatever) you have in that item collection a list of the items touching that day with the sort key of the start time (so you can do the inequality there) and you can use a filter on the end time attribute.
If your chosen time period is days and you need to query across a week then you’ll issue seven queries. So pick a time unit that’s around the same size as your selected time periods.
Remember you need to put all items touching that day (or whatever) into the day collection. If an item spans a week it needs to be inserted 7 times.
Disclaimer: This is a very use-case-specific and non-general approach I took when trying to solve the same problem; it picks up on #hunterhacker 's approach.
Observations from my use case:
The data I'm dealing with is financial/stock data, which spans back roughly 50 years in the past up to 150 years into the future.
I have many thousands of items per year, and I would like to avoid pulling in all 200 years of information
The vast majority of the items I want to query spans a time that fits within a year (ie. most items don't go from 30-Dec-2001 to 02-Jan-2002, but rather from 05-Mar-2005 to 10-Mar-2005)
Based on the above, I decided to add an LSI and save the relevant year for every item whose start-to-end time is within a single year. The items that straddle a year (or more) I set that LSI with 0.
The querying looks like:
if query_straddles_year:
# This doesn't happen often in my use case
result = query_all_and_filter_after()
else:
# Most cases end up here (looking for a single day, for instance)
year_constrained_result = query_using_lsi_for_that_year()
result_on_straddling_bins = query_using_lsi_marked_with_0() # <-- this is to get any of the indexes that do straddle a year
filter_and_combine(year_constrained_result, result_on_straddling_bins)

One to many merge using Akka streams

I have a use case where I have a list of values to be fetched from the database and a list of dates for which the values need to be fetched. I want to use akka streams (Flow or Source with GraphDSL) to make a one to many (or many to one) relationship between them so that I fetch each value for each of the dates
For example,
animals = cow, goat, sheep
years=2018, 2019
expected stream output is
cow & 2018
goat & 2018
sheep & 2018
cow & 2019
goat & 2019
sheep & 2019
If you want a product like this, you don't need the Graph DSL.
def animalsAndYears(animals: Source[Animal, NotUsed], years: Source[Year, NotUsed]): Source[(Animal, Year), NotUsed] =
years.flatMapConcat { year =>
animals.map { animal =>
animal -> year
}
}
So:
animalsAndYears(Source(listOfAnimals), Source(listOfYears))
would give you a stream of animal, year tuples. Let's say that you have a function:
def queryDBForAnimalYear(aandy: (Animal, Year)): Future[Seq[Row]] = ???
Then you can get a stream of the rows with:
val parallelism: Int = ??? // How many queries to have in-flight at a time
animalsAndYears(Source(listOfAnimals), Source(listOfYears))
.mapAsync(parallelism) { params => queryDBForAnimalYear(params) }
.mapConcat(identity) // gives you a Source[Row]

Drools: how to apply rules on all elements in a List and find a subset elements that satisfies given conditions

I am relatively newbie to drools and I am pulling my hair to achieve the following scenario.
I have a list of dates, let's say 10 dates. Now, I have to apply a set of rules among these elements to find matching set of 4 dates that satisfies all rules.
Assuming following are the rules. How do I iterate this in drools and find the four dates amongst provided 10 dates.
date2> date1 + (6W - 4D)
date3> date2 + (4W - 4D)
date4 > date3 + (6M - 4D)
date4 > date1 + (4Y - 4D)
I would suggest you to create a helper class who knows how to do arithmetic with dates.
public class Helper {
public static Date calculate(Date source, String expression) {
//Do the magic
}
}
You can then use this helper class in your rules as follows:
rule "Find subset"
when
$d1: Date()
$d2: Date(this > Helper.calculate($d1, "6W-4D"))
$d3: Date(this > Helper.calculate($d2, "4W-4D"))
$d4: Date(
this > Helper.calculate($d1, "4Y-4D"),
this > Helper.calculate($d3, "6M-4D")
)
then
//Do whatever you need
end
Hope it helps,

Initializing boost date to the first year of AD

I would want to initialize a gregorian date like this
boost::gregorian::date d = { 1, 1, 1 };
but year = 1 is not allowed.
How can I create a day before Jesus was born ?
The current implementation of Gregorian date supports dates in the range 1400-Jan-01 to 9999-Dec-31. So handling a date outside this range is no directly possible.