Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import sys
- import select
- import re
- word_file = open("words_alpha.txt", "r")
- words = word_file.read().splitlines()
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- def find_word(word):
- shift = 1
- while shift < 27:
- tmp_word = ""
- for letter in word:
- new_letter = ord(letter) + shift
- tmp_word += (chr(new_letter) if new_letter <= 122 else chr(new_letter - 26))
- if tmp_word in words:
- shift = 26 - shift
- print(tmp_word, shift)
- return str(shift) + "\n"
- else:
- shift+= 1
- print("Not found", word)
- return "-1"
- def close_all():
- word_file.close()
- sock.close()
- sock.connect(("5.101.72.226", 5001))
- while True:
- msg2 = sock.recv(1024).decode()
- print(msg2)
- if msg2.find("Time is up!") != -1 or msg2.find("Incorrect input!") != -1 or msg2.find("Bad answer!") != -1:
- sys.exit()
- if msg2.find("Good job!") != -1:
- msg2 = msg2.replace("Good job!", "")
- msg = msg2[:-4]
- oldmsgParts = msg.split(" ")
- if len(oldmsgParts) < 4:
- continue
- if len(oldmsgParts) > 4:
- oldmsgParts = oldmsgParts[-4:]
- msgParts = []
- for part in oldmsgParts:
- reg = re.compile('[^a-zA-Z ]')
- msgParts.append(reg.sub('', part))
- print(msgParts, len(msgParts))
- res = "-1"
- if not len(msgParts[0]) < 5:
- res = find_word(msgParts[0])
- if res == "-1" and not len(msgParts[1]) < 5:
- res = find_word(msgParts[1])
- if res == "-1" and not len(msgParts[2]) < 5:
- res = find_word(msgParts[2])
- print(res == "-1")
- if res == "-1":
- res = find_word(msgParts[3])
- if res == "-1":
- print("too bad")
- sys.exit()
- sock.send(res.encode())
- close_all()
Advertisement
Add Comment
Please, Sign In to add comment