dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
1.6 KiB
1.6 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 68cae5b538ff798bbd4da003 | Challenge 65: String Count | 29 | challenge-65 |
--description--
Given two strings, determine how many times the second string appears in the first.
- The pattern string can overlap in the first string. For example,
"aaa"contains"aa"twice. The first twoa's and the second two.
--hints--
count('abcdefg', 'def') should return 1.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count('abcdefg', 'def'), 1)`)
}})
count('hello', 'world') should return 0.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count('hello', 'world'), 0)`)
}})
count('mississippi', 'iss') should return 2.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count('mississippi', 'iss'), 2)`)
}})
count('she sells seashells by the seashore', 'sh') should return 3.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count('she sells seashells by the seashore', 'sh'), 3)`)
}})
count('101010101010101010101', '101') should return 10.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count('101010101010101010101', '101'), 10)`)
}})
--seed--
--seed-contents--
def count(text, parameter):
return text
--solutions--
def count(text, pattern):
if not pattern:
return 0
occurrences = 0
for i in range(len(text) - len(pattern) + 1):
if text[i:i+len(pattern)] == pattern:
occurrences += 1
return occurrences