IPA FE Subject B 2024 Question 5

A question IPA Japan published in 令和6年度 基本情報技術者試験 科目B 公開問題 問5, translated into English. ITPEC is based on the same exams.

From the answer group below, select the correct combination of answers to be inserted into A through C in the program. Here, the array index starts at 1.

There is order data that records, for each order, the list of the items purchased in one order. The table shows an example of the order data.

Table Example of order data

Order numberList of purchased items
1A, B, D
2A, D
3A
4A, B, E
5B
6C, E

From the order data, the degree of association Lxy, which shows the tendency of item x and item y to be purchased in the same order, is calculated by the following formula.

Lxy = Mxy × total number of ordersKx × Ky

Here, Mxy represents the number of orders in which item x and item y were purchased in the same order, Kx represents the number of orders in which item x was purchased, and Ky represents the number of orders in which item y was purchased. In the example in the table, MAB is 2, the total number of orders is 6, KA is 4, and KB is 3, so the degree of association LAB between item A and item B is (2 × 6) / (4 × 3) = 1.0.
The procedure putRelatedItem outputs, based on the order data stored in the global variable orders, one of the items that have the largest degree of association with the item given as the argument, and that degree of association. In the program, an item is represented by a string, an order by an array of the purchased items, and the order data by an array of orders. The order data is assumed to contain two or more kinds of items. In addition, no item other than the items in the order data is given as the argument.

[Program]
// Order data (here, the example in the table is given)
global: string [][]: orders ← {{"A", "B", "D"}, {"A", "D"}, {"A"},
{"A", "B", "E"}, {"B"}, {"C", "E"}}
○putRelatedItem(string: item)
string []: allItems ← an array that stores the strings contained in orders
without duplicates, in dictionary order
// {"A", "B", "C", "D", "E"} in the example in the table
string []: otherItems ← a copy of allItems from which the elements whose value is item
are removed
integer: i, itemCount ← 0
integer []: arrayK ← {as many 0s as the number of elements in otherItems}
integer []: arrayM ← {as many 0s as the number of elements in otherItems}
real: valueL, maxL ← −∞
string []: order
string: relatedItem
for (assign the elements of orders to order one by one)
if (the value of one of the elements of order is equal to the value of item)
increase the value of itemCount by 1
endif
for (increase i from 1 to the number of elements in otherItems by 1)
if (the value of one of the elements of order is equal to the value of otherItems[i])
if (the value of one of the elements of order is equal to the value of item)
increase the value of A by 1
endif
increase the value of B by 1
endif
endfor
endfor
for (increase i from 1 to the number of elements in otherItems by 1)
valueL ← (arrayM[i] × C) ÷ (itemCount × arrayK[i])
/* calculated as real numbers */
if (valueL is greater than maxL)
maxL ← valueL
relatedItem ← otherItems[i]
endif
endfor
output the value of relatedItem and the value of maxL in this order, separated by a comma

Answer group

OptionABC

Explanation

A: arrayM[i], B: arrayK[i], C: the number of elements in orders — in the formula, arrayM[i] must count the orders that hold both items, arrayK[i] the orders that hold otherItems[i], and the multiplier is the total number of orders.

itemCount counts the orders that contain item, so it is Kx. For an order holding otherItems[i], A is increased only when item is in it too, so A counts Mxy: arrayM[i]. B is increased for every order holding otherItems[i], so it counts Ky: arrayK[i]. Then valueL is Mxy × C ÷ (Kx × Ky), so C is the total number of orders.

For item = "A": arrayK = {3, 1, 2, 2} and arrayM = {2, 0, 2, 1} for B, C, D, E, itemCount = 4; the values are 1.0, 0, 1.5 and 0.75, and the output is D, 1.5.

Answer (e)

Why not others:

  • •(a), (b) and (c) — swap A and B, so the divisor holds the joint count: for C, never bought with A, it is 4 × 0, a division by zero, and (b) gives LAB = 2.25, not 1.0
  • •(d) — multiplies by the 5 items in allItems, not the 6 orders: same item, wrong value, D, 1.25
  • •(f) — multiplies by the 4 items in otherItems: D, 1

Key rule: This degree of association is the lift of an association rule: joint count × total orders ÷ the product of the two single-item counts.

Math & Data Science · AI-written, may contain errors

This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.