The function to_poly_solve in maxima returns a %union but I want to work with a list.
How can %unions be turned into lists since listify doesn't work?
abc : %union([a = 2], [b = 3]);
args(abc);
This function worked for me:
LISTIFY(Union) := block(
[res : []],
(for si in Union do res : append(res, [si])),
return (res)
);
Usage:
abc : %union([a = 2], [b = 3]);
LISTIFY(abc);
Output:
%union([a=2],[b=3])
[[a=2],[b=3]]
Related
In javascript you can do the following
const obj1 = {a:1, b:'blue', c:true};
const obj2 = {...obj1, d:3, e:'something else'};
and you object 2 will have all of obj 1 in it.
Is there a way to do this in lua?
You'd have to write your own function for this purpose:
local function add_missing(dst, src)
for k, v in pairs(src) do
if dst[k] == nil then
dst[k] = v
end
end
return dst -- for convenience (chaining)
end
which you may then use as follows:
local obj1 <const> = {a = 1, b = 'blue', c = true}
local obj2 <const> = add_missing({d = 3, e = 'something else'}, {a = 1, b = 'blue', c = true})
Note: JavaScript's spread operator also guarantees a certain order of execution; that is, in your example fields from obj2 would override those from obj1, which is why I've included an explicit nil check to only include missing fields:
> {...{a: 2}, a: 1}
{ a: 1 }
> {a: 1, ...{a: 2}}
{ a: 2 }
I need to print the second largest number on the list, the output from the below code is all elements in the list except the first and the last one.
What is the mistake?
void main () {
List a = [9,6,4,10,13,2,3,5];
a.sort;
for(int x in a){
for (int max in a){
for (int second_last in a){
if (x > max) {
second_last = max;
max = x;
} else if (x > second_last && x != max) {
second_last = x;
print(second_last);
}
}
}
}
}
There are a few things wrong with your code:
You're not actually sorting the list. The sort method returns a new sorted list, it doesn't sort the existing list. So you need to do:
a = a.sort;
You're iterating over the list 3 times when you only need to iterate over it once.
You're not keeping track of the second largest number, you're just printing it out as you find it.
You're not checking for duplicates. If there are duplicate numbers in the list, your code will print them out multiple times.
Here's a corrected pseudo-code
void main() {
List a = [9, 6, 4, 10, 13, 2, 3, 5];
a.sort;
int max = a[0];
int second_last = a[0];
for (int x in a) {
if (x > max) {
second_last = max;
max = x;
} else if (x > second_last && x != max) {
second_last = x;
}
}
print(second_last);
}
I need to print the second largest number on the list
Sort the array (desc).
Access the second element.
List a = [9, 6, 4, 10, 13, 2, 3, 5];
a.sort((a, z) => z - a);
// final b = a.toList()..sort(...); if you do not want to modify the original array.
print(a.take(2)); // [13, 10]
print(a.take(2)[1]); // [10]
print(a.take(2).skip(1)); // [10]
print(a.skip(1).first); // [10]
You are missing () on sort. You can do
void main() {
List a = [9, 6, 4, 10, 13, 2, 3, 5];
a.sort();
print(a[a.length - 2]); //get second large item
List b = [9, 6, 4, 10, 13, 2, 3, 5];
//or like revese sort
b.sort((a, b) => b.compareTo(a));
print(b[1]);
}
If you find a solution that is without using the sort function, and also works with all possible conditions,
so you can use this solution as well,
void main(){
var list =[9,6,4,10,13,2,3,5,13];
var secondLargest = findSecLargest(list);
print(secondLargest);
}
findSecLargest(List<int> list) {
var secLargest =-1;
var largest=list[0];
for(int i = 1 ; i < list.length ; i ++){
if(largest<list[i]){
secLargest = largest;
largest = list[i];
}else if(secLargest<list[i] && list[i]!=largest){
secLargest = list[i];
}
}
return secLargest;
}
I am learning Dart, and I defined a sum function to sum list of numbers.
sum(numberList) => numberList.reduce((num a, num b) => a + b);
When I call it on list of numbers:
main() {
var nl = [4, 2, 4, 5, 9];
print(sum(nl));
}
I got error:
type '(num, num) => num' is not a subtype of type '(int, int) => int' of 'combine'
This confused me, why a function defined for type num cannot be called on list of int? How to fix this problem? If the list 'nl' is coming from outside of my code, how can I cast list of int to list of num? (It seems List of int is not a list of num? puzzled.)
The problem is that you are calling reduce on a List<int>.
The type of that is int reduce(int Function(int, int) combine).
That means that the combine function argument must be a function returning an int.
You try to pass a function which returns a num, and that is not allowed.
You didn't catch that statically because you haven't typed the argument to sum.
Try changing it to:
num sum(List<num> numberList) => numberList.reduce((num a, num b) => a + b);
What you can do is cast the list to List<num> before passing it to sum:
print(sum(nl.cast<num>()));
I'm sorry I cannot give a real answer to your but here are some methods so solve your issue.
First of all obviously cast your list to an num list, either by specifying the generic type or using .cast :
var nl = [4, 2, 4, 5, 9];
print(sum(nl.cast<num>()));
var nl = <num>[4, 2, 4, 5, 9];
print(sum(nl2));
or you might want to use an extension which will work just fine for any list type which extends num:
extension SumList<T extends num> on List<T> {
T sum() => reduce((a, b) => a + b);
}
var nl4 = [4, 2, 4, 5, 9];
print(nl4.sum());
I want to do the equivalent of this Python code in Dart. Possibly as a one liner I could use in a return statement.
m = {1: 'a', 2: 'b', 3: 'c'}
l = [1, 3]
[m[index] for index in l]
this results in a new list like this ['a', 'c']
A long version of this in Dart would be this.
List<String> r = [];
for (var key in l) {
r.add(m[key]);
}
I think this is what you are asking for:
void main() {
final m = {1: 'a', 2: 'b', 3: 'c'};
final l = [1, 3, 4];
List<String> r1 = m.entries.where((e) => l.contains(e.key)).map((e) => e.value).toList();
List<String> r2 = l.where((k) => m.containsKey(k)).map((k) => m[k]).toList();
print(r1); // [a, c]
print(r2); // [a, c]
}
Update 1: Added another example (r2) which iterate over the list instead.
Update 2: Added check to second solution so it only map keys there are in the map.
[1..4] could help to generate
[1;2;3;4]
But I wish to generate a range like this:
[10;8;6;4;2]
How to use range semantics to achieve this(interval+descending)? Is a "for" loop mandatory in this case?
Thanks a lot.
Try this:
[10 .. -2 .. 2] ;;
// val it : int list = [10; 8; 6; 4; 2]
The value in the middle specifies the interval.