What is the equivalent of this python dictionary in Dart?
edges = {(1, 'a') : 2,
(2, 'a') : 2,
(2, '1') : 3,
(3, '1') : 3}
You could use package:collection's EqualityMap to define a custom hash algorithim that uses ListEquality. For example, you could do this:
var map = new EqualityMap.from(const ListEquality(), {
[1, 'a']: 2,
[2, 'a']: 2,
});
assert(map[[1, 'a']] == map[[1, 'a']])
This will be a heavier weight implementation of Map, though.
You have differents way to do this
1. Using a List
var edges = <List, num>{
[1, 'a']: 2,
[2, 'a']: 2,
[2, '1']: 3,
[3, '1']: 3
};
Simple to write, but you won't be able to retrieve data with
edges[[2, 'a']]; // null
Except if you use const
var edges = const <List, num>{
const [1, 'a']: 2,
const [2, 'a']: 2,
const [2, '1']: 3,
const [3, '1']: 3
};
edges[const [2, 'a']]; // 2
2. Using Tuple package
https://pub.dartlang.org/packages/tuple
var edges = <Tuple2<num, String>, num>{
new Tuple2(1, 'a'): 2,
new Tuple2(2, 'a'): 2,
new Tuple2(2, '1'): 3,
new Tuple2(3, '1'): 3
}
edges[new Tuple2(2, 'a')]; // 2
var edges = {[1, 'a'] : 2,
[2, 'a'] : 2,
[2, '1'] : 3,
[3, '1'] : 3};
Except that you won't ever be able to look up those keys, because a new instance of [1, 'a'] will be a different object.
Related
When i have list
[1, 2, 3, 4, 5, 6, 7]
i want to get 3 random elements from list
e.g
[1, 3, 4] or [4, 5, 1] or [3, 2, 5]
how can i solve that simply
is there any dart library??
The previous solution is good but could have been written in a more generic way so we don't loose the type information of the List. Also, take does not return a List but instead Iterable.
I have rewritten the code to be more generic and shorter by using the cascade operator. I am not sure if you want a List or Iterable as output so I have made multiple solutions:
void main() {
final items = [1, 2, 3, 4, 5, 6, 7];
print(pickRandomItems(items, 3)); // (7, 4, 3)
print(pickRandomItemsAsList(items, 3)); // [2, 4, 5]
print(pickRandomItemsAsListWithSubList(items, 3)); // [1, 3, 6]
print(items); // [1, 2, 3, 4, 5, 6, 7] (just to show that the original List is untouched)
}
Iterable<T> pickRandomItems<T>(List<T> items, int count) =>
(items.toList()..shuffle()).take(count);
List<T> pickRandomItemsAsList<T>(List<T> items, int count) =>
(items.toList()..shuffle()).take(count).toList();
List<T> pickRandomItemsAsListWithSubList<T>(List<T> items, int count) =>
(items.toList()..shuffle()).sublist(0, count);
Instead of using take in pickRandomItemsAsList you can instead use sublist. But the catch with subList is that if the length are greater than the List it will give an error but with take you just get all the elements in the List shuffled.
Simply shuffle list and take sublist (slice):
List pickRandomItems(List items, int count) {
final list = List.from(items); // cloning original list
list.shuffle(); // shuffling items
return list.take(count); // taking N items
}
List items = [1, 2, 3, 4, 5, 6, 7];
print(pickRandomItems(items, 3));
I can map list in Dart:
[1,2,3].map((e) => e + 1)
but how can I flatMap this list?
Code presented below does not work.
[1,2,3].flatMap((e) => [e, e+1])
expand method is equivalent to flatMap in Dart.
[1,2,3].expand((e) => [e, e+1])
What is more interesting, the returned Iterable is lazy, and calls fuction for each element every time it's iterated.
Coming from Swift, flatMap seems to have a little different meaning than the OP needed. This is a supplemental answer.
Given the following two dimensional list:
final list = [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]];
You can convert it into a single dimensional iterable like so:
final flattened = list.expand((element) => element);
// (1, 2, 2, 3, 3, 3, 4, 4, 4, 4)
Or to a list by appending toList:
final flattened = list.expand((element) => element).toList();
// [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
Given an array A[] and a number x, check for pair in A[] with sum as x. can anyone help me out on this one in rails?
The ruby array #combination method can give you all combinations of array members of a given number of elements.
[1, 2, 3, 4, 5, 6].combination(2).to_a
=> [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 3], [2, 4], ... [5,6]]
Then you just want to select the elements where they add up to a given number.
[1, 2, 3, 4, 5, 6]combination(2).to_a.select{|comb| comb[0] + comb[1] == 7}
=> [[1, 6], [2, 5], [3, 4]]
To make it work for a different number of combined elements (e.g. 3 instead of 2) you can do...
[1, 2, 3, 4, 5, 6]combination(3).to_a.select{|c| (c.inject(0) {|sum,x| sum + x}) == 7}
This will work for 2, 3, 4, or any number up to the full array size.
It works by
finding combinations of 3
using `#inject' to sum all the elements of each combination
comparing that sum to the target number
You can easily achieve it by own function as:
def sum_as_x?(ary,x)
num=a.find{|e| ary.include?(x-e)}
unless num
puts "not exist"
else
p [x-num,num]
end
end
a = [1,2,3,4,5]
sum_to_x?(a,9)
>> [5, 4]
sum_to_x?(a,20)
>> not exist
If I have two arrays and I try to find their difference..
[1, 2, 3, 2, 6, 7] - [2, 1]
I get :
[3, 6, 7]
But if I flip those arrays around
[2, 1] - [1, 2, 3, 2, 6, 7]
I get :
[]
My question is, being that my two arrays are dynamic, I need to know if there is a difference in between both arrays regardless of their order. What's the simplest expression to find that?
You can define it:
class Array
def diff(o)
(o - self) + (self - o) # alternatively: (o + self) - (o & self)
end
end
[2, 1].diff [1, 2, 3, 2, 6, 7] # [3, 6, 7]
[1, 2, 3, 2, 6, 7].diff [2, 1] # [3, 6, 7]
[2, 3, 3, 1].diff [2, 4, 5] # [4, 5, 3, 3, 1]
[2, 4, 5].diff [2, 3, 3, 1] # [3, 3, 1, 4, 5]
The correct answer probably depends on what you want in the end, though, as the second two examples above show.
If you only want unique values, you'll want to convert the two inputs to sets first, and return the result as an array:
class Array
def diff(o)
(o.to_set ^ to_set).to_a # or simply (o.to_set ^ self).to_a
end
end
[2, 4, 5].diff [2, 3, 3, 1] # [4, 5, 3, 1]
(There might be a built-in Rails method, too.)
Using Set#^:
require 'set'
([2, 1].to_set ^ [1, 2, 3, 2, 6, 7]).to_a
# => [3, 6, 7]
([1, 2, 3, 2, 6, 7].to_set ^ [2, 1]).to_a
# => [3, 6, 7]
According to the documentation:
Set#^ returns a new set containing elements exclusive between the set and
the given enumerable object.
I've been trying for a couple weeks to figure this out, but I'm totally stumped.
I have an array that represents item_id's: [2, 4, 5, 6, 2, 3].
I have another array that represents how many times each item shows up: [1, 1, 3, 3, 2, 5] .
I want to check that all items have been completed so I want to create an array that has the total number of item_id's in it. I will compare that array against a completed items array that will be created as the user completes each item, so, from the example above, the array I'm trying to create is:
[2, 4, 5, 5, 5, 6, 6, 6, 2, 2, 3, 3, 3, 3, 3]
EDIT:
I'm building a workout app, so a user has a workout which has many exercises. Each exercise has one or more sets associated with it. The user completes an exercise when he has completed every set for that exercise, and completes a workout when he completes all exercises for that workout. In this question I'm trying to determine when a user has finished a workout.
EDIT 2:
I wish I could award multiple right answers! Thanks everyone!
Ok, #sameera207 suggested one way, then I will suggest another way (functional style):
arr1 = [2, 4, 5, 6, 2, 3]
arr2 = [1, 1, 3, 3, 2, 5]
arr1.zip(arr2).flat_map { |n1, n2| [n1] * n2 }
item_ids = [2, 4, 5, 6, 2, 3]
counts = [1, 1, 3, 3, 2, 5]
item_ids.zip(counts).map{|item_id,count| [item_id]*count}.flatten
=> [2, 4, 5, 5, 5, 6, 6, 6, 2, 2, 3, 3, 3, 3, 3]
What's going on here? Let's look at it step by step.
zip takes two arrays and "zips" them together element-by-element. I did this to create an array of item_id, count pairs.
item_ids.zip(counts)
=> [[2, 1], [4, 1], [5, 3], [6, 3], [2, 2], [3, 5]]
map takes each element of an array and executes a block. In this case, I'm using the * operator to expand each item_id into an array of count elements.
[1]*3 => [1, 1, 1]
[[2, 1], [4, 1], [5, 3], [6, 3], [2, 2], [3, 5]].map{|item_id,count| [item_id]*count}
=> [[2], [4], [5, 5, 5], [6, 6, 6], [2, 2], [3, 3, 3, 3, 3]]
Finally, flatten takes an array of arrays and "flattens" it down into a 1-dimensional array.
[[2], [4], [5, 5, 5], [6, 6, 6], [2, 2], [3, 3, 3, 3, 3]].flatten
=> [2, 4, 5, 5, 5, 6, 6, 6, 2, 2, 3, 3, 3, 3, 3]
ids = [2, 4, 5, 6, 2, 3]
repeats = [1, 1, 3, 3, 2, 5]
result = []
ids.count.times do |j|
repeats[j].times { result << ids[j] }
end
This is a one way of doing it:
a = [2,4,5,6,2,3]
b = [1,1,3,3,2,5]
c = []
a.each.with_index do |index, i|
b[index].to_i.times {c << i }
end
p c