-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeektrust.py
58 lines (50 loc) · 2.11 KB
/
geektrust.py
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
from Southeros import Southeros
from Message import Message
import sys
def register_all_kingdoms():
"""Registers all the Kingdoms of Southeros
Returns:
Southeros Object: Southeros Class Object
"""
kingdoms = [("SPACE","GORILLA"), ("LAND","PANDA"),("WATER","OCTOPUS"),
("ICE", "MAMMOTH"), ("AIR", "OWL"), ("FIRE", "DRAGON")]
southeros = Southeros(kingdoms)
return southeros
def get_kingdom_and_message(text):
"""Splits the given text to two parts using first space, first part being the kingdom name
and second part being the text message sent to the kingdom
Args:
text (string): string to be splitted
Returns:
tuple: first being the name and second being the message
"""
splitted_text = text.split()
kingdom_name = splitted_text[0]
ciphered_text = ""
for c in splitted_text[1:]:
ciphered_text += c
return kingdom_name, ciphered_text
def main():
"""main function of the Tame of Thrones"""
southeros = register_all_kingdoms() # registers all the kindoms of Southeros
space_kingdom = southeros.get_kingdom("SPACE") # gets the space kingdom object
input_file = sys.argv[1] # file location
# parse the file and process the command
with open(input_file) as fp:
Lines = fp.readlines()
for line in Lines:
kingdom_name, ciphered_text = get_kingdom_and_message(line) # retrieving kingdom name and message
kingdom = southeros.get_kingdom(kingdom_name) # gets the kingdom to which message should be sent
message = Message(space_kingdom, kingdom, ciphered_text) # Creates the message
space_kingdom.send_message(message) # sends the message
# print the output
ruler = southeros.ruler() # get the ruler of Southeros
if ruler is None:
print("NONE") # no ruler got majority
else:
print(ruler.name(), end = " ") # found a majority ruler
for kingdom_name in ruler.get_allies(): # found it's allies
print(kingdom_name, end = " ")
print()
if __name__ == "__main__":
main()