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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
|
import sys from os.path import exists import re
file_name = input('Which text file do you want to use for the puzzle? ' ).removesuffix('\n') if not exists(file_name): print('Incorrect input, giving up.') sys.exit()
name_set = set() all_names = []
combined_text = '' speaker_said_part_list = [] statement_sentence_list = []
speaker_names = []
with open(file_name) as file: for line1 in file: if line1.isspace(): continue line1 = line1.strip() combined_text += line1 + ' '
def find_single_sentences(combined_text): sentence_group = [] combined_text_list = combined_text.split()
cur_index = 0 while cur_index < len(combined_text_list): cur_sentence = '' if combined_text_list[cur_index][0].isupper() or ( combined_text_list[cur_index].startswith('"') and not combined_text_list[ cur_index - 1].endswith(':')):
while cur_index < len(combined_text_list): cur_sentence += combined_text_list[cur_index] + ' ' if (combined_text_list[cur_index].endswith(('."', '!"', '?"'))) or combined_text_list[ cur_index].endswith(('.', '!', '?')): break cur_index += 1 sentence_group.append(cur_sentence.strip()) cur_index += 1 return sentence_group
def partition(sentence_group): for sentence in sentence_group: statement_part = '' speaker_said_part = ''
if '"' in sentence: if sentence.find(' "') != -1 and sentence.find(',"') != -1: if 'Sir' in sentence[:sentence.find(' "')]: speaker_said_part = sentence[:sentence.find(' "')] statement_part = sentence[sentence.find(' "'):sentence.find(',"')] elif 'Sir' in sentence[sentence.find(',"'):]: speaker_said_part = sentence[sentence.find(',"') + 3:] statement_part = sentence[sentence.find(' "'):sentence.find(',"') + 2] elif sentence.find(': "') != -1: if sentence.find(': "') != -1: speaker_said_part = sentence[:sentence.find(': "')] statement_part = sentence[sentence.find(': "'):] elif sentence.find(' "') != -1: speaker_said_part = sentence[:sentence.find(' "')] statement_part = sentence[sentence.find(' "'):] elif sentence.find(',"') != -1: speaker_said_part = sentence[sentence.find(',"') + 3:] statement_part = sentence[:sentence.find(',"') + 3]
elif sentence.find(' "') != -1: speaker_said_part = sentence[:sentence.find(' "')] statement_part = sentence[sentence.find(' "'):] else: continue
speaker_said_part_list.append(speaker_said_part) statement_sentence_list.append(statement_part) else: if 'Sir ' in sentence: name = re.findall('Sir\\s([A-Z][a-z]+)', sentence) for n in name: name_set.add(n) if 'Sirs ' in sentence: name = re.findall('[A-Z][a-z]+', sentence.split('Sirs ')[1]) for n in name: name_set.add(n)
def add_Sir_if_dont_have(statement_sentence_list): ''' Processing the statement part Split into an array of strings Remove punctuation, remove null Find the first Sir, start reading words starting with capital letters, if it is Sir then read the next one, if it is not Sir, the If it is Sir, then read the next one, if it is not Sir, then test whether the next word of the current word is Sir, if not, then replace the next word with 'Sir word' until it meets and or.
Replace the word at the index position after and or with 'Sir word'.
Splice strings to form strings Append back to the temporary list at the end of each round. ''' tmp_list0 = [] for statement_sentence in statement_sentence_list: statement_sentence = statement_sentence.split() statement_sentence = [re.sub('[,:\".?!]', '', element_without_quote) for element_without_quote in statement_sentence] statement_sentence = [element for element in statement_sentence if element != '']
if 'Sir' in statement_sentence: first_sir_index = statement_sentence.index('Sir')
for i in range(first_sir_index + 1, len(statement_sentence)): word = statement_sentence[i]
if word == 'and' or word == 'or': if statement_sentence[i + 1] == 'I': statement_sentence[i + 1] = 'Sir ' + statement_sentence[i + 1] break
if statement_sentence[i + 1][0].islower() and statement_sentence[i + 1] != 'and' and statement_sentence[ i + 1] != 'or': break
if word == 'Sir': continue
if word[0].isupper(): if i + 1 < len(statement_sentence) and statement_sentence[i + 1] != 'Sir' and \ statement_sentence[ i + 1] != 'or' and statement_sentence[i + 1] != 'and': statement_sentence[i + 1] = 'Sir ' + statement_sentence[i + 1] statement_sentence = ' '.join(element for element in statement_sentence) tmp_list0.append(statement_sentence)
elif 'I' in statement_sentence: statement_sentence = ' '.join(element for element in statement_sentence).replace('I', 'Sir I') tmp_list0.append(statement_sentence)
elif 'us' in statement_sentence: statement_sentence = ' '.join(element for element in statement_sentence) tmp_list0.append(statement_sentence)
return tmp_list0
def add_Sir_if_dont_have_speaker_part(speaker_said_part_list): ''' Processing of speaker_said_part_list part Split into an array of strings Remove punctuation, remove blanks Find the first Sir, or Sirs, start to read the word that starts with a capital letter, if it's Sir then read the next one, if it's not Sir or Sirs, if it's a word that starts with a lowercase letter, skip it, if it's a word that starts with a capital letter (definitely not Sir or Sirs) replace it with the 'Sir word', until it encounters and or.
Replace the word at the index position after and or with 'Sir word'.
Splice strings to form strings Append back to the temporary list at the end of each round. ''' tmp_list1 = [] first_sir_index = 0 first_sirs_index = 0 for speaker_said_part in speaker_said_part_list: speaker_said_part = speaker_said_part.split() speaker_said_part = [re.sub('[,:\".?!]', '', element_without_quote) for element_without_quote in speaker_said_part] speaker_said_part = [element for element in speaker_said_part if element != ''] if first_sir_index or first_sirs_index: if first_sir_index: first_sir_index = speaker_said_part.index('Sir') if first_sirs_index: first_sirs_index = speaker_said_part.index('Sirs') if first_sir_index and first_sirs_index: first_index = min(first_sir_index, first_sirs_index) else: first_index = first_sir_index if first_sir_index else first_sirs_index
for i in range(first_index + 1, len(speaker_said_part)): word = speaker_said_part[i]
if word == 'and' or word == 'or': break
if word == 'Sir' or word == 'Sirs' or 'I': continue
if word[0].islower(): continue
if word[0].isupper(): if i + 1 < len(speaker_said_part) and speaker_said_part[i + 1] != 'Sir' and speaker_said_part[ i + 1] != 'Sirs' and \ speaker_said_part[ i + 1] != 'or' and speaker_said_part[i + 1] != 'and': speaker_said_part[i + 1] = 'Sir ' + speaker_said_part[i + 1]
speaker_said_part = ' '.join(element for element in speaker_said_part) tmp_list1.append(speaker_said_part) return tmp_list1
def find_speaker_person(speaker_said_part_list): ''' Find speaker_person Find the index of the last Sir, because it is the last Sir of speaker_part that is the speaker. ''' for speaker_said_part in speaker_said_part_list: last_sir_index = speaker_said_part.split()[::-1].index('Sir') - 1
speaker_names.append(speaker_said_part.split()[::-1][last_sir_index])
def replace_I_in_statement(speaker_names, speaker_said_part_list, statement_sentence_list): ''' Find mentioned person If 'Sir I' is in the statement, replace it with 'Sir speaker_name'. ''' idx = 0 tmp_list2 = [] for speaker, statement in zip(speaker_said_part_list, statement_sentence_list): if 'Sir I ' in statement: statement = statement.replace('Sir I ', f'Sir {speaker_names[idx]} ') idx += 1 tmp_list2.append(statement) return tmp_list2
def find_all_names(speaker_said_part_list, statement_sentence_list): for speak, statement in zip(speaker_said_part_list, statement_sentence_list): speak = speak.split() statement = statement.split() speak_statement = speak + statement for idx in range(len(speak_statement) - 1): if speak_statement[idx] == 'Sir' or speak_statement[idx] == 'Sirs': name_set.add(speak_statement[idx + 1]) name_set.discard('Sir') name_set.discard('Sirs')
ordered_name_set = sorted(name_set) return ordered_name_set
def knight_or_knave(statement_sentence_list): identity_list = [] for statement_sentence in statement_sentence_list: identity = '' if 'Knaves' in statement_sentence or 'Knave' in statement_sentence: identity = 'knave' elif 'Knights' in statement_sentence or 'Knight' in statement_sentence: identity = 'knight' identity_list.append(identity) return identity_list
def collect_principle(statement_sentence_list, statement_names): principle_list = [] for idx, statement_sentence in enumerate(statement_sentence_list): length_mentioned_person = len(statement_names[idx]) principle = '' if 'At least one' in statement_sentence or 'at least one' in statement_sentence and length_mentioned_person >= 2: principle = 'at least' elif 'At most' in statement_sentence or 'at most' in statement_sentence and length_mentioned_person >= 2: principle = 'at most' elif 'Exactly' in statement_sentence or 'exactly' in statement_sentence and length_mentioned_person >= 2: principle = 'exactly' elif 'All' in statement_sentence or 'all' in statement_sentence: principle = 'all' elif ' am ' in statement_sentence and length_mentioned_person == 1: principle += 'am' elif ('at least one' and 'at most one' and 'exactly one' and ' or ' and 'At least one' and 'At most one' and 'Exactly one') not in statement_sentence and 'is' in statement_sentence and length_mentioned_person == 1: principle = 'is' elif ' or ' in statement_sentence and 'is' in statement_sentence and length_mentioned_person >= 2: principle = 'or' elif ' and ' in statement_sentence and ' are ' in statement_sentence: principle = 'and' principle_list.append(principle) return principle_list
def find_pairs(speaker_names, identity_list, principle_list, statement_names): sir_statements_pairs = {} for idx in range(len(speaker_names)): speaker = speaker_names[idx] identity = identity_list[idx] principle = principle_list[idx] mentioned_people = statement_names[idx]
if speaker not in sir_statements_pairs: sir_statements_pairs[speaker] = [[[], [], ""]] sir_statements_pairs[speaker][0][0].extend(mentioned_people) sir_statements_pairs[speaker][0][1] = principle sir_statements_pairs[speaker][0][2] = identity elif speaker in sir_statements_pairs: sir_statements_pairs[speaker].append([mentioned_people, principle, identity])
return sir_statements_pairs
def update_names_list(truth_table_of_names_list, garbage): updated_list = [] garbage_set = set(garbage) for name in truth_table_of_names_list: if name not in garbage_set: updated_list.append(name) return updated_list
def principle_handler(truth_table_of_names_list, all_names, speaker, *statement): name_except_speaker = [] if speaker in statement[0]: for name in statement[0]: if name != speaker: name_except_speaker.append(name) else: name_except_speaker = statement[0]
if not truth_table_of_names_list: return
if statement[1] == 'am': if statement[2] == 'knave': return elif statement[2] == 'knight': pass elif statement[1] == 'is': if statement[2] == 'knave': garbage = [] for single_part in truth_table_of_names_list: for except_speaker in name_except_speaker: if single_part[all_names.index(speaker)] == single_part[all_names.index(except_speaker)]: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif statement[2] == 'knight': garbage = [] for single_part in truth_table_of_names_list: for except_speaker in name_except_speaker: if single_part[all_names.index(speaker)] != single_part[all_names.index(except_speaker)]: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif statement[1] == 'all': if statement[2] == 'knave': garbage = [] for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt == total: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif statement[2] == 'knight': garbage = [] for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 1: for except_speaker in name_except_speaker: if single_part in truth_table_of_names_list and single_part[ all_names.index(except_speaker)] == 0: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif statement[1] == 'and': if statement[2] == 'knave': garbage = [] if speaker in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 1: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 0: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt == total: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif speaker not in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt == total: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt != total: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif statement[2] == 'knight': garbage = [] if speaker in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 1: for except_speaker in name_except_speaker: if single_part in truth_table_of_names_list and single_part[ all_names.index(except_speaker)] == 0: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif speaker not in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 1: for except_speaker in name_except_speaker: if single_part in truth_table_of_names_list and single_part[ all_names.index(except_speaker)] == 0: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 0: cnt = 0 total = len(statement[0]) for except_speaker in name_except_speaker: if single_part in truth_table_of_names_list and single_part[ all_names.index(except_speaker)] == 1: cnt += 1 if cnt == total: garbage.append(single_part)
truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage)
elif statement[1] == 'at least' or statement[1] == 'or': if statement[2] == 'knave': if speaker in statement[0]: garbage = [] for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 1: cnt += 1 if cnt == total: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage)
elif speaker not in statement[0]: garbage = [] for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt != 0: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 1: cnt += 1 if cnt == total: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage)
elif statement[2] == 'knight': if speaker in statement[0]: garbage = [] for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt != total: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif speaker not in statement[0]: garbage = [] for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 1: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt == total: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 0: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt != total: garbage.append(single_part)
truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage)
elif statement[1] == 'exactly': total = len(name_except_speaker) if statement[2] == 'knave': garbage = [] if speaker in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt >= 1: pass else: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt != 1: garbage.append(single_part) truth_table_of_names_list = update_names_list(truth_table_of_names_list, garbage) elif speaker not in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt == 1: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt != 1: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage)
elif statement[2] == 'knight': garbage = [] if speaker in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt0 = 0 cnt1 = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt0 += 1 elif single_part[all_names.index(except_speaker)] == 1: cnt1 += 1 if total == cnt0 or cnt1 > 1: pass else: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt != total: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif speaker not in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt0 = 0 cnt1 = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt0 += 1 elif single_part[all_names.index(except_speaker)] == 1: cnt1 += 1 if total == cnt0 or cnt1 == total: pass else: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 1: cnt += 1 if cnt != 1: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage)
elif statement[1] == 'at most': if statement[2] == 'knave': if speaker in statement[0]: garbage = [] for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt0 = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt0 += 1 if cnt0 >= 1: pass else: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt0 = 0 cnt1 = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt0 += 1 elif single_part[all_names.index(except_speaker)] == 1: cnt1 += 1 if cnt0 != 1 and not cnt1 == total: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif speaker not in statement[0]: garbage = [] for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt0 = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt0 += 1 if cnt0 >= 2: pass else: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt0 = 0 cnt1 = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt0 += 1 elif single_part[all_names.index(except_speaker)] == 1: cnt1 += 1 if cnt0 == 1 or cnt1 == total: pass else: garbage.append(single_part) truth_table_of_names_list=update_names_list(truth_table_of_names_list,garbage) elif statement[2] == 'knight': garbage = [] if speaker in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 1: cnt += 1 if cnt > 1: pass else: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 0: cnt += 1 if cnt == total: pass else: garbage.append(single_part) truth_table_of_names_list = update_names_list(truth_table_of_names_list, garbage) elif speaker not in statement[0]: for single_part in truth_table_of_names_list: if single_part[all_names.index(speaker)] == 0: cnt1 = 0 for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 1: cnt1 += 1 if cnt1 > 1: pass else: garbage.append(single_part) elif single_part[all_names.index(speaker)] == 1: cnt0 = 0 cnt1 = 0 total = len(name_except_speaker) for except_speaker in name_except_speaker: if single_part[all_names.index(except_speaker)] == 1: cnt1 += 1 if single_part[all_names.index(except_speaker)] == 0: cnt0 += 1 if cnt1 == 1 or cnt0 == total: pass else: garbage.append(single_part) truth_table_of_names_list = update_names_list(truth_table_of_names_list, garbage) return truth_table_of_names_list
all_sentences = find_single_sentences(combined_text)
partition(all_sentences)
statement_sentence_list = add_Sir_if_dont_have(statement_sentence_list)
speaker_said_part_list = add_Sir_if_dont_have_speaker_part(speaker_said_part_list)
find_speaker_person(speaker_said_part_list)
statement_sentence_list = replace_I_in_statement(speaker_names, speaker_said_part_list, statement_sentence_list)
all_names = find_all_names(speaker_said_part_list, statement_sentence_list)
print(f"The Sirs are: {' '.join(name for name in all_names)}")
tmp = [] for statement_sentence in statement_sentence_list: if ' us ' in statement_sentence: statement_sentence = statement_sentence.replace(' us ',' '+ ' '.join(['Sir ' + name for name in all_names])+' ') tmp.append(statement_sentence)
statement_sentence_list = tmp
statement_names = [] for element in statement_sentence_list: tmp = [] words = element.split() for idx in range(len(words) - 1): if words[idx] == 'Sir' or words[idx] == 'Sirs': if words[idx + 1][0].isupper(): tmp.append(words[idx + 1]) statement_names.append(tmp)
identity_list = knight_or_knave(statement_sentence_list)
principle_list = collect_principle(statement_sentence_list, statement_names)
sir_statements_pairs = find_pairs(speaker_names, identity_list, principle_list, statement_names)
number_of_all_names = len(all_names) truth_table_of_names_list = [tuple(map(int, f'{i:0{number_of_all_names}b}')) for i in range(2 ** number_of_all_names)]
for speaker, statement in sir_statements_pairs.items(): if len(statement) > 1: for item in [*statement]: truth_table_of_names_list = principle_handler(truth_table_of_names_list, all_names, speaker, *item) elif len(statement) == 1: new_data = [*statement[0]] truth_table_of_names_list = principle_handler(truth_table_of_names_list, all_names, speaker, *new_data)
if truth_table_of_names_list: length_result = len(truth_table_of_names_list) if length_result == 1: print('There is a unique solution:') for index in range(len(truth_table_of_names_list[0])): if truth_table_of_names_list[0][index] == 0: print(f'Sir {all_names[index]} is a Knave.') elif truth_table_of_names_list[0][index] == 1: print(f'Sir {all_names[index]} is a Knight.') elif length_result > 1: print(f'There are {length_result} solutions.') else: print(f'There is no solution.')
|