Skip to content

Instantly share code, notes, and snippets.

View potat-dev's full-sized avatar
🥔
Котик крутится, лабы мутятся

Denis Churilov potat-dev

🥔
Котик крутится, лабы мутятся
View GitHub Profile
@potat-dev
potat-dev / is_prime.cpp
Created February 13, 2022 02:20
Primality test in C++
int is_prime(int n) {
if (n < 3) return (n == 2);
if (~n & 1) return 0;
for (int i = 3; i*i <= n; i += 2)
if (n % i == 0) return 0;
return 1;
}
@potat-dev
potat-dev / smart_rename.py
Created February 10, 2022 21:07
Функция "Умного переименования" файлов
def smart_rename(file):
# renames a file by adding a numeric index to it
# increments the index if the directory already has files with the index
regex = r'(.+)_\((\d+)\)\.(.+)' # filename_(0).ext
make_filename = lambda p: p[0] + f"_({p[1]})." + p[2]
filename, path = os.path.basename(file), os.path.dirname(file)
match = match_regex(regex, filename)
parts = list(match.groups())[::2] if match else filename.split('.')
parts.insert(1, int(match.group(2)) if match else 0)
new_path = os.path.join(path, make_filename(parts))