How do we do math to each item in a list (add numbers from variable and multiply final value) and return another List?
Something like this:
var list = [1, 2, 3, 4, 5];
print(list); // Prints [1, 2, 3, 4, 5]
var list2 = list.map((i) => i * 2 + 5 /* math here*/).toList();
print(list2); // Prints [7, 9, 11, 13, 15]
Related
I want to check if items in array existed and add new values from another array without overwriting element after reloading. I created such code:
//take from that array
List<int> list = [2, 3, 5];
// add to this array and check if this array already has the same element or not
List<int> newValueInt = [2, 6, 7];
list.forEach((item) {
if(!list.contains(item)){
newValueInt.add(item);
print(newValueInt);
}
});
and it shows me that print:
[2, 6, 7, 3]
[2, 6, 7, 3, 5]
List<int> list = [2, 3, 5];
// add to this array and check if this array already has the same element or not
List<int> newValueInt = [2, 6, 7];
List<int> temp = [];
for (var item in list) {
if(!newValueInt.contains(item)){
temp.add(item);
}
}
List<int> result = newValueInt + temp;
print(result); //---> [2, 6, 7, 3, 5]
if(!list.contains(item)){
to
if(! newValueInt.contains(item)){
I want to write a python 3.7 function that has a sorted list of numbers as an input, and a number n which is the max number each one of the integers can be repeated and modifies the list in place, so that any numbers that are repeated more than n times, would be cut to n repeats, and it should be done in O(1) space, no additional data structures allowed (e.g. set()). Special case - remove duplicates where n = 1. Example:
dup_list = [1, 1, 1, 2, 3, 7, 7, 7, 7, 12]
dedup(dup_list, n = 1)
print(dup_list)
[1, 2, 3, 7, 12]
dup_list = [1, 1, 1, 2, 3, 7, 7, 7, 7, 12]
dedup(dup_list, n = 2)
print(dup_list)
[1, 1, 2, 3, 7, 7, 12]
dup_list = [1, 1, 1, 2, 3, 7, 7, 7, 7, 12]
dedup(dup_list, n = 3)
print(dup_list)
[1, 1, 1, 2, 3, 7, 7, 7, 12]
Case n = 1 is easy, the code is below (code is taken from Elements of Prograqmming Interviews, 2008, page 49 except the last line return dup_list[:write_index]):
def dedup(dup_list):
if not dup_list:
return 0
write_index = 1
for i in range(1, len(dup_list)):
if dup_list[write_index-1] != dup_list[i]:
dup_list[write_index] = dup_list[i]
write_index += 1
return dup_list[:write_index]
This should work:
def dedup2(dup_list, n):
count = 1
list_len = len(dup_list)
i = 1
while i < list_len:
if dup_list[i - 1] != dup_list[i]:
count = 1
else:
count += 1
if count > n:
del(dup_list[i])
i -= 1
list_len -= 1
i += 1
return dup_list
print(dedup2([1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 8, 9], 1))
So I'm working on an app in Flutter, and long story short: I have 2 'filter options' which create 2 seperate lists. Now what I want is to use those 2 Lists and find the items which are present in BOTH lists and add that item to a third List.
Example:
List<int> first_list = [1, 2, 3, 4, 5];
List<int> second_list = [1, 2, 8, 9];
Result: [1, 2]
I know I could potentially loop through 1 list and then check with 'contains()' if the item is present in the other list. But it could be that 1 (or both) lists are empty and then my third list will be empty as I simply will never loop to add a duplicate item to the third list
Something like this?
void main() {
List<int> first_list = [1, 2, 3, 4, 5];
List<int> second_list = [1, 2, 8, 9];
final shared = [...first_list.where(second_list.contains)];
print(shared); // [1, 2]
}
What you need is intersection between two collections. Most likely it makes sense not to use lists, but rather sets of items.
I would solve this like that:
void main() {
List<int> first_list = [1, 2, 3, 4, 5];
List<int> second_list = [1, 2, 8, 9];
final shared = first_list.toSet().intersection(second_list.toSet());
print(shared);
}
If you can work with sets, not with lists it would be even simpler:
void main() {
Set<int> first_list = {1, 2, 3, 4, 5};
Set<int> second_list = {1, 2, 8, 9};
final shared = first_list.intersection(second_list);
print(shared);
}
You can use intersection
List<int> firstList = [1, 2, 3, 4, 5];
List<int> secondList = [1, 2, 8, 9];
final firsToSet = firstList.toSet();
final secondToSet = secondList.toSet();
final res = firsToSet.intersection(secondToSet);
print(res);
In Swift there is a Form... equivalent for the Sets methods intersection(), symmetricDifference() and union(), i.e. formIntersection(), formSymmetricDifference() and formUnion().
But for the method subtracting() there is no method called formSubtracting. Does anyone know why this is so, because it seams I now have to use something like mySet = mySet.subtracting(anotherSet)
subtract(_:) is what you are looking for:
Removes the elements of the given set from this set.
Example:
var mySet: Set = [1, 2, 3, 4, 5]
let anotherSet : Set = [2, 4, 6, 8]
mySet.subtract(anotherSet)
print(mySet) // [3, 1, 5]
There is also a variant which takes another sequence (of the same element type) as the argument, e.g. an array:
var mySet: Set = [1, 2, 3, 4, 5]
let anotherSequence = [2, 4, 6, 8]
mySet.subtract(anotherSequence)
print(mySet) // [3, 1, 5]
With an unordered array of Ints as such:
let numbers = [4, 3, 1, 5, 2]
Is it possible in Swift, using .sorted { }, to order the array with one item prioritised and placed in the first index of the array. So instead of returning [1, 2, 3, 4, 5] we could get [3, 1, 2, 4, 5]?
You can declare a function like this :
func sort(_ array: [Int], prioritizing n: Int) -> [Int] {
var copy = array
let pivot = copy.partition { $0 != n }
copy[pivot...].sort()
return copy
}
Which uses the partition(by:) function.
You could use it like so:
let numbers = [4, 3, 1, 5, 2]
let specialNumber = 3
sort(numbers, prioritizing: specialNumber) //[3, 1, 2, 4, 5]
Here are some test cases :
sort([3, 3, 3], prioritizing: 3) //[3, 3, 3]
sort([9, 4, 1, 5, 2], prioritizing: 3) //[1, 2, 4, 5, 9]
Here an alternative solution that uses sorted(by:) only :
let numbers = [4, 3, 1, 5, 2]
let vipNumber = 3
let result = numbers.sorted {
($0 == vipNumber ? Int.min : $0) < ($1 == vipNumber ? Int.min : $1)
}
print(result) //[3, 1, 2, 4, 5]