Advertisement
Pandaaaa906

python_you_knew

Oct 16th, 2019
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.57 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import ast
  4. import signal
  5. import traceback
  6. from copy import deepcopy
  7.  
  8. RED = "\033[31m"
  9. GREEN = "\033[32m"
  10. YELLOW = "\033[33m"
  11. END = "\033[0m"
  12.  
  13.  
  14. def timeout(*args, **kwargs):
  15.     print("Timeout!")
  16.     exit(-1)
  17.  
  18.  
  19. class Challenges:
  20.     def __init__(self):
  21.         signal.signal(signal.SIGALRM, timeout)
  22.         self.challenges = {}
  23.         self.cleared = {}
  24.         for method in dir(self):
  25.             if method.startswith("challenge_"):
  26.                 n = int(method.split("_")[-1])
  27.                 self.challenges[n] = getattr(self, method)
  28.                 self.cleared[n] = False
  29.  
  30.     def do_challenge(self, n):
  31.         while True:
  32.             inp = input("Your answer: ")
  33.             if len(inp) > 100:
  34.                 print("Input too long")
  35.                 continue
  36.             try:
  37.                 answer = ast.literal_eval(inp)
  38.                 break
  39.             except Exception:
  40.                 print("Invalid input!")
  41.                 continue
  42.  
  43.         signal.alarm(1)
  44.         print(YELLOW, end="")
  45.         try:
  46.             if self.challenges[n](answer):
  47.                 print(f"Challenge {n:02d} cleared!")
  48.                 self.cleared[n] = True
  49.             else:
  50.                 print(f"Challenge {n:02d} failed!")
  51.         except Exception:
  52.             print(traceback.format_exc(), end="")
  53.         print(END, end="")
  54.         signal.alarm(0)
  55.  
  56.     def show_status(self):
  57.         print()
  58.         print("------ Do you know Python? ------")
  59.         for i, (challenge, cleared) in enumerate(sorted(self.cleared.items())):
  60.             color = GREEN if cleared else RED
  61.             text = "Y" if cleared else "N"
  62.             print(color + f"{challenge:02d}: {text}" + END, end="  ")
  63.             if i % 5 == 4:
  64.                 print()
  65.         print()
  66.  
  67.     def cleared_count(self):
  68.         return sum(self.cleared.values())
  69.  
  70.     def total_count(self):
  71.         return len(self.challenges)
  72.  
  73.     def challenge_1(self, answer):
  74.         if answer == "Hello":
  75.             return True
  76.         # "Hello"
  77.  
  78.     def challenge_2(self, answer):
  79.         a, b, c, d = answer
  80.         if a == b and a is b and c == d and c is not d:
  81.             return True
  82.         # (1,1,1,True)
  83.  
  84.     def challenge_3(self, answer):
  85.         if answer in answer == answer:
  86.             return True
  87.         # ""
  88.  
  89.     def challenge_4(self, answer):
  90.         a1, b1 = answer
  91.         a2, b2 = answer
  92.         if a1 * 2 != a1 and b1 * 2 != b1:
  93.             a1 *= 2
  94.             b1 *= 2
  95.             if a1 == a2 and b1 != b2:
  96.                 return True
  97.         # ([[]], 2)
  98.  
  99.     def challenge_5(self, answer):
  100.         r = reversed([1, 2, 3])
  101.         if list(r) == list(r) + answer:
  102.             return True
  103.         # [3,2,1]
  104.  
  105.     def challenge_6(self, answer):
  106.         a, b = answer
  107.         if max(a, b) != max(b, a):
  108.             return True
  109.         # {2, 3}, {1,2}
  110.  
  111.     def challenge_7(self, answer):
  112.         a, b, c = answer
  113.         for x in a, b, c:
  114.             if isinstance(x, float) or isinstance(x, complex):
  115.                 return False
  116.         if a * (b + c) != a * b + a * c:
  117.             return True
  118.         # (2, 'a', 'b')
  119.  
  120.     def challenge_8(self, answer):
  121.         a, b, c = answer
  122.         for x in a, b, c:
  123.             if isinstance(x, float) or isinstance(x, complex):
  124.                 return False
  125.         if a * (b * c) != (a * b) * c:
  126.             return True
  127.         # (-1,-1,'a')
  128.  
  129.     def challenge_9(self, answer):
  130.         a, b = answer
  131.         for x in a, b:
  132.             if isinstance(x, float) or isinstance(x, complex):
  133.                 return False
  134.         if type(a ** b) != type(b ** a):
  135.             return True
  136.  
  137.         # (2, -1)
  138.  
  139.     def challenge_10(self, answer):
  140.         a, b = answer
  141.         if a and a.count(b) > len(a):
  142.             return True
  143.        
  144.         # (b'a',b'')
  145.  
  146.     def challenge_11(self, answer):
  147.         if max(answer) != max(*answer):
  148.             return True
  149.         # ("asdf",)
  150.  
  151.     def challenge_12(self, answer):
  152.         a, b = answer
  153.         if a < b and all(x > y for x, y in zip(a, b)):
  154.             return True
  155.  
  156.         # ('', 'b')
  157.  
  158.     def challenge_13(self, answer):
  159.         a, b = answer
  160.         if b and not (a ^ b) - a:
  161.             return True
  162.         # ({1,2}, {2})
  163.  
  164.     def challenge_14(self, answer):
  165.         backup = deepcopy(answer)
  166.         try:
  167.             answer[0] += answer[1]
  168.         except:
  169.             if backup != answer:
  170.                 return True
  171.         # ([0], [1],)
  172.        
  173.     def challenge_15(self, answer):
  174.         item, l = answer
  175.         if item in l and not min(l) <= item <= max(l):
  176.             return True
  177.         # ('', 'asf')
  178.  
  179.     def challenge_16(self, answer):
  180.         item, l = answer
  181.         if item == 233 and item in l and l in l:
  182.             return True
  183.         # (233, b'\xe9')
  184.  
  185.     def challenge_17(self, answer):
  186.         item, l = answer
  187.         if l[0] == item and item not in l:
  188.             return True
  189.         # 'a', {0: 'a'}
  190.  
  191.     def challenge_18(self, answer):
  192.         a, b = answer
  193.         if (a - b) != -(b - a):
  194.             return True
  195.         # (2e308, 2e308)
  196.        
  197.  
  198.     def challenge_19(self, answer):
  199.         if answer.isdecimal():
  200.             if len(answer) < 5:
  201.                 if sum(ord(c) - ord("0") for c in answer) == 23333:
  202.                     return True
  203.         ## [1776, 7235, 7257, 7257]
  204.         # '۰᱃᱙᱙'
  205.  
  206.     def challenge_20(self, answer):
  207.         if len(set(str(x) for x in answer)) == 7 and all(x == 0 for x in answer):
  208.             return True
  209.  
  210.         # (False, 0, 0.0, 0j, -0.0, -0j, -0.0-0.0j)
  211.  
  212.  
  213. if __name__ == "__main__":
  214.     c = Challenges()
  215.     while True:
  216.         c.show_status()
  217.         inp = input("Which one do you want to play? ")
  218.         try:
  219.             n = int(inp)
  220.         except ValueError:
  221.             print("Invalid input!")
  222.             continue
  223.         if n not in c.challenges:
  224.             print("Challenge not found!")
  225.             continue
  226.         c.do_challenge(n)
  227.         print(f"Your progress: {c.cleared_count()}/{c.total_count()}")
  228.         if c.cleared_count() == c.total_count():
  229.             print("You are the master of Python.")
  230.             print("Here is the final flag:")
  231.             print(open("flag3").read().strip())
  232.             break
  233.         elif c.cleared_count() >= c.total_count() * 3 // 4:
  234.             print(open("flag2").read().strip())
  235.         elif c.cleared_count() >= c.total_count() // 2:
  236.             print(open("flag1").read().strip())
  237.         else:
  238.             print("Go on to get your flags!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement