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
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A):...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A): A is a list of n integers 1 for i = 1 to n-1 do 2   x=aix=ai 3 j = i − 1 4 while (j ≥≥ 0) do 5 if x≥ajx≥aj then 6 break 7 end if 8   aj+1=ajaj+1=aj 9 j = j − 1 a end while b   aj+1=xaj+1=x c end for d return A The complexity of this algorithm is O(n2)O(n2)...
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 (...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
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...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
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...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT