entry #1
written by moshikoi
submitted at
5 likes
guesses
- BeatButton (by MattiDragon)
- HelloBoi (by BeatButton)
- IFcoltransG (by Olivia)
- IFcoltransG (by SlaveGirlSofia)
- LyricLy (by razetime)
- LyricLy (by Olive)
- Olive (by Palaiologos)
- SlaveGirlSofia (by IFcoltransG)
- SlaveGirlSofia (by LyricLy)
- moshikoi (by SoundOfSpouting)
- quintopia (by HelloBoi)
- razetime (by quintopia)
- razetime (by SliceOfArdath)
comments 0
primality.py New Line Delimited JSON text data
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | [0][0]#{ FALSE Polyglot because why not (Tested on TIO) # All the [0][0]#{ are to create comments in FALSE import sys code = r''' { Get integer from stdin } 0^[$1_>]['0-\10*+^]#% { Store in a } a: { Flag for primality } a;1=b: { Loop from a to 2, if divisible then set flag } a;[1-$1>][$$a;\/*a;=b;|b:]# % { Print result (add one; -1 (not prime) => 0; 0 (prime) => 1) } 1b;+. ''' [0][0]#{ class Interpreter: class Variable: def __init__(self, name): self.name = name class Lambda: def __init__(self, code): self.code = code def __init__(self, code, vars = None, stack = None): self.code = code self.pos = 0 self.vars = vars or dict() self.stack = stack or list() self.input = sys.stdin self.output = sys.stdout def set_input(self, inp): self.input = inp def set_output(self, out): self.output = out def run(self): while True: self.skip_whitespace() if self.current_char() == None: return elif self.current_char() == '{': self.skip_comment() elif self.current_char() == '\'': self.advance() self.stack.append(ord(self.current_char())) self.advance() elif self.current_char() == '$': self.stack.append(self.stack[-1]) self.advance() elif self.current_char() == '%': self.stack.pop() self.advance() elif self.current_char() == '\\': self.swap() self.advance() elif self.current_char() == '@': self.rotate() self.advance() elif self.current_char() == 'ΓΈ': self.pick() self.advance() elif self.current_char() == '+': self.stack.append(self.stack.pop() + self.stack.pop()) self.advance() elif self.current_char() == '-': a = self.stack.pop() self.stack.append(self.stack.pop() - a) self.advance() elif self.current_char() == '*': self.stack.append(self.stack.pop() * self.stack.pop()) self.advance() elif self.current_char() == '/': q = self.stack.pop() self.stack.append(self.stack.pop() // q) self.advance() elif self.current_char() == '_': self.stack.append(-self.stack.pop()) self.advance() elif self.current_char() == '&': self.stack.append(self.stack.pop() & self.stack.pop()) self.advance() elif self.current_char() == '|': self.stack.append(self.stack.pop() | self.stack.pop()) self.advance() elif self.current_char() == '~': self.stack.append(~self.stack.pop()) self.advance() elif self.current_char() == '>': b = self.stack.pop() self.stack.append(-1 if self.stack.pop() > b else 0) self.advance() elif self.current_char() == '=': b = self.stack.pop() self.stack.append(-1 if self.stack.pop() == b else 0) self.advance() elif self.current_char() == '[': self.push_lambda() elif self.current_char() == '#': self.advance() self.while_loop() elif self.current_char().isdigit(): self.push_num() elif self.current_char().isalpha(): self.stack.append(__class__.Variable(self.current_char())) self.advance() elif self.current_char() == ':': var = self.stack.pop() self.vars[var.name] = self.stack.pop() self.advance() elif self.current_char() == ';': var = self.stack.pop() self.stack.append(self.vars[var.name]) self.advance() elif self.current_char() == '^': ch = self.input.read(1) self.stack.append(ord(ch) if ch else -1) self.advance() elif self.current_char() == '.': self.output.write(str(self.stack.pop())) self.advance() else: raise Exception("Invalid character " + self.current_char()) def push_lambda(self): self.advance() res = '' while self.current_char() != ']': res += self.current_char() self.advance() self.advance() self.stack.append(__class__.Lambda(res)) def run_lambda(self, l): lambda_runner = Interpreter(l.code, self.vars, self.stack) lambda_runner.set_input(self.input) lambda_runner.set_output(self.output) lambda_runner.run() def while_loop(self): body = self.stack.pop() cond = self.stack.pop() while True: self.run_lambda(cond) if self.stack.pop() == 0: break else: self.run_lambda(body) def pick(self): i = self.stack.pop() + 1 val = self.stack.pop(-i) self.stack.append(val) def rotate(self): a = self.stack.pop(0) self.stack.append(a) def swap(self): a = self.stack.pop() b = self.stack.pop() self.stack.append(a) self.stack.append(b) def push_num(self): res = 0 while self.current_char() and self.current_char().isdigit(): res = res * 10 + int(self.current_char()) self.advance() self.stack.append(res) def skip_whitespace(self): while self.current_char() and self.current_char().isspace(): self.advance() def skip_comment(self): while self.current_char() != '}': [0][0]#{ self.advance() self.advance() def current_char(self): try: return self.code[self.pos] except: return None def advance(self): self.pos += 1 from io import StringIO def entry(n): interpreter = Interpreter(code) inp = StringIO(str(n)) out = StringIO() interpreter.set_input(inp) interpreter.set_output(out) interpreter.run() return out.getvalue() == '1' [0][0]# If you're reading this you're a cool person :) } |
post a comment