add old cpp files

This commit is contained in:
Julin S 2023-05-02 15:17:56 +05:30
parent 081f13ab50
commit 068325ceff
13 changed files with 417 additions and 0 deletions

29
bash/z-eyes.sh Executable file
View File

@ -0,0 +1,29 @@
#!/bin/bash
# got to redirect stderr of zenity to /dev/null. Otherwise error message about lack of parent window will show up in the terminal
rest_time=20m
distant_stare_time=20
function show_progress_bar {
for i in {0..100..5} # equiv of 20s because 1s per iterations as there are 20 iterations for this loop.
do
echo $i
sleep $(( $distant_stare_time / 20 ))
done |
zenity --title='z-eyes' --text='Look 20 feet away for 20 seconds' --progress --auto-close 2> /dev/null
if [ "$?" -eq 0 ] # Didn't cancel
then
zenity --notification --text "20 seconds is up." 2> /dev/null
else # Cancelled
zenity --notification --text "20 seconds was not completed." 2> /dev/null
fi
}
while true
do
sleep "$rest_time" && zenity --info --text "Your eyes are tired. Rest?" --title="z-eyes" 2> /dev/null
show_progress_bar
done

3
cpp/README.md Normal file
View File

@ -0,0 +1,3 @@
## Some C++ sample programs
To get familiar with some STL.

42
cpp/deque.cpp Normal file
View File

@ -0,0 +1,42 @@
/*
Reference: https://en.cppreference.com/w/cpp/container/deque
Tried on: g++ 7.5.0
Double ended queue.
Pros:
Easy insertion/deletion at front/back.
Supports indexing (random access).
Has all methods of queue (NOT SURE)
*/
#include<iostream>
#include<deque>
void print_dq(std::deque<int> dq) {
std::cout << "Deque is: ";
for(auto elem: dq) {
std::cout << elem << ",";
}
std::cout << "\n";
}
int main() {
std::deque<int> intdq = {10, 20, 30, 40, 50};
print_dq(intdq);
intdq.push_back(60);
print_dq(intdq);
intdq.push_front(5);
print_dq(intdq);
intdq.pop_front();
print_dq(intdq);
intdq.pop_back();
print_dq(intdq);
std::cout << "intdq[2]: " << intdq[2] << "\n";
}

31
cpp/exceptions.cpp Normal file
View File

@ -0,0 +1,31 @@
/*
Reference: https://en.cppreference.com/w/cpp/error
Tried on: g++ 7.5.0
*/
#include<iostream>
#include<vector>
int divide(int a, int b) {
if(b == 0) {
throw std::runtime_error("Divide by zero!");
}
return a / b;
}
int main() {
try {
std::cout << divide(40, 0) << "\n";
} catch(std::runtime_error &e) {
std::cout << e.what() << "\n";
}
std::vector<int> vec = {1, 2};
try {
std::cout << vec.at(2) << "\n";
} catch(std::out_of_range &e) {
std::cout << e.what() << "\n";
}
std::cout << vec[2] << "\n"; // [] operator doesn't throw exception
// but at() does
}

16
cpp/files.cpp Normal file
View File

@ -0,0 +1,16 @@
/*
Reference: https://en.cppreference.com/w/cpp/container/vector
Tried on: g++ 7.5.0
*/
#include<iostream>
#include<fstream>
int main() {
int x, y;
std::ifstream fin("input.txt");
fin >> x >> y >> y;
std::cout << x << ", " << y << "\n";
}

73
cpp/list.cpp Normal file
View File

@ -0,0 +1,73 @@
/*
Reference: https://en.cppreference.com/w/cpp/container/list
Tried on: g++ 7.5.0
LIke a singly linked list.
Pros:
Easy insertion/deletion at front/back.
Sorting possible.
Cons:
No random access. Got to iterate to access required indices.
*/
#include<iostream>
#include<list>
void print_list(std::list<int> lst) {
std::cout << "List is:";
for(auto elem: lst) {
std::cout << elem <<",";
}
std::cout << "\n";
}
int main() {
std::list<int> intlist = {1, 2};
print_list(intlist);
// Insert at the end of the list
intlist.push_back(3);
print_list(intlist);
// Insert at the begining
intlist.push_front(0);
print_list(intlist);
std::cout << "Current size: " << intlist.size() << "\n";
//std::cout << "Max size: " << intlist.max_size() << "\n";
std::cout << "First elem (font): " << intlist.front() << "\n";
std::cout << "Last elem (back): " << intlist.back() << "\n";
// Accessing elements
std::list<int>::iterator itr = intlist.begin(); // iterator from beginning
// of list
std::advance(itr, 2); // advance iterator by two positions
std::cout << *itr << "\n";
++itr; // advance itertor by 1
std::cout << *itr << "\n";
print_list(intlist);
// Modifying elements
*itr = 33;
print_list(intlist);
++itr;
*itr = 4; // No effect. Outside the list now. Iteration over.
print_list(intlist);
//Remove next to the last element
//std::list<int>::reverse_iterator ritr = intlist.rend();
itr = intlist.begin();
itr++; // Now points to next to the last element
intlist.erase(itr); // Remove 1 from index 1
print_list(intlist);
// Reverse list inplace
intlist.reverse();
print_list(intlist);
}

32
cpp/queue.cpp Normal file
View File

@ -0,0 +1,32 @@
/*
Reference: https://en.cppreference.com/w/cpp/container/queue
Tried on: g++ 7.5.0
Pros:
Easy insertion at back.
Easy deletion at front.
Easy access front/back.
Cons:
Printing not easy.
No easy initialization from array (NOT SURE)
*/
#include<iostream>
#include<queue>
int main() {
std::queue<int> intq;
// Push
intq.push(60);
intq.push(70);
intq.push(80);
std::cout << "Size: " << intq.size() << "\n";
std::cout << "Front: " << intq.front() << "\n";
std::cout << "Back: " << intq.back() << "\n";
// Pop
intq.pop();
std::cout << "Front: " << intq.front() << "\n";
}

19
cpp/reading_input.cpp Normal file
View File

@ -0,0 +1,19 @@
/*
Reference: https://en.cppreference.com/w/cpp/io/cin
https://en.cppreference.com/w/cpp/string/basic_string/getline
Tried on: g++ 7.5.0
*/
#include<iostream>
#include<string>
int main() {
std::string msg;
std::getline(std::cin, msg);
std::cout << msg << "\n";
std::cin >> msg; // only read till first white space
std::cout << msg << "\n";
}

27
cpp/set.cpp Normal file
View File

@ -0,0 +1,27 @@
/*
Reference: https://en.cppreference.com/w/cpp/container/set
Tried on: g++ 7.5.0
*/
#include<iostream>
#include<set>
void print_set(std::set<int> st) {
std::cout << "Set is: ";
for(auto elem: st) {
std::cout << elem << ",";
}
std::cout << "\n";
}
int main() {
std::set<int> intset = {10, 20, 30, 40, 50};
print_set(intset);
// Check if set is empty
std::cout << intset.empty() << "\n";
// Set size
std::cout << intset.size() << "\n";
}

34
cpp/strings.cpp Normal file
View File

@ -0,0 +1,34 @@
/*
Reference: https://en.cppreference.com/w/cpp/string
Tried on: g++ 7.5.0
*/
#include<iostream>
#include<string>
int main() {
std::basic_string<char> str = "hello";
std::cout << str << "\n";
// Char-by-char access
std::cout << str[1] << "\n";
std::cout << str.at(2) << "\n";
// Length
std::cout << str.length() << "\n";
std::cout << str.size() << "\n";
// Appending
str += " wo";
std::cout << str << "\n";
str.append("rld");
std::cout << str << "\n";
// String to int
str = "234";
std::cout << std::stoi(str) << "\n";
// String to float
str += ".5";
std::cout << std::stof(str) << "\n";
}

62
cpp/vector.cpp Normal file
View File

@ -0,0 +1,62 @@
/*
Reference: https://en.cppreference.com/w/cpp/container/vector
Tried on: g++ 7.5.0
Like an array.
Pros:
Supports indexing (random access)
Easy insertion or deletion at back.
Cons:
No sorting.
*/
#include<iostream>
#include<vector>
void print_vector(std::vector<int> vec) {
std::cout << "Vector is: ";
for(auto elem: vec) {
std::cout << elem << ",";
}
std::cout << "\n";
}
int main() {
std::vector<int> intvec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
print_vector(intvec);
//Size
std::cout << "Size: " << intvec.size() << "\n";
std::cout << "Capacity: " << intvec.capacity() << "\n";
//std::cout << "Max size: " << intvec.max_size() << "\n";
// Random access
intvec[2] = 33;
print_vector(intvec);
std::cout << "intvec[1]: " << intvec[1] << "\n";
std::cout << "intvec.at(1): " << intvec.at(1) << "\n";
// Insert
//std::vector<int>::const_iterator itr = intvec.cbegin();
std::vector<int>::iterator itr = intvec.begin();
intvec.insert(itr + 1, 132);
print_vector(intvec);
// Push back
intvec.push_back(100);
print_vector(intvec);
// Pop back (pop_back() returns void)
intvec.pop_back();
print_vector(intvec);
// Exchange (swap) contents of two vectors
std::vector<int> othervec = {11, 22};
othervec.swap(intvec);
std::cout << "intvec:-";
print_vector(intvec);
std::cout << "othervec:-";
print_vector(othervec);
}

26
python/keylog.py Normal file
View File

@ -0,0 +1,26 @@
# For now, ignore specials.
from pynput.keyboard import Key, Listener
def on_press(key):
#print(f"{key} pressed")
#key = ord(key.char)
#print(f"{key:x}")
try:
#print(f"{key.char},{key} up")
print(f"{key:x}")
except ValueError:
#print(f"special key {key} up")
pass
def on_release(key):
print('{0} release'.format(key))
#if key == Key.esc:
# Stop listener
# return False
# Collect events until released
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()

23
python/sympy-hello.py Normal file
View File

@ -0,0 +1,23 @@
"""
: |
: &
¬: ~
: >>
: <<
True: sympy.S.true
False: sympy.S.false
XOR: ^
NAND: Nand()
NOR: Nor()
Equivalence: Equivalent()
"""
import sympy
a, b = sympy.symbols("a,b")
# = sympy.logic.boolalg.Boolean.__or__