diff --git a/algorithms/geometry/convex_hull.cpp b/algorithms/geometry/convex_hull.cpp index 33a0f2da16290fe9029ba7e43b66be1a6cb295a4..9c28b2f8d3103ab06e5a49964d8c471ec4d51864 100644 --- a/algorithms/geometry/convex_hull.cpp +++ b/algorithms/geometry/convex_hull.cpp @@ -1,5 +1,5 @@ -/// Convex Hull -/// +/// Convex Hull +/// /// Complexity (Time): O(n log n) /// Complexity (Space): O(n) @@ -25,19 +25,19 @@ struct ConvexHull { // Uppermost part of convex hull for (int i = 0; i < v.size(); ++i) { - while (k >= 2 && cross(v[ans[k - 2]], v[ans[k - 1]], v[i]) < 0) + while (k >= 2 && cross(v[ans[k - 2]], v[ans[k - 1]], v[i]) < 0) k--; ans[k++] = i; } // Lowermost part of convex hull for (int i = v.size() - 2, t = k + 1; i >= 0; --i) { - while (k >= t && cross(v[ans[k - 2]], v[ans[k - 1]], v[i]) < 0) + while (k >= t && cross(v[ans[k - 2]], v[ans[k - 1]], v[i]) < 0) k--; ans[k++] = i; } - // The ans vector contains the indices (relative to the sorted vector!) + // The ans vector contains the indices (relative to the sorted vector!) // of the points belonging to the convex hull ans.resize(k - 1); return ans; diff --git a/algorithms/geometry/geometry_functions.cpp b/algorithms/geometry/geometry_functions.cpp index 82a944cd83ea17fac1e1e461db71e412111097c5..01edb1a59cc5089d8fde0407a6b179566ec5799f 100644 --- a/algorithms/geometry/geometry_functions.cpp +++ b/algorithms/geometry/geometry_functions.cpp @@ -1,4 +1,4 @@ -/// Geometry functions +/// Geometry Functions template <typename T> struct Point { diff --git a/algorithms/graph/articulations_bridges.cpp b/algorithms/graph/articulations_bridges.cpp index d92ff848d069457e1dd2726144a802ce67bab44c..23d7fb4f4e27e5ab979e819564f3628a34603caf 100644 --- a/algorithms/graph/articulations_bridges.cpp +++ b/algorithms/graph/articulations_bridges.cpp @@ -1,4 +1,4 @@ -/// Tarjan - Articulations and Bridges +/// Articulations and Bridges - Tarjan /// /// Complexity (Time): O(V + E) /// Complexity (Space): O(V + E) diff --git a/algorithms/graph/edmonds_karp.cpp b/algorithms/graph/edmonds_karp.cpp index a9f6a159fafa351d91123e9150ecda22b7dcf44f..70a831c25cafb83653d38e27fef46c8dce862896 100644 --- a/algorithms/graph/edmonds_karp.cpp +++ b/algorithms/graph/edmonds_karp.cpp @@ -1,5 +1,5 @@ -/// Edmonds-Karp -/// +/// Edmonds-Karp +/// /// Complexity (time): O(V*E^2) /// Complexity (space): O(V^2) @@ -11,7 +11,7 @@ struct EdmondsKarp { vector<int> par, vis; EdmondsKarp(int N) : - N(N), par(N), vis(N), + N(N), par(N), vis(N), {} void init() { diff --git a/algorithms/graph/floyd_warshall.cpp b/algorithms/graph/floyd_warshall.cpp index 67affe34d57044fd3d97bd10cd8d266737449317..bae4c37b4b0a8996d4db35718bc61e86240134b4 100644 --- a/algorithms/graph/floyd_warshall.cpp +++ b/algorithms/graph/floyd_warshall.cpp @@ -1,4 +1,4 @@ -/// Floyd Warshall Algorithm +/// Floyd Warshall /// /// Complexity (Time): O(V^3) /// Complexity (Space): O(V^2) diff --git a/algorithms/graph/lca.cpp b/algorithms/graph/lca.cpp index 50f87011deaf64db426d396478a63edc5d24006c..177510e9200eac6da5bdb1f7518484fdd078ec48 100644 --- a/algorithms/graph/lca.cpp +++ b/algorithms/graph/lca.cpp @@ -1,4 +1,4 @@ -/// Lowest Common Ancestor - LCA +/// Lowest Common Ancestor (LCA) /// /// Complexity (Time): /// - preprocess: O(V log V) diff --git a/algorithms/math/big_integer.cpp b/algorithms/math/big_integer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e21ec2ceb539630595b327c4e42a673cf5f79f5 --- /dev/null +++ b/algorithms/math/big_integer.cpp @@ -0,0 +1,175 @@ +/// Big Integer +/// +/// Complexity (space): O(n) +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +const int base = 1000000000; +const int base_digits = 9; + +struct BigInteger { + int sign = 1; + vector<int> num; + + BigInteger() {} + BigInteger(const string &x) { read(x); } + + BigInteger operator+(const BigInteger &x) const { + if (sign != x.sign) + return *this - (-x); + + BigInteger ans = x; + for (int i = 0, carry = 0; i < max(size(), x.size()) || carry; ++i) { + if (i == ans.size()) + ans.push_back(0); + + if (i < size()) + ans[i] += carry + num[i]; + else + ans[i] += carry; + + carry = ans[i] >= base; + if (carry) + ans[i] -= base; + } + + return ans; + } + + BigInteger operator-(const BigInteger& x) const { + if (sign != x.sign) + return *this + (-x); + if (abs() < x.abs()) + return -(x - *this); + + BigInteger ans = *this; + for (int i = 0, carry = 0; i < x.size() || carry; ++i) { + if (i < x.size()) + ans[i] -= carry + x[i]; + else + ans[i] -= carry; + + carry = ans[i] < 0; + if (carry) + ans[i] += base; + } + + ans.trim(); + return ans; + } + + /// Remove leading zeros. + void trim() { + while (!num.empty() && num.back() == 0) + num.pop_back(); + + if (num.empty()) + sign = 1; + } + + bool operator<(const BigInteger &x) const { + if (sign != x.sign) + return sign < x.sign; + + if (size() != x.size()) + return (size() * sign) < (x.size() * x.sign); + + for (int i = size() - 1; i >= 0; i--) + if (num[i] != x[i]) + return (num[i] * sign) < (x[i] * x.sign); + + return false; + } + + bool operator>(const BigInteger &x) const { return (x < *this); } + bool operator<=(const BigInteger &x) const { return !(x < *this); } + bool operator>=(const BigInteger &x) const { return !(*this < x); } + bool operator==(const BigInteger &x) const { return !(*this < x) && !(x < *this); } + bool operator!=(const BigInteger &x) const { return !(*this == x); } + + /// Handles -x (change of sign). + BigInteger operator-() const { + BigInteger ans = *this; + ans.sign = -sign; + return ans; + } + + /// Returs absolute value. + BigInteger abs() const { + BigInteger ans = *this; + ans.sign *= ans.sign; + return ans; + } + + /// Transforms string into BigInteger. + void read(const string &s) { + sign = 1; + num.clear(); + int pos = 0; + while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+')) { + if (s[pos] == '-') + sign = -sign; + ++pos; + } + + for (int i = s.size() - 1; i >= pos; i -= base_digits) { + int x = 0; + for (int j = max(pos, i - base_digits + 1); j <= i; j++) + x = x * 10 + s[j] - '0'; + num.push_back(x); + } + + trim(); + } + + friend istream& operator>>(istream &stream, BigInteger &v) { + string s; stream >> s; + v.read(s); + return stream; + } + + friend ostream& operator<<(ostream &stream, const BigInteger &x) { + if (x.sign == -1) + stream << '-'; + + stream << (x.empty() ? 0 : x.back()); + for (int i = x.size() - 2; i >= 0; --i) + stream << setw(base_digits) << setfill('0') << x.num[i]; + + return stream; + } + + /// Handles vector operations. + int back() const { return num.back(); } + bool empty() const { return num.empty(); } + size_t size() const { return num.size(); } + void push_back(int x) { num.push_back(x); } + + int &operator[](int i) { return num[i]; } + int operator[](int i) const { return num[i]; } +}; + +int main() { + BigInteger x, y; + cin >> x >> y; + cout << x + y << ende; + return 0; +} diff --git a/algorithms/math/euler_totient.cpp b/algorithms/math/euler_totient.cpp index f41cdff24475a365225253a313717e36f1f4d411..009c5fda9817ec0a45d44ca690248841f58c02c9 100644 --- a/algorithms/math/euler_totient.cpp +++ b/algorithms/math/euler_totient.cpp @@ -1,4 +1,4 @@ -/// Euler Totient (phi) +/// Euler Totient ($\phi$) /// /// Complexity (time): O(sqrt(n)) /// Complexity (space): O(1) diff --git a/algorithms/math/matrix.cpp b/algorithms/math/matrix.cpp index ac1c43d185861dd13148ef589c3a169367b114ab..11c2ed3ffe68f9d5d8b67ba70c9c19a489a02d85 100644 --- a/algorithms/math/matrix.cpp +++ b/algorithms/math/matrix.cpp @@ -1,4 +1,4 @@ -/// Matrix +/// Matrix template <typename T> struct matrix { @@ -24,7 +24,7 @@ struct matrix { for (int k = 0; k < c; k++) res[i][j] += m[i][k] * a[k][j]; } - + return res; } diff --git a/algorithms/math/sieve_of_eratosthenes.cpp b/algorithms/math/sieve_of_eratosthenes.cpp index d90f8afb9482c6ad7437a5053dcb52f0b00ef42f..29790366d88e343ee5b0eecf2c00e3b0c99e0c5e 100644 --- a/algorithms/math/sieve_of_eratosthenes.cpp +++ b/algorithms/math/sieve_of_eratosthenes.cpp @@ -1,4 +1,4 @@ -/// Sieve of Eratosthenes +/// Sieve of Eratosthenes /// /// Complexity (Time): O(n*log(log(n))) /// Complexity (Space): O(n) diff --git a/algorithms/paradigm/edit_distance.cpp b/algorithms/paradigm/edit_distance.cpp index 2d9af1f9cf84a755813ab9fd462b7a45dc820a1a..c8f9d0d82b75b47c0e8d78b77e5fbc1c635d63cf 100644 --- a/algorithms/paradigm/edit_distance.cpp +++ b/algorithms/paradigm/edit_distance.cpp @@ -1,6 +1,6 @@ -/// Edit Distance +/// Edit Distance /// -/// Complexity (Time): O(m*n) +/// Complexity (Time): O(m*n) /// Complexity (Space): O(m*n) struct EditDistance { diff --git a/algorithms/paradigm/lis.cpp b/algorithms/paradigm/lis.cpp index f6150a40453d41c933f6ca3dc49ec10225d2a5f1..2aa433e36f250042adc7a04752499db970627ef4 100644 --- a/algorithms/paradigm/lis.cpp +++ b/algorithms/paradigm/lis.cpp @@ -1,4 +1,4 @@ -/// Longest Increasing Subsequence (LIS) +/// Longest Increasing Subsequence (LIS) /// /// Complexity (Time): O(n^2) /// Complexity (Space): O(n) @@ -6,7 +6,7 @@ struct LIS { /// Returns the length of the longest increasing subsequence - /// @param v input vector + /// @param v input vector int run(vector<int> v) { vector<int> lis(v.size()); lis[0] = 1; diff --git a/algorithms/structure/bitmask.cpp b/algorithms/structure/bitmask.cpp index 956cd98ea408b5d7379569bd57fa7ebc5669251d..0f63995962a44694b63467d81f1a9d760ab91f19 100644 --- a/algorithms/structure/bitmask.cpp +++ b/algorithms/structure/bitmask.cpp @@ -1,4 +1,4 @@ -/// Bitmask +/// Bitmask /// /// Complexity (Time): O(1) /// Complexity (Space): O(1) diff --git a/algorithms/structure/trie.cpp b/algorithms/structure/trie.cpp index 90f8c13253882baf2c886d1e13071a93b0861dee..9bdf69753558459b400338038073b228b7dcc33c 100644 --- a/algorithms/structure/trie.cpp +++ b/algorithms/structure/trie.cpp @@ -1,4 +1,4 @@ -/// Trie +/// Trie /// /// Complexity (Time): /// Insert -> O(m) diff --git a/contests/Cadernaveis/EASUDOKU.cpp b/contests/Cadernaveis/EASUDOKU.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6aa1709b05481cd3a85030b12760b472df55868 --- /dev/null +++ b/contests/Cadernaveis/EASUDOKU.cpp @@ -0,0 +1,80 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +vector<vector<int>> mat, filled; +vector<int> init = {0, 0, 0, 3, 3, 3, 6, 6, 6}; + +int check(int r, int c) { + int ans = 0; + for (int i = 0; i < 9; ++i) ans |= (1 << mat[r][i]); + for (int i = 0; i < 9; ++i) ans |= (1 << mat[i][c]); + + for (int i = init[r]; i < init[r] + 3; ++i) + for (int j = init[c]; j < init[c] + 3; ++j) + ans |= (1 << mat[i][j]); + + return ans; +} + +bool solve(int i, int j) { + if (filled[i][j]) { + if (i == 8 && j == 8) return true; + else return solve(i + (j == 8), (j + 1) % 9); + } + + int poss = check(i, j); + for (int k = 1; k <= 9; ++k) { + if ((poss & (1 << k)) == 0) { + mat[i][j] = k; + if (i == 8 && j == 8) return true; + if (solve(i + (j == 8), (j + 1) % 9)) return true; + mat[i][j] = filled[i][j]; + } + } + + return false; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + mat.resize(9, vector<int>(9)); + + int n; cin >> n; + for (int cas = 0; cas < n; ++cas) { + for (auto &i : mat) + for (auto &j : i) + cin >> j; + + filled = mat; + + if (solve(0, 0)) + for (auto &i : mat) { + for (auto j : i) cout << j << " "; + cout << ende; + } + else + cout << "No solution" << ende; + } + + return 0; +} diff --git a/contests/Cadernaveis/JANELA13.cpp b/contests/Cadernaveis/JANELA13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..766d7cd18bbe2ad3139b23e49905277ee4629971 --- /dev/null +++ b/contests/Cadernaveis/JANELA13.cpp @@ -0,0 +1,56 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int n; n = 3; + vector<ii> v; + v.pb(ii(0, -1)); + v.pb(ii(600, 2)); + for (int i = 0; i < n; ++i) { + int x; cin >> x; + v.pb(ii(x, 0)); + v.pb(ii(x+200, 1)); + } + + int curr = 0, ans = 0; + bool count = false; + sort(all(v)); + for (int i = 0; i < v.size(); ++i) { + if (v[i].se == 0) + curr++; + else if (v[i].se == 1) + curr--; + + if (count) { + ans += v[i].fi - v[i-1].fi; + count = false; + } + + if (curr == 0) + count = true; + } + + cout << ans * 100 << ende; + return 0; +} diff --git a/contests/Cadernaveis/ORKUT.cpp b/contests/Cadernaveis/ORKUT.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b594758f752ec16dbe8e7d28295145d08a9b5fa --- /dev/null +++ b/contests/Cadernaveis/ORKUT.cpp @@ -0,0 +1,116 @@ +#include <bits/stdc++.h> + +#define MAX 1000 +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +vector<int> graph[MAX]; + +struct TopologicalSort { + int N; + stack<int> S; + vector<int> cont; + + TopologicalSort(int N) : + N(N), cont(N) + {} + + void init() { + fill(all(cont), 0); + } + + bool dfs(int x) { + cont[x] = 1; + + for (auto i : graph[x]) { + if (cont[i] == 1) + return true; + if (!cont[i] && dfs(i)) + return true; + } + + cont[x] = 2; + S.push(x); + + return false; + } + + bool run(vector<int> &tsort) { + init(); + + bool cycle = false; + for (int i = 0; i < N; ++i) { + if (!cont[i]) + cycle |= dfs(i); + } + + if (cycle) + return true; + + while (!S.empty()) { + tsort.pb(S.top()); + S.pop(); + } + + return false; + } +}; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int n; + for (int cas = 1; cin >> n && n; ++cas) { + for (int i = 0; i < MAX; ++i) + graph[i].clear(); + + vector<string> v(n); + map<string,int> M; + for (int i = 0; i < n; ++i) { + cin >> v[i]; + M[v[i]] = i; + } + + for (int i = 0; i < n; ++i) { + string a; cin >> a; + int m; cin >> m; + for (int j = 0; j < m; ++j) { + string b; cin >> b; + graph[M[b]].pb(M[a]); + } + } + + TopologicalSort ts(n); + vector<int> ans; + bool cycle = ts.run(ans); + + cout << "Teste " << cas << ende; + if (cycle) + cout << "impossivel" << ende; + else { + for (auto i : ans) + cout << v[i] << " "; + cout << ende; + } + cout << ende; + } + + return 0; +} diff --git a/contests/Cadernaveis/URI1860.cpp b/contests/Cadernaveis/URI1860.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75329ee0c266fc2f79a3ddd62dc2a68198f766b0 --- /dev/null +++ b/contests/Cadernaveis/URI1860.cpp @@ -0,0 +1,49 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; +using dd = pair<double,double>; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cout << setprecision(2) << fixed; + + int n; + double l = 0.0, r; + cin >> n >> r; + + vector<dd> v(n); + for (auto &i : v) cin >> i.fi >> i.se; + + double grtl, grtr; + for (int i = 0; i < 100; ++i) { + double lt = (r - l) / 3.0 + l; + double rt = ((r - l) * 2.0) / 3.0 + l; + + grtl = 0.0; for (auto j : v) grtl = max(grtl, hypot(j.fi - lt, j.se)); + grtr = 0.0; for (auto j : v) grtr = max(grtr, hypot(j.fi - rt, j.se)); + + if (grtl > grtr) l = lt; + else r = rt; + } + + cout << l << " " << grtl << ende; + return 0; +} diff --git a/contests/Cadernaveis/URI2305.cpp b/contests/Cadernaveis/URI2305.cpp new file mode 100644 index 0000000000000000000000000000000000000000..488b197c5e8454c6342829e5f0d77eeb62791a41 --- /dev/null +++ b/contests/Cadernaveis/URI2305.cpp @@ -0,0 +1,47 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int l, c, m, n; cin >> l >> c >> m >> n; + vector<vector<int>> mat(l, vector<int>(c)); + for (auto &i : mat) for (auto &j : i) cin >> j; + + vector<vector<int>> sum(l+1, vector<int>(c+1, 0)); + for (int i = 1; i <= l; ++i) + for (int j = 1; j <= c; ++j) + if (j == 1) sum[i][j] = mat[i-1][j-1]; + else sum[i][j] = sum[i][j-1] + mat[i-1][j-1]; + + for (int i = 2; i <= l; ++i) + for (int j = 1; j <= c; ++j) + sum[i][j] += sum[i-1][j]; + + int ans = 0; + for (int i = m; i <= l; ++i) + for (int j = n; j <= c; ++j) + ans = max(ans, sum[i][j] - sum[i-m][j] - sum[i][j-n] + sum[i-m][j-n]); + + cout << ans << ende; + return 0; +} diff --git a/contests/Cadernaveis/in b/contests/Cadernaveis/in new file mode 100644 index 0000000000000000000000000000000000000000..7fe77f6847af1c61e722fd515fb19ab9b3007682 --- /dev/null +++ b/contests/Cadernaveis/in @@ -0,0 +1,20 @@ +2 +0 0 0 0 6 9 8 3 0 +9 8 0 0 0 0 0 7 6 +6 0 0 0 3 8 0 5 1 +2 0 5 0 8 1 0 9 0 +0 6 0 0 0 0 0 8 0 +0 9 0 3 7 0 6 0 2 +3 4 0 8 5 0 0 0 9 +7 2 0 0 0 0 0 6 8 +0 5 6 9 2 0 0 0 0 + +0 0 0 0 6 9 8 3 0 +9 8 0 0 0 0 0 7 6 +6 0 0 0 3 8 0 5 1 +2 0 5 4 8 1 0 9 0 +0 6 0 0 0 0 0 8 0 +0 9 0 3 7 0 6 0 2 +3 4 0 8 5 0 0 0 9 +7 2 0 0 0 0 0 6 8 +0 5 6 9 2 0 0 0 0 diff --git a/gen_notebook b/gen_notebook index 7f12fa6d139bbeb276e5df7db9d5658f7efe4ee5..823f91269d02d17d4dc70984a8813e53ef999098 100755 --- a/gen_notebook +++ b/gen_notebook @@ -1,10 +1,15 @@ #!/bin/bash -#tex_file=$(mktemp) -python3 notebook/gen_latex.py # --header=notebook/header.tex --output=$tex_file +if [ "$1" = "DEBUG" ]; then + tex_file=result.tex + python3 notebook/gen_latex.py --header=notebook/header.tex --output=$tex_file +else + tex_file=$(mktemp) + python3 notebook/gen_latex.py --header=notebook/header.tex --output=$tex_file -#pdflatex $tex_file -output-directory . && -#pdflatex $tex_file -output-directory . + pdflatex $tex_file -output-directory . && + pdflatex $tex_file -output-directory . -#mv tmp.pdf caderno.pdf -#rm tmp* + mv tmp.pdf caderno.pdf + rm tmp* +fi diff --git a/misc/template.cpp b/misc/template.cpp index c4a76b363a6b6102a3955dd56a441a68df14d155..2f5418f9f3490d8a5947b9933c134c79cb1ee2e5 100644 --- a/misc/template.cpp +++ b/misc/template.cpp @@ -1,3 +1,4 @@ +/// Template #include <bits/stdc++.h> #define EPS 1e-6 diff --git a/notebook/gen_latex.py b/notebook/gen_latex.py index 9d0bb0807414c5bf86c77b2a6132ef3c3cd8a207..9b49dbe481c8b6ef99ead4e516d5fb74c39742fa 100644 --- a/notebook/gen_latex.py +++ b/notebook/gen_latex.py @@ -1,88 +1,123 @@ +import argparse from pathlib import Path class Tree: - - # Constructor. - # @param dirs list of directories to build Latex over def __init__(self, dirs): self.dirs = dirs; self.tree = {} self.build() - # Sorts the lists on tree leaves. - def sort(self, sub): - if type(sub) == list: - return sorted(sub) - else: - for i in sub: - sub[i] = self.sort(sub[i]) - return sub - # Builds a tree represented as dict, with structure of files # and directories. def build(self): for di in self.dirs: - path_list = Path(di).glob('**/*.cpp') + path_list = sorted(list(Path(di).glob('**/*.cpp'))) for path in path_list: branch = str(path).split('/') - curr = self.tree + node = self.tree for i in branch[:-2]: - if not i in curr: - curr[i] = {} - curr = curr[i] + if i not in node: + node[i] = {} + node = node[i] - if not branch[-2] in curr: - curr[branch[-2]] = [] - curr[branch[-2]].append(str(branch[-1])) - - self.tree = self.sort(self.tree) + if branch[-2] not in node: + node[branch[-2]] = [] + node[branch[-2]].append(str(branch[-1])) + # Allows access to tree dict from instance of class. def __getitem__(self, arg): return self.tree[arg] + # Allows iteration on tree dict from instance of class. def __iter__(self): return iter(self.tree) class LatexGenerator: - def __init__(self, tree): - # TODO: Create output file, add header and finish code parsing and latex + def __init__(self, tree, output, header): + self.output = open(output, 'w') + self.header = open(header, 'r') + self.tree = tree - self.hierarchy = ['chapter'] + [i*'sub' + 'section' for i in range(3)] + self.files = {} + self.hierarchy = ['part', 'chapter'] + [i*'sub' + 'section' for i in range(3)] + + self._add_header() self.gen_latex(tree) + self.output.write('\end{document}') + + # Adds Latex header to the output. + def _add_header(self): + lines = self.header.readlines() + for i in lines: + self.output.write(i) + self.output.write('\\newpage\n') + self.output.write('\n') # Prints section title in Latex format. - # TODO: Instead of parsing format, get name of section through header of - # the algorithm/problem file. Capitalization should be used only for - # directories. def gen_title_latex(self, content, depth): - text = ' '.join(list(map(lambda x : x.capitalize(), - content.split('.')[0].split('_')))) - print('\\' + self.hierarchy[depth] + '{' + text + '}') + self.output.write('\\' + self.hierarchy[depth] + '{' + content + '}' + '\n') # Prints code in Latex format. def gen_code_latex(self, path): - # TODO: Add code parsing funcion and code to latex generation - pass + # TODO: Add code parsing funcion to extract text + self.output.write('\\begin{lstlisting}[style=customcpp]\n') + + with open(path) as f: + lines = f.readlines() + for i in lines: + self.output.write(i) + + self.output.write('\\end{lstlisting}\n') + self.output.write('\n') # Generates Latex for entire tree recursively def gen_latex(self, sub, path = '', depth = 0): if type(sub) == list: for i in sub: - self.gen_title_latex(i, depth) + self.gen_title_latex(self._parse_file_name(path + i), depth) self.gen_code_latex(path + i) else: for i in sub: - self.gen_title_latex(i, depth) + self.gen_title_latex(self._parse_dir_name(i), depth) self.gen_latex(sub[i], path + i + '/', depth + 1) + #self.output.write('\\newpage\n') + self.output.write('\n') + + # Parses name of the section (capitalize) + def _parse_dir_name(self, name): + return ' '.join(list(map(lambda x : x.capitalize(), name.split('_')))) + + # Parses name of file by getting the first line of the source code. + def _parse_file_name(self, name): + with open(name) as f: + result = f.readline() + return result[4:-1] + + +def get_args(): + parser = argparse.ArgumentParser() + + parser.add_argument('--header', + action='store', + type=str, + help='Header of Latex file') + + parser.add_argument('--output', + action='store', + type=str, + help='Output Latex file') + + return parser.parse_args() def main(): - dirs = [ 'algorithms', 'misc' ] - tree = Tree(dirs) - tex = LatexGenerator(tree) + args = get_args() + + tree = Tree(['algorithms', 'misc']) + tex = LatexGenerator(tree, args.output, args.header) if __name__ == "__main__": main() diff --git a/notebook/header.tex b/notebook/header.tex index 655b998d10210e59a72a90db29b1170b22b82141..6310e5968c9293308de90ebb040970f334030bc8 100644 --- a/notebook/header.tex +++ b/notebook/header.tex @@ -1,4 +1,4 @@ -\documentclass{article} +\documentclass[oneside]{book} \usepackage{listings} \usepackage{titlesec} \usepackage[a4paper, total={6in, 8in}]{geometry}