Question

In: Computer Science

convert this code to Python and make sure you use chained map and filter as well....

convert this code to Python and make sure you use chained map and filter as well.

https://book.pythontips.com/en/latest/map_filter.html

CODE BELOW IS IN JAVASCRIPT

let people = [
{name: "Amy", pounds_weight: 152, inches_height: 63},
{name: "Joe", pounds_weight: 120, inches_height: 64},
{name: "Tom", pounds_weight: 210, inches_height: 78},
{name: "Jim", pounds_weight: 180, inches_height: 68},
{name: "Jen", pounds_weight: 120, inches_height: 62},
{name: "Ann", pounds_weight: 252, inches_height: 63},
{name: "Ben", pounds_weight: 240, inches_height: 72},
];

//functions to convert pounds to kg and inches to meters
let poundstokg = (pounds)=> pounds * 0.453592;
let inchestometers = (inches)=> inches * 0.0254;

//add bmi function
let addbmi = (person) => {
person["bmi"] = poundstokg(person["pounds_weight"])/(inchestometers(person["inches_height"]) * inchestometers(person["inches_height"]));
return person;
}
//checks if the person is overweight
let isOverweight = (person) => (person["bmi"] >= 25.0 && person["bmi"] < 30);
//checks if the person is obese
let isObese = (person) => (person["bmi"] >= 30);
// array of overweight and obese people
let overweight_people = people.map(addbmi).filter(isOverweight);
let obese_people = people.map(addbmi).filter(isObese);

console.log(overweight_people);
console.log(obese_people);

Solutions

Expert Solution

#functions to convert pounds to kg and inches to meters
def poundstokg(pounds):
    return (pounds * 0.453592)

def inchestometers(inches):
    return (inches * 0.0254)

people = [
{"name": "Amy", "pounds_weight": 152, "inches_height": 63},
{"name": "Joe", "pounds_weight": 120, "inches_height": 64},
{"name": "Tom", "pounds_weight": 210, "inches_height": 78},
{"name": "Jim", "pounds_weight": 180, "inches_height": 68},
{"name": "Jen", "pounds_weight": 120, "inches_height": 62},
{"name": "Ann", "pounds_weight": 252, "inches_height": 63},
{"name": "Ben", "pounds_weight": 240, "inches_height": 72},
]

def addbmi(person):
    person["bmi"] = poundstokg(person["pounds_weight"])/(inchestometers(person["inches_height"]) ** 2);
    return person

# checks if the person is overweight
def isOverweight(person):
    if person["bmi"] >= 25.0 and person["bmi"] < 30:
        return person["name"]

# checks if the person is obese
def isObese(person):
    if person["bmi"] >= 30:
        return person["name"]
  
# list of overweight and obese people
map(addbmi, people)
overweight_people = filter(isOverweight, people)
obese_people = filter(isObese, people)

print "overweight_people:"
for person in overweight_people:
    print "\t"+person["name"]
print "\nobese_people:"
for person in obese_people:
    print "\t"+person["name"]

Note: Take care of Indentations


Related Solutions

can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Please convert This java Code to C# (.cs) Please make sure the code can run and...
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make...
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make sure that you add preprocessor guards to the .h files to allow multiple #includes. Overview You will be writing the classes Grade and GradeCollection. You will be writing testing code for the Grade and GradeCollection classes. Part 1 – Create a Grade Class Write a class named Grade to store grade information. Grade Class Specifications Include member variables for name (string), score (double). Write...
Draw a concept map over the topic of Alzheimer's Disease. When you draw it, make sure...
Draw a concept map over the topic of Alzheimer's Disease. When you draw it, make sure to include Right ventricular hypertrophy in the center, then radiating out from that the 5 following categories: 1. Symptoms 2. Causes 3. Risks 4. Diagnoses 5. Treatments Then from each of those categories put a few major factors, Then from each of those categories put 1 or 2 specific factors. Then try to connect items from different categories that are somehow related
Draw a concept map over the topic of Alzheimer's Disease. When you draw it, make sure...
Draw a concept map over the topic of Alzheimer's Disease. When you draw it, make sure to include Right ventricular hypertrophy in the center, then radiating out from that the 5 following categories: 1. Symptoms 2. Causes 3. Risks 4. Diagnoses 5. Treatments Then from each of those categories put a few major factors, Then from each of those categories put 1 or 2 specific factors. Then try to connect items from different categories that are somehow related
* Make sure you turn in your code (take a screen shot of your code in...
* Make sure you turn in your code (take a screen shot of your code in R)and answers. Conduct the hypothesis and solve questions by using R. 2) A random sample of 12 graduates of a secretarial school averaged 73.2 words per minute with a standard deviation of 7.9 words per minute on a typing test. What can we conclude, at the .05 level, regarding the claim that secretaries at this school average less than 75 words per minute on...
LOOK over code Python The task aims to develop a Kalman filter that is able to...
LOOK over code Python The task aims to develop a Kalman filter that is able to hit the moving target (the pink box) in as many situations as possible. However, there are some limitations: IT IS NOT PERMITTED TO CHANGE THE PRODEIL AND TARGET CODE IT IS ALSO NOT ALLOWED TO CHANGE THE GAME LOOP, OTHER THAN WHAT HAS BEEN COMMENTS SHALL BE CHANGED WHEN THE Kalman CLASS IS IMPLEMENTED I have made the callman class, but my question is;...
Make a menu for a restaurant using python. Make sure there are choices on the menu....
Make a menu for a restaurant using python. Make sure there are choices on the menu. Give me a little description of how you did it.
make multiply function with ‘add’ command, Convert to mips code Don’t use ‘mult’ Use 'add' multiple...
make multiply function with ‘add’ command, Convert to mips code Don’t use ‘mult’ Use 'add' multiple times Get input from key-board and display the result in the console window
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT