Skip to content

Instantly share code, notes, and snippets.

@fahnub
Created June 19, 2023 12:55
Show Gist options
  • Save fahnub/3e739248bd8f84562313a4baa9e72afb to your computer and use it in GitHub Desktop.
Save fahnub/3e739248bd8f84562313a4baa9e72afb to your computer and use it in GitHub Desktop.
Foobar Level 1 Problem 1 (the cake is not a lie)
from math import sqrt
def get_factors(n):
factors = set()
for i in range(1, int(sqrt(n)) + 1):
if n % i == 0:
factors.update([i, n//i])
return sorted(factors)
def get_substrings(s):
substrings = []
for factor in get_factors(len(s)):
substrings.append(s[0: factor])
return substrings
def solution(s):
if len(s) == 1:
return 1
if s.count(s[0]) == 1:
return 1
if s.index(s[0], 1) > len(s) / 2:
return 1
substrings = get_substrings(s)
for substring in substrings:
if s.count(substring) * len(substring) == len(s):
return s.count(substring)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment