Thursday, November 28, 2024
Google search engine
HomeLanguagesPython program to Occurrences of i before first j in list

Python program to Occurrences of i before first j in list

Given a list, the task is to write a Python program to count the occurrences of ith element before the first occurrence of jth element.

Examples:

Input : test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9], i, j = 4, 8 
Output : 3 
Explanation : 4 occurs 3 times before 8 occurs.

Input : test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9], i, j = 5, 8 
Output : 1 
Explanation : 5 occurs 1 time before 8 occurs. 

Method #1: Using loop

In this, we increment counter whenever i is encountered and stop when any j has occurred, i.e breaking from the loop.

Python3




# Python3 code to demonstrate working of
# Occurrences of i before first j
# Using loop
 
# initializing Matrix
test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing i, j
i, j = 4, 8
 
res = 0
for ele in test_list:
 
    # breaking on 1st j
    if ele == j:
        break
 
    # counting i till 1st j
    if ele == i:
        res += 1
 
# printing result
print("Number of i's till 1st j : " + str(res))


Output:

The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
Number of i's till 1st j : 3

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using index() + slicing + count()

In this, we perform the task of getting the index of j, and then slice list till there, count() is used to get a count of i in the sliced list to get the required result.

Python3




# Python3 code to demonstrate working of
# Occurrences of i before first j
# Using index() + slicing + count()
 
# initializing Matrix
test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing i, j
i, j = 4, 8
 
# getting index
jidx = test_list.index(j)
 
# slicing list
temp = test_list[:jidx]
 
# getting count
res = temp.count(i)
 
# printing result
print("Number of i's till 1st j : " + str(res))


Output:

The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
Number of i's till 1st j : 3

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Using index(), slicing, operator.countOf() methods

  1. Find the index of j using index()
  2. Slice the list till the index.
  3. Find the occurrence of i in that sliced list(using operator.countOf())
  4. Display the occurrence.

Example:

Python3




# Python3 code to demonstrate working of
# Occurrences of i before first j
# Using loop
 
# initializing Matrix
test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
             
# printing original list
print("The original list is : " + str(test_list))
 
# initializing i, j
i, j = 4, 8
 
res = 0
x=test_list.index(j)
y=test_list[:x]
import operator
res=operator.countOf(y,i)
# printing result
print("Number of i's till 1st j : " + str(res))


Output

The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
Number of i's till 1st j : 3

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #4: Using index() and list comprehension

This method finds the index of the first occurrence of j in the list using the index() method, and then uses list comprehension and slicing to count the occurrences of i before the first occurrence of j.

Python3




# Python3 code to demonstrate working of
# Occurrences of i before first j
# Using index() and list comprehension
 
# initializing Matrix
test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing i, j
i, j = 4, 8
 
# finding index of first j in list
j_index = test_list.index(j)
 
# using list comprehension to count i's before first j
count_i = sum([1 for ele in test_list[:j_index] if ele == i])
 
# printing result
print("Number of i's till 1st j : " + str(count_i))


Output

The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
Number of i's till 1st j : 3

Time complexity: O(n), where n is the length of the list,
Auxiliary space: O(1), as it only uses a few variables to store the index and the count.

Method 5: Using for loop

  1. Initialize the input list test_list.
  2. Print the original list using the print() function and the string concatenation operator +.
  3. Initialize the variables i and j to represent the values we are looking for.
  4. Initialize the variable count_i to 0 to keep track of the number of occurrences of i before the first occurrence of j.
  5. Loop through the elements of the list using a for loop.
  6. For each element, check if it is equal to j. If it is, exit the loop.
  7. Otherwise, check if the element is equal to i. If it is, increment the count_i variable.
  8. After the loop has finished, print the value of count_i.

Example:

Python3




# Python3 code to demonstrate working of
# Occurrences of i before first j
# Using for loop
 
# initializing Matrix
test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing i, j
i, j = 4, 8
 
# initializing count_i
count_i = 0
 
# looping through the list until the first occurrence of j is found
for ele in test_list:
    if ele == j:
        break
    if ele == i:
        count_i += 1
 
# printing result
print("Number of i's till 1st j : " + str(count_i))


Output

The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
Number of i's till 1st j : 3

Time complexity: O(n), where n is the length of the list. 
Auxiliary space: O(1) as we only use a constant amount of extra space to store the count_i variable.

Method 6: using the itertools.takewhile() function

Python3




# Python3 code to demonstrate working of
# Occurrences of i before first j
# Using itertools.takewhile()
 
import itertools
 
# initializing Matrix
test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing i, j
i, j = 4, 8
 
# count the occurrences of i before the first occurrence of j
count_i = sum(1 for x in itertools.takewhile(
    lambda x: x != j, test_list) if x == i)
 
# printing result
print("Number of i's till 1st j : " + str(count_i))


Output

The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]
Number of i's till 1st j : 3

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(1), as it does not create any additional data structures. 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments