I am using Python 3.5.2 3. Create a function called filter_interesting(a_list) t
ID: 3572930 • Letter: I
Question
I am using Python 3.5.2
3. Create a function called filter_interesting(a_list) that, given a list of words, returns a list containing only those words from the list a_list that are found to be interesting according to the criteria of exercise #2 above. The function header should be as follows: def filter_interesting(a_list) For example, given the following list a_list_of_words = [“hulk”,“spiderman”,“batman”,“superman”,”thor”,”ironman”,“captainamerica”, “ultron”, “falcon”], the call filter_interesting(a_list_of_words) returns the list [‘spiderman’, 'batman', 'superman', ‘ironman', ‘captainamerica', ‘ultron’, ‘falcon’]
Explanation / Answer
def is_interesting(a_string):
count = 0
for i in range(0, len(a_string)):
ch = a_string[i]
if ch == 'a' or ch =='i' or ch == 'e' or ch == 'o' or ch == 'u':
count = count + 1
if isPrime(count):
return True;
else:
return False;
def isPrime(n):
if n < 2:
return False;
for i in range(2, n/2):
if n % i == 0:
return False;
return True;
def filter_interesting(a_list):
list = []
for i in range(0,len(a_list)):
if is_interesting(a_list[i]):
list.append(a_list[i])
return list;
a_list_of_words = ["hulk","spiderman","batman","superman","thor","ironman","captainamerica", "ultron", "falcon"]
print filter_interesting(a_list_of_words);
Output:
sh-4.3$ python main.py
['spiderman', 'batman', 'superman', 'ironman', 'captainamerica', 'ultron', 'falcon']
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.