Question

In: Computer Science

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 containing all the substrings (contiguous or non-contiguous) of a String parameter s, and test it on the String "abcde".

// Return all substrings of the String s

public static ArrayList allSubstrings(String s) { ... }

Question 2

Implement a function that reads numbers in from a file with one number per line and outputs all the possible sums that can be formed by subsets of the numbers. For instance, if the numbers in the file are 1 2 4, then the output would be 0, 1, 2, 4, 3, 5, 6, 7. Note that 0 is in the output because it uses none of the numbers, while 7 is the sum of all of the numbers.

// Return all sums that can be formed from subsets of elements in arr

public static ArrayList allSums( ArrayList arr ) { ... }

q1.java

package a1;

/**

* Name:

* Student ID:

* Description of solution:

*/

import static org.junit.Assert.*;

public class Q3 {

// Return the number of ways to choose a subset of k distinct elements from a

set of n elements

public static int C( int n, int k ) {

return 0;

}

public static void main(String[] args) {

System.out.println("Testing...");

assertEquals(C(14,3), 364);

assertEquals(C(14,11), 364);

assertEquals(C(18,8), 43758);

System.out.println("Success!");

}

}

q2.java

package a1;

/**

* Name:

* Student ID:

* Description of solution:

*/

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.ArrayList;

import static org.junit.Assert.*;

public class Q2 {

// Return all sums that can be formed from subsets of elements in arr

public static ArrayList allSums( ArrayList arr ) {

return null;

}

public static void main(String[] args) {

//https://www.baeldung.com/java-file-to-arraylist

ArrayList result = new ArrayList();//=

Files.readAllLines(Paths.get("nums.txt"));

try {

BufferedReader br = new BufferedReader(new

FileReader("nums.txt"));

while (br.ready()) {

result.add(Integer.getInteger(br.readLine()));

}

} catch (Exception e) {

System.out.println(e.getMessage());

}

ArrayList sums = allSums(result);

System.out.println("Testing...");

assertEquals(sums.size(), 8);

assertEquals(sums.contains(0), true);

assertEquals(sums.contains(1), true);

assertEquals(sums.contains(2), true);

assertEquals(sums.contains(4), true);

assertEquals(sums.contains(3), true);

assertEquals(sums.contains(5), true);

assertEquals(sums.contains(6), true);

assertEquals(sums.contains(7), true);

System.out.println("Success!");

}

}

q3.java

package a1;

/**

* Name:

* Student ID:

* Description of solution:

*/

import java.util.ArrayList;

import static org.junit.Assert.*;

public class Q1 {

// Return all substrings of the String s

public static ArrayList allSubstrings(String s){

return null;

}

public static void main(String[] args) {

ArrayList s = allSubstrings("abcde");

System.out.println("Testing...");

assertEquals(s.size(), 32);

assertEquals(s.contains(""), true);

assertEquals(s.contains("abcd"), true);

assertEquals(s.contains("abce"), true);

assertEquals(s.contains("abcde"), true);

System.out.println("Success!");

}

}

nums.txt

1

2

4

Solutions

Expert Solution

package a1;

/**

* Name:

* Student ID:

* Description of solution:

*/

import java.util.ArrayList;

import static org.junit.Assert.*;

public class Q1 {

// Return all substrings of the String s

public static ArrayList<String> allSubstrings(String s) {
        ArrayList<String> result = new ArrayList<>();
        if(s.isEmpty()) {
                result.add("");
                return result;
        }
        
        ArrayList<String> subResult = allSubstrings(s.substring(0, s.length() - 1));
        char c = s.charAt(s.length() - 1);
        
        result.addAll(subResult);
        for(String x: subResult) {
                result.add(x + c);
        }
        
        return result;
}

public static void main(String[] args) {

        ArrayList s = allSubstrings("abcde");

        System.out.println("Testing...");

        assertEquals(s.size(), 32);

        assertEquals(s.contains(""), true);

        assertEquals(s.contains("abcd"), true);

        assertEquals(s.contains("abce"), true);

        assertEquals(s.contains("abcde"), true);

        System.out.println("Success!");

}

}





Answering your first question, Since both are different question and require significant efforts on both.. Please ask them separately. 

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

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....
Assume that bits is a string that only contains the characters "0" and "1". n is...
Assume that bits is a string that only contains the characters "0" and "1". n is an integer variable that is less than the length of bits. Fill in the blanks below to replace bits with a new string that consists of all of the characters of bits from index n through the end, followed by the first n characters of bits. For example, if bits was "1101010" and n was 3, the new value of bits would be "1010110"....
Python Assume s is a string of numbers. Write a program that prints the longest substring...
Python Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print: Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
For this question, a block is a sequence of 20 characters, where each character is one...
For this question, a block is a sequence of 20 characters, where each character is one of the 26 lowercase letters a-z. For example, these are blocks: iwpiybhunrplsovrowyt rpulxfsqrixjhrtjmcrr fxfpwdhwgxtdaqtmxmlf How many different blocks are there? A block is squarefree if no character appears two times consecutively. The first and third example above are squarefree, but the second example is not because of the two consecutive occurrences of r. How many squarefree blocks are there? A block is non-local if...
Please do ACxz and BBCC for this question To verify that a string of characters belongs...
Please do ACxz and BBCC for this question To verify that a string of characters belongs to a language defined by a grammar, we must create a parse tree that shows that the string can be generated by the grammar. Consider the following grammar: <list> -> <item> , <list> | <item> <item> -> <left> <item> <right> | <left> <right> <left> -> A| B | C <right> -> x | y | z Choose a string that is in this language...
use java recursion find # of times a substring is in a string but it has...
use java recursion find # of times a substring is in a string but it has to be the exact same, no extra letters attached to it and capitalization matters. input is 2 strings, output is an int input: ("Hello it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello", "hello") should output: 3 input: (" Hello it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello", "Hello") should output: 1 input: (" Hello...
use java recursion find # of times a substring is in a string but it has...
use java recursion find # of times a substring is in a string but it has to be the exact same, no extra letters attached to it and capitalization matters. input: school is boring with schooling and School substring: school output: 1
From 1+K*L(s)=0 L(s) = 1/((s+1)(s+2)(s+10)) Solve for gain K where the root locus passes through the...
From 1+K*L(s)=0 L(s) = 1/((s+1)(s+2)(s+10)) Solve for gain K where the root locus passes through the damping ratio. z=0.176. With out Matlab. Please show all work. Note: I know we need to use some trig and the magnitude criteria but u cannot seem to figure it out. Thank you!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT