set

Time complexity of checking whether a set is contained in another set

六月ゝ 毕业季﹏ 提交于 2020-04-30 06:27:18
问题 I am trying to implement the example of finding the shortest substring of a given string s containing the pattern char . My code is working fine, but my goal is to attain the time complexity of O(N) where N is length of s . Here is my code; def shortest_subtstring(s,char): #smallest substring containing char.use sliding window start=0 d=defaultdict(int) minimum=9999 for i in range(len(s)): d[s[i]]+=1 #check whether all the characters from char has been visited. while set(char).issubset(set([j

What are these set operations, and why do they give different results?

五迷三道 提交于 2020-04-29 13:01:08
问题 I had seen this test question on Pluralsight: Given these sets: x = {'a', 'b', 'c', 'd'} y = {'c', 'e', 'f'} z = {'a', 'g', 'h', 'i'} What is the value of x | y ^ z ? The expected answer is: {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'} Combines the sets (automatically discarding duplicates), and orders them from lowest to greatest. My questions are: What is this expression called? Why do I get 3 different results from 3 different Python versions? Result on Python 3.7.5 on Ubuntu 18.04: {'c',

What are these set operations, and why do they give different results?

帅比萌擦擦* 提交于 2020-04-29 13:01:07
问题 I had seen this test question on Pluralsight: Given these sets: x = {'a', 'b', 'c', 'd'} y = {'c', 'e', 'f'} z = {'a', 'g', 'h', 'i'} What is the value of x | y ^ z ? The expected answer is: {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'} Combines the sets (automatically discarding duplicates), and orders them from lowest to greatest. My questions are: What is this expression called? Why do I get 3 different results from 3 different Python versions? Result on Python 3.7.5 on Ubuntu 18.04: {'c',

Why is set function on a numpy array returning slightly different values?

ⅰ亾dé卋堺 提交于 2020-04-18 05:42:34
问题 I had to check whether a matrix had eigenvalue with multiplicity>1 or not. Using numpy's eig function I got an array and converted it into set,which should remove the repeated eigenvalue and comparing the length of the list and the set,we can infer whether whether there are repeated eigenvalues or not.The code is given below- from numpy.linalg import eig A=[[3,1,1],[2,4,2],[-1,-1,1]] if len(eig(A)[0])!=len(set(eig(A)[0])): print "Multiple eigenvalues found!" else: print "All distinct" I got

SET client_min_messages to specific ROLE postgres

╄→гoц情女王★ 提交于 2020-04-17 19:01:11
问题 Hi Im trying to set the client_min_messages = error to a role using the postgres user, but when I login in the role I check current_setting('client_min_messages') and I get DEFAULT VALUE (notice). Ive already tried restarting config and also done some tests. To summarize I did: Login with postgres role. check default values of client_min_messages(notice) and log_min_messages(warning). ALTER ROLE anne SET client_min_messages = error; ALTER ROLE anne SET log_min_messages = panic; SELECT pg

SET client_min_messages to specific ROLE postgres

◇◆丶佛笑我妖孽 提交于 2020-04-17 19:00:23
问题 Hi Im trying to set the client_min_messages = error to a role using the postgres user, but when I login in the role I check current_setting('client_min_messages') and I get DEFAULT VALUE (notice). Ive already tried restarting config and also done some tests. To summarize I did: Login with postgres role. check default values of client_min_messages(notice) and log_min_messages(warning). ALTER ROLE anne SET client_min_messages = error; ALTER ROLE anne SET log_min_messages = panic; SELECT pg

perl6 What is a quick way to de-select array or list elements?

你说的曾经没有我的故事 提交于 2020-04-12 09:03:09
问题 To select multiple elements from an array in perl6, it is easy: just use a list of indices: > my @a = < a b c d e f g >; > @a[ 1,3,5 ] (b d f) But to de-select those elements, I had to use Set: > say @a[ (@a.keys.Set (-) (1,3,5)).keys.sort ] (a c e g) I am wondering if there is an easier way because the arrays I use are often quite large? 回答1: sub infix:<not-at> ($elems, @not-ats) { my $at = 0; flat gather for @not-ats -> $not-at { when $at < $not-at { take $at++ xx $not-at - $at } NEXT { $at

第十章 Scala 容器基础(二十一):从集合中提取不重复的元素

点点圈 提交于 2020-04-07 06:02:48
Problem 你有一个集合,内部有很多重复元素,你想要把这些重复的元素只保留一份。 Solution 使用Distinct方法: scala> val x = Vector(1, 1, 2, 3, 3, 4) x: scala.collection.immutable.Vector[Int] = Vector(1, 1, 2, 3, 3, 4) scala> val y = x.distinct y: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4) 这个distinct方法返回一个新的集合,重复元素只保留一份。记得使用一个新的变量来指向这个新的集合,无论你使用的是mutable集合还是immutable集合。 如果你突然需要一个set,那么直接吧你的集合转化成为一个set也是去掉重复元素的方式: scala> val s = x.toSet s: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4) 因为Set对于一样的元素只能保存一份,所以把Array,List,Vector或者其他的集合转化成Set可以去掉重复元素。实际上这就是distinct方法的工作远离。Distinct方法的源代码显示了他就是实用了一个mutable.HashSet的实例。

集合类操作优化经验总结(三)

瘦欲@ 提交于 2020-03-24 16:36:01
3 月,跳不动了?>>> 集合类实践 ArrayList、Vector、LinkedList 均来自 AbstractList 的实现,而 AbstractList 直接实现了 List 接口,并扩展自 AbstarctCollection。ArrayList 和 Vector 使用了数组实现,ArrayList 没有对任何一个方法提供线程同步,因此不是线程安全的,Vector 中绝大部分方法都做了线程同步,是一种线程安全的实现。LinkedList 使用了循环双向链表数据结构,由一系列表项连接而成,一个表项总是包含 3 个部分,元素内容、前驱表项和后驱表项。 当 ArrayList 对容量的需求超过当前数组的大小时,需要进行扩容。扩容过程中,会进行大量的数组复制操作,而数组复制时,最终将调用 System.arraycopy() 方法。LinkedList 由于使用了链表的结构,因此不需要维护容量的大小,然而每次的元素增加都需要新建一个 Entry 对象,并进行更多的赋值操作,在频繁的系统调用下,对性能会产生一定的影响,在不间断地生成新的对象还是占用了一定的资源。而因为数组的连续性,因此总是 在尾端增加元素时,只有在空间不足时才产生数组扩容和数组复制。 ArrayList 是基于数组实现的,而数组是一块连续的内存空间,如果在数组的任意位置插入元素

Memory consumption of a list and set in Python

房东的猫 提交于 2020-03-21 18:07:27
问题 >>> from sys import getsizeof >>> a=[i for i in range(1000)] >>> b={i for i in range(1000)} >>> getsizeof(a) 9024 >>> getsizeof(b) 32992 My question is, why does a set consume so much more memory compared to a list? Lists are ordered, sets are not. Is it an internal structure of a set that consumes memory? Or does a list contain pointers and set does not? Or maybe sys.getsizeof is wrong here? I've seen questions about tuples, lists and dictionaries, but I could not find any comparison between