Explorar o código

changement du script challenge-1337.py en challenge-heure.py, avec l'heure passée en second argument

Théo Ertzscheid %!s(int64=2) %!d(string=hai) anos
pai
achega
b96610135d
Modificáronse 2 ficheiros con 43 adicións e 18 borrados
  1. 1 1
      .gitignore
  2. 42 17
      challenge-heure.py

+ 1 - 1
.gitignore

@@ -1 +1 @@
-result.json
+*.json

+ 42 - 17
challenge-1377.py → challenge-heure.py

@@ -12,46 +12,71 @@ def import_array(filename):
   array = data['messages']
   return array
 
-def check_challenge_win(message):
+def check_challenge_win(message, hour, minute):
   # Check that the message has text and that the text is "1337"
-  if "text" in message and message["text"] == "1337":
+  if "text" in message and message["text"] == hour + minute:
     # Check if the message was edited, return false
     if "edited" in message:
       return False
     else:
       # get the hour and minutes from the date
       dt = datetime.fromtimestamp(int(message["date_unixtime"]))
-    
-    #finally, check if the hour is 13 and minute is 37
-    if dt.hour == 13 and dt.minute == 37 :
+
+    #finally, check if the hour and minute corresponds to the text of the message
+    if (dt.hour == int(hour) and dt.minute == int(minute)):
       return True
     else:
       return False
 
+def analyze_messages(messages, hour, minute):
+  # Initialize the dict that we will return
+  wins = {}
 
+  # check the messages
+  for message in messages:
+    if(check_challenge_win(message, hour, minute)):
+      #print("Message gagnant de " + message["from"] + " le " + message["date"])
+      #If it's the first win, add the key with value 1
+      if message["from"] not in wins:
+        wins[message["from"]] = 1
+      # Else, add 1 to the number of wins
+      else:
+        wins[message["from"]] += 1
 
+  return wins
 
+def verify_time_string(time):
+  # Check that the time string is made of 4 digits
+  if not(time.isdigit() and len(time) ==4):
+    print("Erreur : format de chaine non valide", file=sys.stderr)
+    return False
+  # Check that the hour and minute numbers are valid
+  elif not (0 < int(time[:2]) < 23 and 0 < int(time[2:]) < 59):
+    print("Erreur : heure non valide", file=sys.stderr)
+    return False
+  else: 
+    return True
 
 def main():
   # Get the file name from the first command line argument
   filename = sys.argv[1]
 
+  # Get the time chosen for the challenge (1337,1312, 2222, etc.)
+  time = sys.argv[2]
+
+  # Verify that the time string is a valid time of day
+  if verify_time_string(time):
+    hour = time[:2]
+    minute = time[2:]
+  else: 
+    quit()
+
+
   # Import the array from the JSON file
   messages = import_array(filename)
   
   # create a dict containing the number of wins for each sender
-  wins = {}
-
-  # check the messages
-  for message in messages:
-    if(check_challenge_win(message)):
-      #print("Message gagnant de " + message["from"] + " le " + message["date"])
-      #If it's the first win, add the key with value 1
-      if message["from"] not in wins:
-        wins[message["from"]] = 1
-      # Else, add 1 to the number of wins
-      else:
-        wins[message["from"]] += 1
+  wins = analyze_messages(messages, hour, minute)
   
   # print the resulting wins dict
   print(wins)