Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 11th, 2012, 10:31 PM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
Order of list?

klskdl

Reply With Quote
  #2  
Old November 11th, 2012, 11:51 PM
JonthnC JonthnC is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2010
Posts: 64 JonthnC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 22 h 45 m 54 sec
Reputation Power: 3
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:
Original - python Code
  1. def get_most_popular(movies_in_stock, customer_list):
  2.      result = []
  3.      for movie in movies_in_stock:
  4.          requested = False;
  5.          for customer in customer_list:
  6.              movies = customer_list[customer]
  7.              if movie in movies:
  8.                  if movies.index(movie) <= 1:
  9.                      requested = True
  10.                      break
  11.          if (requested):
  12.              result.append(movie)
  13.       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.

Reply With Quote
  #3  
Old November 11th, 2012, 11:56 PM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
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?

Reply With Quote
  #4  
Old November 12th, 2012, 02:34 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 17 m 55 sec
Reputation Power: 65
Quote:
Originally Posted by JonthnC
Here's a simple algorithm that gets the job done


Or even simpler:

python Code:
Original - python Code
  1. def get_most_popular(movies_in_stock, customer_list):
  2.     result = []
  3.     for movie in movies_in_stock:
  4.         for customer, top_movies in customer_list.items():
  5.             if movie in top_movies[:2] and movie not in result:
  6.                 result.append(movie)
  7.     return result
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #5  
Old November 12th, 2012, 02:40 AM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
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

Reply With Quote
  #6  
Old November 12th, 2012, 06:18 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,383 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 44 m 29 sec
Reputation Power: 383
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!

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Order of list?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap