Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main():
- n,l=map(int,raw_input().split())
- #print n
- #print l
- r = 0 #result
- p = 0 #previous
- q = 0 #current
- b = [] #buffer
- for c in sys.stdin.read(): #character
- if c == ' ':
- q = int(''.join(b))
- b = []
- r += q ^ p #yes there is a bug with A[0] but lets optimize the loop for now
- p = q
- else:
- b.append(c)
- r += int(''.join(b)) ^ p
- print r
- main()
- import re
- import sys
- from collections import deque
- def main():
- n,l=map(int,raw_input().split())
- #print n
- #print l
- r = 0
- p = 0
- q = 0
- b = sys.stdin.read(l)
- b = deque(b.rsplit(' ',4000000))
- n = len(b)
- while n == 4000001:
- c = b.popleft()
- b = map(int,b)
- for i in xrange(n-2,0,-1):
- r += b[i] ^ b[i-1]
- m = b[0]
- b = deque(c.rsplit(' ',3999999))
- b.append(m)
- n = len(b)
- b = map(int,b)
- for i in xrange(n-1,0,-1):
- r += b[i] ^ b[i-1]
- print r
- main()
- f = open("digits.txt", "rb")
- try:
- bytes = []
- previous_num = None
- byte = f.read(1)
- while byte != "":
- if byte != " ":
- bytes.append(byte)
- else:
- # convert bytes to a number and reset list
- current_num = int(''.join(map(str, bytes)))
- if not previous_num:
- previous_num = current_num
- else:
- # do your operation on previous and current number
- bytes = []
- byte = f.read(1)
- finally:
- f.close()
- #!python2.7
- from __future__ import print_function
- import os, time
- numbers = "100 69 38 24 17 11 3 22 "
- print("Numbers:", numbers)
- if not os.path.isfile('numbers.txt'):
- with open('numbers.txt', 'w') as outfile:
- n = 7*1000*1000
- print("Repeating %d times..." % n)
- print(numbers * n, file=outfile)
- print("Starting read. Time:", time.strftime("%c"))
- total = 0
- with open('numbers.txt') as f:
- prv = None
- for nxt in f.read().split():
- nxt = int(nxt)
- if prv is not None:
- total += prv ^ nxt
- prv = nxt
- print("Finished. Time:", time.strftime("%c"))
- print("Total:", total)
- $ python2.7 test.py
- Numbers: 100 69 38 24 17 11 3 22
- Starting read. Time: Fri Feb 3 19:20:32 2017
- Finished. Time: Fri Feb 3 19:21:36 2017
- Total: 2603999886
- %timeit numpy.fromfile('numbers.txt', dtype=int, sep=' ')
- 1 loop, best of 3: 23.6 s per loop
- %timeit numpy.fromfile('numbers.bin')
- 1 loop, best of 3: 2.55 s per loop
- from itertools import tee, izip as zip
- import re
- def pairwise(iterable):
- a,b = tee(iterable)
- next(b,None)
- return zip(a,b)
- def process_data(data):
- return sum( a^b for a,b in pairwise(data) )
- def process_str_file_re(fname):
- exp = re.compile(r"d+")
- with open(fname,"rb") as archi:
- return process_data( int( data.group() ) for data in exp.finditer(archi.read()) )
Add Comment
Please, Sign In to add comment