Clojure: Iterating over a vector of vectors to find the first vector that meets a certain condition
Date : March 29 2020, 07:55 AM
around this issue To learn Clojure I'm working on a little Tic Tac Toe game. After completing the first part of the game with relative ease, I've struggled trying to build an intelligent computer player. , First of all, I advice you to use this structure for board position: (def board [[1 1 0]
[0 0 0]
[1 0 1]])
(def winning-sets
'([[0 0] [0 1] [0 2]]
[[1 0] [1 1] [1 2]]
[[2 0] [2 1] [2 2]]
[[0 0] [1 0] [2 0]]
[[0 1] [1 1] [2 1]]
[[0 2] [1 2] [2 2]]
[[0 0] [1 1] [2 2]]
[[0 2] [1 1] [2 0]]))
(defn check
[target combo]
(= (map #(count (filter (partial = %) combo)) [target 0]) '(2 1)))
(defn extract
[coords]
(apply vector (map (fn [[f s]] ((board f) s)) coords)))
(filter #(check 1 (extract %)) winning-sets)
user=> (filter #(check 1 (extract %)) winning-sets)
([[0 0] [0 1] [0 2]]
[[2 0] [2 1] [2 2]]
[[0 0] [1 0] [2 0]]
[[0 0] [1 1] [2 2]])
|
Iterating in a sorted manner over a std::vector<std::pair<T,U> > object
Date : March 29 2020, 07:55 AM
Hope this helps I am reading a object from a database of type Foo, as defined below. This object is a vector of Foo Members, where a Foo Members consists of a string id and a container object. , You can use std::sort #include <algorithm>
bool comparator(const FooMember& i, const FooMember& j)
{
std::string str1 = i.first;
boost::algorithm::to_lower(str1);
std::string str2 = j.first;
boost::algorithm::to_lower(str2);
return (str1 < str2);
}
void sortFoo(Foo& value) {
std::sort (value.begin(), value.end(), comparator);
}
|
Fastest way to find all-pair shortest paths in a undirected cycle
Date : March 29 2020, 07:55 AM
To fix this issue In one pass starting from A traverse the graph clockwise, and for every node compute the distance from A. Let's say the distance to the node X is a[X]. This way for any pair (X, Y) of nodes the distance will be: min(abs(aX - aY), total - abs(aY - aX))
min(abs(aB - aF), total - abs(aB - aF)) =
min(abs( 1 - 3), 6 - abs( 1 - 3)) =
min( 2, 4) =
2
|
Fastest way to find a vector in a vector of vectors
Date : March 29 2020, 07:55 AM
|
Find int in Vector of pairs vector <pair <int, vector <SavingsAccount*>>> using find_if
Tag : cpp , By : Jonathan Bernard
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further My data structure is: , Use a lambda as predicate for std::find_if(): #include <algorithm>
#include <utility>
#include <vector>
#include <iostream>
using namespace std;
struct SavingsAccount{};
int main()
{
vector<pair<int, vector<SavingsAccount>>> accVec;
vector<pair<int, vector<SavingsAccount>>>::iterator iter;
do {
cout << "Enter The Account Number In Which You Want To Deposit: ";
int accID;
cin >> accID;
iter = find_if(accVec.begin(), accVec.end(),
[=](pair<int, vector<SavingsAccount>> const &accPair) {
return accPair.first == accID;
}
);
if (iter == accVec.end()) {
cout << endl << "ERROR: Account Does Not Exist. Try Again." << endl;
}
} while (iter == accVec.end());
}
#include <algorithm>
#include <utility>
#include <vector>
#include <iostream>
using namespace std;
struct SavingsAccount{};
class AccountFinder
{
int accID;
public:
AccountFinder(int accID) : accID{ accID } {};
bool operator()(pair<int, vector<SavingsAccount>> const &accPair)
{
return accPair.first == accID;
}
};
int main()
{
vector<pair<int, vector<SavingsAccount>>> accVec;
vector<pair<int, vector<SavingsAccount>>>::iterator iter;
do {
cout << "Enter The Account Number In Which You Want To Deposit: ";
int accID;
cin >> accID;
iter = find_if(accVec.begin(), accVec.end(), AccountFinder(accID));
if (iter == accVec.end()) {
cout << endl << "ERROR: Account Does Not Exist. Try Again." << endl;
}
} while (iter == accVec.end());
}
|