Question

In: Computer Science

Define a sequence of string of 0’s and 1’s: 1 The first string, s0, is just...

Define a sequence of string of 0’s and 1’s: 1 The first string, s0, is just the empty string "". The second string, s1, is 1. The third, fourth, fifth, . . . strings are defined as follows: si = si−11ti−1 where ti−1 is the reverse of si−1 with all 0s replaced by 1s and all 1s replaced by 0s. The first few strings are s0 = "", s1 = 1, s2 = 110, s3 = 1101100, s4 = 110110011100100. Write a Java program named “ q3d.java” that prints the first 10 strings in this sequence.

Solutions

Expert Solution

To implement above problem we will use following steps:

Algorithm:

1.Take array of string str of size 10 as str[10]

2.Assign str[0]="";

3.Assign str[1]="1";

4.Print str[0];

5.Print str[1];

6.Take a variable i and assign its value 2.

7.Check if value of i is less than 10 if yes then goto step 8 otherwise goto step 13.

8. Call function rev(str[i-1]) and store the result in string t and then reverse the value of string t.

10 Concat string si-1 and "1" with string t and store the result in str[i]

11.Print str[i]

12.Increment the value of i by 1 and then goto step 7.

13 End

Function rev(str):

1.Take a variable i and assign its value to 0.

2.Calculate length of string str and store result in l.

3.Implement a for loop from i =0 to l.

    3.1.) Check if str[i]=='1' if yes then str[i]='0'

   3.2) Else str[i]='1'

4 . Return str;

Java code for this problem.

public class q3d {
    
    public static String rev(String str)
    {
        //implementing rev function
        int i;
        int l =str.length(); //calculating length
        String newstr="";
        for(i=0;i<l;i++)
        {
            if(str.charAt(i)=='0') //if string contains 0 
            {
                newstr=newstr+'1'; //change it to 1
            }
            else
            {
                newstr=newstr+'0'; //else change it to 0
            }
        }
        return newstr;
    }
    public static void main(String args[]) {
    
    String s[]=new String[10]; //take string of array
    s[0]=""; //initialize first sequence
    s[1]="1"; //initialize second sequence
    int i=2; //intiialize i;
    
    String t=""; //for reverse
    while(i<10)
    {
        t=rev(s[i-1]); // calling rev function
        
        StringBuffer sbr = new StringBuffer(t); //creating string buffer for reverse
        
        sbr.reverse(); //reverse the string
        
        s[i]=s[i-1]+1+t; //concating the strings
        
        System.out.println(s[i]); //printing the sequence
        
        i=i+1;
    }
    
    
    
    }
}

Sample Output of above code :


Related Solutions

Question 1 A substring of String s is a sequence of k >= 0 characters in...
Question 1 A substring of String s is a sequence of k >= 0 characters in s, in the order in which they occur in s. The letters in a substring may be either contiguous (next to each other) or non-contiguous in the original String s. For instance, these are the substrings of String s="abc". String s All substrings of s ======== ================================== "abc" "" "a" "b" "ab" "c" "ac" "bc" "abc" Write a function allSubstrings that returns an ArrayList...
Consider the sequence sn defined as: s0 = 1 s1 = 1 sn = 2sn-1 +...
Consider the sequence sn defined as: s0 = 1 s1 = 1 sn = 2sn-1 + sn-2 What is the base case for this recursive relation? Find s5 Write the pseudocode for a recursive function to find Sn for any arbitrary value of n. Create a non-recursive formula for finding the nth term in the sequence in O(1) time.
1) Define a sequence of polynomials H n (x ) by H 0 (x )=1, H...
1) Define a sequence of polynomials H n (x ) by H 0 (x )=1, H 1 (x )=2 x , and for n>1 by H n+1 (x )=2 x H n (x )−2 n H n−1 (x ) . These polynomials are called Hermite polynomials of degree n. Calculate the first 7 Hermite polynomials of degree less than 7. You can check your results by comparing them to the list of Hermite polynomials on wikipedia (physicist's Hermite polynomials). 2)...
Let S = {-3, -2, -1, 0, 1, 2, 3}. Define a relation R on S...
Let S = {-3, -2, -1, 0, 1, 2, 3}. Define a relation R on S by: xRy if and only if x = y + 4n for some integer n. a) Prove that R is an equivalence relation. b) Find all the distinct equivalence classes of R.
rogram that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x // 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The function integer_to_reverse_binary() should return a string of 1's...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or list) s and an element e, and that calls itself recursively to find and return the index of the last occurrence of e in s. If s is a string, e will be a single-character string; if s is a list, e can be any value. Don’t forget that the index of the first element in a sequence is 0. Important notes: If e...
. Suppose {et : t = −1, 0, 1, . . .} is a sequence of...
. Suppose {et : t = −1, 0, 1, . . .} is a sequence of iid random variables with mean zero and variance 1. Define a stochastic process by xt = et − 0.5et−1 + 0.5et−2, t = 1, 2, . . . a. Is xt stationary? Show your work. b. Is xt weakly dependent? Again, show your work. Plz help. maybe need use SAS to solve it
Consider the following page reference string: 0, 1, 2, 3, 1, 0, 4, 5, 1, 0,...
Consider the following page reference string: 0, 1, 2, 3, 1, 0, 4, 5, 1, 0, 1, 2, 6, 5, 2, 1, 0, 1, 2, 5 How many page faults would occur for the following replacement algorithms, assuming one, three, five, and seven frames? Remember that all frames are initially empty, so your first unique pages will cost one fault each. Optimal replacement LRU replacement CLOCK replacement FIFO replacement
define aray and string?
define aray and string?
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
USE Coral Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is 6, the output is:011(6 in binary is 110; the algorithm outputs the bits in reverse).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT