Skip to content

Instantly share code, notes, and snippets.

@namthatman
Created April 13, 2019 07:13
Show Gist options
  • Save namthatman/eb604629bb0cf49641cfd97c180e11c8 to your computer and use it in GitHub Desktop.
Save namthatman/eb604629bb0cf49641cfd97c180e11c8 to your computer and use it in GitHub Desktop.
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the repeatedString function below.
def repeatedString(s, n):
findChar = 'a'
count = 0
if n > len(s):
#count findChar inside original string
for a in range(len(s)):
if s[a] == findChar:
count += 1
gap = int(n / len(s))
count = count * gap
remain = n % len(s)
for a in range(remain):
if s[a] == 'a':
count += 1
else:
#when n < len(s)
for a in range(n):
if s[a] == findChar:
count += 1
return count
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
n = int(input())
result = repeatedString(s, n)
fptr.write(str(result) + '\n')
fptr.close()
Lilah has a string, s, of lowercase English letters that she repeated infinitely many times.
Given an integer, n, find and print the number of letter a's in the first n letters of Lilah's infinite string.
For example, if the string s = 'abcac' and n = 10, the substring we consider is abcacabcac, the first 10 characters of her infinite string. There are 4 occurrences of a in the substring.
Function Description
Complete the repeatedString function in the editor below. It should return an integer representing the number of occurrences of a in the prefix of length n in the infinitely repeating string.
repeatedString has the following parameter(s):
s: a string to repeat
n: the number of characters to consider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment