Given this imperative Python snippet, please re-write it to be more functional: people = [{'name': 'Sabah', 'height': 170}, {'name': 'Tim', 'height': 180}, {'name': 'Saly'}] height_total = 0 height_count = 0 for person in people: if 'height' in person: height_total += person['height'] height_count += 1 if height_count > 0: average_height = height_total / height_count print average_height

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Given this imperative Python snippet, please re-write it to be more functional:

people = [{'name': 'Sabah', 'height': 170},

{'name': 'Tim', 'height': 180},

{'name': 'Saly'}] height_total = 0

height_count = 0
for person in people:

if 'height' in person:
height_total += person['height'] height_count += 1

if height_count > 0:
average_height = height_total / height_count print average_height

 

Expert Solution
Step 1: Algorithm of the code

Here's the algorithm for the code that calculates the average height of people with recorded heights using a functional approach:

1. Input:

  • Create a list named people containing dictionaries, each representing a person with a 'name' and an optional 'height' value.

2. Extract Heights:

  • Use a list comprehension to iterate through the people list and extract the 'height' values of individuals who have it.
  • Create a new list named heights to store these extracted heights.

3. Calculate Average Height:

  • Check if the heights list is not empty (i.e., there are recorded heights).
  • If there are recorded heights:
    • Calculate the total height by using the sum function to sum all the values in the heights list.
    • Calculate the number of recorded heights by using the len function on the heights list.
    • Compute the average height by dividing the total height by the number of recorded heights.

4. Output:

  • If there are recorded heights, print the calculated average height.

5. End of Algorithm.

This algorithm outlines the step-by-step process of extracting heights, calculating the average, and providing the result. It follows a functional programming style to make the code more concise and readable.

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education