├── .gitignore ├── LICENSE.md ├── README.md ├── divsion.py ├── linear_time_selec.cpp ├── permutations.cpp ├── rpn_parser ├── Eval.java ├── Parser.java └── Tokenizer.java ├── subsets.cpp └── variations.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | linear 3 | generics 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the "Software"), 5 | to deal in the Software without restriction, including without limitation the 6 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 8 | 9 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #### Fundamental Algorithms Series,C++, Python and Java, so far. 2 | More here: https://lion137.blogspot.com/2019/01/fundamental-algorithms.html 3 | - Order Statistics; 4 | - Polynomial Division; 5 | - Permuatations; 6 | - Subsets; 7 | - Variations With Repetitions; 8 | - Parser to Reverse Polish Notation and Eval RPN Expression; 9 | working code, also here: https://github.com/lion137/Java-Numeric-Calculator 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /divsion.py: -------------------------------------------------------------------------------- 1 | # polynomial division, modulo and gc 2 | 3 | 4 | # input: coefficients of polynomials as a lists u, v != 0 5 | # in order u[0] = free coefficient of u(x), .... 6 | # returns polynomials q, r such that u = vq + r 7 | # length u >= length v >= 0 8 | def poly_divide(u, v): 9 | m = len(u) - 1 10 | n = len(v) - 1 11 | q = [0] * (m - n + 1) 12 | lim = m - n 13 | for k in range(lim, -1, -1): 14 | q[k] = u[n + k] / v[n] 15 | for j in range(n + k - 1, k - 1, -1): 16 | u[j] = u[j] - q[k] * v[j - k] 17 | return q, u[:n] 18 | 19 | 20 | # polynomial u modulo v 21 | def poly_mod(u, v): 22 | return poly_divide(u, v)[1] 23 | 24 | def poly_gcd(u, v): 25 | if not any(v): return u 26 | else: return poly_gcd(v, poly_mod(u, v)) 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /linear_time_selec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int partition(int a[], int s, int p); 6 | int partition_random(int a[], int s, int p); 7 | int quick_select(int a[], int s, int n, int k); 8 | float median(int a [], int n); 9 | void printAr(int a[], int n); 10 | void quicksort(int a[], int s, int n); 11 | 12 | int main() { 13 | int A[8] = {1, 10, -9, 801, -388, 137, 123, -9}; 14 | printAr(A, 8); 15 | int s = 8; 16 | for (int i = 1; i < 9;i++){ 17 | int e = quick_select(A, 0, 8, i); 18 | std::cout << i <<"th smallest:"<< e << " \n"; 19 | } 20 | std::cout << median(A, 8) << " <- median"; 21 | std::cout << "\n"; 22 | quicksort(A, 0, 8); 23 | printAr(A, 8); 24 | std::cout << "\n"; 25 | return 0; 26 | } 27 | 28 | int partition(int a[], int p, int r) { 29 | int t = a[r]; 30 | int i = p - 1; 31 | for (int j = p; j < r;j++){ 32 | if (a[j] <= t) { 33 | i += 1; 34 | std::swap(a[i], a[j]); 35 | } 36 | } 37 | std::swap(a[i + 1], a[r]); 38 | return i + 1; 39 | } 40 | 41 | int quick_select(int a[], int s, int n, int k) { 42 | int r = partition_random(a, s, n); 43 | if (r - s == k - 1) {return a[r];} 44 | else { 45 | 46 | if (k - 1 < r - s) { 47 | return quick_select(a, s, r - 1, k); 48 | } 49 | else { 50 | return quick_select(a, r + 1 , n, k - r + s - 1); 51 | } 52 | } 53 | } 54 | 55 | int partition_random(int a[], int p, int r) { 56 | std::random_device rd; 57 | std::mt19937 rng(rd()); 58 | std::uniform_int_distribution uni(p, r); 59 | auto rn = uni(rng); 60 | std::swap(a[r], a[rn]); 61 | return partition(a, p, r); 62 | } 63 | 64 | float median(int a [], int n) { 65 | if (n % 2 == 1) 66 | return quick_select(a, 0, n - 1, n / 2 + 1); 67 | int l = n / 2; 68 | int r = n / 2 + 1; 69 | return (1.0 * (quick_select(a, 0, n - 1, l) + 70 | quick_select(a, 0, n - 1, r))) / 2; 71 | 72 | } 73 | 74 | void quicksort(int a[], int s, int n) { 75 | if (s < n) { 76 | int r = partition_random(a, s, n); 77 | quicksort(a, s, r - 1); 78 | quicksort(a, r + 1, n); 79 | } 80 | } 81 | 82 | void printAr(int a[], int n) { 83 | for (int i = 0; i < n; i++) 84 | std::cout << a[i] << " "; 85 | std::cout << "\n"; 86 | } 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /permutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void printList(vector v); 7 | vector remainder(vector a, int i); 8 | vector append(vector a, int elem); 9 | 10 | //show all the permuatations of the vector r, s = {} 11 | void recPermutation(vector s, vector r) { 12 | if (r.empty()) { 13 | printList(s); 14 | } 15 | else { 16 | for (int i = 0; i < r.size(); i++) { 17 | vector n = append(s, r[i]); 18 | vector rem = remainder(r, i); 19 | recPermutation(n, rem); 20 | } 21 | } 22 | } 23 | 24 | // wrapper function, shows all the permuatations of a 25 | void showPermutations(vector a) { 26 | vector e = {}; 27 | recPermutation(e, a) 28 | } 29 | 30 | int main() { 31 | showPermutations(vector{1, 2, 3}); 32 | /* -> 33 | 1 2 3 34 | 1 3 2 35 | 2 1 3 36 | 2 3 1 37 | 3 1 2 38 | 3 2 1 39 | */ 40 | return 0; 41 | } 42 | 43 | vector remainder(vector a, int i) { 44 | vector tmp; 45 | for (int k = 0; k < a.size(); k++) { 46 | if (k != i) 47 | tmp.push_back(a[k]); 48 | } 49 | return tmp; 50 | 51 | } 52 | 53 | vector append(vector a, int elem) { 54 | vector tmp; 55 | a.push_back(elem); 56 | tmp = a; 57 | return tmp; 58 | } 59 | 60 | void printList(vector v) { 61 | for (auto & e : v) 62 | cout << e << " "; 63 | cout <<"\n"; 64 | } 65 | -------------------------------------------------------------------------------- /rpn_parser/Eval.java: -------------------------------------------------------------------------------- 1 | package rpn; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | 8 | public class Eval { 9 | public BigDecimal evalPostfix(List expList){ 10 | //operands stack 11 | ArrayList operStack = new ArrayList(); 12 | String num = "(\\d+\\.\\d+)|(\\d+\\.)|(\\d+)"; //Floating point 13 | for (String token: expList){ 14 | if (token.matches(num)){ 15 | BigDecimal b1 = new BigDecimal(token); 16 | operStack.add(0, b1); 17 | } 18 | else { 19 | BigDecimal oper2 = operStack.remove(0); 20 | BigDecimal oper1 = operStack.remove(0); 21 | BigDecimal res = compute(token, oper1, oper2); 22 | operStack.add(0, res); 23 | } 24 | } 25 | return operStack.remove(0); 26 | } 27 | private BigDecimal compute(String oper, BigDecimal op1, BigDecimal op2){ 28 | if (oper.equals("/")){ 29 | return op1.divide(op2); 30 | } 31 | else if (oper.equals("*")){ 32 | return op1.multiply(op2); 33 | } 34 | else if (oper.equals("+")){ 35 | return op1.add(op2); 36 | } 37 | else { 38 | return op1.subtract(op2); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /rpn_parser/Parser.java: -------------------------------------------------------------------------------- 1 | package rpn; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | 7 | 8 | public class Parser { 9 | public List infixToPostfix(List tokens){ 10 | /* Takes an infix expression as a list of tokens and return post 11 | fix expression (as a list): 12 | (A + B) - (C * D) -> A B + C D * - 13 | */ 14 | // Regular objects to match the tokens. 15 | String num = "(\\d+\\.\\d+)|(\\d+\\.)|(\\d+)"; //Floating point 16 | String oper = "\\+|-|\\*|\\/|"; //numbers 17 | HashMap prec = new HashMap(); 18 | // Symbol table with operators precedence. 19 | prec.put("*", 3); 20 | prec.put("/", 3); 21 | prec.put("+", 2); 22 | prec.put("-", 2); 23 | prec.put("(", 1); 24 | ArrayList postfixL = new ArrayList<>(); 25 | ArrayList operStack = new ArrayList<>();//Operators 26 | for (String token: tokens){ 27 | if ( (token.matches(num))){ 28 | postfixL.add(token); 29 | } 30 | else if (token.equals("(")) { 31 | operStack.add(0, token); 32 | } 33 | else if (token.equals(")")){ 34 | String top = operStack.remove(0); 35 | while ( !(top.equals("(")) ) { 36 | postfixL.add(top); 37 | top = operStack.remove(0); 38 | } 39 | } 40 | else { 41 | while ( !(operStack.isEmpty()) && 42 | (prec.get(operStack.get(0))) >= prec.get(token)) { 43 | postfixL.add(operStack.remove(0)); 44 | } 45 | operStack.add(0, token); 46 | } 47 | } 48 | while (!(operStack.isEmpty())){ 49 | postfixL.add(operStack.remove(0)); 50 | } 51 | return postfixL; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /rpn_parser/Tokenizer.java: -------------------------------------------------------------------------------- 1 | package rpn; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class Tokenizer { 7 | public List tokenize(String exp){ 8 | /*Take a string expression and returns a list of tokens; ex.: 9 | "(A + B -1)/ 3. 5" -> [(. A, +, B, -, 1, ), /, 3.5] 10 | */ 11 | exp = exp.replace(" ", ""); 12 | exp = exp.replace("+", " + ").replace("-", " - ").replace("/", " / "); 13 | exp = exp.replace("*", " * "); 14 | exp = exp.replace("(", " ( ").replace(")", " ) ").trim(); 15 | List exp_list = Arrays.asList(exp.split("\\s+")); 16 | return exp_list; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /subsets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | //find subsets of the set of n - 1 elements 7 | void subsets(int k, int n, vector s) { 8 | if (k == n) { 9 | for (auto & a : s) 10 | cout << a << " "; 11 | cout << "\n"; 12 | } else { 13 | subsets(k + 1, n, s); 14 | s.push_back(k); 15 | subsets(k + 1, n, s); 16 | s.pop_back(); 17 | } 18 | } 19 | 20 | 21 | int main() { 22 | vector p; 23 | subsets(0, 2, p); // -> 1, 0, 0 1, {} 24 | cout << "{}\n"; 25 | cout << "\n"; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /variations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | 8 | //find all the variations with repetitions of the subset of n - 1 elements 9 | void variations(int n, vector p, vector c) { 10 | if (p.size() == (size_t) n) { 11 | for (auto & e : p) 12 | cout << e << " "; 13 | cout << "\n"; 14 | } else { 15 | for (int i = 0; i < n; i++) { 16 | if (c[i]) 17 | continue; 18 | c[i] == true; 19 | p.push_back(i); 20 | variations(n, p, c); 21 | c[i] = false; 22 | p.pop_back(); 23 | } 24 | } 25 | } 26 | 27 | 28 | int main() { 29 | vector p; 30 | vector c(2); 31 | variations(2, p, c); // => 0 0, 0 1, 1 0, 1 1 32 | cout << "\n"; 33 | return 0; 34 | } 35 | --------------------------------------------------------------------------------