Author name: Satya Sighakolli

Casting-out nines

At ancient times,There are no calculators this casting out nines approach is used to tally the calculations and this is a very good approach to find the digital root of a number. def digital_root(n): return 9 if n%9==0 else n%9 OUTPUT print(digital_root(942)) 6

String Compression in Python

Sometimes,It become very essential to Compress a code in python,In realworld context to find motifs in DNA sequences compressing them in a certain way is necessary.One such example is presented below def compress_string(s): n=len(s) for size in range(1,n//2+1): if n%size==0: sub=s[:size] if sub*(n//size)==s: return f”{n//size}({sub})” return s s=”abcabcabcaba” print(compress_string(s))   OUTPUT: 4(abc)

Scroll to Top