import sqlite3
import os
import sys
import random
import time
from datetime import datetime
TIMEOUT_DELAY=10
CURRENT_DIRECTORY = os.path.normpath(
os.path.relpath(
os.path.dirname(__file__),
os.getcwd())
)
DB_PATH = os.path.normpath(os.path.join(CURRENT_DIRECTORY, './db/lists.db'))
def initBase():
with sqlite3.connect(DB_PATH) as connect:
cursor = connect.cursor()
cursor.execute("""
CREATE TABLE lists(
userid INTEGER primary key,
value INTEGER
)
""")
connect.commit()
if not os.path.exists(DB_PATH):
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
initBase()
ID = None
if len(sys.argv) > 1:
ID = int(sys.argv[1])
else:
ID = int(datetime.now().timestamp() * 1000) % 10000 + 10000
print(f'ID не передан. сгенерирован идентефикатор {ID}')
while True:
with sqlite3.connect(DB_PATH, timeout=TIMEOUT_DELAY) as connect:
cursor = connect.cursor()
cursor.execute("""SELECT userid, value FROM lists""")
print(cursor.fetchall())
x = random.randint(100, 999)
cursor.execute("""
INSERT INTO lists(value, userid)
VALUES (?, ?)
ON CONFLICT(userid) DO UPDATE SET
value=?
WHERE userid=?
""", [x, ID]*2)
connect.commit()
time.sleep(1)
import os
import re
from collections import Counter
PROJECT_DIRECTORY = '.'
ignoreName=set(['__pycache__', '.git', '.gitignore', 'README.md'])
ignoreExt=set(['wav', 'jpg', 'png'])
CURRENT_DIRECTORY = os.path.dirname(__file__)
splitToLiteralsReg = re.compile(r"[^a-zA-Zа-яА-Я0-9\_]+")
target_path = os.path.normpath(os.path.relpath(os.path.join(CURRENT_DIRECTORY, PROJECT_DIRECTORY), os.getcwd()))
def shouldIgnore(root, name):
if name in ignoreName:
return True
if name.split('.')[-1] in ignoreExt:
return True
return False
counter = Counter()
for root, dirs, files in os.walk(target_path):
dirs[:] = [
foldername for foldername in dirs
if not shouldIgnore(root, foldername)
]
for filename in files:
if not shouldIgnore(root, filename) :
fullpath = os.path.join(root, filename)
print('чтение', fullpath)
with open(fullpath) as file:
for line in file:
#counter.update(line.split(' '))
counter.update(filter(lambda literal: literal, splitToLiteralsReg.split(line)))
index = 0
for literal, count in counter.most_common():
index+=1
print(f'{index}. "{literal}" {count} повторений')
const fs = require('fs')
const path = require('path')
const PROJECT_DIRECTORY = '.'
const ignoreName=new Set(['__pycache__', '.git', '.gitignore', 'README.md'])
const ignoreExt=new Set(['wav', 'jpg', 'png'])
const CURRENT_DIRECTORY = __dirname
const splitToLiteralsReg = /[^a-zA-Zа-яА-Я0-9\_]+/gim
const target_path = path.join(CURRENT_DIRECTORY, PROJECT_DIRECTORY)
function shouldIgnore(root, name){
if(ignoreName.has(name))
return true
if(ignoreExt.has(name.split('.').slice(-1)[0]))
return true
return false
}
function Counter(){
this.literals = {}
}
Counter.prototype.learnLiteral = function learnLiteral(literal){
this.literals[literal] = 1 + (this.literals[literal] || 0)
}
Counter.prototype.update = function update(iterable){
for(let curr of iterable)
this.learnLiteral(curr)
}
Counter.prototype.most_common = function most_common(){
return Object.entries(this.literals).sort((a,b) => b[1] - a[1])
}
const counter = new Counter()
function walk(root, callback){
const content = fs.readdirSync(root).reduce(([dirs, files], name) => {
(fs.lstatSync(path.join(root,name)).isDirectory() ? dirs : files).push(name)
return [dirs, files]
}, [[], []]);
callback(root, ...content)
for(const dirname of content[0]){
walk(path.join(root, dirname), callback)
}
}
walk(target_path, (root, dirs, files) =>{
const filtratedDirs = dirs.filter(name => !shouldIgnore(root, name))
dirs.splice(0)
dirs.push(...filtratedDirs)
for(const filename of files){
if(!shouldIgnore(root, filename)){
const fullName=path.join(root, filename)
console.log('чтение' ,fullName)
const text = fs.readFileSync(fullName, {encoding :'utf-8'})
counter.update(text.split(splitToLiteralsReg))
}
}
})
let index = 0
for(const [literal, count] of counter.most_common()){
index++;
console.log(`${index}. "${literal}" ${count} повторений`)
}
const closestTo = (dateToCompare, datesArray) => {
const buff = datesArray.filter(date => date >=dateToCompare).sort()
return buff.length ? buff[0] : undefined
}
def binsearch(min, max, check):
support = (min + max) // 2
result = check(support)
if result == 'меньше':
return binsearch(support+1, max, check)
if result == 'больше':
return binsearch(min, support-1, check)
return support
def guess(num):
print('загадано', num)
def check(req):
result = 'равно' if req == num else 'меньше' if req < num else 'больше'
print(f'это {req}?', '-', 'Да' if result=='равно' else f'Нет, твоё число {result}')
return result
return check
binsearch(0, 100, guess(10))
print('='*30)
binsearch(0, 100, guess(27))
print('='*30)
binsearch(0, 100, guess(50))
print('='*30)
binsearch(0, 100, guess(100))
загадано 10
это 50? - Нет, твоё число больше
это 24? - Нет, твоё число больше
это 11? - Нет, твоё число больше
это 5? - Нет, твоё число меньше
это 8? - Нет, твоё число меньше
это 9? - Нет, твоё число меньше
это 10? - Да
==============================
загадано 27
это 50? - Нет, твоё число больше
это 24? - Нет, твоё число меньше
это 37? - Нет, твоё число больше
это 30? - Нет, твоё число больше
это 27? - Да
==============================
загадано 50
это 50? - Да
==============================
загадано 100
это 50? - Нет, твоё число меньше
это 75? - Нет, твоё число меньше
это 88? - Нет, твоё число меньше
это 94? - Нет, твоё число меньше
это 97? - Нет, твоё число меньше
это 99? - Нет, твоё число меньше
это 100? - Да
import random
class Shuffler:
def __init__(self, lst, depth=2):
self.lst = lst
self.depth = depth
random.shuffle(self)
def __getlevel(self, index, depth):
curr = self.lst
curr_depth = depth
while curr_depth > 0:
curr_depth -= 1
curr_size = len(curr)
curr = curr[index % curr_size]
index //= curr_size
return curr, index
def __getitem__(self, index):
value, index = self.__getlevel(index, self.depth)
if index > 0:
raise IndexError('list index out of range')
return value
def __setitem__(self, index, value):
last_line, line_index = self.__getlevel(index, self.depth - 1)
last_line[line_index] = value
return value
def __len__(self):
acc = 1
curr = self.lst
curr_depth = self.depth
while curr_depth > 0:
curr_depth -= 1
acc *= len(curr)
if acc:
curr = curr[0]
return acc
lst = [[i * 10 + j for j in range(10)] for i in range(10)]
Shuffler(lst, 2)
print(*lst, sep='\n')
[45, 50, 74, 1, 3, 29, 43, 2, 93, 68]
[41, 10, 51, 99, 85, 20, 95, 54, 59, 8]
[46, 64, 24, 12, 26, 15, 6, 76, 39, 5]
[58, 13, 44, 60, 36, 70, 63, 79, 42, 18]
[9, 69, 25, 66, 67, 65, 35, 47, 23, 87]
[78, 80, 22, 73, 97, 31, 91, 75, 82, 90]
[57, 38, 49, 11, 84, 17, 62, 7, 89, 71]
[40, 30, 86, 94, 21, 53, 14, 19, 0, 32]
[33, 37, 61, 92, 81, 88, 56, 83, 98, 34]
[96, 16, 52, 4, 27, 55, 48, 72, 77, 28]
words = [(0,'What'),(1,'the'),(2,'heck?')]
for key,word in words:
print('key:', key)
print('word:', word)
print()
key: 0
word: What
key: 1
word: the
key: 2
word: heck?
words = [(0,'What'),(1,'the'),(2,'heck?')]
for _,word in words:
print(word)
a = {}
a[()] = 5
print(a)
{(): 5}