PLAY_WITH_PI / pi_shard.py
Dhruv1102's picture
Update pi_shard.py
e625584 verified
raw
history blame contribute delete
715 Bytes
import re
def clean_text(text):
return re.sub(r'\s+', ' ', text).strip()
def get_pi_digits(n=100):
with open('pi_digits.txt', 'r') as f:
digits = f.read().replace('.', '').replace('\n', '')
return [int(d) for d in digits[:n] if d.isdigit()]
def pi_shard(text, max_chunks=50):
text = clean_text(text)
pi_digits = get_pi_digits()
chunks = []
index = 0
i = 0
while index < len(text) and len(chunks) < max_chunks:
length = pi_digits[i % len(pi_digits)] + 1
chunk = text[index:index + length * 50]
overlap = pi_digits[(i+1) % len(pi_digits)] * 5
chunks.append(chunk)
index += length * 50 - overlap
i += 1
return chunks