- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find out the XOR values of specific elements from a generated list in Python
Suppose we are given a list that contains natural numbers. Now from that list, we remove all the numbers that contain two consecutive 1s in its binary representation and generate another list called Z. Now we are given another list 'input_list' that contains some integer values. We have to find out the XOR value of the specified elements from Z whose indexes are specified in input_list.
So, if the input is like input_list = [3, 4, 5], then the output will be 9.
In indexes 3, 4, and 5 of Z; the values are 4, 5, and 8. So, 4 XOR 5 XOR 8 = 9.
To solve this, we will follow these steps −
- Define a function zeck_num() . This will take k, f_list
- res := 0
- for i in range (size of f_list -1) to -1, decrease by 1, do
- if k >= f_list[i], then
- res := res + 2^i
- k := k - f_list[i]
- if k >= f_list[i], then
- return res
- MOD := 10^9 + 7
- max_val := 10^18
- f_list := a new list containing values 1 and 2
- while last element of f_list <= max_val, do
- insert last element of f_list + second last element of f_list at the end of f_list
- res := 0
- for each index in input_list, do
- res := res XOR zeck_num(index, f_list)
- return res mod MOD
Example
Let us see the following implementation to get better understanding −
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 def solve(input_list): MOD = 10**9+7 max_val = 10**18 f_list = [1,2] while f_list[-1] <= max_val: f_list.append(f_list[-1] + f_list[-2]) res = 0 for index in input_list: res ^= zeck_num(index, f_list) return res % MOD print(solve([3, 4, 5]))
Input
[3, 4, 5]
Output
9
- Related Articles
- Program to find out the scalar products of vectors generated from an infinite sequence in Python
- Program to find duplicate item from a list of elements in Python
- Program to find the kth missing number from a list of elements in Python
- Python program to find N largest elements from a list
- Program to find sum of odd elements from list in Python
- Program to find list of elements which are less than limit and XOR is maximum in Python
- Program to find out the number of submatrices from a matrix where the sum of elements is equal to a specific value in C++
- Program to Find Out the Smallest Substring Containing a Specific String in Python
- Program to Find Out the Maximum Final Power of a List in Python
- Program to find only even indexed elements from list in Python
- Find the list elements starting with specific letter in Python
- Python Program to remove a specific digit from every element of the list
- How to find a specific record from a list of values with semicolon in MySQL?
- Python program to find sum of elements in list
- Python Program to find out the sum of values in hyperrectangle cells

Advertisements