Question

The following algorithm finds the initial substring of y that can be reversed and found in...

The following algorithm finds the initial substring of y that can be reversed and found in y. For example, longestInitialReverseSubstringLength(“aabaa”) = 5, because “aabaa” is the same string forwards and backwards, so the longest initial substring that can be reversed and found in the string is “aabaa”. Also, longestInitialReverseSubstringLength(“bbbbababbabbbbb”) is 6, because “babbbb” can be found in the string (see color-highlighted portions of the string), but no longer initial string exists reversed in any part of the string.

longestInitialReverseSubstringLength(String y)

01 Max ← 0; //this will be our answer.

02 for i ← 1 to y.length() do: //loop (Main) tries to find reversed initial substrings of...

03 x ← empty string //...all possible lengths

04 for r ← i to 1 do: //reverses the initial i characters and stores in x

05 x ← x○y[r] // “○” indicates concatenation.

06 end for //end reversal loop

07 found ← false;

08 for j ← 1 to y.length() - x.length() + 1 do: //loop (A), which searches for x in y.

09 f ← true //unless contradicted, assume x is in y.

10 for k ← 1 to x.length() do: // loop (B), which searches y[j...j+k] for x.

11 if y[j+k] ≠ x[k] then: //there is a mismatch...

12 f ← false //so x was not found.

13 end if

14 end for //end loop (B)

15 if f then: //x was in y

16 found = true //remember it

17 break

18 end if

19 end for // ends the loop (A)

20 if found then: max = i //***Note the assignment*** new max length found.

21 end for //end loop (Main).

22 return max; //return the answer.

end function

  1. show your program running and taking 1 second to run.

  2. Analyze the algorithm’s runtime mathematically and express the runtime using big-oh notation (provide a tight upper bound).

  3. Analyze the function experimentally.  

    1. Implement the algorithm

    2. Let x be the string length at which your program takes 2 seconds to run, collect running times for your algorithm using the following string lengths: x/4, 2x/4, 3x/4, x, 5x/4, 6x/4, 7x/4, 2x.

    3. Generate your strings by reading the attached file, only reading as many characters as you need.

    4. Plot your results (x-axis is string length, y-axis should be time)

    5. Draw conclusions based on your graph. You may also need to plot another curve (e.g., n, n2, or n3, to support your conclusions).

  4. Give the runtime of the following algorithm, which calculates the average of an array of length 100. Note, importantly, this function only works if the length of A is 100.  

average(A) :

If length(A) != 100 then:

ERROR

End If

Sum ← 0

For x in A do:

Sum ← Sum + x

End For

Return Sum / 100

end average

Homework Answers

Answer #1

Here is your code what you wanted from the above algorithm.

import sys

def printSubStr(st, low, high) :

sys.stdout.write(st[low : high + 1])

sys.stdout.flush()

return ''

def longestInitialReverseSubstringLength(st) :

n = len(st)

table = [[0 for x in range(n)] for y

in range(n)]

maxLength = 1

i = 0

while (i < n) :

table[i][i] = True

i = i + 1

start = 0

i = 0

while i < n - 1 :

if (st[i] == st[i + 1]) :

table[i][i + 1] = True

start = i

maxLength = 2

i = i + 1

k = 3

while k <= n :

i = 0

while i < (n - k + 1) :

j = i + k - 1

if (table[i + 1][j - 1] and

st[i] == st[j]) :

table[i][j] = True

if (k > maxLength) :

start = i

maxLength = k

i = i + 1

k = k + 1

print( printSubStr(st, start, start + maxLength - 1) )

return maxLength

st = input("Enter your string : - ")

l = longestInitialReverseSubstringLength(st)

print( l )

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Let x be a string of length n, and let y be a string of length...
Let x be a string of length n, and let y be a string of length n − k, for 1 ≤ k < n. We wish to line up the symbols in x with the symbols in y by adding k blanks to y. Suppose that we add two separate blocks of blanks, one of size i and one of size k − i, for 1 ≤ i < k. How many ways are there to do this? Every...
CRYPTOGRAPHY PROBABILITY DISTRIBUTION Bad Shuffles Consider the following card shuffling algorithm. There are three cards in...
CRYPTOGRAPHY PROBABILITY DISTRIBUTION Bad Shuffles Consider the following card shuffling algorithm. There are three cards in the deck and they are each represented by an element in {X, Y, Z}. [Bonus marks if you use the deck {W, X, Y, Z}] Algorithm Shuffle --------------------------------------------------- deck := [X,Y,Z] for i from 0 to 2 do j := RANDOM(i) swap values of deck[i] and deck[j] end for return deck --------------------------------------------------- For each of the following definitions of RANDOM(i), compute the probability distribution...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but mostly readable way called “leet” – short for “elite”. In essence, it was a simple character replacement algorithm, where a single “regular” character was replaced by one or more “leet” characters; numbers remained the same. Here’s one of the most readable versions: a 4 g 9 m /\\/\\ s $ y ‘/ b B h |-| n |\\| t 7 z Z c (...
The function y(x,t) = 0.3 sin( 2 π t -2 π x + π /4) represents...
The function y(x,t) = 0.3 sin( 2 π t -2 π x + π /4) represents the vertical position of an element of a taut string upon which a transverse wave travels. This function depends on the horizontal position along the string, x, and time, t. y and x are in units of meters and t is in units of seconds. Do not use symbols in any of your answers below. Only use integers or decimals. a. Determine the angular...
Write a program of wordSearch puzzle that use the following text file as an input. The...
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! Hints • The puzzle can be represented as a right-sized two-dimensional array of characters (char). • A String can be converted into a right-sized array of characters via the String method toCharArray. . A word can occur in any of 8...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features: The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default). The data files will be text...
x y s t P 1 -3 1 0 0 12 1 2 0 1 0...
x y s t P 1 -3 1 0 0 12 1 2 0 1 0 3 -6 -4 0 0 1 0 The pivot element for the initial simplex tableau show is the red 1. So we need to zero out the other elements of column x. What is the formula used to zero out row 1 and column x? Multiply Row _____by_______ and then add the result to Row_____ What is the formula used to zero out row...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
utility function u(x,y) = x3 ·y2 I am going to walk you through the process of...
utility function u(x,y) = x3 ·y2 I am going to walk you through the process of deriving the optimal quantity of apples and bananas that will make you the happiest. To do this, we are going to apply what we learnt about derivatives. a) First, you have a budget. You cannot just buy an infinite amount since you would not be able to afford it. Suppose the price of a single apple is Px = 2 while the price of...
Given the following initial simplex tableau: x y z u v w P 2 4 3...
Given the following initial simplex tableau: x y z u v w P 2 4 3 1 0 0 0 4 6 7 1 0 1 0 0 8 6 6 5 0 0 1 0 18 -8 -11 -4 0 0 0 1 0 The pivot element that would be selected if you follow the standard convention taught in this course is the entry in row  , column  . Now, use this pivot element and complete ONE STEP using the simplex...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT