/
ch-2.py
29 lines (23 loc) · 949 Bytes
/
ch-2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# https://theweeklychallenge.org/blog/perl-weekly-challenge-230/
"""
Task 2: Count Words
Submitted by: Mohammad S Anwar
You are given an array of words made up of alphabetic characters and a prefix.
Write a script to return the count of words that starts with the given prefix.
Example 1
Input: @words = ("pay", "attention", "practice", "attend")
$prefix = "at"
Ouput: 2
Two words "attention" and "attend" starts with the given prefix "at".
Example 2
Input: @words = ("janet", "julia", "java", "javascript")
$prefix = "ja"
Ouput: 3
Three words "janet", "java" and "javascripr" starts with the given prefix "ja".
"""
def countMatchPrefix( lst, prefix ): return len(tuple(filter(lambda x: x.startswith(prefix), lst )))
for lst, prefix,count in (
(("pay", "attention", "practice", "attend"), "at", 2),
(("janet", "julia", "java", "javascript"), "ja", 3),
):
print(countMatchPrefix(lst, prefix)==count)