Node.js tries to solve the reasoning questions of the Criminal Investigation Department of Jiangsu internet police

 

A month ago, Jiangsu network police A set of "2018 criminal investigation subject reasoning test questions" was released on Weibo, which is hard to defeat many heroes, and the comment area is full of leather voice.

However, when this incident came to the ears of ape family, the painting style suddenly changed. Heroes from Java, Python, PHP and other schools have used the school language to solve the criminal investigation and reasoning array of Jiangsu online police with the little wisdom of enumeration.

We are not willing to lag behind, so we have today's "Node.js to solve the reasoning test of Criminal Investigation Department of Jiangsu online police".

If you don't want to talk much, first picture:

Surrender code to show Innocence:

question.js

/**
 * Created by lonelydawn at 2018-03-29.
 */
var options = ['A', 'B', 'C', 'D']
// Define the question answer array, set the first element of the array to null, and align the array subscript and question number with offset
var answers = [, 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']
var result = []
/** 
 * 01. The answer to this question is
 * A. A    B. B    C. C    D. D
 */

/**
 * 02. The answer to question 5 is
 * A. C    B. D    C. A    D. B
 */
var getQues02 = function (answers) {
	if (answers[5] === 'C') {
		return 'A'
	} else if (answers[5] === 'D') {
		return 'B'
	} else if (answers[5] === 'A') {
		return 'C'
	} else if (answers[5] === 'B') {
		return 'D'
	}
	return -1
}

/**
 * 03. Which of the following answers is different from the other three
 * A. 3. B. 6. C. 2. D. 4
 */
var getQues03 = function (answers) {
	if (answers[3] !== answers[6] && answers[3] !== answers[2]
			&& answers[3] !== answers[4]) {
		return 'A'
	} else if (answers[6] !== answers[3] && answers[6] !== answers[2]
			&& answers[6] !== answers[4]) {
		return 'B'
	} else if (answers[2] !== answers[3] && answers[2] !== answers[6]
			&& answers[2] !== answers[4]) {
		return 'C'
	} else if (answers[4] !== answers[3] && answers[4] !== answers[6]
			&& answers[4] !== answers[2]) {
		return 'D'
	}
	return -1
}

/**
 * 04. Which two of the following questions have the same answer
 * A. 1,5 B. 2,7 C. 1,9 D. 6,10
 */
var getQues04 = function (answers) {
	if (answers[1] === answers[5]) {
		return 'A'
	} else if (answers[2] === answers[7]) {
		return 'B'
	} else if (answers[1] === answers[9]) {
		return 'C'
	} else if (answers[6] === answers[10]) {
		return 'D'
	}
	return -1
}

/**
 * 05. Which of the following is the same as this question
 * A. 8. B. 4. C. 9. D. 7
 */
var getQues05 = function (answers) {
	if (answers[8] === 'A') {
		return 'A'
	} else if (answers[4] === 'B') {
		return 'B'
	} else if (answers[9] === 'C') {
		return 'C'
	} else if (answers[7] === 'D') {
		return 'D'
	}
	return -1
}

/**
 * 06. Which two of the following questions have the same answer as question 8
 * A. 2,4 B. 1,6 C. 3,10 D. 5,9
 */
var getQues06 = function (answers) {
	if (answers[8] === answers[2] && answers[8] === answers[4]) {
		return 'A'
	} else if (answers[8] === answers[1] && answers[8] === answers[6]) {
		return 'B'
	} else if (answers[8] === answers[3] && answers[8] === answers[10]) {
		return 'C'
	} else if (answers[8] === answers[5] && answers[8] === answers[9]) {
		return 'D'
	}
	return -1
}
/**
 * 07. In the ten questions, the letter of the choice with the least number of times selected is
 * A. C    B. B    C. A     D. D
 */
var getQues07 = function (answers) {
	var counter = [0, 0, 0, 0]
	answers.forEach(function (answer) {
		options.forEach(function (option, index) {
			if (answer === option) {
				counter[index]++
			}
		})
	})
	return options[counter.indexOf(Math.min(counter[0], counter[1], counter[2], counter[3]))]
}
/**
 * 08. Which of the following is not adjacent to the first in letters
 * A. Question 7 B. 5 C. 2 D. 10
 * Solution: A, B, C and D are four letters. First, list which letters are not adjacent to each other, and then judge
 * PS: There may be multiple solutions to this question, but only the first one is taken because the question is clearly marked with "single choice"
 */
var getQues08 = function (answers) {
	var notNeighbor = {
		A: ['C', 'D'],
		B: ['D'],
		C: ['A'],
		D: ['A', 'B']
	}[answers[1]]
	if (notNeighbor.indexOf(answers[7]) > -1) {
		return 'A'
	} else if (notNeighbor.indexOf(answers[5]) > -1) {
		return 'B'
	} else if (notNeighbor.indexOf(answers[2]) > -1) {
		return 'C'
	} else if (notNeighbor.indexOf(answers[10]) > -1) {
		return 'D'
	}
	return -1
}
/**
 * 09. It is known that "the answers of the first and sixth questions are the same" and "the answers of the X and 5 questions are the same" are true and false
 * A. 6. B. 10. C. 2. D. 9
 * Solution: 
 * The answer to question X may be the same as or different from question 5, 
 * So we judge it in two ways
 * PS: 
 * If the answers of the first and sixth questions are the same, then x and 5 answers are not the same, and X's answers may have multiple values (A/B/C/D)
 * If it is different, then x is the same as 5, and the answer of X can only take a unique value
 * But even if the answer value of X is unique, X may have multiple values (1-10)
 * I intersect the candidate array of these values with the four options of the topic, so as to limit the unique X value
 */
var getQues09 = function (answers) {
	var arr = []
	var x = -1
	if (answers[1] === answers[6]) {
		for (var i = 0; i < answers.length; i++) {
			if (i !== 5 && answers[i] !== answers[5]) {
				var answer = ['A', 'B', 'C', 'D'][[6, 10, 2, 9].indexOf(i)]
				if (answer) {
					return answer
				}
			}
		}
	} else {
		for (var i = 0; i < answers.length; i++) {
			if (i !== 5 && answers[i] === answers[5]) {
				var answer = ['A', 'B', 'C', 'D'][[6, 10, 2, 9].indexOf(i)]
				if (answer) {
					return answer
				}
			}
		}
	}
}
/**
 * 10. In the 10 questions, the difference between the most and the least of the four letters of ABCD is
 * A. 3    B. 2    C. 4     D. 1
 */
var getQues10 = function (answers) {
	var counter = [0, 0, 0, 0]
	answers.forEach(function (answer) {
		options.forEach(function (option, index) {
			if (answer === option) {
				counter[index]++
			}
		})
	})
	var max = Math.max(counter[0], counter[1], counter[2], counter[3])
	var min = Math.min(counter[0], counter[1], counter[2], counter[3])
	return {
		1: 'D',
		2: 'B',
		3: 'A',
		4: 'C'
	}[max - min]
}

// Recursion + loop to establish enumeration queue
var recurse = function (answers, index) {
	var answers = JSON.parse(JSON.stringify(answers))
	if (index < 10) {
		for (var i = 0; i < options.length; i++) {
			answers[index] = options[i]
			recurse(answers, index + 1)
			if (getQues02(answers) !== answers[2]) {
				continue
			}
			if (getQues03(answers) !== answers[3]) {
				continue
			}
			if (getQues04(answers) !== answers[4]) {
				continue
			}
			if (getQues05(answers) !== answers[5]) {
				continue
			}
			if (getQues06(answers) !== answers[6]) {
				continue
			}
			if (getQues07(answers) !== answers[7]) {
				continue
			}
			if (getQues08(answers) !== answers[8]) {
				continue
			}
			if (getQues09(answers) !== answers[9]) {
				continue
			}
			if (getQues10(answers) !== answers[10]) {
				continue
			}
			result = JSON.parse(JSON.stringify(answers))
		}
	}
}

var begin = new Date().getTime()
recurse(answers, 1)
console.log('\nresult:', result.length > 0 ? result.slice(1, result.length) : 'none')
console.log('time:', (new Date().getTime() - begin) / 1000 + 's')

PS: all in the comments. If you are interested, please comment.

Keywords: JSON network Java Python

Added by covert215 on Wed, 01 Apr 2020 10:46:44 +0300