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


Messages In This Thread
Need tips for speeding up python code - by iineo - May-28-2018, 06:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Speeding up iterating over rows NeoXx 3 1,153 Nov-29-2022, 05:08 AM
Last Post: Pedroski55
  Help speeding up ode_int andeye 2 2,097 Jan-27-2019, 04:04 PM
Last Post: andeye
  Speeding up Twitter parser kiton 3 3,714 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