HOTS Question on Object Oriented Programming – Python

Question:

  1. I was solving a practice paper on coursera and i just can’t solve this question can anyone help please?
  2. And apparently the topic of object oriented programming is optional in that course so is it not important or not useful enough??

Here is the question:
"The City class has the following attributes: name, country (where the city is located), elevation (measured in meters), and population (approximate, according to recent statistics). Fill in the blanks of the max_elevation_city function to return the name of the city and its country (separated by a comma), when comparing the 3 defined instances for a specified minimal population. For example, calling the function for a minimum population of 1 million: max_elevation_city(1000000) should return "Sofia, Bulgaria"."

# define a basic city class
class City:
    name = ""
    country = ""
    elevation = 0 
    population = 0

# create a new instance of the City class and
# define each attribute
city1 = City()
city1.name = "Cusco"
city1.country = "Peru"
city1.elevation = 3399
city1.population = 358052

# create a new instance of the City class and
# define each attribute
city2 = City()
city2.name = "Sofia"
city2.country = "Bulgaria"
city2.elevation = 2290
city2.population = 1241675

# create a new instance of the City class and
# define each attribute
city3 = City()
city3.name = "Seoul"
city3.country = "South Korea"
city3.elevation = 38
city3.population = 9733509

def max_elevation_city(min_population):
    # Initialize the variable that will hold 
# the information of the city with 
# the highest elevation 
    return_city = City()

    # Evaluate the 1st instance to meet the requirements:
    # does city #1 have at least min_population and
    # is its elevation the highest evaluated so far?
    if ___
        return_city = ___
    # Evaluate the 2nd instance to meet the requirements:
    # does city #2 have at least min_population and
    # is its elevation the highest evaluated so far?
    if ___
        return_city = ___
    # Evaluate the 3rd instance to meet the requirements:
    # does city #3 have at least min_population and
    # is its elevation the highest evaluated so far?
    if ___
        return_city = ___

    #Format the return string
    if return_city.name:
        return ___
    else:
        return ""

print(max_elevation_city(100000)) # Should print "Cusco, Peru"
print(max_elevation_city(1000000)) # Should print "Sofia, Bulgaria"
print(max_elevation_city(10000000)) # Should print ""
Asked By: Salik Malik

||

Answers:

The following snippet does the trick:

...
    # Evaluate the 1st instance to meet the requirements:
    # does city #1 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city1.population >= min_population:
        return_city = city1
    # Evaluate the 2nd instance to meet the requirements:
    # does city #2 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city2.population >= min_population and city2.elevation > return_city.elevation:
        return_city = city2
    # Evaluate the 3rd instance to meet the requirements:
    # does city #3 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city3.population >= min_population and city3.elevation > return_city.elevation:
        return_city = city3

    #Format the return string
    if return_city.name:
        return f"{return_city.name}, {return_city.country}" 
    else:
        return ""

First if checks wether the minimum population given is exceeded by city1. Since we know, that return_cityis not set yet, we don’t need to check if the elevation is the greatest.

Second and third ifcheck the same but additionaly if the elevation is greater then the one which may have been set previously.

In the return statement we use an f string to format.

I wonder where you were stuck exactly, since there is no OOP magic taking place here.

Regarding your question if OOP is important:
As always – it depends! Knowing about OOP in python will give you a deeper understanding on how python works and OOP in general is helpful in many projects. But if you want to use python merely as scripting language you won’t need too much OOP knowledge.

Answered By: Linosch Artox

You can also use this

...
            if city1.population >= min_population and city1.elevation > return_city.elevation:
                return_city = city1
        
            if city2.population >= min_population and city2.elevation >return_city.elevation:
                return_city = city2
         
            if city3.population >= min_population and city3.elevation > return_city.elevation:
                return_city = city3
    
            if return_city.name:
                return "{}, {}".format(return_city.name, return_city.country)
            else:
                return ""
Answered By: Shivam Suchak
if city1.population >= min_population:
    return_city = city1

elif  city2.population >= min_population:
    return_city = city2

elif  city3.population >= min_population:
    return_city = city3


if return_city.name:
    return "{}, {}".format(return_city.name, return_city.country)
else:
    return ""
Answered By: Adel Abu Hashim
# Evaluate the 1st instance to meet the requirements:
# does city #1 have at least min_population and
# is its elevation the highest evaluated so far?
if city1.population >= min_population:
    return_city = city1
# Evaluate the 2nd instance to meet the requirements:
# does city #2 have at least min_population and
# is its elevation the highest evaluated so far?
if city2.population >= min_population and city2.elevation > return_city.elevation:
    return_city = city2
# Evaluate the 3rd instance to meet the requirements:
# does city #3 have at least min_population and
# is its elevation the highest evaluated so far?
if city3.population >= min_population and city3.elevation > return_city.elevation:
    return_city = city3

#Format the return string
if return_city.name:
    return ("{}, {}".format(return_city.name, return_city.country))
else:
    return ""
Answered By: SSV 1
    # define a basic city class
class City:
    name = ""
    country = ""
    elevation = 0 
    population = 0

# create a new instance of the City class and
# define each attribute
city1 = City()
city1.name = "Cusco"
city1.country = "Peru"
city1.elevation = 3399
city1.population = 358052

# create a new instance of the City class and
# define each attribute
city2 = City()
city2.name = "Sofia"
city2.country = "Bulgaria"
city2.elevation = 2290
city2.population = 1241675

# create a new instance of the City class and
# define each attribute
city3 = City()
city3.name = "Seoul"
city3.country = "South Korea"
city3.elevation = 38
city3.population = 9733509

def max_elevation_city(min_population):
    highest_elevation = 0
    # Initialize the variable that will hold 
    # the information of the city with 
    # the highest elevation 
    return_city = City()

    # Evaluate the 1st instance to meet the requirements:
    # does city #1 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city1.population >= min_population and city1.elevation > return_city.elevation:
        highest_elevation = return_city.elevation 
        return_city = city1
    # Evaluate the 2nd instance to meet the requirements:
    # does city #2 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city2.population >= min_population and city2.elevation > return_city.elevation:
        highest_elevation = return_city.elevation 
        return_city = city2
    # Evaluate the 3rd instance to meet the requirements:
    # does city #3 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city3.population >= min_population and city3.elevation > return_city.elevation:
        highest_elevation = return_city.elevation 
        return_city =  city3

    #Format the return string
    if return_city.name:
        return "{}, {}".format(return_city.name, return_city.country)
    else:
        return ""

print(max_elevation_city(100000)) # Should print "Cusco, Peru"
print(max_elevation_city(1000000)) # Should print "Sofia, Bulgaria"
print(max_elevation_city(10000000)) # Should print ""

#Outputs
#Cusco, Peru
#Sofia, Bulgaria
#
Answered By: Emre Kılıç

You can simplify the solution like this

    if city1.population >= min_population:
    return_city = city1

#We Make sure that the city have the required minimum population and that the previous city requirements are rejected

if city2.population >= min_population and not return_city.name:
    return_city = city2

#We Make sure that the city have the required minimum population and that the previous city requirements are rejected

    if city3.population >= min_population and not return_city.name:
        return_city = city3

    if return_city.name:
        return ','.join([return_city.nam ,return_city.country]) 
    else:
        return ""
Answered By: KMB90
# define a basic city class
class City:
    name = ""
    country = ""
    elevation = 0 
    population = 0

# create a new instance of the City class and
# define each attribute
city1 = City()
city1.name = "Cusco"
city1.country = "Peru"
city1.elevation = 3399
city1.population = 358052

# create a new instance of the City class and
# define each attribute
city2 = City()
city2.name = "Sofia"
city2.country = "Bulgaria"
city2.elevation = 2290
city2.population = 1241675

# create a new instance of the City class and
# define each attribute
city3 = City()
city3.name = "Seoul"
city3.country = "South Korea"
city3.elevation = 38
city3.population = 9733509

def max_elevation_city(min_population):
    # Initialize the variable that will hold 
# the information of the city with 
# the highest elevation 
    return_city = City()

    # Evaluate the 1st instance to meet the requirements:
    # does city #1 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city1.population >= min_population:
        return_city = city1
    # Evaluate the 2nd instance to meet the requirements:
    # does city #2 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city2.population >= min_population and city2.elevation > return_city.elevation:
        return_city = city2
    # Evaluate the 3rd instance to meet the requirements:
    # does city #3 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city3.population >= min_population and city3.elevation > return_city.elevation:
        return_city = city3

    #Format the return string
    if return_city.name:
        return f"{return_city.name}, {return_city.country}" 
    else:
        return ""
Answered By: Waly

This gave me the correct answer.

 # define a basic city class
    class City:
        name = ""
        country = ""
        elevation = 0 
        population = 0

# create a new instance of the City class and
# define each attribute
city1 = City()
city1.name = "Cusco"
city1.country = "Peru"
city1.elevation = 3399
city1.population = 358052

# create a new instance of the City class and
# define each attribute
city2 = City()
city2.name = "Sofia"
city2.country = "Bulgaria"
city2.elevation = 2290
city2.population = 1241675

# create a new instance of the City class and
# define each attribute
city3 = City()
city3.name = "Seoul"
city3.country = "South Korea"
city3.elevation = 38
city3.population = 9733509

def max_elevation_city(min_population):
    # Initialize the variable that will hold 
# the information of the city with 
# the highest elevation 
    return_city = City()

    # Evaluate the 1st instance to meet the requirements:
    # does city #1 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city1.population > min_population and city1.elevation > return_city.elevation:
        return_city = city1
    # Evaluate the 2nd instance to meet the requirements:
    # does city #2 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city2.population > min_population and city2.elevation > return_city.elevation:
        return_city = city2
    # Evaluate the 3rd instance to meet the requirements:
    # does city #3 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city3.population > min_population and city3.elevation > return_city.elevation:
        return_city = city3

    #Format the return string
    if return_city.name:
        return "{}, {}".format(return_city.name, return_city.country)
    else:
        return ""

print(max_elevation_city(100000)) # Should print "Cusco, Peru"
print(max_elevation_city(1000000)) # Should print "Sofia, Bulgaria"
print(max_elevation_city(10000000)) # Should print ""
Answered By: Tiamiyu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.