how to find the max value of nested arraylist of string using streams - arraylist

I have an ArrayList in the below format
List<List<String>> ll = [["0", "a"], ["1", "b"], ["0", "c"], ["1", "d"]]
I want to find the maximum value by considering the first position in the nested list. How can I do it using streams?
Using streams how to find the maximum value by taking the position at
Integer.parseInt(ll.get(i).get(0))

Firstly, the code you posted doesn't even compile. One approach of fetching the largest number from a nested list.
List<List<String>> ll = Arrays.asList(Arrays.asList("10", "a"), Arrays.asList("21", "b"), Arrays.asList("10", "c"),
Arrays.asList("11", "d"));
OptionalInt max = ll.stream().flatMap(l -> l.stream()).filter(str -> Character.isDigit(str.charAt(0)))
.distinct().mapToInt(i -> Integer.parseInt(i)).max();
System.out.println(max.getAsInt());

You may do it like so,
int max = ll.stream().mapToInt(l -> Integer.parseInt(l.get(0))).max()
.orElseThrow(IllegalArgumentException::new);

Related

R code for matching multiple stings in two columns and returning into a third separated by a comma

I have two dataframes. The first df includes column b&c that has multiple stings seperated by a comma. the second has three columns, one that includes all stings in column B, two that includes all strings in c, and three is the resulting string I want to use.
x <- data.frame("uuid" = 1:2, "first" = c("jeff,fred,amy","tina,cat,dog"), "job" = c("bank teller,short cook, sky diver, no job, unknown job","bank clerk,short pet, ocean diver, hot job, rad job"))
x1 <- data.frame("meta" = c("ace", "king", "queen", "jack", 10, 9, 8,7,6,5,4,3), "first" = c("jeff","jeff","fred","amy","tina","cat","dog","fred","amy","tina","cat","dog"), "job" = c("bank teller","short cook", "sky diver", "no job", "unknown job","bank clerk","short pet", "ocean diver", "hot job", "rad job","bank teller","short cook"))
The result would be
result <- data.frame("uuid" = 1:2, "combined" = c("ace,king,queen,jack","5,9,8"))
Thank you in advance!
I tried to beat my head against the wall and it didn't help
Edit- This is the first half of the puzzle BUT it does not search for and then concat the strings together in a cell, only returns the first match found rather than all matches.
Is there a way to exactly match a string in one column with couple of strings in another column in R?

How do I build a variable from a character repeated N number of times, in Lua?

I have an input value of N.
I want to turn that into a variable consisting of N characters.
for example, if N = 12, and I'm repeating the character "H", the value of the created variable should look like this: "HHHHHHHHHHHH"
I need it to be a variable, because I intend to use it in a few other places.
I am completely new to Lua, by the way. I just started a few days ago.
You are looking for string.rep.
For example:
local result = string.rep("H", 12)
print(result) -- prints "HHHHHHHHHHHH"
The datatype string has a metatable with all string functions attached as methods in __index.
Therefore you also can do...
local str, N = "H", 12
str = str:rep(N)
You can use the load() function to do it.
load(string.format("%s=4711", string.rep("H", 12)))()
print(HHHHHHHHHHHH)

How to write a function that evaluates two data frame columns of choice and returns the output?

Given the following data frame:
site <- c("site_1", "site_2", "site_3", "site_4", "site_5", "site_6")
protein1 <- c("M", "Q", "W", "F", "M", "M")
protein2 <- c("M", "W", "V", "M", "M", "M")
protein3 <- c("M", "D", "W", "F", "M", "M")
df <- data.frame(site, protein1, protein2, protein3)
I would like to extract the first column of the data frame, and two additional columns (proteins) that are being compared. However, the latter two columns will vary, depending on the comparison, and only the rows (site_number) where the two proteins differ should be returned. I have achieved this using subset(), but I was hoping to avoid copying and pasting the same line many times and replacing the name of the columns in each line. Here's what I've been able to do, but what I feel is more script than necessary:
comparison1 <- subset(df, protein1 != protein2, select = c(site, protein1, protein2))
comparison2 <- subset(df, protein1 != protein3, select = c(site, protein1, protein3))
#in each case, this produces the desired result of showing the "site" and "protein" values, in rows where the "protein" values differ.
In a large dataset, one would have many columns (18) with different names. Additionally, two different pairwise comparisons would be performed for each column. So I thought it would be wise to write a function that takes the column names of interest as input. Not having so much experience, I tried the following function before learning that you should not use subset() inside functions:
#to establish the function:
compare <- function(first, second){
result <- subset(df, df$first != df$second, select = c(site, first, second))
return(result)
}
#then to do my comparisons:
compare(protein1, protein2)
compare(protein1, protein3)
This returned the following error:
Error in `[.data.frame`(x, r, vars, drop = drop) :
undefined columns selected
Downstream, I would like to put the results into a list of data frames.
I'm quite sure I'm overlooking something simple. Perhaps the answer lies in using square brackets ([[). At least it seems that R is not converting the "first" and "second" variables to character strings that can match names of columns, as the error shows columns are undefined. If anyone knows whether writing a function for this is the right thing to do, or if I should do something else, then I would be very grateful for the feedback!
Thanks, and take care,
A

How to sum specific elements of a mixed Kotlin collection

If I had a mixed type Kotlin collection how would I be able to access and sum specific elements within that list. for example:
var mixedList = listOf("dog", "cat", 7, "Apple", "Orange", 10)
elements[2] and [5] are Ints. So how would I access and add them together for 17?
As already suggested this problem can be easily solved using filterIsInstance and sum functions defined for Iterable as well as Sequence in kotlin statndard library. but one small, yet very important detail is performance implication of Iterable vs Sequence
For small data set
val sum = yourList.filterIsInstance<Int>.sum()
// In first pass list is filtered, in second pass sum is calculated
For large data sets
val sum = yourList.asSequence().filterIsInstance<Int>().sum()
// Item is filtered and added to sum in single pass
fun main() {
val mixedList = listOf("dog", "cat", 7, "Apple", "Orange", 10)
val sum = mixedList.filterIsInstance<Int>().sum()
println(sum)
}
Output:
17

Get source rows of LINQ query result?

In the following LINQ example, how can you get a list of index numbers from the original rows that the group is made up of? I would like to show the user where the data comes from.
Dim inputDt As New DataTable
inputDt.Columns.Add("Contractor")
inputDt.Columns.Add("Job_Type")
inputDt.Columns.Add("Cost")
inputDt.Rows.Add({"John Smith", "Roofing", "2408.68"})
inputDt.Rows.Add({"John Smith", "Electrical", "1123.08"})
inputDt.Rows.Add({"John Smith", "Framing", "900.99"})
inputDt.Rows.Add({"John Smith", "Electrical", "892.00"})
Dim results = From rows In inputDt Where rows!Contractor <> ""
Group rows By rows!Job_Type
Into cost_total = Sum(CDec(rows!Cost))
For Each r In results
' Show results.
'r.Job_Type
'r.cost_total
' Show line numbers of original rows... ?
Next
For the result (Job_Type="Electrical", cost_total=2015.08), the original index numbers are 1 and 3.
Thanks
First and perhaps foremost, set Option Strict On. This will not allow the old VB6 style rows!Cost type notation. But this is for the better because that way always returns Object and the data rarely is. This is no loss at all as NET has better ways to type and convert variables.
Second, and somewhat related, is that all your DataTable columns are text even though one is clearly decimal. Next, your query relates to working with the data in the table but you want to also include a DataRow property which is a bit odd. Better would be to add an Id or Number to the data to act as the identifier. This will also help the results make sense if the View (order of rows) changes.
You did not clarify whether you wanted a CSV of indices (now IDs) or a collection of them. A CSV of them seems simpler, so thats what this does.
The code also uses more idiomatic names and demonstrates casting data to the needed type using other extension methods. It also uses the extension method approach. First the DataTable with non-string Data Types specified:
Dim inputDt As New DataTable
inputDt.Columns.Add("ID", GetType(Int32))
inputDt.Columns.Add("Contractor")
inputDt.Columns.Add("JobType")
inputDt.Columns.Add("Cost", GetType(Decimal))
inputDt.Rows.Add({1, "John Smith", "Roofing", "2408.68"})
inputDt.Rows.Add({5, "John Smith", "Electrical", "1123.08"})
inputDt.Rows.Add({9, "John Smith", "Framing", "900.99"})
inputDt.Rows.Add({17, "John Smith", "Electrical", "892.00"})
then the query:
Dim summary = inputDt.AsEnumerable().GroupBy(Function(g) g.Field(Of String)("JobType"),
Function(k, v) New With {.Job = k,
.Cost = v.Sum(Function(q) q.Field(Of Decimal)("Cost")),
.Indices = String.Join(",", inputDt.AsEnumerable().
Where(Function(q) q.Field(Of String)("JobType") = k).
Select(Function(j) j.Field(Of Int32)("Id")))
}).
OrderBy(Function(j) j.Cost).
ToArray()
' Debug, test:
For Each item In summary
Console.WriteLine("Job: {0}, Cost: {1}, Ids: {2}", item.Job, item.Cost, item.Indices)
Next
The excessive scroll is unfortunate but I left it to allow the clauses to align with what "level" they are acting at. As you can see, a separate query is run on the DataTable to get the matching Indicies.
It is a little more typical to write such a thing as
Dim foo = Something.GroupBy(...).Select(...)
But you can skip the SELECT by using this overload of GroupBy as the above does:
Dim foo = Something.GroupBy(Function (g) ..., Function (k, v) ... )
Results:
Job: Framing, Cost: 900.99, Ids: 9
Job: Electrical, Cost: 2015.08, Ids: 5,17
Job: Roofing, Cost: 2408.68, Ids: 1