# Most Python Coders Cannot Solve This Puzzle (IQ>120)

5/5 - (1 vote)

The following Python code puzzle comprises of multiple Python tricks — in a single puzzle. Most Finxters cannot solve this puzzle on the Finxter app:

```# List of books read by each member
["1984", "To Kill a Mockingbird", "The Great Gatsby"],
["The Catcher in the Rye", "1984", "To Kill a Mockingbird"],
["Pride and Prejudice", "1984"]
]

# Set comprehension
unique_books = {book for member in books_read for book in member}

# Ternary operator
popular_book = "1984" if all("1984" in member for member in books_read) else "Varies"

# Multiple assignment

x = len(unique_books)
x *= len(popular_book)
x += total_books * members_count - 2
print(x)
```

This code snippet is performing several operations on a list of books read by members of a book club. Let’s break down each part of the code and then analyze the final computation.

1. List of books read by each member:
`books_read` is a list of lists, where each sublist represents the books read by an individual member of the book club.
2. Set Comprehension (unique_books):
This line creates a set of unique books read by all members. Set comprehension is used to iterate over each member’s list and add each book to a set, automatically removing duplicates.
3. Ternary Operator (popular_book):
This line checks if “1984” is read by all members. The `all()` function checks if the condition `"1984" in member` is `True` for every sublist in `books_read`. If it is true for all members, `popular_book` is set to “1984”; otherwise, it is set to “Varies”.
4. Multiple Assignment (total_books, members_count):
Here, `total_books` is assigned the total number of books read by all members, calculated by summing the lengths of each member’s book list. `members_count` is assigned the total number of members, which is the length of the `books_read` list.
5. Final Computation (x):
• `x = len(unique_books)`: Sets `x` to the number of unique books.
• `x *= len(popular_book)`: Multiplies `x` by the length of the string in `popular_book` (“1984” or “Varies”).
• `x += total_books * members_count - 2`: Adds to `x` the product of `total_books` and `members_count`, then subtracts 2. Finally, `x` is printed.

Let’s calculate the value of `x` step by step. First, we need to determine the values of `unique_books`, `popular_book`, `total_books`, and `members_count`.

• `unique_books` will be a set of all the unique books from the lists.
• `popular_book` will be “1984” because it’s in every member’s list.
• `total_books` will be the sum of the lengths of the sublists in `books_read`.
• `members_count` will be the length of the `books_read` list.

Then, we’ll use these values to compute `x`.

The calculated value of `x` in the given code snippet is 42. This result is derived by first determining the unique books, the popularity of “1984” among the book club members, and then combining these with the total number of books and members to perform the final computation.