Skip to main content
correct indentation and add language hint to code excerpts for better syntax highlighting
Source Link
def XORsubqueries(array, queries):
result = []
for pair in queries:
    value = array[pair[0]]
    i = pair[0]
    while i < pair[1]:
        if pair[0] == pair[1]:
            result.append(array[pair[i]] ^ array[pair[i]])
            break
        else:
            i += 1
            value ^= array[i]
    result.append(value)
return result
def XORsubqueries(array, queries):
    result = []
    for pair in queries:
        value = array[pair[0]]
        i = pair[0]
        while i < pair[1]:
            if pair[0] == pair[1]:
                result.append(array[pair[i]] ^ array[pair[i]])
                break
            else:
                i += 1
                value ^= array[i]
        result.append(value)
    return result
def XORsubqueries(array, queries):

result = [0] * len(array)
def XOR(array, queries):     
    for pair in queries:
        value = array[pair[0]]
        i = pair[0]
        while i < pair[1]:
            i += 1
            value ^= array[i]
            yield value
return list(XOR(array, queries))
def XORsubqueries(array, queries):
    
    result = [0] * len(array)
    def XOR(array, queries):     
        for pair in queries:
            value = array[pair[0]]
            i = pair[0]
            while i < pair[1]:
                i += 1
                value ^= array[i]
                yield value
    return list(XOR(array, queries))
def XORsubqueries(array, queries):
result = []
for pair in queries:
    value = array[pair[0]]
    i = pair[0]
    while i < pair[1]:
        if pair[0] == pair[1]:
            result.append(array[pair[i]] ^ array[pair[i]])
            break
        else:
            i += 1
            value ^= array[i]
    result.append(value)
return result
def XORsubqueries(array, queries):

result = [0] * len(array)
def XOR(array, queries):     
    for pair in queries:
        value = array[pair[0]]
        i = pair[0]
        while i < pair[1]:
            i += 1
            value ^= array[i]
            yield value
return list(XOR(array, queries))
def XORsubqueries(array, queries):
    result = []
    for pair in queries:
        value = array[pair[0]]
        i = pair[0]
        while i < pair[1]:
            if pair[0] == pair[1]:
                result.append(array[pair[i]] ^ array[pair[i]])
                break
            else:
                i += 1
                value ^= array[i]
        result.append(value)
    return result
def XORsubqueries(array, queries):
    
    result = [0] * len(array)
    def XOR(array, queries):     
        for pair in queries:
            value = array[pair[0]]
            i = pair[0]
            while i < pair[1]:
                i += 1
                value ^= array[i]
                yield value
    return list(XOR(array, queries))
Source Link

Python3: using generator to process a very large list of integers

This is from a leetcode question I am working on. The problem is the following:

create an algorithm that works in the the following fashion:

arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]

the output is: [2,7,14,8]

by applying the following series of operations: The XOR values for queries are:

[0,1] = 1 xor 3 = 2

[1,2] = 3 xor 4 = 7

[0,3] = 1 xor 3 xor 4 xor 8 = 14

[3,3] = 8

The input is a very large list for arr and an even longer list for queries. The output has to be a list and I have tried this several ways.

first attempt: (building a gigantic list in memory and returning it using append() )

def XORsubqueries(array, queries):
result = []
for pair in queries:
    value = array[pair[0]]
    i = pair[0]
    while i < pair[1]:
        if pair[0] == pair[1]:
            result.append(array[pair[i]] ^ array[pair[i]])
            break
        else:
            i += 1
            value ^= array[i]
    result.append(value)
return result

As you can imagine this takes FOREVER (53 seconds on my i5 dual core). I then remembered about generator expressions for lazily building values and thought they might come in handy just for this kind of thing.

def XORsubqueries(array, queries):

result = [0] * len(array)
def XOR(array, queries):     
    for pair in queries:
        value = array[pair[0]]
        i = pair[0]
        while i < pair[1]:
            i += 1
            value ^= array[i]
            yield value
return list(XOR(array, queries))

If i remove the list off that return statement it figures it out in like 0.0001 seconds ! But I have to return it as a list. Is there a way I can rewrite my algorithm to do what I am trying to do with this generator or did I paint myself into a corner ?