Эх сурвалжийг харах

première version script challenge 1337

Théo Ertzscheid 2 жил өмнө
parent
commit
e80f029d76
2 өөрчлөгдсөн 62 нэмэгдсэн , 1 устгасан
  1. 1 1
      .gitignore
  2. 61 0
      challenge-1377.py

+ 1 - 1
.gitignore

@@ -1 +1 @@
-results.json
+result.json

+ 61 - 0
challenge-1377.py

@@ -0,0 +1,61 @@
+#!/usr/bin/python3
+import json
+import sys
+from datetime import datetime
+
+def import_array(filename):
+  # Load the JSON file
+  with open(filename, 'r') as f:
+    data = json.load(f)
+
+  # Access the "messages" array
+  array = data['messages']
+  return array
+
+def check_challenge_win(message):
+  # Check that the message has text and that the text is "1337"
+  if "text" in message and message["text"] == "1337":
+    # 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 :
+      return True
+    else:
+      return False
+
+
+
+
+
+def main():
+  # Get the file name from the first command line argument
+  filename = sys.argv[1]
+
+  # 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
+  
+  # print the resulting wins dict
+  print(wins)
+
+
+if __name__ == '__main__':
+  main()