checking if two lists are equal in Maple - list

I've got the following lists :
list1:=[1, 5, 14, 30, 55, 91, 140, 204, 285, 385, 506, 650, 819, 1015,
1240, 1496, 1785, 2109, 2470, 2870]
list2:=[1, 5, 14, 30, 55, 91, 140, 204, 285, 385, 506, 650, 819, 1015,
1240, 1496, 1785, 2109, 2470, 2870]
each generated by a procedure I defined. I need to verify that they are equal, which is the case. However, when I tried to use the evalb function as well as a flag that I was updating during a loop, in both cases, I got 'false' as the answer along with the error message:
"error, final value in a for loop must be numeric or a character"
What I am doing wrong?

Maple will automatically resolve multiple copies of lists with identical entries to the same object. So to test equality, you don't even need to traverse the lists programmatically. You can just do:
evalb(list1=list2);
If however you'd like to do a more sophisticated comparison, you can use the verify command. For example, this will verify that the first list has the second list as a sublist:
verify([1, 2, 3, 4, 5], [2, 3, 4], superlist);
Calling verify with no second argument is equivalent to the first evalb test, e.g.:
verify(list1, list2);

Related

Why does nulls_last=False not put the nulls first in Django?

I'm finding that while nulls_last=True works, nulls_last=False doesn't. Example below is in a Django shell.
In [10]: [x.date for x in Model.objects.all().order_by(F('date').asc(nulls_last=True))]
Out[10]:
[datetime.datetime(2020, 3, 10, 16, 58, 7, 768401, tzinfo=<UTC>),
datetime.datetime(2020, 3, 10, 17, 4, 51, 601980, tzinfo=<UTC>),
None,
]
[ins] In [11]: [x.last_run_created_at for x in Model.objects.all().order_by(F('date').asc(nulls_last=False))]
Out[11]:
[datetime.datetime(2020, 3, 10, 16, 58, 7, 768401, tzinfo=<UTC>),
datetime.datetime(2020, 3, 10, 17, 4, 51, 601980, tzinfo=<UTC>),
None,
]
In [12]:
I've tried this with both desc() and asc().
The mistake is assuming that the opposite of nulls_last=True is nulls_last=False. It isn't.
nulls_last=True does the following to the query:
SELECT ... ORDER BY ... ASC NULLS LAST
Whereas nulls_last=False just means use the DB default:
SELECT ... ORDER BY ... ASC
What you want instead is to use nulls_first=True OR nulls_last=True to explicitly get the order you want.
This is mentioned in the docs, but perhaps not as explicitly as it could be:
Using F() to sort null values
Use F() and the nulls_first or
nulls_last keyword argument to Expression.asc() or desc() to control
the ordering of a field’s null values. By default, the ordering
depends on your database.

"how to fix MathJax linebreaking?"

I'm using doubleslash(\\) for line-breaking ,the cursor is pointing to the next line but a single slash(\) is appending with my data.
This is the input I am giving:
Find the median of the given data:"\\ "13, 16, 12, 14, 19, 12, 14, 13, 14"
The output is:
Find the median of the given data: \13, 16, 12, 14, 19, 12, 14, 13, 14.
Single slash is appending to the data.
Try using \\\\. Your content management system may be using \ as a special character, an that may turn \\ into \ in the resulting HTML. For example, Markdown usually does that.

Remove duplicates based on specific column

I do have a big text file in the following format on my Linux CentOS 7.
430004, 331108, 075, 11, 19, Chunsuttiwat Nattika
431272, 331108, 075, 11, 19, Chunsuttiwat Nattika
435979, 335086, 803, 6, 19, ANNI BRENDA
436143, 335151, 545, 4, 23, Agrawal Abhishek
436723, 335387, 386, 2, 19, Bhati Naintara
438141, 325426, 145, 11, 19, Teh Joshua
I would like to remove duplicate lines including the origin if it matches on second column.
Expected Output:
435979, 335086, 803, 6, 19, ANNI BRENDA
436143, 335151, 545, 4, 23, Agrawal Abhishek
436723, 335387, 386, 2, 19, Bhati Naintara
438141, 325426, 145, 11, 19, Teh Joshua
Update:
sort + uniq + awk pipeline:
sort -k2,2 file | uniq -f1 -c -w7 | awk '$1==1{ sub(/[[:space:]]*[0-9]+[[:space:]]*/,"",$0); print}'
sort -k2 -n file - sort the file by the 2nd field numerically
uniq -f1 -c - output lines with the number of occurrences (-f1 - skips the 1st field in the file)
awk '$1==1{ $1=""; print}' - print the lines which occur only once ($1==1 - check for count value from uniq command)
Using awk
#Input
awk '{R[i++]=$0;f=$1;$1="";N[$0]++;}
END{for(i=0;i<NR;i++){
temp=R[i];sub(/^[[:digit:]]*\, /,"",R[i]);
if(N[" "R[i]]==1){print temp}}}' filename
#Output
435979, 335086, 803, 6, 19, ANNI BRENDA
436143, 335151, 545, 4, 23, Agrawal Abhishek
436723, 335387, 386, 2, 19, Bhati Naintara
438141, 325426, 145, 11, 19, Teh Joshua
This is all you need:
$ awk 'NR==FNR{c[$2]++;next} c[$2]==1' file file
435979, 335086, 803, 6, 19, ANNI BRENDA
436143, 335151, 545, 4, 23, Agrawal Abhishek
436723, 335387, 386, 2, 19, Bhati Naintara
438141, 325426, 145, 11, 19, Teh Joshua

I want to append a list of numbers to another list; using another list for the range of appending

Just to be clear there are 3 different lists involved. la is a list of integers and the posfinList is a list of numbers where each integer from la should be appended until it reaches the first number in the list then moves to the next in the posfinList. The numbers in posfinList will change everytime I use different data.
posfinList=[83, 81, 83, 82, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 86, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 85, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83]
la is a list of 6,932 integers
rosen = 0
blos=0
lafin = []
lafins=[]
while rosen<len(la):
while rosen<(posfinList[blos]):
lafin.append(la[rosen])
if rosen >=(posfinList[blos]):
lafins.append(lafin)
blos+=1
rosen+=1
print lafins
Well it's not entirely clear what you're trying to do. Could you clarify please? Or even better, what output would you expect for this input?:
la=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
posfinList=[2, 4, 6]
(StackOverflow people: Yes, I know that should be a comment, but I don't have the reputation for it yet!)
Even without a clarification, however, I can confirm that you have an infinite loop. If posfinList[0]<6932, as it is in your example, then the inner loop terminates with rosen=posfinList[0] and rosen has no way to increase or reset. The outer loop continues and never terminates, causing the infinite loop you're seeing.
I'm assuming you want to chop up the list la into several pieces, so that the n:th piece has a length equal to posfinList[n] for all n. Please confirm/deny.
The following code is based on that assumption, see if it works the way you want:
lafins = []
for p in postfinList:
lafins.append( la[rosen:rosen+p] )
rosen += p
(If la[rosen:rosen+p] is unfamiliar notation to you, you can read about it (it's called 'slice' notation) in the Python Documentation. You'll find the relevant bits a little way down under the 'Strings' header.)
I have mostly used the same variable names as you, though they're not all that descriptive. Taking the time to choose variable names that actually document what the variables do generally pays off handsomely when testing and debugging, not to mention helps to make sure that you yourself know what those variables are meant to do.
As for your original code, there are at least three problems:
the stuff inside if rosen >=(posfinList[blos]) will never execute. The if itself is contained in while rosen<(posfinList[blos]), which actually makes sure that rosen>=(postfinList[blos]) will never be true inside it.
when while rosen<(posfinList[blos]) exits (unless by coincidence rosen>=len(la)), while rosen<len(la) will enter an infinite loop: rosen will always be less than len(la). Neither of those values will ever change again, so the while condition will always be true.
this one is difficult to explain, so I'm just going to say this: for your loop to work, in the example case you gave, postfinList should be [83, 164, 247, 329, ...] instead of [83, 81, 83, 82, ...], that is, it should be [83, 83+81, 83+81+83, 83+81+83+82, ...]. Think about why that is.

input output in prolog

i want to read characters from a file in prolog and place them in a list.
could some one help me out with it?
thanks
SWI-Prolog offers read_file_to_codes/3. Usage example:
?- read_file_to_codes('/etc/passwd', Codes, []).
Codes = [114, 111, 111, 116, 58, 120, 58, 48, 58|...].