Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find out the XOR values of specific elements from a generated list in Python
Sometimes we need to generate a special sequence by removing numbers with consecutive 1s in their binary representation, then compute XOR values from specific positions. This involves generating a Zeckendorf-like sequence and performing bitwise operations.
Problem Understanding
Given a list of natural numbers, we remove all numbers containing two consecutive 1s in their binary representation to create list Z. Then we find the XOR of elements at specified indices from Z.
For example, if input_list = [3, 4, 5], we need elements at indices 3, 4, and 5 from Z, which are 4, 5, and 8. So 4 XOR 5 XOR 8 = 9.
Algorithm Steps
The solution uses a mathematical approach with Fibonacci-like sequences ?
- Generate a Fibonacci sequence to represent valid numbers
- Convert indices to their Zeckendorf representation
- XOR all the converted values
Implementation
def zeck_num(k, f_list):
"""Convert number k to its Zeckendorf representation"""
res = 0
for i in range(len(f_list)-1, -1, -1):
if k >= f_list[i]:
res += 2**i
k -= f_list[i]
return res
def solve(input_list):
"""Find XOR of elements at specified indices from filtered list Z"""
MOD = 10**9 + 7
max_val = 10**18
# Generate Fibonacci-like sequence
f_list = [1, 2]
while f_list[-1] <= max_val:
f_list.append(f_list[-1] + f_list[-2])
# Calculate XOR of specified elements
res = 0
for index in input_list:
res ^= zeck_num(index, f_list)
return res % MOD
# Test with example
result = solve([3, 4, 5])
print(f"XOR result: {result}")
XOR result: 9
How It Works
The algorithm generates a Fibonacci sequence [1, 2, 3, 5, 8, 13, ...] where each number represents positions of valid numbers (without consecutive 1s in binary). The zeck_num() function converts each index to its Zeckendorf representation using this sequence.
Step-by-Step Example
def demonstrate_process():
"""Show step-by-step calculation for input [3, 4, 5]"""
f_list = [1, 2, 3, 5, 8, 13, 21, 34, 55] # Truncated for demo
indices = [3, 4, 5]
values = []
for index in indices:
value = zeck_num(index, f_list)
values.append(value)
print(f"Index {index} converts to: {value}")
# Calculate XOR step by step
xor_result = 0
for i, val in enumerate(values):
xor_result ^= val
print(f"After XOR with {val}: {xor_result}")
return xor_result
def zeck_num(k, f_list):
res = 0
for i in range(len(f_list)-1, -1, -1):
if k >= f_list[i]:
res += 2**i
k -= f_list[i]
return res
result = demonstrate_process()
print(f"\nFinal XOR result: {result}")
Index 3 converts to: 4 Index 4 converts to: 5 Index 5 converts to: 8 After XOR with 4: 4 After XOR with 5: 1 After XOR with 8: 9 Final XOR result: 9
Key Points
- Uses Fibonacci sequence to represent valid positions
- Zeckendorf representation converts indices to actual values
- XOR operation is performed bitwise:
4 ? 5 ? 8 = 9 - Result is taken modulo 10? + 7 for large numbers
Conclusion
This solution efficiently finds XOR values using mathematical properties of Fibonacci sequences and Zeckendorf representations. The approach avoids generating the entire filtered list Z, making it memory-efficient for large indices.
