I'm trying to get SML/NJ to print out a result at the top level without
putting # signs everywhere.
According to some old docs (and a post to this newsgroup on 2001), it
should be possible to use Compiler.Control.Print.printDepth
However, on SML/NJ version 110.7, this just gives an error:
- Compiler.Control.Print.printDepth := 100;
stdIn:1.1-30.8 Error: unbound structure: Control in path Compiler.Control.Print.printDepth
You might wan't to be more precise in the future. You could for example give some sample output and a link to where you found the above.
If I understand your problem correct, then the last line below is your problem? (code snippet
- datatype tree = leaf | node of int * tree * tree;
datatype tree = leaf | node of int * tree * tree
- val t = node (1, node (2, node (3, leaf, leaf), leaf), leaf);
val t = node (1,node (2,node #,leaf),leaf) : tree
Then the Control.Print structure is what you are looking for. So just drop the Compiler part and use
Control.Print.printDepth := 100;
Do note that this is SML/NJ specific and not ml-yacc as such.
Related
I have two lists and I want to return a result in the following way:
the result should contain elements that are in list one and list two
output should be same order as per first list
Input :
val first = listOf(1, 2, 3, 4, 5,7,9,15,11)
val second = listOf(2, 15 , 4,3, 11)
Output:
val output = listOf(2,3,4,15,11)
Please help me to learn how to get common values in both lists in order of list first in Kotlin.
You can do
val output = first.filter { second.contains(it) }
What you are looking for is the intersection of the two lists:
val output = first.intersect(second)
As pointed out by #Ivo the result is a Set which can be turned into a list with output.toList(). However, since the result is a set, it contains no duplicates, e.g. if first is listOf(1,2,3,1,2,3) and second is listOf(2,4,2,4), the result will be equal to setOf(2).
If this is not acceptable, the solution of #Ivo should be used instead.
I'm trying to sort an entire list according to one property. Afterwards I'd like to sort this list according to a second property, but in groups of 4. So, after sorting the list once, I want to look at the first 4 positions and sort only these 4 according to the second property - then move on to the next 4 positions and sort these again, and so on...
This is what I have so far:
class myElements {
int Position;
String text;
int Top;
int Left;
myElements(int Position, String text, int Top, int Left){
this.Position = Position;
this.text = text;
this.Top = Top;
this.Left = Left;
}
}
var FirstList = new List<myElements>();
var newList = new List<myElements>();
Adding Elements to my first list:
myElements Test = myElements(ElementNumber, text, Top, Left);
FirstList.add(Test);
Then sorting for the first time according to 'Top':
Comparator<myElements> TextComparator = (a, b) => a.Top.compareTo(b.Top);
FirstList.sort(TextComparator);
Here is where I'm stuck. I'm trying to sort the list again, but only in groups of 4 - this time according to 'Left':
for (int i = 0; i < FirstList.length; i += 4) {
Comparator<myElements> TextComparator2 = (a, b) =>
a.Left.compareTo(b.Left);
newList.addAll(FirstList.sublist(i, i + 3).sort(TextComparator2)); //this line does not work
}
I think I am stuck trying to access my sorted sublist: (FirstList.sublist(i, i + 4).sort(TextComparator2) . If I could add these to a new list, it should work.
However any other suggestions are more than welcome.
Thanks so much!
newList.addAll(FirstList.sublist(i, i + 3).sort(TextComparator2)); //this line does not work
Your code is almost correct. You have the right idea, but you ended up trying to do too much in one line of code.
Breaking it down a bit, your code is equivalent to:
var sublist = FirstList.sublist(i, i + 3);
newList.addAll(sublist.sort(...)); // Does not work
And that doesn't work because List.sort does not return a value. It mutates the list instead of returning a new list.
It would work if you instead did:
var sublist = FirstList.sublist(i, i + 3);
sublist.sort();
newList.addAll(sublist);
Also, List.sublist uses an exclusive end index. If you want to create sublists with 4 elements, you would need to use sublist(i, i + 4).
I am trying to extract particular lines from txt output file. The lines I am interested in are few lines above and few below the key_string that I am using to search through the results. The key string is the same for each results.
fi = open('Inputfile.txt')
fo = open('Outputfile.txt', 'a')
lines = fi.readlines()
filtered_list=[]
for item in lines:
if item.startswith("key string"):
filtered_list.append(lines[lines.index(item)-2])
filtered_list.append(lines[lines.index(item)+6])
filtered_list.append(lines[lines.index(item)+10])
filtered_list.append(lines[lines.index(item)+11])
fo.writelines(filtered_list)
fi.close()
fo.close()
The output file contains the right lines for the first record, but multiplied for every record available. How can I update the indexing so it can read every individual record? I've tried to find the solution but as a novice programmer I was struggling to use enumerate() function or collections package.
First of all, it would probably help if you said what exactly goes wrong with your code (a stack trace, it doesn't work at all, etc). Anyway, here's some thoughts. You can try to divide your problem into subproblems to make it easier to work with. In this case, let's separate finding the relevant lines from collecting them.
First, let's find the indexes of all the relevant lines.
key = "key string"
relevant = []
for i, item in enumerate(lines):
if item.startswith(key):
relevant.append(item)
enumerate is actually quite simple. It takes a list, and returns a sequence of (index, item) pairs. So, enumerate(['a', 'b', 'c']) returns [(0, 'a'), (1, 'b'), (2, 'c')].
What I had written above can be achieved with a list comprehension:
relevant = [i for (i, item) in enumerate(lines) if item.startswith(key)]
So, we have the indexes of the relevant lines. Now, let's collected them. You are interested in the line 2 lines before it and 6 and 10 and 11 lines after it. If your first lines contains the key, then you have a problem – you don't really want lines[-1] – that's the last item! Also, you need to handle the situation in which your offset would take you past the end of the list: otherwise Python will raise an IndexError.
out = []
for r in relevant:
for offset in -2, 6, 10, 11:
index = r + offset
if 0 < index < len(lines):
out.append(lines[index])
You could also catch the IndexError, but that won't save us much typing, as we have to handle negative indexes anyway.
The whole program would look like this:
key = "key string"
with open('Inputfile.txt') as fi:
lines = fi.readlines()
relevant = [i for (i, item) in enumerate(lines) if item.startswith(key)]
out = []
for r in relevant:
for offset in -2, 6, 10, 11:
index = r + offset
if 0 < index < len(lines):
out.append(lines[index])
with open('Outputfile.txt', 'a') as fi:
fi.writelines(out)
To get rid of duplicates you can cast list to set; example:
x=['a','b','a']
y=set(x)
print(y)
will result in:
['a','b']
I have a List[Set[Path]]: Update: Each Path in a Set is unique and represents a particular directory location. There are no duplicates. So, what I am looking for is the total number of path elements/
val miceData = List(Set(C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test7.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test2.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test6.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test5.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test8.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test3.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\aPowerPoint.pptx, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test1.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test4.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory2\Test10.txt), Set(C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory2\Test6.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory2\Test3.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory2\Test4.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory2\Test70.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory2\Test8.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory2\Test5.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory2\Test2.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\FlatDirectory\Test2.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\FlatDirectory\Test3.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\FlatDirectory\Test1.txt), Set(C:\Users\lulu\Documents\mice_data\data_mining_folder\FlatDirectory\Test80.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\FlatDirectory\Test7.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\FlatDirectory\Test40.txt, C:\Users\lulu\Documents\GitHub\data_mining_folder\FlatDirectory\Test6.txt, C:\Users\lulu\Documents\mice_data\data_mining_folder\FlatDirectory\Test5.txt), Set(C:\Users\lulu\Documents\mice_data\data_mining_folder\zipfile.zip), Set(C:\Users\lulu\Documents\mice_data\data_mining_folder\micetest.txt,C:\Users\lulu\Documents\mice_data\data_mining_folder\riley.jpg))
There are 5 Sets in this List, each Set holding Path(s). The total number of such Paths is 28, if I counted correctly.
Now, I want to find out the total number of Path elements across all Sets in this List.
I could have done this computation in an area of my code upstream, but I am curious to do so now, and learn more about Scala in the process.
Something like:
val totalPaths = <<iterate over this List and count all the paths>>
I would like the shortest, most idiomatic piece of code to accomplish this.
val paths = for { //gives you a list of all paths on all sets
set <- miceData
path <- set
} yield path
val totalPaths = paths.toSet.size // converting it to set will remove duplicates if any
I think flatten is just enough
val toto = List(Set(1,2,3), Set(6,7,8))
println(toto.flatten.count)
val totalPaths = miceData.map(_.size).sum
If you have duplicates, you can do :
val totalPaths = miceData.flatten.distinct.size
val totalPaths = miceData.flatten.size
OR
val totalPaths = miceData.flatten.length
And you might want to give your paths as triple qouted Strings. because with single quotes REPL is giving the following error.
<console>:1: error: invalid escape character
List(Set("C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test7.txt", "C:\Users\lulu\Documents\mice_data\data_mining_folder\DeeplyNestedDirectory\FlatDirectory\Test2.txt")).flatten.size
I implementing Family program in prolog i have a problem to implement some rules.
first of all i implement this rule:
number_of_children_couple(_list):-
findall(children(_f,_m,_n),children(_f,_m,_n),_list).
return list:
19 ?- number_of_children_couple(_list).
_list = [children(mordechai, miriam, 1), children(salax, naima, 1), children(eli, bella, 2), children(..., ..., ...)|...].
my problem is how to implement :
number_of_children_person(_list28,list_person):-
first argument:
_list28 = _list //the above list ,that return from the rule
and second argument is :
list_person = [children(mordechai, 1), children(salax, 1),children(eli, 2),children(..., ...)|...]
and i also use with:
%_num is number of children for couple
children(_father,_mother,_num):-
couple(_mother,_father),
findall(_child,parents(_mother,_father,_child),_children),
length1(_children,_num).
%_num is number of children for _person
children(_person,_num):-
gender(_person,_),
findall(_child,parent(_person,_child),_list),
length1(_list,_num).
if what you want is to drop an argument from children/3 structure, can be done in a number of ways, the simpler being
number_of_children_person([],[]).
number_of_children_person([children(A,_,C)|R],[children(A,C)|T]) :-
number_of_children_person(R,T).
or more succintly
number_of_children_person(L3,L2) :-
findall(children(A,C), member(children(A,B,C),L3), L2).