Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- import random
- import select
- def main():
- n = random.randint(1, 1e6)
- sys.stderr.write(f"begin with {n=}\n")
- v = [x + 1 for x in range(n)]
- random.shuffle(v)
- idx = 0
- opt = 0
- sys.stdout.write(f"{v[idx]}\n")
- sys.stdout.flush()
- while True:
- '''
- r, _, _ = select.select([sys.stdin], [], [], 0.1)
- if not r:
- sys.stderr.write("not data, skip\n")
- continue
- '''
- line = sys.stdin.readline().strip()
- sys.stderr.write(f"read {line}\n")
- if line[0] == '!':
- exp = int(line[1:])
- if exp == n:
- sys.stderr.write(f"get right ans {exp=}\n")
- return 0
- else:
- raise Exception(f"real {n=}, {exp=}\n")
- else:
- op = line[0]
- cnt = int(line[1:])
- if op == '+':
- idx = (idx + cnt) % n
- else:
- idx = (idx - cnt) % n
- sys.stdout.write(f"{v[idx]}\n")
- sys.stdout.flush()
- opt += 1
- if opt % 100 == 0:
- sys.stderr.write(f"opt get ${opt}\n")
- if opt >= 1000:
- break
- raise Exception("Too many try")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement