Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hello everyone I want the time complexity for this pseudocode and how we got it step by step with explanations also the best case and

hello everyone

I want the time complexity for this pseudocode and how we got it step by step with explanations also the best case and worst case of the algorithm in details

""" Created on Wed Feb 1 11:43:04 2023

@author: aseel """

# important libraries we need import pandas as pd import time

path = r"C:\Users\aseel\Desktop\project\pcr_data.csv"

# Read the CSV file into a DataFrame with 100 rows only df = pd.read_csv(path, nrows=100) df = df.astype(str)

# Join all the elements in each row with no separator df["joined_row"] = df.apply(lambda row: ''.join(row), axis=1)

# Initialize a variable to store the result result = -1

#**********************************************************************

# variables that include casting the dataframe into different types of data structures data_list = list(df["joined_row"]) data_tuple = tuple(df["joined_row"]) data_set = set(df["joined_row"])

#**********************************************************************

# Python3 program for Naive Pattern # Searching algorithm #----------------------------------------------------------------------

def naiveAlgorithm(string, target): # size of the string n = len(string) # size of the target pattern m = len(target) # loop in the range of the target window ( assume s is 4 and target is 2,, 4-2+1 = 3) for i in range(n - m + 1): # intiliazing the target counter j j = 0 # while j is less than the size of the target while j < m and string[i + j] == target[j]: # increment j to check the next element in the target pattern j += 1 # if the target counter = size of the target we found the pattern return the index i if j == m: return i return -1

#----------------------------------------------------------------------

# ask the user for the target string or pattern they are looking for target = input("Please enter the pattern you want to find (only in 0's and 1's'): ", )

# start time start=time.perf_counter()

# Iterate through the joined_row column of the DataFrame for i, string in enumerate(data_set): # use the variable with the desired data structure # Call the naive method to Compare the current string to the target string index = naiveAlgorithm(string, target) # if the index is in bounds of the columns if index != -1: # If a match is found, store the index in the result variable result = i print("Pattern match found at index", result, "and column number", index) # otherwise we did not find a match elif index == i: break print("No match found.")

# otherwise we did not find a match # end time end = time.perf_counter() print("Time Complixity is") print(end-start)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions