I want to be able to take a list in clojure like
(1 2 3 4 5 6 7 8 9)
and change the order like this:
(9 8 7 6 5 4 3 2 1)
I could not find a built-in function for (inverting? Is that a good term?) turning lists back to front or whatever you wanna call the action, if there is I'd be very thankful if someone pointed that out since creating this function is not my goal it's just a step I need to pass to do what I really want to do. Anyway I wrote a function that should do it but I get this error:
StackOverflowError clojure.core/seq (core.clj:133)
Related
Im a noob programmer currently making a small family database using cpp but i have trouble deleting a family from the the list...
My list looks something like this
start-of-family 1
jim
joe
bob
sam
end-of-family 1
start-of-family 2
rob
max
end-of-family 2
start-of-family 3
sue
tom
kim
end-of-family 3
If i wanted to delete family 1, I would locate start-of-family 1 and end-of-family 1. Then run a loop but how do i locate it if the user only inputs an int to represent a family number. Also how do i make the succeeding family numbers deduct by 1 so that family 2 will be 1 and family 3 will be 2.
thanks a lot
If I were doing this problem I would start by making each family into a vector of names. Then, I would create a vector containing those family vectors.
The result would look something like:
{
<(jim), (joe), (bob), (sam) >
<(rob), (max) >
<(sue), (tom), (kim) >
}
Then, if the user wants to delete one of the families, you can use vector.remove(n) where n is the index of the family to be removed.
This sounds like a school or text book assignment. Have you gotten to vectors yet? Where are the names coming from? Are you hard coding them into the list? Or reading them from a .txt file? What kind of list structure are you storing them in right now?
i realized that clearing the db and updating it with what ive got is way easier than modifying the db and updating my program
I have an assignment where I need to find minimum set cover of some points. I want to be able to store each row of numbers in an individual sets but I do not know the best data structure or approach to do this. I have the number of rows there will be. For example, the .txt file will look like this:
1 2 3 4 5 6
5 6 8 9
1 4 7 10
2 5 7 8 11
3 6 9 12
10 11
Is there a way to dynamically create multiple data structures to store each row of numbers? I was thinking something that works like this if it exists:
list<int> myList[6]; // create 6 lists
myList[0].insert(num); // insert numbers into this list
myList[1].insert(num); //insert numbers into the second list
I do not want to individually create lists because in a .txt file, there can be up to 300 sets of numbers.
edit: my main issue is figuring out how to dynamically create some data structure, preferably if it works with std::set_union since it looks useful to my assignment
If you want to control the number of lists programmatically, you can use a std::vector of datasets. So in your case the declaration would be
std::vector<std::list<int>> lists(6);
Adding new, empty lists to the lists set is done by
lists.push_back({}).
So if we want a collection of unique items we can use a 'set'.
If we already have a collection of items that we want to dedupe, we could pass them to the set function, or alternatively we could use the distinct or dedupe functions.
What are the situations for using each of these (pros/cons)?
Thanks.
The differences are:
set will create a new set collection eagerly.
distinct will create a lazy sequence with duplicates from the input collection removed. It has an advantage over set if you process big collections and lazyness might save you from eagerly evaluating the input collection (e.g. with take)
dedupe removes consecutive duplicates from the input collection so it has a different semantics than set and distinct. For example it will return (1 2 3 1 2 3) when applied to (1 1 1 2 3 3 1 1 2 2 2 3 3)
Set and lazy seq have different APIs available (e.g. disj, get vs nth) and performance characteristics (e.g. O(log32 n) look up for set and O(n) for lazy seq) and they should be chosen depending on how you would like to use their results.
Additionally distinct and dedupe return a transducer when called without argument.
I'm facing this problem in programming it says that if jack knows john and john know mark, then jack should know mark
the input is like this
1 - 3
3 - 4
1 - 4
1 knows 3 and 3 knows 4 so 1 knows 4, the answer is yes otherwise if the third line isn't there the answer is false because there's no connection between these three persons.
How can I implement this in c++ to make a connection between these three (on large inputs).
It is pretty simple if you know c++;
you just have to make a friend List of every person, then you can make a function
bool isFriend(person1,person2)
Which will return true if person1 and person2 knows each other, then you can make relationship of 3rd person if he knows any of them he'll know both of them as your requirement.
To make friend list you can use jagged array data structure and adjacency list with link list;
I'm trying to create a simple class for maps in my game. Maps are 2 dimensional vectors made of "tile" structs. I'm trying to figure out the best way to input/output these tiles in rows.
For example, I used to use a .txt based loading format. The .txt file would look like this:
1 0 2 0 0 0 2 4 5 6 3
2 4 5 0 0 0 2 0 0 3 4
0 3 5 2 5 3 0 5 5 3 4
0 2 0 5 0 6 0 5 7 8 4
I would then go line by line to find the ID integer of each tile. 1 would represent grass, 0 would mean water, and so on. When the parser would reach the end of a line, it would skip down to the next line of ints.
Now I'm trying to do this via fwrite and fread with binary files and structs instead of ints. How would I go about doing this? All I've seen is how to store an array of structs in a binary file, not how to store a multidimensional array of structs. Any ideas?
EDIT: Yes, I could just store the 2D vector in the file, but that wouldn't allow me to do seamless map loading, which I need. I have large map files, so having 100k tiles loaded at once would hog CPU.
If you want your map to be human readable, you need to write a parser.
However, if you don't care, you should do something completely different: Use serialization, e.g. from Boost.
A third alternative for your case, which is quite common for games, is to store the map as an image file. You can then edit the map in your favorite image manipulation program, and it is easily interpreted by your program.
Many libs can read image data for you (and present it to you as a 2-D array), and in a game you maybe have something like that already in place.