Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are the same and their lengths differ by at most 1. The lengths of a subset may not be empty. You must output YES if subsets exist that fit this criteria otherwise output NO.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class LinkedList {
private Node head;
static class Node {
private int data;
private Node next;
public Node(int d) {
data = d;
next = null;
}
}
public void append(int data) {
Node n = new Node(data);
if (head == null) {
head = new Node(data);
} else {
n.next = null;
Node last = head;
while (last.next != null)
last = last.next;
last.next = n;
}
}
public int sum(int index, int size) {
Node current = head;
int count = 0;
int sum = 0;
boolean found = false;
while (current != null) {
if (count == index) {
found = true;
break;
}
count++;
current = current.next;
}
int i = 1;
while (found && current != null && i <= size) {
sum += current.data;
i++;
current = current.next;
}
return sum;
}
public void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) throws FileNotFoundException {
LinkedList list = new LinkedList();
File file = new File("in.txt");
Scanner sc = new Scanner(file);
int count = 0;
while (sc.hasNext()) {
count++;
list.append(Integer.parseInt(sc.next()));
}
System.out.println("\nCreated Linked list is: ");
list.printList();
if (count == 0) {
return;
}
System.out.println("\n\n Subset exist? " + list.equalSubsets(count));
}
public boolean equalSubsets(int count) {
for (int k = 2; k < (count - 1); k++) {
for (int i = 0; i <= (count - k); i++) {
int sum = sum(i, k);
for (int j = 0; j <= (count - k); j++) {
if (i != j) {
if (sum == sum(j, k))
return true;
else if (sum == sum(j, k + 1))
return true;
}
}
}
}
return false;
}
}
Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...
java question: Write a program that reads in data from a text file named in.txt. Compute the sum of all the valid integers in the input file. Likewise, compute the sum of all the valid doubles in the input file. Write the former to an output file called int_total.txt, and write the latter to an output file called double_total.txt. B) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For...
JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...
C++
3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...
JAVA
Write a program that
1. opens a text file
2. reads lines of text from the file
3. Converts each line to uppercase
4. saves them in an ArrayList
5. Writes the list of Strings to a new file named
"CAPSTEXT.txt"
Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...
In Python, write a program that reads a text file that is provided by the user - ensure that the file exists before processing it! The file might be in a different directory, so be sure to test you logic - the user will provide the absolute path to the file if it is not in the same directory as the Python script! You should then ask the user for what string to search for in the file. Your program...
Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file contains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates. Ex: If the input is: inputl.csv and the contents of input1.csv are: hello, cat, man, hey, dog, boy, Hello, man, cat, woman, dog, Cat, hey, boy the output is: hello 1 cat 2 man...
write a C++ program that reads in a text file and reads each character or lexemes and states what token it is and writes to another file for example: while (fahr < upper) a = 23.00 whileend Output: token lexeme -------------------------------- keyword while separator ( identifier fahr operator < identifier upper separator ) identifier a operator = real 23.00 keyword whileend the program aslo reads in comments but does not add to the list. characters that are commented out or...
Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.
Help in JAVA please Write a program that reads a list of students (first names only) from a file. It is possible for the names to be in unsorted order in the file but they have to be placed in sorted order within the linked list. The program should use a doubly linked list. Each node in the doubly linked list should have the student’s name, a pointer to the next student, and a pointer to the previous student. Here...
Write a program in Java that reads the file and does two things: Write to an output file called dataSwitch.txt the same data from the input file, but switch the order of the data entries on each line. For example, if the first line of the input file is “90001 57110”, the first line of the output file would be “57110 90001”. Additionally, print to the screen the number of lines in the file with a label