Removing Trailing Spaces in a List - dart

I have a list that has many space and would like to remove the trailing spaces.
[ , , A, B, C, D, , , , , E, C, D, E, , , , ]
I would like to have it like this
[ , , A, B, C, D, , , , , E, C, D, E]
I tried using trim by doing arr.join().trim().split('') but that also removed the starting spaces and
[A, B, C, D, , , , , E, C, D, E]
How do I achieve this
Thanks

var l = [ '', '', 'A', 'B', 'C', 'D', '', '', '', '', 'E', 'C', 'D', 'E', '', '', '', ''];
var result = l.reversed.skipWhile((s) => s.isEmpty).toList().reversed.toList();

use trimRight() instead of trim() https://api.dartlang.org/stable/2.2.0/dart-core/String/trimRight.html

Related

Dart How to distinct (unique) `List<List<Object>>`

I have a List<List<Object>>>. What is the best readable way to unique my list?
For instance:
[[A, B], [B, A], [B, C, B]]
to:
[[A, B], [B, C, B]]
If you are alright with modelling the result as a Set<Set<Object>>, I would suggest this approach.
I am using the collection package because it provides an easy way to check if two collections are equal.
import 'dart:collection';
import 'package:collection/collection.dart';
void main() {
List<List<String>> items = [
['A', 'B'],
['B', 'A'],
['B', 'C'],
];
Set<Set<String>> unique = HashSet<Set<String>>(
equals: SetEquality().equals,
hashCode: SetEquality().hash,
);
unique.addAll(items.map((v) => v.toSet()));
print(items);
print(unique);
}
output
[[A, B], [B, A], [B, C]]
{{A, B}, {B, C}}
Since the inner lists
are arbitrary length
can contain duplicates
are not necessarily in the same order (but we want to treat different orderings as the same list)
It does make things more complicated, but you can use the same general approach. Here we will keep the inner elements as lists, but we will provide definitions for equals and hashCode that take the above constraints into account.
If the elements implement Comparable then you can use .sorted() to account for constraint #3, and ListEquality().equals and ListEquality().hash to account for constraints #1 and #2.
import 'dart:collection';
import 'package:collection/collection.dart';
void main() {
List<List<String>> items = [
['A', 'B'],
['B', 'A'],
['B', 'C', 'B'],
];
Set<List<String>> unique = HashSet<List<String>>(
equals: (a, b) => ListEquality().equals(a.sorted(), b.sorted()),
hashCode: (a) => ListEquality().hash(a.sorted()),
);
unique.addAll(items);
print(items);
print(unique);
}
output
[[A, B], [B, A], [B, C, B]]
{[A, B], [B, C, B]}
However, what if the elements don't implement Comparable?
You have a few options in this case.
First, the .sorted() method optionally accepts a function that you can use to provide custom sorting logic.
The other approach would be to get a count of occurrences of each element in the list and compare the counts. I have implemented this approach below.
import 'dart:collection';
import 'package:collection/collection.dart';
void main() {
List<List<String>> items = [
['A', 'B'],
['B', 'A'],
['B', 'C', 'B'],
];
Set<List<String>> unique = HashSet<List<String>>(
equals: (a, b) => MapEquality().equals(counts(a), counts(b)),
hashCode: (a) => MapEquality().hash(counts(a)),
);
unique.addAll(items);
print(items);
print(unique);
}
Map<T, int> counts<T>(List<T> items) {
Map<T, int> result = {};
for (final item in items) {
result.update(item, (v) => v + 1, ifAbsent: () => 1);
}
return result;
}
output
[[A, B], [B, A], [B, C, B]]
{[B, C, B], [A, B]}
Note that the elements are in a different order than the previous solution, this is because HashSet does not preserve the insertion order, if you do want to preserve the order you can use a LinkedHashSet instead.

What method should i use to find the type of pattern from 2 input arrays?

I want to ask about the method of solving a case I ran into. I have data with various variations with each index interrelated.
Data Example:
1 :
x1 = [ 'S' , 'M' , 'L', 'XL', 'XXL']
x2 = [ 10 , 30 , 20, 30, 20]
pattern = A
2 :
x1 = [ 'H' , 'X' , 'A', 'DL', 'AXL']
x2 = [ 10 , 40 , 10, 30, 10]
pattern = G
3 :
x1 = [ 'SS' , 'AM' , 'L', 'XL', 'SXL']
x2 = [ 90 , 30 , 30, 50, 10]
pattern = B
.
.
.
.
.
(n data)
x1 -> type of clothes
x2 -> qty of clothes
pattern = the result ( A, B, C, D, E, F, G)
Altogether I have about 10,000 data with a pattern like that and I want to find the formula (r) using x1 array and x2 array. What method or algorithm can I use or what kind of solution exists for this case?

Query Filter Results

I am trying to query and then filter my results based on the drop down in N4
=
IF(and(N4="ALL"), query(Vendors!1:1000,"select A, B, C, D, E, F, G, H, I, J, K, L", 2),
IF(and(N4="Trained"), query(Vendors!1:1000,"select A, B, C, D, E, F, G, H, I, J, K, L", 2),
IF(and(N4="Requested"), query(Vendors!1:1000,"select A, B, C, D, E, F, G, H, I, J, K, L", 2),
IF(and(N4="Invited"), query(Vendors!1:1000,"select A, B, C, D, E, F, G, H, I, J, K, L", 2)))))
Those all return results. However, now I need help omitting the results that dont match the dropdown.
Sorry I didn't read it correctly. Try this.
=IF (N4 <>"ALL",IF(OR(OR( N4="Trained",N4="Requested",N4="Invited")),query(Vendors!1:1000,"select A, B, C, D, E, F, G, H, I, J, K, L where A = '"& N4 &"'")),query(Vendors!2:1000,"select A, B, C, D, E, F, G, H, I, J, K, L where A<>''"))
Key in the Header row. Change Column A to whatever column "Trained", "Invited", etc are in.

How to do complex querying with logical operations by using searchkick

Iam using searchkick library as an elasticsearch client for Product searching.
https://github.com/ankane/searchkick
It is possible to create 'OR' condition and 'AND' condition;
AND operation
Product.search where: {price: {lte: 200}, in_stock: true}
OR operation
Product.search where: {or: [[{in_stock: true}, {backordered: true}]]}
But Iam stuck with creating multiple 'AND' 'OR' conditions with searchkick.
I need something like
A OR B OR ( C AND D )
or I need like this,
A AND B AND ( C OR D )
Please guide me, how to achieve this
Thanks
A OR B OR ( C AND D )
Product.search where: {or: [[{brand: 'nike'}, {in-stock: true}, {price: {lte: 12}, color: 'red'}]]}
A AND B AND ( C OR D )
Product.search where: {brand: 'nike', in-stock: true, or: [ [{price: {lte: 12}}, {color: 'red'}] ]}
Update
(A OR B) AND (C OR D)
Product.search where: {or: [[ {or: [[{brand: "nike"}, {in-stock: "true"}]]}], [{or: [[{price: 100}, {color: "red"}]]}]]}
For (A OR B) AND (C OR D) it should be (no double brackets needed):
where: { _and: [ { _or: [ {A}, {B} ] }, { _or: [{C}, {D}] } ] }

Rails method for concatenate

Is it possible to concatenate Array ['a', 'b', 'c'] to String "a, b and c" ?
But ['a', 'b'] should transform to "a and b".
Rails provides a to_sentence helper:
> ['a', 'b'].to_sentence
=> "a and b"
> ['a', 'b', 'c'].to_sentence
=> "a, b, and c"
If you want a, b and c rather than a, b, and c you can change the last_word_connector:
> ['a', 'b', 'c'].to_sentence(last_word_connector: " and ")
=> "a, b and c"
a = %w{ a b }
str = a[0..-3].each_with_object("") do |item, res|
res << "#{item}, "
res
end
str << "#{a[-2]} and #{a[-1]}"
p str
a = ['a', 'b', 'c']
result = a[0...-1].join ', '
result += " and #{a[-1]}" if a.length > 1
result # => a, b and C
a = ['a', 'b']
result = a[0...-1].join ', '
result += " and #{a[-1]}" if a.length > 1
result # => a and b

Resources