Skip to content

Instantly share code, notes, and snippets.

@hsiuhsiu
hsiuhsiu / sort_by_time.py
Last active January 9, 2024 07:50
sort bean count entries by time
"""Sort Beancount Entries by meta['time']
Although beancount says that time is meaningless, there are situations
where an account that should not be negative may have a negative balance
at some point because transfers that occurred on the same day are
scheduled later and spending records are scheduled earlier.
This is not a big deal, but it is a bit odd.
2021-10-01 balance Assets:Cash 500 USD
@hsiuhsiu
hsiuhsiu / remove_if.cpp
Last active October 5, 2021 04:54
Remove elements in a vector #cpp
auto p = std::remove_if(
std::begin(a), std::end(a), [](const auto& e) { return e.seleted(); });
a.erase(p, std::end(a))
// in C++20
auto p = ranges::remove_if(a, &E::selected);
a.erase(p, std::end(a))
@hsiuhsiu
hsiuhsiu / array_to_tuple.cpp
Last active September 7, 2020 16:33
C++ Tricks
// base case
template <class... Types, size_t n, size_t... indices>
std::tuple<Types...> toAnyTuple(
std::array<std::any, n> const& arr,
std::index_sequence<indices...>) {
return std::make_tuple(std::any_cast<Types&>(arr[indices])...);
}
// general case
template <
class... Types,
@hsiuhsiu
hsiuhsiu / color.sh
Last active March 21, 2019 20:20
print 256 colors in terminal
#!/bin/bash
effs=(0 1 2 7 9)
effs2=(0 1 2 3 4 5 6 8)
for eff in "${effs[@]}" ; do
for i in {30..37} ; do
for j in {40..47} ; do
ansi_eff="${eff};${i};${j}m"
printf "\033[${ansi_eff} ${ansi_eff} \033[0m"
@hsiuhsiu
hsiuhsiu / ifstream.cpp
Created December 19, 2018 16:37
[Read Files] files handling related snippet #file
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main(int argc, char *argv[]) {
string input_filename = argv[1];
ifstream input_stream;
input_stream.open(input_filename);
@hsiuhsiu
hsiuhsiu / fft.cpp
Last active April 18, 2024 23:08
[FFT] Fast Fourier Transform in c++ and basic applications #Algorithm #ACM
#include <complex>
#include <iostream>
#include <valarray>
#include <vector>
using namespace std;
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;