**Counting the ****frequency** of numbers in a dataset or a number stream is a fundamental operation in data analysis and statistics. In real life, this frequency counting algorithm is applied to solve complex business problems.

For example, consider an inventory store where customers purchase their daily use products. We can utilize frequency counting to identify the best selling products, track customer visit frequencies, monitor product stockout frequencies, and analyze purchase patterns over time.

In this Answer, we’ll take a stream of numbers and count the frequencies of both even and odd numbers in it.

We’ll use two variables to count even and odd numbers within the stream of numbers. We’ll iterate through the entire stream, checking each number to determine whether it’s even or odd. If it’s even, we’ll increment the even count variable by 1, and if it’s odd, we’ll increment the odd count variable by 1.

Here’s a code example:

# Initialize count variablesevenCount = 0oddCount = 0# Define a stream of numbersnumberStream = [2, 5, 8, 7, 10, 13, 6, 3]# Iterate the number streamfor number in numberStream:# If the number is even, increment evenCount by 1if number % 2 == 0:evenCount += 1else:# If the number is odd, increment oddCount by 1oddCount += 1# Display the frequncyprint("Even frequency:", evenCount)print("Odd frequency:", oddCount)

**Lines 2–3:**We initialized the`evenCount`

and`oddCount`

variables with`0`

.**Line 5:**We list the`numberStream`

array initialized with the numbers.**Lines 9–15:**We use the`for`

loop to iterate through the list of numbers, checking each number. If the number is even, it increments the`evenCount`

variable by`1`

and if the number is odd, it increments the`oddCount`

variable by`1`

.**Lines 18–19:**We print the frequency of both even and odd numbers.

In this code example, we solve a problem using a function and AND method. We modify the code to check if each number in a list is even or odd.

def CountingEvenOdd(numberStream, size):evenCount = 0oddCount = 0for i in range(size): # Iterate the number streamif (numberStream[i] & 1 == 1): # If the number is odd, incerement oddCount by 1oddCount += 1else:evenCount += 1. # If the number is even, incerement evenCount by 1print("Number of even elements = ",evenCount)print("Number of odd elements = ",oddCount)numberStream = [2, 3, 4, 5, 6] # Define a stream of numbersn = len(numberStream)CountingEvenOdd(numberStream, n) # Call the function

**Line 1:**We define a function named`CountingEvenOdd()`

with two parameters:`numberStream`

(a list of numbers) and`size`

(the size of the list).**Line 5:**We start a loop to iterate through each value in the`numberStream`

list.**Line 6:**We implement the condition: if the least significant bit of the number at index`i`

is 1, and the bitwise AND operation with`1`

results in`1`

, then the number at the index`i`

is odd; otherwise even.**Line 18:**We compute the length of the`numberStream`

list and assign it to the variable`n`

.**Line 21:**We call the`CountingEvenOdd()`

function with the`numberStream`

list and its length`n`

as arguments.

Copyright ©2024 Educative, Inc. All rights reserved

TRENDING TOPICS