Multiply K to every Nth element using Python


In this problem statement we are required to multiply K to every Nth item in the given list or sequence and implement the solution using Python. So We will solve this problem using basic Python programming.

Understanding the Problem

The problem at hand is that we have to multiply a specific value called K to each Nth item in the given list. Generally we create a function to apply on each item of the list but here in our problem we have to multiply a constant with some items in the list. To understand better let’s dive in the example below:

Logic for The Above Problem

To solve the given problem for multiplying K in every Nth item we will use for loop to iterate over the list and look for the items which are at place which are multiples of N. After that we will multiply each Nth item with the value of K. This process will be continued until all Nth items will not be multiplied by K.

Algorithm

  • Step 1 − Start algorithm by defining the function called multiply_nth_item to multiply K with every Nth item in the given sequence. This function will accept three parameters like series, K and N. Here the series is the sequence of items, K is the value we have to multiply in the Nth item.

  • Step 2 − Initialize a variable to keep track of the count and give it a name as count and its initial value will be 1.

  • Step 3 − Next using a for loop we will traverse each item in the given input series.

  • Step 4 − Now we will check the condition, if the value of count is divisible by N then we will multiply that item with the value of K.

  • Step 5 − And if the above condition is false then we will increment the value of count by 1.

  • Step 6 − At the end we will return the modified series.

Example

# Define the function for multiply k to the nth item
def multiply_nth_item(series, K, N):
   count = 1
   for i in range(len(series)):
      if count % N == 0:
         series[i] *= K
      count += 1
   return series

# Initialize the series, K and N
input_series = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
K = 3
N = 2
#Call the function
Output = multiply_nth_item(input_series, K, N)
print(Output)

Output

[11, 36, 13, 42, 15, 48, 17, 54, 19, 60]

Algorithm - Using Numpy

  • Step 1 − As we are using numpy to solve the given problem in this algorithm so Importing the numpy library.

  • Step 2 − Define function as multiply_nth_item and pass three parameters called series, K and N. The series is the input list, K is the value to multiply, and N is the position of the item to update.

  • Step 3 − Create a numpy array from the given input series as we need the index of each item.

  • Step 4 − The array will show the positions of every item in the given series.

  • Step 5 − After that we will create a position variable with the help of it and we will check that the index is divisible by N or not. If the condition is true then we will update it by multiplying with K.

  • Step 6 − The position variable will be used to update the chosen item in the series.

  • Step 7 − Now again convert the numpy array into the regular Python list with the help of tolist function.

Example - Using Numpy

# import the numpy library
import numpy as np
# define the function to multiply K
def multiply_nth_item(series, K, N):
   series = np.array(series)
   index = np.arange(len(series))
   position = index % N == 0
   series[position] *= K
   return series.tolist()

# Initialize the series, K, and N
input_series = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
K = 3
N = 2
# Call the function
Output = multiply_nth_item(input_series, K, N)
print(Output)

Output

[6, 4, 18, 8, 30, 12, 42, 16, 54, 20]

Complexity

If there are n number of items present in the given list then the time complexity for multiplying K with each Nth item using Python is O(n). Because we have used a basic iteration process in both the approaches to multiply every Nth item in the series.

Conclusion

So multiplying K to every Nth item in the given series using Python can be achieved with the above-mentioned algorithms with the time complexity of O(n). And the time complexity for both the algorithms is linear which indicates that the algorithms are efficient.

Updated on: 16-Oct-2023

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements