Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Implement a User Profile Composed of Address and Contacts

Design a UserProfile class that composes Address and Contact objects. Practice composition and selecting a primary contact.

Python practice15 minComposition & Real-World ModelingIntermediateLast updated April 11, 2026

Problem statement

Create three classes: Address, Contact, and UserProfile. A UserProfile should contain an Address instance and a list of Contact instances (composition, not inheritance). Implement a helper function build_user(first_name, last_name, address_tuple, contacts_list) that constructs and returns a UserProfile. Implement user_summary(user) that returns a single-line string containing the user's full name, a nicely formatted address, and the primary contact's value. Primary contact selection rules: - If one or more contacts have preferred=True, select the first preferred contact in the list. - If none are preferred but contacts exist, select the first contact in the list. - If there are no contacts, the primary contact is None. Address formatting rules: - Always include street and city. - Append "state zip" together if either state or zip is non-empty. If both are empty, omit that part. - Separate parts with commas. End the address part with a period before the Primary label. Return format example: "Jane Doe, 123 Main St, Springfield, IL 62704. Primary: jane@example.com" Follow composition principles: UserProfile should store Address and Contact objects, not raw tuples.

Task

Use composition to model a user profile with an address and a list of contacts. Implement functions to build profiles and produce a concise user summary string that selects the primary contact correctly.

Examples

Basic example with preferred contact

Input

user_summary(build_user('Jane','Doe', ('123 Main St','Springfield','IL','62704'), [('email','jane@example.com', True), ('phone','555-1234', False)]))

Output

Jane Doe, 123 Main St, Springfield, IL 62704. Primary: jane@example.com

build_user constructs objects, user_summary picks the preferred email and formats the address and primary contact.

Input format

The tests will call user_summary(build_user(first, last, address_tuple, contacts_list)). address_tuple is (street, city, state, zip). contacts_list is a list of tuples (type, value, preferred_bool).

Output format

A single string summarizing the user: "<First> <Last>, <street>, <city>[, <state> <zip>]. Primary: <primary_value or None>"

Constraints

Do not use inheritance for Address/Contact (use composition). Do not print; return a string from user_summary. Keep classes and functions pure and deterministic.

Samples

Sample 1

Input

user_summary(build_user('John','Smith', ('10 Downing','London','', ''), [('phone','+44-20-0000', False)]))

Output

John Smith, 10 Downing, London. Primary: +44-20-0000

State and zip are empty so they are omitted from the address. The first contact is chosen as primary because none are preferred.