# version code d345910f07ae
coursera = 1
# Please fill out this stencil and submit using the provided submission script.





## 1: (Task 1) Movie Review
## Task 1
def movie_review(name):
    """
    Input: the name of a movie
    Output: a string (one of the review options), selected at random using randint
    """
    from random import randint
    return ["excellent","great","good","so so", "bad"][randint(0,4)]
    

## 2: (Task 2) Make Inverse Index
def makeInverseIndex(strlist):
    """
    Input: a list of documents as strings
    Output: a dictionary that maps each word in any document to the set consisting of the
            document ids (ie, the index in the strlist) for all documents containing the word.
    Distinguish between an occurence of a string (e.g. "use") in the document as a word
    (surrounded by spaces), and an occurence of the string as a substring of a word (e.g. "because").
    Only the former should be represented in the inverse index.
    Feel free to use a loop instead of a comprehension.

    Example:
    >>> makeInverseIndex(['hello world','hello','hello cat','hellolot of cats']) == {'hello': {0, 1, 2}, 'cat': {2}, 'of': {3}, 'world': {0}, 'cats': {3}, 'hellolot': {3}}
    True
    """
    keys = []
    values = []

    for x in strlist:
        tmp = x.split() 
        keys += tmp
    keys = list(set(keys))

    for i in keys:    
        value = []
        for n in range(len(strlist)):
            for e in strlist[n].split(' '):
                if e == i:
                    value.append(n)        
        values.append({x for x in value})
    mydict = {}

    for i in range(len(keys)):
        mydict[keys[i]]=values[i]
    return mydict



## 3: (Task 3) Or Search
def orSearch(inverseIndex, query):
    """
    Input: an inverse index, as created by makeInverseIndex, and a list of words to query
    Output: the set of document ids that contain _any_ of the specified words
    Feel free to use a loop instead of a comprehension.
    
    >>> idx = makeInverseIndex(['<NAME>', '<NAME>', '<NAME>', '<NAME> the Elder', ' <NAME>',  '<NAME>'])
    >>> orSearch(idx, ['Bach','the'])
    {0, 2, 3, 4, 5}
    >>> orSearch(idx, ['Johann', 'Carl'])
    {0, 2, 3, 4, 5}
    """
    return {j for i in query for j in inverseIndex[i]}



## 4: (Task 4) And Search
def andSearch(inverseIndex, query):
    """
    Input: an inverse index, as created by makeInverseIndex, and a list of words to query
    Output: the set of all document ids that contain _all_ of the specified words
    Feel free to use a loop instead of a comprehension.

    >>> idx = makeInverseIndex(['<NAME>', '<NAME>', '<NAME>', '<NAME>', ' <NAME>',  '<NAME>'])
    >>> andSearch(idx, ['Johann', 'the'])
    {2, 3}
    >>> andSearch(idx, ['Johann', 'Bach'])
    {0, 4}
    """
    store = []
    select = []
    tmp = [i for i in query]
    for key in tmp:
        for value in inverseIndex[key]:
            store.append(value)
    for things in store:
        if store.count(things) == len(query):
            select.append(things)
    select = list(set(select))
    return {x for x in select}


# version code d345910f07ae
coursera = 1
# Please fill out this stencil and submit using the provided submission script.





## 1: (Task 1) Movie Review
## Task 1
def movie_review(name):
    """
    Input: the name of a movie
    Output: a string (one of the review options), selected at random using randint
    """
    from random import randint
    return ["excellent","great","good","so so", "bad"][randint(0,4)]
    

## 2: (Task 2) Make Inverse Index
def makeInverseIndex(strlist):
    """
    Input: a list of documents as strings
    Output: a dictionary that maps each word in any document to the set consisting of the
            document ids (ie, the index in the strlist) for all documents containing the word.
    Distinguish between an occurence of a string (e.g. "use") in the document as a word
    (surrounded by spaces), and an occurence of the string as a substring of a word (e.g. "because").
    Only the former should be represented in the inverse index.
    Feel free to use a loop instead of a comprehension.

    Example:
    >>> makeInverseIndex(['hello world','hello','hello cat','hellolot of cats']) == {'hello': {0, 1, 2}, 'cat': {2}, 'of': {3}, 'world': {0}, 'cats': {3}, 'hellolot': {3}}
    True
    """
    keys = []
    values = []

    for x in strlist:
        tmp = x.split() 
        keys += tmp
    keys = list(set(keys))

    for i in keys:    
        value = []
        for n in range(len(strlist)):
            for e in strlist[n].split(' '):
                if e == i:
                    value.append(n)        
        values.append({x for x in value})
    mydict = {}

    for i in range(len(keys)):
        mydict[keys[i]]=values[i]
    return mydict



## 3: (Task 3) Or Search
def orSearch(inverseIndex, query):
    """
    Input: an inverse index, as created by makeInverseIndex, and a list of words to query
    Output: the set of document ids that contain _any_ of the specified words
    Feel free to use a loop instead of a comprehension.
    
    >>> idx = makeInverseIndex(['<NAME>', '<NAME>', '<NAME>', '<NAME> the Elder', ' <NAME>',  '<NAME>'])
    >>> orSearch(idx, ['Bach','the'])
    {0, 2, 3, 4, 5}
    >>> orSearch(idx, ['Johann', 'Carl'])
    {0, 2, 3, 4, 5}
    """
    return {j for i in query for j in inverseIndex[i]}



## 4: (Task 4) And Search
def andSearch(inverseIndex, query):
    """
    Input: an inverse index, as created by makeInverseIndex, and a list of words to query
    Output: the set of all document ids that contain _all_ of the specified words
    Feel free to use a loop instead of a comprehension.

    >>> idx = makeInverseIndex(['<NAME>', '<NAME>', '<NAME>', '<NAME>', ' <NAME>',  '<NAME>'])
    >>> andSearch(idx, ['Johann', 'the'])
    {2, 3}
    >>> andSearch(idx, ['Johann', 'Bach'])
    {0, 4}
    """
    store = []
    select = []
    tmp = [i for i in query]
    for key in tmp:
        for value in inverseIndex[key]:
            store.append(value)
    for things in store:
        if store.count(things) == len(query):
            select.append(things)
    select = list(set(select))
    return {x for x in select}


# version code d345910f07ae # Please fill out this stencil and submit using the provided submission script. ## 1: (Task 1) Movie Review ## Task 1 Input: the name of a movie     Output: a string (one of the review options), selected at random using randint ## 2: (Task 2) Make Inverse Index Input: a list of documents as strings     Output: a dictionary that maps each word in any document to the set consisting of the             document ids (ie, the index in the strlist) for all documents containing the word.     Distinguish between an occurence of a string (e.g. "use") in the document as a word     (surrounded by spaces), and an occurence of the string as a substring of a word (e.g. "because").     Only the former should be represented in the inverse index.     Feel free to use a loop instead of a comprehension.      Example:     >>> makeInverseIndex(['hello world','hello','hello cat','hellolot of cats']) == {'hello': {0, 1, 2}, 'cat': {2}, 'of': {3}, 'world': {0}, 'cats': {3}, 'hellolot': {3}}     True ## 3: (Task 3) Or Search Input: an inverse index, as created by makeInverseIndex, and a list of words to query     Output: the set of document ids that contain _any_ of the specified words     Feel free to use a loop instead of a comprehension.          >>> idx = makeInverseIndex(['<NAME>', '<NAME>', '<NAME>', '<NAME> the Elder', ' <NAME>',  '<NAME>'])     >>> orSearch(idx, ['Bach','the'])     {0, 2, 3, 4, 5}     >>> orSearch(idx, ['Johann', 'Carl'])     {0, 2, 3, 4, 5} ## 4: (Task 4) And Search Input: an inverse index, as created by makeInverseIndex, and a list of words to query     Output: the set of all document ids that contain _all_ of the specified words     Feel free to use a loop instead of a comprehension.      >>> idx = makeInverseIndex(['<NAME>', '<NAME>', '<NAME>', '<NAME>', ' <NAME>',  '<NAME>'])     >>> andSearch(idx, ['Johann', 'the'])     {2, 3}     >>> andSearch(idx, ['Johann', 'Bach'])     {0, 4}