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...
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...
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
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 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
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.
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...
Please use javascript. Starting with an array containing the numbers 1 through 10, use filter, map...
Please use javascript. Starting with an array containing the numbers 1 through 10, use filter, map and reduce to produce the following. Use console.log to display the results. a. An array of odd numbers b. An array of numbers divisible by 2 or 5 c. An array of numbers divisible by 3 squared d. The sum of the following: square the numbers divisible by 5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT