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

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)...
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...
. 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
define aray and string?
define aray and string?
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current...
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current string has greater length than #max start and end end=j start=i i=j; avg=0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('Teh average is',avg/(end-start)) s=input('Enter a string') longest(s) i need a definition and explanation of this code that how it works?
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
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).
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:
In Java  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:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
Q22 Variable s is defined as a string vector. We display the first a couple of...
Q22 Variable s is defined as a string vector. We display the first a couple of elements: s = [ ′′height′′ , “12.5” , “weight ′′ , ′′103-9′′ , ′′NWD′′, ... ]. Code to do the following. replace all elements “height” with “dimension” for s. Solve this using loop replace all elements “height” with “dimension” for s. Solve this without using loop create a new character vector s1, which concatenate all elements of s horizontally. That is, s1 = ′′height12.5weight...
# O W L S f(O,W,L,S) 0 0 0 0 0 0 1 0 0 0...
# O W L S f(O,W,L,S) 0 0 0 0 0 0 1 0 0 0 1 0 2 0 0 1 0 1 3 0 0 1 1 1 4 0 1 0 0 0 5 0 1 0 1 1 6 0 1 1 0 1 7 0 1 1 1 X 8 1 0 0 0 0 9 1 0 0 1 0 10 1 0 1 0 0 11 1 0 1 1 1 12 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT