Skip to content

Commit 55ee91f

Browse files
committed
Updated version
1 parent c44a7f8 commit 55ee91f

22 files changed

+674
-24
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
mkcpr-config.json
2-
Output.tex
32
Template.tex
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int main() {
2+
ios state(nullptr);
3+
state.copyfmt(cout); //Saves current format state
4+
double D = 13.34567;
5+
cout << setprecision(4) << D << endl // 13.35
6+
<< fixed << D << endl; // 13.3457
7+
cout.copyfmt(state); //Restores format
8+
int N = 13;
9+
cout << setw(4) << N << endl //" 13"
10+
<< setfill('0') << N << endl //"0013"
11+
<< left << N << endl // "1300"
12+
<< right << N << endl // "0013"
13+
<< hex << N << endl // "000d"
14+
<< uppercase << N << endl // "000D"
15+
<< nouppercase << N << endl // "000d"
16+
<< oct << N << endl // "0015"
17+
<< dec << N << endl; // "0013"
18+
}

CodeFolder/C++/Permutations.cpp renamed to CodeFolder/Basics/C++/Permutations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define Dt int //Data type int long long int string ...
1+
#define Dt int //Datatype int,long long,string,etc.
22
#define T vector<Dt>;
33

44
vector<T> permutations(T v) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct Object {
2+
int x, y;
3+
};
4+
5+
int main() {
6+
// Comparison function cmp for objects
7+
auto cmp = [](const Object &a, const Object &b) {
8+
return a.x > b.x;
9+
};
10+
// Comparison function cmpd for datatypes
11+
auto cmpd = [](const int &a, const int &b) {
12+
return a > b;
13+
};
14+
// For object
15+
priority_queue<Object, vector<Object>,
16+
decltype(cmp)> pq(cmp);
17+
// For datatypes
18+
priority_queue<int, vector<int>,
19+
decltype(cmpd)> pq(cmpd);
20+
}

CodeFolder/Basics/C++/Random.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mt19937_64 seed(chrono::steady_clock::now()
2+
.time_since_epoch()
3+
.count());
4+
5+
int random(int min, int max) { // [min, max]
6+
return uniform_int_distribution<int>(min,
7+
max)(seed);
8+
}
9+
10+
double random(double min, double max) { // [min, max)
11+
return uniform_real_distribution<double>(min,
12+
max)(seed);
13+
}

CodeFolder/Basics/C++/Read Line.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// if mixing 'cin' with 'getline', remember to put 'ignore' between 'cin' and 'getline' in that order
2+
3+
int main(){
4+
string s;
5+
getline(cin, s);
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct Object {
2+
int x, y;
3+
};
4+
5+
int main() {
6+
// Comparison function cmp for objects
7+
auto cmp = [](const Object &a, const Object &b) {
8+
return a.x > b.x;
9+
};
10+
// Comparison function cmpd for datatypes
11+
auto cmpd = [](const int &a, const int &b) {
12+
return a < b;
13+
};
14+
//For object
15+
set<Object, decltype(cmp)> pq(cmp);
16+
//For datatypes
17+
set<Object, decltype(cmpd)> pq(cmpd);
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct Object {
2+
char first;
3+
int second;
4+
};
5+
6+
bool cmp(const Object &a, const Object &b) {
7+
return a.second > b.second;
8+
}
9+
10+
int main() {
11+
vector<Object> v = {{'c', 3}, {'a', 1}, {'b', 2}};
12+
sort(v.begin(), v.end(), cmp);
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
bool sortbysec(const pair<int,int> &a,
2+
const pair<int,int> &b) {
3+
return a.second < b.second;
4+
}
5+
6+
vector<pair<int, int>> pairs;
7+
//Sort by first element
8+
sort(pairs.begin(), pairs.end());
9+
//Sort by second element
10+
sort(pairs.begin(), pairs.end(), sortbysec);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vector<string> split(string str, char token) {
2+
stringstream ss(str);
3+
vector<string> v;
4+
while (getline(ss, str, token)) v.push_back(str);
5+
return v;
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
int main() {
2+
// String to Int
3+
int n = stoi("123") + 1;
4+
cout << n << endl; // output: 124
5+
// stoll for long long int
6+
// stoull for unsigned long int
7+
// stod for double
8+
// stold for long double
9+
10+
// Int to String
11+
// to_string converts int, double, long long int, etc. to string
12+
string str = "str+" + to_string(123 + 1);
13+
cout << str << endl; // output: str+124
14+
return 0;
15+
}

CodeFolder/Basics/C++/Substring.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// [l, r)
2+
string substr(string &s, int l, int r) {
3+
return string(s.begin() + l, s.begin() + r);
4+
}

CodeFolder/C++/Template.cpp renamed to CodeFolder/Basics/C++/Template.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// 26
12
#include <bits/stdc++.h> //Include all c++ libraries
23
using namespace std;
34
#define io_op ios_base::sync_with_stdio(false);cin.tie(NULL); //IO Optimation
@@ -18,13 +19,13 @@ using namespace std;
1819
#define p_b push_back
1920
#define all(v) v.begin(), v.end()
2021
#define alla(arre, size) arre, arre + size
21-
#define forv(v, i) for(int i=0; i<v.size(); i++) //Vector for
22-
#define rforv(v, i) for(int i=v.size()-1; i>-1; i--) //Reverse vector for
23-
#define forx(x, i) for(int i=0; i<x; i++) //Num for
22+
#define forv(v, i) for(int i=0; i<v.size(); i++) //For Vector
23+
#define rforv(v, i) for(int i=v.size()-1; i>-1; i--) //For Reverse vector
24+
#define forx(x, i) for(int i=0; i<x; i++) //For Number
2425
#define showv(v, i) for(auto i:v) cout << i << ' '; //Display vector
2526
#define npermute next_permutation
2627
#define ppermute prev_permutation
27-
28+
// 4
2829
int main(){
2930
io_op
3031
return 0;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import itertools
2+
# from arr choose k = > combinations(arr, k)
3+
# example arr=[1, 2, 3] and k=3
4+
print(list(itertools.combinations([1, 2, 3], 3)))

CodeFolder/Basics/Python/Fast IO.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from sys import stdin, stdout
2+
3+
N = 10
4+
# Reads N chars from stdin(it counts '\n' as char)
5+
stdin.read(N)
6+
# Reads until '\n' or EOF
7+
line = stdin.readline()
8+
# Reads all lines in stdin until EOF
9+
lines = stdin.readlines()
10+
# Writes a string to stdout, it doesn't add '\n'
11+
stdout.write(line)
12+
# Writes a list of strings to stdout
13+
stdout.writelines(lines)
14+
# Reads numbers separated by space in a line
15+
numbers = list(map(int, stdin.readline().split()))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import itertools
2+
# All permutations of an arr
3+
# Example arr=[1, 2, 3]
4+
print(list(itertools.permutations([1, 2, 3])))

CodeFolder/Basics/Python/Random.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import random
2+
# Initialize the random number generator.
3+
random.seed(None)
4+
# Returns a random integer N such that a <= N <= b
5+
random.randint(a, b)
6+
# Returns a random integer N such that 0 <= N < b
7+
random.randrange(b)
8+
# Returns a random integer N such that a <= N < b
9+
random.randrange(a, b)
10+
# Returns an integer N with k random bits.
11+
random.getrandbits(k)
12+
# Shuffles a list
13+
random.shuffle(li)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 5
2+
class MyObject :
3+
def __init__(self, first, second, third):
4+
self.first = first
5+
self.second = second
6+
self.third = third
7+
8+
li = [MyObject('b', 3, 1), MyObject('a', 3, 2), MyObject('b', 3, 3)]
9+
# returns list sorted by first then by second then by third in increasing order
10+
ol = sorted(li, key = lambda x: (x.first, x.second, x.third), reverse=False)
11+
# sorts inplace by first then by second then by third in increasing order
12+
li.sort(key = lambda x: (x.first, x.second, x.third), reverse=False)

CodeFolder/Basics/Python/Sort List.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
li = ['a', 'c', 'b']
2+
# sorts inplace in descending order
3+
li.sort(reverse=True)
4+
# returns sorted list ascending order
5+
ol = sorted(li)

CodeFolder/C++/Output Format.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

Reference.pdf

64.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)