EzDevInfo.com

set interview questions

Top set frequently asked interview questions

Get difference between two lists

I have two lists in Python, like these:

temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']

I need to create a third list with items from the first list which aren't present in the second one. From the example I have to get:

temp3 = ['Three', 'Four']

Are there any fast ways without cycles and checking?


Source: (StackOverflow)

Advertisements

Convert Set to List without creating new List

I am using this code to convert a Set to a List:

Map<String, List> mainMap = new HashMap<String, List>();

for(int i=0; i<something.size(); i++){
  Set set = getSet(...); //return different result each time
  List listOfNames = new ArrayList(set);
  mainMap.put(differentKeyName,listOfNames);
}

I want to avoid creating a new list in each iteration of the loop. Is that possible?


Source: (StackOverflow)

Why does tuple(set([1,"a","b","c","z","f"])) == tuple(set(["a","b","c","z","f",1])) 85% of the time with hash randomization enabled?

Given Zero Piraeus' answer to another question, we have that

x = tuple(set([1, "a", "b", "c", "z", "f"]))
y = tuple(set(["a", "b", "c", "z", "f", 1]))
print(x == y)

Prints True about 85% of the time with hash randomization enabled. Why 85%?


Source: (StackOverflow)

Obtaining a powerset of a set in Java

The powerset of {1, 2, 3} is:

{{}, {2}, {3}, {2, 3}, {1, 2}, {1, 3}, {1, 2, 3}, {1}}

Let's say I have a Set in Java:

Set<Integer> mySet = new HashSet<Integer>();
mySet.add(1);
mySet.add(2);
mySet.add(3);
Set<Set<Integer>> powerSet = getPowerset(mySet);

How do I write the function getPowerset, with the best possible order of complexity? (I think it might be O(2^n).)


Source: (StackOverflow)

Most concise way to convert a Set to a List

I am currently doing this:

Set<String> listOfTopicAuthors = ....

List<String> list = Arrays.asList( 
    listOfTopicAuthors.toArray( new String[0] ) );

Can you beat this ?


Source: (StackOverflow)

Difference between Iterator and Listiterator?

Iterator ite = Set.iterator();
Iterator ite = List.iterator();

ListIterator listite = List.listIterator();

We can use Iterator to traverse a Set or a List or a Map. But ListIterator can only be used to traverse a List, it can't traverse a Set. Why?

I know that the main difference is that with iterator we can travel in only one direction but with ListIterator we can travel both directions. Are there any other differences? And any advantages of ListIterator over Iterator?


Source: (StackOverflow)

How to retrieve an element from a set without removing it?

Suppose the following:

>>>s = set([1, 2, 3])

How do I get a value (any value) out of s without doing s.pop()? I want to leave the item in the set until I am sure I can remove it - something I can only be sure of after an asynchronous call to another host.

Quick and dirty:

>>>elem = s.pop()
>>>s.add(elem)

But do you know of a better way? Ideally in constant time.


Source: (StackOverflow)

Picking a random element from a set

How do I pick a random element from a set? I'm particularly interested in picking a random element from a HashSet or a LinkedHashSet, in Java. Solutions for other languages are also welcome.


Source: (StackOverflow)

In Python, when to use a Dictionary, List or Set?

When should I use a dictionary, list or set?

Are there scenarios that are more suited for each data type?


Source: (StackOverflow)

In Java, How to Easily Convert an Array to a Set

I would like to convert an array to a Set in Java. There are some obvious ways of doing this (i.e. with a loop) but I would like something a bit neater, something like:

java.util.Arrays.asList(Object[] a);

Any ideas?


Source: (StackOverflow)

C# Set collection?

Does anyone know if there is a good equivalent to Java's Set collection in C#? I know that you can somewhat mimic a set using a Dictionary or a HashTable by populating but ignoring the values, but that's not a very elegant way.


Source: (StackOverflow)

Is there any kind of hash code function in JavaScript?

Basically, I'm trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as,

set[obj] = true;

This works, up to a point. It works great with string and numbers, but with other objects, they all seem to "hash" to the same value and access the same property. Is there some kind of way I can generate a unique hash value for an object? How do strings and numbers do it, can I override the same behavior?


Source: (StackOverflow)

How to check that an element is in a std::set?

How do you check that an element is in a set?

Is there a simpler equivalent of the following code:

myset.find(x) != myset.end()

Source: (StackOverflow)

How to set background color of a View

I'm trying to set the background color of a View (in this case a Button).

I use this code:

// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();

It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?

Thanks.


Source: (StackOverflow)