How to fine duplicate characters in a string occurence for at least 2 characters and return the highest count in the Dart language - dart

EXAMPLE :
highfrequencyoccurence(“bowwow”) => will return 2 (as “ow” is twice)
highfrequencyoccurence(“possessionlessness”) => will return 4 (as “ss” is 4 times)
EXAMPLE :
highfrequencyoccurence(“bowwow”) => will return 2 (as “ow” is twice)
highfrequencyoccurence(“possessionlessness”) => will return 4 (as “ss” is 4 times)

Related

Why isn't my LUA interpreter able to handle string key values?

When testing code with both a predefined script and the LUA runtime environment, LUA will not take any form of string key values. However, if a numerical value key is used LUA will work with it as intended. The exception to this rule when I am using Tshark with a LUA file to parse packet captures. This allows the string key value syntax to work normally. Is there something I may be performing wrong?
I have tried creating several .lua script files with different variations including:
testArray.NewItem = "value1"
testArray["NewItem"] = "value1"
NewItemValue = "NewItem"
testArray[NewItemValue] = "value1"
These all result in an nil value or an error due to trying to call a nil value depending on the return style used to check.
> tcpstream = {}
> stream1 = tostring(14356)
> tcpstream[stream1] = "nothing"
> print(#tcpstream)
0
> print(tcpstream[1])
nil
> tcpstream[1] = "nothing"
> print(#tcpstream)
1
> print(tcpstream[1])
nothing
the output of the print(#tcpstream) after the tcpstream[stream1] = "nothing" should show 1 not zero. The subsequent print(tcpstream[1]) should also show "nothing".
From http://lua-users.org/wiki/TablesTutorial
The # operator doesn't count all the items in the table (!). Instead it finds the last integer (non-fractional number) key. Because of how it's implemented its results are undefined if all the integer keys in the table aren't consecutive. Which is why it shouldn't be used for tables used as sparse arrays[2]).
The '#' is not a good(sometimes not correct) way to count the number of elements in Lua table.
As for
> stream1 = tostring(14356)
> tcpstream[stream1] = "nothing"
> print(#tcpstream)
0
> print(tcpstream[1])
nil
Lua uses key,value pairs, not explicitly index. If you do 'arr[1] = 22', it means the value for the key '1' is 22, not the value for the first element is 22.
The length operator(#) does not work as you believe, this is a common mistake for beginners in Lua.
The default behavior for #sometable is to return the number of consecutive key starting at the number 1(or after any nil value for 5.3). String keys are never evaluated with the default # operator for a table.
In 5.3 if your sequence contains multiple nil values the behavior of # is non-deterministic.
Lua 5.3 Reference Manual: 3.4.7 – The Length Operator
Lua 5.1 Reference Manual: 2.5.5 – The Length Operator
I will include the lines from 5.1 as i feel it covers the information regarding the operator and tables well. While note identical to how 5.3 work it maybe easier to understand why you see the behavior you do.
2.5.5 – The Length Operator
The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte).
The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t1 is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).
Here are some examples of different table and their # results:
table1 = { --number keys in array
true,
true,
true,
}
table2 = { -- number keys in hash
[1] = true,
[2] = true,
[3] = true,
}
table3 = { -- only strings as key
['1'] = true,
['2'] = true,
['3'] = true,
}
table4 = { -- No key 2 defined
[1] = true,
-- [2] = true,
[3] = true,
}
table5 = { -- table with both string and number keys
[1] = true,
['2'] = true,
}
print(#table1) -- 3
print(#table2) -- 3
print(#table3) -- 0
print(#table4) -- v5.3(1 or 3) v5.1(1)
print(#table5) -- 1

Counting items by substrings in a Multiset in Java

I have the following data in a guava Multiset. Each item is the combined string of 3 items separated by a ':'. I know all the values for each of the slots. I'm using the values to generate a data file for an interactive graph (by stuffing the split values into an object and then using Gson to print the object).
What's the best way to grab the cumulative count for all items that match just one, one:two, or one:two:three of the substrings? I keep going round and round with streams, forEach, maps and filters, but can't seem to write an elegant set of loops. Any suggestions or examples would be helpful.
Executive:Healthcare:United States x 5
Executive:Healthcare:Malaysia x 2
Executive:Financials:United States x 1
FinancialHealth:Technology:Malaysia x 3
FinancialHealth:Technology:United States x 2
FinancialHealth:Energy:United States x 1
Executive = 8
FinancialHealth = 6
Executive:Heathcare = 7
Executive:Financials = 1
FinancialHealth:Technology = 5
FinancialHealth:Energy = 1
Executive:Healthcare:United States = 5
etc.
Streams can help a great deal here, and it is not even difficult.
We need to take three steps in a stream:
allTheStrings.stream()
// First, we will multiply each string "A:B:C" using `flatMap`
// so that the stream contains "A", "A:B", and "A:B:C":
.flatMap(s -> Stream.of(s.substring(0, s.indexOf(":")),
s.substring(0, s.lastIndexOf(":")),
s))
// next, we are going to summarize multiple occurrences
// of the strings using a groupingBy collector:
.collect(Collectors.groupingBy(Function.identity(),
// This would return a Map<String, List<String>> containing each unique
// string mapped to its occurrences. But because you don't need the
// single occurrences, but instead just their number, we add a step
// to the collect which will make it return a Map<String, Long>
Collectors.counting()))
So, as a full example:
Stream.of("Executive:Healthcare:United States", "Executive:Healthcare:United States",
"Executive:Healthcare:United States", "Executive:Healthcare:United States",
"Executive:Healthcare:United States", "Executive:Healthcare:Malaysia",
"Executive:Healthcare:Malaysia", "Executive:Financials:United States",
"FinancialHealth:Technology:Malaysia", "FinancialHealth:Technology:Malaysia",
"FinancialHealth:Technology:Malaysia", "FinancialHealth:Technology:United States",
"FinancialHealth:Technology:United States", "FinancialHealth:Energy:United States")
.flatMap(s -> Stream.of(s.substring(0, s.indexOf(":")), s.substring(0, s.lastIndexOf(":")), s))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.forEach(System.out::println);
will output
Executive=8
Executive:Healthcare=7
FinancialHealth:Technology=5
FinancialHealth=6
FinancialHealth:Energy=1
FinancialHealth:Technology:Malaysia=3
FinancialHealth:Energy:United States=1
Executive:Healthcare:United States=5
Executive:Financials:United States=1
FinancialHealth:Technology:United States=2
Executive:Healthcare:Malaysia=2
Executive:Financials=1

Lambda : GroupBy multiple columns where one of them is DbFunctions.TruncateTime()

NOTE : This is NOT a duplicate of this useful SO question, my problem is all about the TruncateTime inside the GroupBy clause. See explanations below :
I'd like to use DbFunctions.TruncateTime in a multiple GroupBy clause, but it doesn't seem to work in my ASP.NET MVC5 project.
Here is a first lambda I wrote, it gives me the total number of views per day for a set of data.
It gives me the expected result :
var qViews = dbContext.TABLE_C.Where(c => c.IdUser == 1234)
.Join(dbContext.TABLE_V.Where(v => v.Date > DbFunctions.AddMonths(DateTime.Now, -1)), c => c.Id, v => v.Id, (c, v) => new { c, v })
.GroupBy(x => DbFunctions.TruncateTime(x.v.MyDateTimeColumn))
.Select(g => new
{
Date = (DateTime)g.Key,
NbViews = g.Count(),
}).ToDictionary(p => p.Date, p => p.NbViews);
Result is something like that :
...
Date | Views
03/07/2018 | 15
03/08/2018 | 8
03/09/2018 | 23
Now, I'd like a more detailled result, with the number of views per day AND PER ITEM on the same set of data.
Here is what I'd like to write :
var qViews = dbContext.TABLE_C.Where(c => c.IdUser == 1234)
.Join(dbContext.TABLE_V.Where(v => v.Date > DbFunctions.AddMonths(DateTime.Now, -1)), c => c.Id, v => v.Id, (c, v) => new { c, v })
.GroupBy(x => new { DbFunctions.TruncateTime(x.v.MyDateTimeColumn), x.c.Id}) // Issue #1
.Select(g => new
{
Date = g.Key.Date, //Issue #2
NbViews = g.Count(),
}).ToDictionary(p => p.Date, p => p.NbViews);
And I expected something like that :
...
Date | Views | ID Item
03/07/2018 | 4 | 456789
03/07/2018 | 11 | 845674
03/08/2018 | 6 | 325987
03/08/2018 | 1 | 548965
03/08/2018 | 1 | 222695
03/09/2018 | 23 | 157896
So, this request have two issues (see comments above)
Issue #1 : It seems I can't GroupBy multiple columns, which one of them use DbFunctions. If I use .GroupBy(x => new { x.v.MyDateTimeColumn, x.c.Id }), code compiles, but doesn't give me the expected result, as I want to group by date, not date + time
Issue #2 : Date = g.Key.Date, seems wrong for the compiler. When I wrote g.Key, autocompletion only suggests me the Id column, but it doesn't see the truncated date.
Why can't I GroupBy multiple columns, with one of them is a truncated Date ?
Is there any workaround ?
You need to give your anonymous type's properties names if you want to use them later on:
.GroupBy(x => new
{ Date = DbFunctions.TruncateTime(x.v.MyDateTimeColumn),
Id = x.c.Id
})
Then you can project on that:
.Select(g => new
{
Date = g.Date,
NbViews = g.Count(),
})
And finally you cannot do this:
.ToDictionary(p => p.Date, p => p.NbViews);
because you will get this error:
An item with the same key has already been added.
Why? Because the Date is not unique since you just grouped by Date and Id so Date(s) will be duplicated. It is the same as this but this is a list of string:
var nums = new List<string> { "1", "1", "1", "2" };
nums.ToDictionary(x => x, x => x);
But, perhaps, you may want to do this:
var lu = nums.ToLookup(x => x, x => x);
And now you can look them up:
// Returns 3 items since there are 3 "1"s
IEnumerable<string> ones = lu["1"];

Filtering Rows in F#

I have a code where I have the following frame and filter rows as follows:
let dfff=
[ "year" => series [ 1 => 1990.0; 2 => 1991.00; 3 => 1992.0; 4 => 1993.0]
"gold" => series [ 1 => 10.0; 2 => 10.00; 3 => 15.0; 4 => 20.0]
"silver" => series [ 1 => 20.0; 2 => 30.00; 3 => 45.0; 4 => 55.0] ]
|> frame
let dfff2 = dfff |> Frame.filterRows (fun key row -> row?year <= 1992.0 )
Why do I have to write key in
Frame.filterRows (fun key row -> row?year <= 1992.0)
if my function only depends on row? What role does key play here? I will appreciate if anybody could explain me the logic. Thanks!
In Deedle, frames have row keys and column keys. In your case, you have Frame<int, string> meaning that the row keys are integers (just numbers) and column keys are strings (column names) - but you might also have dates or other more interesting things as row keys.
The filterRows function gives you the row key together with row data. The key parameter is just the row key - in your case, this is (uninteresting) int index, but it might be e.g. useful date in other scenarios.
F# lets you write _ to explicitly ignore the value:
let dfff2 = dfff |> Frame.filterRows (fun _ row -> row?year <= 1992.0 )
In the Series module, we have Series.filter and Series.filterValues where the first one gives you key & value and the second one gives you just the value. So, we could follow the same pattern and add Frame.filterRowValues.
This would actually be quite easy, so if you want to contribute, please send a pull request with a change somewhere around here :-).

Number of returned values

I've got a function which is returning a bunch of values and I can't know how many arguments are returned.
function ascii( value )
[...]
if type( value ) == "string" then
if #value > 1 then
local temp = {}
for i = 1, #value do
table.insert( temp , (value:byte(i)) )
end
return unpack( temp ) --<-- unknown number of return values
else
return value:byte(1)
end
end
[...]
end
How can I know how many values are returned?
My first idea was this:
return numberOfValues, unpack( temp )
But in most cases, the number of values is irrelevant.
Is there a way to get around that extra effort which is unnecessary in 95% of all cases?
Keep your function definition as it is and call it like this:
local values = {ascii(myString)}
local n = #values
Number of returned values is not the same as the number of elements in the table built from returned values. It works in the example you have, but only because the example doesn't include any nil values.
In the general case when the returned values may include nil, it's almost always better to use select('#'...) instead:
print(#{(function() return 1, nil, 2 end)()})
print(select('#', (function() return 1, nil, 2 end)()))
This code prints 1 3 in Lua 5.1, but 3 3 in Lua 5.2 and Lua 5.3. Adding one more nil to the return values changes this to 1 4 under all these versions of Lua.
These functions can be used as wrappers that returns the number of return values and also return a list or a table with the values:
function wrap(...) return select('#', ...), ... end
function wrapt(...) return select('#', ...), {...} end
Using one of these functions, print(wrap((function() return 1, nil, 2, nil end)())) prints 4 1 nil 2 nil as expected.

Resources