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

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....
* 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;...
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 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
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types. 2. You must use a switch statement to check the user's menu option choice. 3. You may create additional functions in addition to the required functions listed above if you would like. 4. If user provides option which is not a choice...
Complete the code so that it can convert the date to day of week using python,...
Complete the code so that it can convert the date to day of week using python, the code should pass the doctest def convert_datetime_to_dayofweek(datetime_string): """ This function takes date in the format MON DAY YEAR HH:MM(PM/AM) and returns the day of the week Assume input string is UTC    >>> convert_datetime_to_dayofweek('Jun 1 2005 1:33PM') 'Wednesday' >>> convert_datetime_to_dayofweek('Oct 25 2012 2:17AM') 'Thursday' """ # code goes here
Write code for a game of picking match sticks. You must make sure that the computer...
Write code for a game of picking match sticks. You must make sure that the computer always wins. The game is as follows There are 26 matchsticks initially. The computer will ask the user to pick anywhere between 1 and 4 matchsticks. The player (whether the user or the computer) who has to pick up the last matchstick (or matchsticks) is the loser. You will have to use a combination of branching and looping commands with appropriate prompts and the...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a dictionary, WITHOUT using pandas. I'm able to import pickle and can open my file...but I'm not sure how to create the dictionary.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT