Kaggle Python course Exercise: Strings and Dictionaries Q. no. 2

  kaggle, python

Here’s the question,
A researcher has gathered thousands of news articles. But she wants to focus her attention on articles including a specific word. Complete the function below to help her filter her list of articles.

Your function should meet the following criteria:

Do not include documents where the keyword string shows up only as a part of a larger word. For example, if she were looking for the keyword “closed”, you would not include the string “enclosed.”

She does not want you to distinguish upper case from lower case letters. So the phrase “Closed the case.” would be included when the keyword is “closed”

Do not let periods or commas affect what is matched. “It is closed.” would be included when the keyword is “closed”. But you can assume there are no other types of punctuation.

Here’s my ans(I want to solve this just using loops and ifs):

def word_search(doc_list, keyword):
    """
    Takes a list of documents (each document is a string) and a keyword. 
    Returns list of the index values into the original list for all documents 
    containing the keyword.

Example:
  

  doc_list = ['The Learn Python Challenge Casino', 'They bought a car, and a horse', 'Casinoville?']
word_search(doc_list, 'casino')
    >>> [0]
    """
    #non-course provided and my own code starts here.
    
    k=0
    print(doc_list,keyword)
    for string in doc_list:
        print(string)
        for char in string:
            if char.upper()==keyword[0] or char.lower()==keyword[0]:
                print(char,string[string.index(char)-1])
                if (string[string.index(char)-1]==" " or string[string.index(char)-1]=="" or string[string.index(char)-1]==".") and (string[string.index(char)+len(keyword)]==" " or string[string.index(char)+len(keyword)]=="" or string[string.index(char)+len(keyword)]=="."):
                    print(string[string.index(char)-1])
                    for k in range(len(keyword)):
                        print(k)
                        if string[string.index(char)+k].upper()==keyword[k] or string[string.index(char)+k].lower()==keyword[k]:
                            c=c+k
                            if len(c)==len(keyword):
                                x=[doc_list.index(string)]
                            return x

But after running the check code:

q2.check() #returns,

Incorrect: Got a return value of None given doc_list=['The Learn Python Challenge Casino', 'They bought a car, and a horse', 'Casinoville?'], keyword='casino', but expected a value of type list. (Did you forget a return statement?)

Here’s what gets printed out after executing the code:

['The Learn Python Challenge Casino', 'They bought a car, and a horse', 
'Casinoville?'] casino
The Learn Python Challenge Casino
C  
C  
They bought a car, and a horse
c  
Casinoville?
C ?

The code is compiling successfully without syntax and other explicit errors. But I can’t find any implicit bugs that’s generating a wrong ans after struggling for 5+ hrs. please help!

Source: Python Questions

2 thoughts on - Kaggle Python course Exercise: Strings and Dictionaries Q. no. 2

  • Suppose you have collected hundreds of blog articles on web
    programming. But you want the ability to find articles including a specific
    keyword. Example – You can type “python” to search for all articles
    containing the word the python. Write a python function below to filter
    your list of articles. Your function should meet the following criteria:
    A. The function takes a list of strings and a keyword as parameters.
    B. The function returns a list of strings which contain the keyword
    C. Do not include strings where the keyword string shows up only as a
    part of a larger word. For example, if you are looking for the keyword
    “closed”, you would not include the string “enclosed.”
    D. Your search should not be case sensitive. So the string “CSS is for
    styling.” would be included when the keyword is “css”.
    E. Do not let periods or commas affect what is matched. “Interactivity is
    created using JavaScript.” would be included when the keyword is
    “JavaScript”. You can assume there are no other types of punctuation.

  • We’re using python lists to record students who attended our class and what
    order they arrived in. For example, the following list represents a class with 8
    students, in which Yash showed up first and Jony was the last to arrive:
    students = [‘Yash’, ‘Nabila’, ‘Maria’, ‘Rafi’, ‘Imam’, ‘Sinthiya’, ‘Sumon’, ‘Jony’]
    A student is considered ‘fashionably late’ if they arrived after at least half of
    the participants. However, they must not be the very last participant (that’s
    taking it too far). In the above example, Imam, Sinthiya and Sumon are the
    only students who were fashionably late.
    Write a function which takes a list of students and the name of one
    student and prints whether that person is “fashionably late”. If the student is
    not in the list, print “Student was absent”.

LEAVE A COMMENT