Hello Friends,
Hope all are doing fine.
Here is a tricky issue.
my input file is like this
07 10 14 20 21
03 15 27 30 32
01 10 11 19 30
02 06 14 15 17
01 06 20 25 29
Logic:
- Please print another column as "0-0-0-0-0" for the first and second rows.
- Read the first column of third row, which is 1. Look for this value in all columns of first and second row. 1 is not present in first or second rows, so print a value 2 for this.
- Then read the second column of third row, which is 10. There is 10 in first and not in second rows. So, it basically skipped the second row only. Now print a value of 1.
- Then read the third column of third row, which is 11. 11 does not appear in first or second rows, so print a value of 2.
- 19 has no appearances in first or second rows, so its value will be 2.
- 30 did not appear in first row, but it appears in second row, which is the IMMEDIATE row of the current row that is being read which is row no.3. So, since no rows were skipped, we will print 0 for this one
So far the output will be
07 10 14 20 21 0-0-0-0-0
03 15 27 30 32 0-0-0-0-0
01 10 11 19 30 2-1-2-2-0
02 06 14 15 17
01 06 20 25 29
Logic Continued:
7. Now read the first column of fourth row, which is 2. Look for this value across anywhere in all the three rows above. Since 2 is not present, we will print a value 3 for this. Because it is not present in the three rows above.
8. Now read the second column of 4th row, which is 6. 6 is also not present across any of the three rows above. So its value will also be 3.
9. Read the third column of 4th row, which is 14. 14 is present in first row only but not in the second or third rows, so it skipped two rows. So print a value of 2 for this.
10. Read forth column of 4th row, which is 15. It is not present in first row. Fine. It is present in second row and not in third row. It basically skipped one IMMEDIATE row which is the third. We don't really care for the first row here. All we worry about is the number of times a value skipped after it appeared in the input file. So, the value for 15 will be 1.
11. Read last column of 4th row, which is 17. It is not present in any of the three rows above. So, we will print 3. Basically, if a value is not present across all top rows of the current row being considered, we HYPOTHESIZE that this value was PRESENT before the first line of the input. That is the reason we are printing 3 for the values that are not seen in any of the rows above row number 4.
So far, the output looks like this
07 10 14 20 21 0-0-0-0-0
03 15 27 30 32 0-0-0-0-0
01 10 11 19 30 2-1-2-2-0
02 06 14 15 17 3-3-2-1-3
01 06 20 25 29
Logic Continued:
12. Now the last row's first value which is 01. This iss present in third row and skipped the 4th. So, its value will be 1. If you see a value present in any row above the current row, then you DONT have to move any way further up because you have already seen that value.
13. Second column of last row, which is 06. This is present in 4th row. So, the value will be zero and DO NOT check any lines above because a value has been encountered.
14. Third column of last row, which is 20. It is present in first row but not in second, third or fourth rows. So, it skipped three rows. Print a value of 3 for this.
15. Forth column of last row, which is 25. This is not present anywhere. "Remember our hypothesis - this value occurred before the first line". So, we are printing 4 for this.
16. Fifth column of last row, which is 29. Present nowhere. So, print a value of 4.
Here is the final output
07 10 14 20 21 0-0-0-0-0
03 15 27 30 32 0-0-0-0-0
01 10 11 19 30 2-1-2-2-0
02 06 14 15 17 3-3-2-1-3
01 06 20 25 29 1-0-3-4-4
I would also like to have the frequency of unique numbers in the output column like this here
0=12times
1=3times
2=4times
3=4times
4=2times
Please ask me any questions or comments in case of any doubt.
P.S:
a. My columns are always 5.
b. My input file always has 25 records only.
c. A bonus of 5000 bits will be awarded to the best working solution.
Thank You!