How to resolve this 3x3 matrix with equations requirements - java

I was in an interview today and I got the following exercice :
write a program to find all the 3x3 matrix X for each element x varying from 0 to 100
A B C
D E F
G H I
that meet the following requirements
A + B - C = 4
+ - -
D - E * F = 4
/ * -
G + H + I = 4
= = =
4 4 4
write a program in Java.

It is not really clear what your question is, but this seems quite straightforward to simply bruteforce by trying all choices in a sensible order.
For example, this Python code:
for G in range(1,4+1):
for H in range(4+1-G):
I = 4 - H - G
for A in range(0,4+1):
D = G*(4-A)
if not 0<=D<=100:
continue
for E in range(100+1):
for F in range(100+1):
if D-E*F==4:
for B in range(100+1):
C=A+B-4
if 0<=C<=100:
if B-E*H==4:
if C-F-I==4:
print A,B,C
print D,E,F
print G,H,I
print A+B-C,D-E*F,G+H+I,A+D/G,B-E*H,C-F-I
finds the following 4 solutions:
0 10 6
4 6 0
1 1 2
4 4 4 4 4 4
2 7 5
4 3 0
2 1 1
4 4 4 4 4 4
1 8 5
6 2 1
2 2 0
4 4 4 4 4 4
2 6 4
4 1 0
2 2 0
4 4 4 4 4 4

Related

(no 0 return) java.lang.IllegalArgumentException: Comparison method violates its general contract

I am not sure what is wrong. I looked up other similar titled questions and answers on stack overflow but not sure what is wrong with my method. I am a beginner in java so any help would be great. Thank you.
My code is:
import java.util.*;
class LargestNumber {
static void printLargest(Vector<String> arr){
Collections.sort(arr, new Comparator<String>(){
#Override
public int compare(String X, String Y) {
String XY=X + Y;
String YX=Y + X;
return XY.compareTo(YX) > 0 ? -1:1;
}
});
Iterator it = arr.iterator();
while(it.hasNext())
System.out.print(it.next());
}
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
int i,n;
n=s.nextInt();
Vector<String> arr;
arr = new Vector<>();
for(i=0;i<n;i++){
arr.add(s.next());
}
printLargest(arr);
}
}
error:
100
2 8 2 3 6 4 1 1 10 6 3 3 6 1 3 8 4 6 1 10 8 4 10 4 1 3 2 3 2 6 1 5 2 9 8 5 10 8 7 9 6 4 2 6 3 8 8 9 8 2 9 10 3 10 7 5 7 1 7 5 1 4 7 6 1 10 5 4 8 4 2 7 8 1 1 7 4 1 1 9 8 6 5 9 9 3 7 6 3 10 8 10 7 2 5 1 1 9 9 5
Your output:
Your stderr:
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:777)
at java.util.TimSort.mergeAt(TimSort.java:514)
at java.util.TimSort.mergeCollapse(TimSort.java:441)
at java.util.TimSort.sort(TimSort.java:245)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.Vector.sort(Vector.java:1345)
at java.util.Collections.sort(Collections.java:177)
at LargestNumber.printLargest(LargestNumber.java:6)
at LargestNumber.main(LargestNumber.java:32)
Correct output:
9999999998888888888887777777776666666666555555554444444443333333333222222222111111111111111101010101010101010
(Time used: 0.12/1.50, memory used: 23568384/536870912.)`enter code here`
Problem is that your statement XY.compareTo(YX) > 0 ? -1:1 does not return 0 when two variables are equal, which is IllegalArgumentException: Comparison method violates its general contract!
From doc:
Parameters:
o1 - the first object to be compared.
o2 - the second object to be compared.
Returns: a negative integer, zero, or a positive integer as the first
argument is less than, equal to, or greater than the second.
Use following code to compare:
arr.sort((X, Y) -> {
String XY = X + Y;
String YX = Y + X;
return XY.compareTo(YX);
});
What's happening is that you are entering
2 8 2 3 6 4 1 1 10 6 3 3 6 1 3 8 4 6 1 10 8 4 10 4 1 3 2 3 2 6 1 5 2 9 8 5 10 8 7 9 6 4 2 6 3 8 8 9 8 2 9 10 3 10 7 5 7 1 7 5 1 4 7 6 1 10 5 4 8 4 2 7 8 1 1 7 4 1 1 9 8 6 5 9 9 3 7 6 3 10 8 10 7 2 5 1 1 9 9 5
with spaces. When that happens, the input gets treated like a one string without another string to compare to, which is why you got IllegalArgumentException. To fix this code, change your for loop in your main method like this...
for(i=0;i<n;i++){
arr.add(String.valueOf(new Random().nextInt(17)));
}

Using nested for loops to print numbers (java)

I am trying to use nested for loops (java) to print out the following:
331
330
322
311
300
222
111
and I am having some trouble. So far I have:
for(int a = 3; a >=0; a--)
{
for(int b = 3; b>=0; b--)
{
for(int c = 2; c>=0; c--)
{
System.out.println(a + " "+ b +" "+ c);
}
}
}
but that prints out something more like this:
3 3 2
3 3 1
3 3 0
3 2 2
3 2 1
3 2 0
3 1 2
3 1 1
3 1 0
3 0 2
3 0 1
3 0 0
2 3 2
2 3 1
2 3 0
2 2 2
2 2 1
2 2 0
2 1 2
What is wrong with my code? How can I get it to print out the first sequence, not the second? I'm pretty sure it has something to do with the middle loop, but I'm really not sure.
Thanks!
If you have nested for loops, unless you set the limits on the inner loops based on the values of the variables in the outer loops, the pattern generated by the inner loops (second and third numbers, in this case) is gonna be the same in each iteration of the outer loop.
I can't code right now, but it seems to me that if you set b to run from a to zero, instead of from 3 to zero you might get a little closer to what you want.
But still, since it seems there isn't a pattern in what you want to get, it's hard to think of an algorithm to print them. What do these numbers mean?

Java regex split string into different variables

I have been doing web-scraping for a project and I couldn't figure out how to split retrieved strings into different variables.
Array of strings I have retrieved
K. KAYMAKLI TSK 6 5 0 1 19 4 15 15
YEN?CAM? AK 6 4 1 1 14 7 7 13
MORMENEK?E GB 6 4 0 2 10 7 3 12
LEFKE TSK 6 3 2 1 10 8 2 11
SERDARLI GB 6 2 2 2 6 5 1 8
HAM?TKÖY ?HSK 6 2 2 2 8 8 0 8
ÇET?NKAYA TSK 6 2 2 2 6 7 -1 8
DO?AN TBSK 6 2 2 2 12 15 -3 8
YEN? BO?AZ?Ç? DSK 6 2 1 3 9 8 1 7
B. BA?CIL SK 6 1 4 1 8 9 -1 7
MA?USA TÜRK GÜCÜ 6 2 1 3 7 9 -2 7
C?HANG?R GSK 6 1 3 2 8 8 0 6
GENÇL?K GÜCÜ SK 6 1 0 5 6 14 -8 3
YALOVA SK 6 0 2 4 4 18 -14 2
I would like to put the characters until the first integer (in this case 6) into one string
and then each integer into separate variables.
eg.
String team = "YALOVA SK";
int p = 6;
int x = 0;
int y = 2;
int z = 4;
int m = 4;
int n = 18;
int k = -14;
int h = 2;
I was able to split the string by checking character by character to find the first integer and split it there and then assign each character after that to an integer. How can I solve it by using regex?
Note ?'s are turkish characters that are not displayed correctly on the console but are display correctly in the application.
The split() method on a string can be used to split the string based on a particular 'regex'. For reference on split function check Oracle java documentation link. There is also a nice tutorial about using regex in java in the Vogella website.
You can split the string based on '\s'(short white space character) or '\s+'and then use the returned array.
The syntax will be something like this.
String retrievedString = "YALOVA SK 6 0 2 4 4 18 -14 2";
String[] teamInfo=retrievedString.split("\\s");
You can use the string you retrieve from web scraping in place of "retrievedString".
Note the \\ used in the split method is to escape \.
Hope this helps...
I think Scanner class fit perfect to your needs:
Description: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

creating a number pattern with day number

Okay so I need to make a number pattern with day numbers for example: 1 - monday, 2 - tuesday, 3 - wednesday until 7 - sunday. If I put an input "n" I would get the following:
n=4
1 2 3 4
n=7
1 2 3 4 5 6 7
n=12
1 2 3 4 5 6 7 1 2 3 4 5
I've succeeded making this program if n<=14 but if n>14 I get:
n=17
1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10
when it should be:
n=17
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3
this is my code
for (x=1;x<=n;x++){
System.out.print(x+" ");
if (x==7){
for (x=1;x<=(n-7);x++)
System.out.print(x+" ");
break;
}
}
thanks in advance
Try this instead:
for (int i = 0; i < n; i++)
System.out.print(i % 7 + 1 + " ");
Whenever you want to have that "repeating" behavior, where a sequence of numbers goes up to a certain value and then restarts, use the % operator and a bit of modular arithmetic to achieve the desired effect. For n = 17 the above will print:
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3

how to read digits from text file and save it in an array "using MATLAB or JAVA"?

I have similar problem with little changes, which is:
I have text file contain large number of lines with different sizes "i.e. not all lines has same length"
each line contain integers only.
as an example A.txt =
4 6 4 1 2 2 5 7 7
0 9 5 5 3 2 43 3 32 9 0 1 3 1
3 4 5 6 7 4
34 5 8 9 0 7 6 2 4 5 6 6 7 5 4 3 2 21 4 9 8 4 2 1 5
I want to put these integers into an array so each integer will be an element in the array and saving lines from "overlapping" i.e. I need to keep each line as it is.
Could anybodyy help me with this?
a = dlmread('a.txt')
a =
Columns 1 through 21
4 6 4 1 2 2 5 7 7 0 0 0 0 0 0 0 0 0 0 0 0
0 9 5 5 3 2 43 3 32 9 0 1 3 1 0 0 0 0 0 0 0
3 4 5 6 7 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
34 5 8 9 0 7 6 2 4 5 6 6 7 5 4 3 2 21 4 9 8
Columns 22 through 25
0 0 0 0
0 0 0 0
0 0 0 0
4 2 1 5
I would do the following:
1) Create a new Array for each line
2) Read lines, one at a time from file
3) Split each line by the "space" char
4) Iterate through the String[] that you get from the split operation, passing each value to Integer.parseInt(value);
5) Store value in array;
6) When reading next line, create new array to store the new lines values.
You can read the data using Scanner, one line at a time, and store the numbers in a List, e.g. an ArrayList:
import java.util.*;
import java.io.*;
public class Numbers
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner data = new Scanner(new File("A.txt"));
List<List<Integer>> ints = new ArrayList<List<Integer>>();
while (data.hasNextLine()) {
List<Integer> lineInts = new ArrayList<Integer>();
Scanner lineData = new Scanner(data.nextLine());
while (lineData.hasNextInt()) {
lineInts.add(lineData.nextInt());
}
ints.add(lineInts);
}
System.out.println(ints);
}
}
This code opens the file for reading, and creates a two-dimensional ArrayList. The outer list contains a list for each line in the file. The inner lists contain the integers on the respective lines. Note that empty lines result in empty lists. Also, you will have to properly handle any IO exception, unlike the code shown above.
If you really want the integers in a two dimensional array instead of an ArrayList, then you'll either have to call toArray, or alter the code above. That is left as an exercise for the reader.

Categories