Python Forum
Need tips for speeding up python code
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need tips for speeding up python code
#1
from numpy import array, bitwise_or
from itertools import combinations, chain 

n,k=map(int,input().split())
arr=list(map(int,input().split()))
tup=map(list,chain.from_iterable(map(lambda x:combinations(arr,x),range(1,k+1)))) 
ans=map(lambda x:bitwise_or.reduce(array(x,dtype='int')),tup)
print(len(set(ans)))
n is the number of elements in set
the code makes subsets of size <= k and performs bitwise or on each subset and prints the number of unique elements formed
Reply
#2
Do you know what part is slow? If not, you might want to try profiling it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Speeding up iterating over rows NeoXx 3 1,106 Nov-29-2022, 05:08 AM
Last Post: Pedroski55
  Help speeding up ode_int andeye 2 2,040 Jan-27-2019, 04:04 PM
Last Post: andeye
  Speeding up Twitter parser kiton 3 3,632 Jun-19-2017, 06:01 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020