Thread: Order of list?

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    3

    Order of list?


    klskdl
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    68
    Rep Power
    5
    I think you have a few more things wrong than you think . I looked through the directions and it requires that no duplicates appear in the list and only movies that are at least one customer's top two picks are displayed. What you wrote doesn't meet either of those two requirements or the one you're asking about.

    Here's a simple algorithm that gets the job done

    python Code:
    def get_most_popular(movies_in_stock, customer_list):
         result = []
         for movie in movies_in_stock:
             requested = False;
             for customer in customer_list:
                 movies = customer_list[customer]
                 if movie in movies:
                     if movies.index(movie) <= 1:
                         requested = True
                         break
             if (requested):
                 result.append(movie)
          print(result)


    This loops through each movie in stock. It then checks is that movie is one of the top 2 choices of at least 1 customer. If both of the prior conditions are true, it then adds the movie to the variable 'result'. Now, the output is stored in the order needed so there's no need to sort it, only movies that are one of a customer's top two choices are selected, and no duplicates will occur.

    I think I explained everything, but if something is still confusing you just ask and I should respond.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    3
    Can you explain why you made the variable requested eqal to True or False?? that part is confusing me,

    And i am aware that we weren't supposed to have duplicates, but i didnt exactly know what a duplicate was.. which is what made me think what i did was right

    So can you explain those two things to me,

    Thank you.
    Also, for functions like these, i see that i was supposed to loop through both parameters, and compare them, but wouldnt that give some sort of error?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    Originally Posted by JonthnC
    Here's a simple algorithm that gets the job done
    Or even simpler:

    python Code:
    def get_most_popular(movies_in_stock, customer_list):
        result = []
        for movie in movies_in_stock:
            for customer, top_movies in customer_list.items():
                if movie in top_movies[:2] and movie not in result:
                    result.append(movie)
        return result
    My armada: openSUSE 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    3
    Thank You for all your help..

    I also wanted some more help.

    I am on another function.. that i think i am ding right, but can you tell me if i am on the right track??? Because i kind of wasnt last time.

    For this function i am taking a file.txt and looping through it. then i have to put it in a list of list of strings: Can you let me know if i am on the right track, and if not what am i doing wrong? And does my iff condition need to include something like:

    if next_item != '' or next_item != None:
    result.append/??


    Code:
    def build_grocery_list(file):
    
        '''(io.TextIOWrapper) -> list of list of str
        The parameter is an open file containing groceries to purchase from a grocery
        store with one grocery item per line. To make it easier to shop for items, the groceries
        are separated into "grocery groups", and these groups are separated by exactly one
        blank line. Download the sample file foods.txt and notice that there is a blank line
        between grocery groups. The function returns a list of lists, where each inner list
        contains the groceries from one grocery group. Your inner lists are required to be
        in the same order as in file, and there should not be any newline characters anywhere
        in your list. Your function must work for any file of this format, not just the sample we
        have given you.
    
        >>>build_grocery_list(open("foods.txt", "r"))
        [['milk', 'cheese', 'cream cheese', 'eggs'], ['bread', 'buns', 'pita']
    
        '''
        result = []
        counter = 0
        string = ' '
        for next_item in file:
            next_item = next_item.strip()
            result.append(next_item)
        print(result)

    Example of text file =

    milk
    sugar
    candy

    sour cream
    onions
    chips

    bananas
    apples
    oranges
    mango
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Whoever wrote this assignment does good work. Stay with it. We learn that "The parameter is an open file". Usually a great technique. Testing is easier because (as in the example) you can pass TextIOWrapper for testing. Or the source could be somewhere on the internet. The function is more general.

    The function comes with a doctest. Learn to run doctests. Learn to write doctests. Write the tests before you write the function body.
    python -m doctest your_program.py
    runs the doctests.

    The assignment says that your function should return a list. Your function doesn't have a return statement. Therefore it returns None. None is not a list. A doctest to ensure you've returned the value:
    Code:
    '''
    >>> a = build_grocery_list(open("foods.txt", "r"))
    >>> a == [['milk', 'cheese', 'cream cheese', 'eggs'], ['bread', 'buns', 'pita']
    True
    '''
    (Note also that the doctest you posted is missing a space character in the python prompt.

    As for your code, it hasn't got a test for the blank lines between groups, and to make nested lists you'll need a pattern equivalent to
    Code:
    result=list()
    for whatever:
        subList = []
        result.append(subList)
        for whichever:
            subList.append(someobject)
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo