Skip to content

Instantly share code, notes, and snippets.

@argonism
Created January 17, 2021 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save argonism/eca921f34e4db916ab8746f021ee00b4 to your computer and use it in GitHub Desktop.
Save argonism/eca921f34e4db916ab8746f021ee00b4 to your computer and use it in GitHub Desktop.
# p: posting list
# p must be like [docID_1, docID_2, docID_3, ...]
def union(p1, p2):
answer = []
p1_idx = 0
p2_idx = 0
count = 0
while len(p1) > p1_idx or len(p2) > p2_idx:
if len(p1) <= p1_idx:
answer.append(p2[p2_idx])
p2_idx += 1
continue
elif len(p2) <= p2_idx:
answer.append(p1[p1_idx])
p1_idx += 1
continue
if p1[p1_idx] == p2[p2_idx]:
answer.append(p1[p1_idx])
p1_idx += 1
p2_idx += 1
else:
if p1[p1_idx] < p2[p2_idx]:
answer.append(p1[p1_idx])
p1_idx += 1
else:
answer.append(p2[p2_idx])
p2_idx += 1
count += 1
# print("count:", count)
return answer, count
# p: posting list
# p must be like [docID_1, docID_2, docID_3, ...]
def intersect(p1, p2):
answer = []
p1_idx = 0
p2_idx = 0
count = 0
while len(p1) > p1_idx and len(p2) > p2_idx:
if p1[p1_idx] == p2[p2_idx]:
answer.append(p1[p1_idx])
p1_idx += 1
p2_idx += 1
else:
if p1[p1_idx] < p2[p2_idx]:
p1_idx += 1
else:
p2_idx += 1
count += 1
print("count:", count)
return answer, count
if __name__ == "__main__":
def _not(doc_list, p):
return [ e for e in doc_list if not e in p]
def print_variables():
x = len(Brutus)
y = len(Caesar)
N = len(D)
print(f"x: {x}")
print(f"y: {y}")
print(f"N: {N}")
print(f"NOT(Caesar): {NOT_caesar}")
print(f"NOT(Caesar) length: {len(NOT_caesar)}")
print(f"N - y > y: {N - y > y}")
print(f"x + y: {x + y}")
print(f"loop count: {count}")
print("\n\n -- test intersect case -- \n\n")
Brutus = [1, 2, 4, 11, 31, 45, 173, 174]
D = [ n for n in range(1, 200)]
print(D)
print("********* N - y > y のとき *********")
Caesar = [1, 2, 4, 5, 6, 16, 57, 132, 133]
NOT_caesar = _not(D, Caesar)
result, count = intersect(Brutus, NOT_caesar)
print_variables()
print("********* N - y < y のとき *********")
# y > N/2 となるように、Dから3の倍数を除いたものを使う。
Caesar = [n for n in D if not n % 3 == 0]
NOT_caesar = _not(D, Caesar)
result, count = intersect(Brutus, NOT_caesar)
print_variables()
print("\n\n -- test union case -- \n\n")
print("********* N - y > y のとき *********")
Caesar = [1, 2, 4, 5, 6, 16, 57, 132, 133]
NOT_caesar = _not(D, Caesar)
result, count = union(Brutus, NOT_caesar)
print_variables()
print("********* N - y < y のとき *********")
# y > N/2 となるように、Dから3の倍数を除いたものを使う。
Caesar = [n for n in D if not n % 3 == 0]
NOT_caesar = _not(D, Caesar)
result, count = union(Brutus, NOT_caesar)
print_variables()
@argonism
Copy link
Author

D: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199]

********* intersect case *********

----- N - y > y のとき -----

Caesar: [1, 2, 4, 5, 6, 16, 57, 132, 133]
x: 8
y: 9
N: 199
N - y: 190
NOT(Caesar): [3, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199]
NOT(Caesar) length: 190
N - y > y: True
x + y: 17
loop count: 168
result: [11, 31, 45, 173, 174]

----- N - y < y のとき -----

Caesar: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80, 82, 83, 85, 86, 88, 89, 91, 92, 94, 95, 97, 98, 100, 101, 103, 104, 106, 107, 109, 110, 112, 113, 115, 116, 118, 119, 121, 122, 124, 125, 127, 128, 130, 131, 133, 134, 136, 137, 139, 140, 142, 143, 145, 146, 148, 149, 151, 152, 154, 155, 157, 158, 160, 161, 163, 164, 166, 167, 169, 170, 172, 173, 175, 176, 178, 179, 181, 182, 184, 185, 187, 188, 190, 191, 193, 194, 196, 197, 199]
x: 8
y: 133
N: 199
N - y: 66
NOT(Caesar): [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 102, 105, 108, 111, 114, 117, 120, 123, 126, 129, 132, 135, 138, 141, 144, 147, 150, 153, 156, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 192, 195, 198]
NOT(Caesar) length: 66
N - y > y: False
x + y: 141
loop count: 64
result: [45, 174]

********* union case *********

----- N - y > y のとき -----

Caesar: [1, 2, 4, 5, 6, 16, 57, 132, 133]
x: 8
y: 9
N: 199
N - y: 190
NOT(Caesar): [3, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199]
NOT(Caesar) length: 190
N - y > y: True
x + y: 17
loop count: 168
result: [1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199]

----- N - y < y のとき -----

Caesar: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80, 82, 83, 85, 86, 88, 89, 91, 92, 94, 95, 97, 98, 100, 101, 103, 104, 106, 107, 109, 110, 112, 113, 115, 116, 118, 119, 121, 122, 124, 125, 127, 128, 130, 131, 133, 134, 136, 137, 139, 140, 142, 143, 145, 146, 148, 149, 151, 152, 154, 155, 157, 158, 160, 161, 163, 164, 166, 167, 169, 170, 172, 173, 175, 176, 178, 179, 181, 182, 184, 185, 187, 188, 190, 191, 193, 194, 196, 197, 199]
x: 8
y: 133
N: 199
N - y: 66
NOT(Caesar): [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 102, 105, 108, 111, 114, 117, 120, 123, 126, 129, 132, 135, 138, 141, 144, 147, 150, 153, 156, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 192, 195, 198]
NOT(Caesar) length: 66
N - y > y: False
x + y: 141
loop count: 64
result: [1, 2, 3, 4, 6, 9, 11, 12, 15, 18, 21, 24, 27, 30, 31, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 102, 105, 108, 111, 114, 117, 120, 123, 126, 129, 132, 135, 138, 141, 144, 147, 150, 153, 156, 159, 162, 165, 168, 171, 173, 174, 177, 180, 183, 186, 189, 192, 195, 198]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment