w3resource

Python: Find the largest product of the pair of adjacent elements from a given list of integers


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:

Python: Find the largest product of the pair of adjacent elements from a given list of integers.

Flowchart:

Flowchart: Python - Find the largest product of the pair of adjacent elements from a given list of integers.

For more Practice: Solve these Related Problems:

  • Write a Python program to find the largest product of any two adjacent numbers in a list.
  • Write a Python program to compute the maximum product obtained from pairs of consecutive elements in an array.
  • Write a Python program to iterate through a list, calculate products of adjacent numbers, and return the highest product.
  • Write a Python program to determine which adjacent pair in an array produces the largest multiplication result.

Go to:


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.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.