In: Computer Science
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);
#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