Question

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

convert Javascript 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);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In Python you can not chain map and filter calls.
OUTPUT:


following is the implementation of the above code in python:
CODE:

class Person:
    def __init__(self, name, weight, height, bmi=0):
        self.name = name
        # weight in pound
        self.weight = weight
        # height in inches
        self.height = height
        # initialize bmi to 0
        self.bmi = bmi

    def __str__(self):
        return "[Name: " + self.name + ", BMI: " + str(self.bmi) + "]"


def pounds_to_kg(pounds):
    return pounds * 0.453592


def inch_to_meter(inches):
    return inches * 0.0254


def bmi(person):
    return pounds_to_kg(person.weight)/pow(inch_to_meter(person.height), 2);


def add_bmi(person):
    return Person(person.name, person.weight, person.height, bmi(person))


def is_overweight(person):
    return 25.0 <= bmi(person) < 30


def is_obese(person):
    return bmi(person) >= 30


if __name__ == "__main__":
    people = [Person("Amy", 152, 63), Person("Joe", 120, 64), Person("Tom", 210, 78), Person("Jim", 180, 68), Person("Jen", 120, 62), Person("Ann", 252, 63), Person("Ben", 240, 72)]
    overweight_people = list(map(add_bmi, people))
    overweight_people = list(filter(is_overweight, overweight_people))
    print("overweight People:")
    for person in overweight_people:
        print(person)

    print("\n\nObese People:")
    obese_people = list(map(add_bmi, people))
    obese_people = list(filter(is_obese, obese_people))
    for person in obese_people:
        print(person)

Add a comment
Know the answer?
Add Answer to:
convert Javascript code to Python and make sure you use chained map and filter as well....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT