Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7443619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:21:32+00:00 2026-05-29T11:21:32+00:00

def greedyAdvisor(subjects, maxWork, comparator): ”’subjects is a dictionary with keys classes and values of

  • 0
def greedyAdvisor(subjects, maxWork, comparator):
    '''subjects is a dictionary with keys classes and values of tuples of class value and class work. maxWork is the maximum work a student wants to put in. comparator is a function that takes two tuples of the aforementioned class value/class work variety and returns which one has a higher value. The function returns a dictionary with the most valuable classes to take within the given parameter of the willingness to work. I am supposed to use a greedy algorithem'''
    greedy_dict = {}
    highest_subjects = []
    total_work = 0
    while total_work < maxWork:
        highest_value = 0, 0
        highest_class = 0.0 
        for i in subjects:
            if comparator (subjects[i], highest_value) and i not in highest_subjects and total_work + int(subjects[i][WORK]) <= maxWork:
                highest_value = subjects[i]
                highest_class = i
                print highest_class, highest_value
        highest_subjects.append(highest_class)
        total_work += int(highest_value[WORK])
        greedy_dict[highest_class] = highest_value
    print greedy_dict

    return greedy_dict

the data, subjects is a dictionary which maps courses like 6.00, 7.01, etc to tuples with values 1-10 for the value of the class and workload 1-20 for how many hours a problem is supposed to take. Well it started in a text file and I turned it into a dictionary just dandy. The problem is from mit ocw intro to programming problem 8, the text is in a file called subjects.txt. I hope this addressed your concerns about the data.

The problem I’m having is the subjects dictionary has class values up to 10, but the greedy_dictionary keeps thinking the maximum value is 9. The comparator in the parameters is a function which returns True if the first tuple[VALUE] is greater than the second tuple[VALUE].

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:21:33+00:00Added an answer on May 29, 2026 at 11:21 am

    It is… if you’re comparing strings.

    >>> '10' < '9'
    True
    

    Try converting them to numbers first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def singleton_class class << self self end end class Human proc = lambda {
def greatest(values): value_generator = (v for k,v in values) max_value = max(value_generator) return (k
def plural(value, string) #{value} #{value.abs == 1 ? string.singularize : string.pluralize} end If not,
def makecounter(): return collections.defaultdict(int) class RankedIndex(object): def __init__(self): self._inverted_index = collections.defaultdict(list) self._documents = []
def ExtractViewState(string): m = re.match(__viewstate[^>]+value=\\(\?<Value>[^\]*\), string, re.IGNORECASE) return m.group(0) I think I'm missing something,
def remove_whitespaces(value): Remove all whitespaces p = re.compile(r'\s+') return p.sub(' ', value) The above
def foo (): x = re.compile('^abc') foo2(x) def foo2(x): How do I get x
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def partial(template, *args) options = args.extract_options! options.merge!(:layout => false) if collection = options.delete(:collection) then
def test_method [a, b, c].map {|i| yield(i) } end If I call test_method like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.