In: Computer Science
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<Integer> allSums( ArrayList<Integer> arr ) { ... }
FUNCTION
// Function to remove duplicates from an ArrayList
public static <T> ArrayList<T>
removeDuplicates(ArrayList<T> list)
{
// Create a new ArrayList
ArrayList<T> newList = new ArrayList<T>();
// Traverse through the first list
for (T element : list) {
// If this element is not present in newList
// then add it
if (!newList.contains(element)) {
newList.add(element);
}
}
// return the new list
return newList;
}
//method to generate the all combination of numbers
public static void generate()
{
String fname;int i,j,a,b,c,sum=0;
//create two arraylists
ArrayList<Integer> alist=new
ArrayList<Integer>();
ArrayList<Integer> alist1=new
ArrayList<Integer>();
//create scanner object
Scanner scan = new Scanner(System.in);
/* enter filename with extension to open and read its content
*/
System.out.print("Enter File Name to Open (with extension like
file.txt) : ");
fname = scan.nextLine();
/* this will reference only one line at a time */
String line = null;
try
{
/* FileReader reads text files in the default encoding */
FileReader fileReader = new FileReader(fname);
/* always wrap the FileReader in BufferedReader */
BufferedReader bufferedReader = new
BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null)
{
//add the elements into arraylist
alist.add(Integer.parseInt(line));
}
//close the file
bufferedReader.close();
//add 0 as first element into alist1
alist1.add(0);
//add all elements into the alist1
for (i = 0; i < alist.size();i++)
{
sum=sum+(int)alist.get(i);//find the sum
alist1.add(alist.get(i));
}
//compute the combinations with all elements of alist
for (i = 0; i < alist.size();i++)
{
for(j=i+1;j<alist.size();j++)
{
a=(int)(alist.get(i));
b=(int)(alist.get(j));
c=a+b;
alist1.add(c);//add the combinations to alist1
}
}
alist1.add(sum); //add the sum of all elements into
alist1
// Remove duplicate elements if any
ArrayList<Integer>
newList = removeDuplicates(alist1);
System.out.println(newList); //display the new list as the
result
}
catch(IOException ex)
{
System.out.println("Error reading file named '" + fname +
"'");
}
}
IMPLEMENTATION OF FUNCTION IN JAVA PROGRAM
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class JavaProgram
{
// Function to remove duplicates from an ArrayList
public static <T> ArrayList<T>
removeDuplicates(ArrayList<T> list)
{
// Create a new ArrayList
ArrayList<T> newList = new ArrayList<T>();
// Traverse through the first list
for (T element : list) {
// If this element is not present in newList
// then add it
if (!newList.contains(element)) {
newList.add(element);
}
}
// return the new list
return newList;
}
//method to generate the all combination of numbers
public static void generate()
{
String fname;int i,j,a,b,c,sum=0;
//create two arraylists
ArrayList<Integer> alist=new
ArrayList<Integer>();
ArrayList<Integer> alist1=new
ArrayList<Integer>();
//create scanner object
Scanner scan = new Scanner(System.in);
/* enter filename with extension to open and read its content
*/
System.out.print("Enter File Name to Open (with extension like
file.txt) : ");
fname = scan.nextLine();
/* this will reference only one line at a time */
String line = null;
try
{
/* FileReader reads text files in the default encoding */
FileReader fileReader = new FileReader(fname);
/* always wrap the FileReader in BufferedReader */
BufferedReader bufferedReader = new
BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null)
{
//add the elements into arraylist
alist.add(Integer.parseInt(line));
}
//close the file
bufferedReader.close();
//add 0 as first element into alist1
alist1.add(0);
//add all elements into the alist1
for (i = 0; i < alist.size();i++)
{
sum=sum+(int)alist.get(i);//find the sum
alist1.add(alist.get(i));
}
//compute the combinations with all elements of alist
for (i = 0; i < alist.size();i++)
{
for(j=i+1;j<alist.size();j++)
{
a=(int)(alist.get(i));
b=(int)(alist.get(j));
c=a+b;
alist1.add(c);//add the combinations to alist1
}
}
alist1.add(sum); //add the sum of all elements into alist1
// Remove duplicate elements if any
ArrayList<Integer>
newList = removeDuplicates(alist1);
System.out.println(newList); //display the new list as the
result
}
catch(IOException ex)
{
System.out.println("Error reading file named '" + fname +
"'");
}
}
public static void main(String[] input)
{
generate(); //call to the method
}
}
OUTPUT