Python: Find the largest product of the pair of adjacent elements from a given list of integers
Python Basic - 1: Exercise-94 with Solution
Largest Adjacent Product
Write a Python program to find the largest product of a pair of adjacent elements from a given list of integers.
Sample Solution:
Python Code:
# Define a function named adjacent_num_product that takes a list of numbers (list_nums) as an argument.
def adjacent_num_product(list_nums):
# Use a generator expression with zip to calculate the product of adjacent elements.
# The expression max(a*b for a, b in zip(list_nums, list_nums[1:])) finds the maximum product of adjacent elements.
return max(a * b for a, b in zip(list_nums, list_nums[1:]))
# Test the function with different lists of numbers and print the results.
# Test case 1
nums = [1, 2, 3, 4, 5, 6]
# Print the original list of numbers.
print("Original list: ", nums)
# Print the largest product of adjacent elements in the list.
print("Largest product of the pair of adjacent elements of the said list:", adjacent_num_product(nums))
# Test case 2
nums = [1, 2, 3, 4, 5]
# Print the original list of numbers.
print("\nOriginal list: ", nums)
# Print the largest product of adjacent elements in the list.
print("Largest product of the pair of adjacent elements of the said list:", adjacent_num_product(nums))
# Test case 3
nums = [2, 3]
# Print the original list of numbers.
print("\nOriginal list: ", nums)
# Print the largest product of adjacent elements in the list.
print("Largest product of the pair of adjacent elements of the said list:", adjacent_num_product(nums))
Sample Output:
Original list: [1, 2, 3, 4, 5, 6] Largest product of the pair of adjacent elements of the said list: 30 Original list: [1, 2, 3, 4, 5] Largest product of the pair of adjacent elements of the said list: 20 Original list: [2, 3] Largest product of the pair of adjacent elements of the said list: 6
Explanation:
Here is a breakdown of the above Python code:
- Function definition:
- The code defines a function named "adjacent_num_product()" that takes a list of numbers (list_nums) as an argument.
- Generator Expression:
- The function uses a generator expression with 'zip' to calculate the product of adjacent elements in the input list.
- Max Function:
- The max(a * b for a, b in zip(list_nums, list_nums[1:])) expression finds the maximum product of adjacent elements in the list.
- Test cases:
- The function is tested with different lists of numbers using print(adjacent_num_product(...)).
Visual Presentation:
Flowchart:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to find the middle character(s) of a given string. If the length of the string is even return the two middle characters. If the length of the string is odd, return the middle character.
Next: Write a Python program to check whether every even index contains an even number and every odd index contains odd number of a given list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-94.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics