Creation of unexpected tuple value - python-2.7

I am dealing with somewhat of a mystery and hoped for some clarity. I wrote a script for finding dice roll combinations adding to 24 that looks like the following:
start=[3,3,3,3,3,3,3,3]
outcomes=set(tuple(start)) #Use a set to ensure uniqueness
index_list=np.random.randint(0,8,1000)
#This little snippet adds one and subtracts one randomly, keeping total at 24
for i in xrange(0,500):
upper=index_list[i]
downer=index_list[i+20]
if start[upper]!=6 and start[downer]!=1:
start[upper]=start[upper]+1
start[downer]=start[downer]-1
outcomes.add(tuple(start))
print outcomes
What I am running into, is that When I look at outcomes, there is a single 3 of type 'int' in there.
set([(4, 4, 4, 3, 2, 2, 2, 3), 3, (2, 5, 4, 3, 1, 4, 2, 3), (4, 4, 4, 2, 3, 1, 3, 3),(4, 2, 5, 2, 3, 4, 1, 3)])
While I could certainly remove it, I am just curious how it is getting in there to begin with? My initial guess was the index list might be producing an index outside of [0-7], but it is not. I've looked for a similar question other places, but have yet to find a similar issue. Thanks!

set expects an iterable. You're passing a tuple which is an iterable.
set iterates through it, leaving just 1 value: 3 (because your tuple only contains the same 3 value).
You have to put your element in a list or tuple so it is seen as a single element (exactly the same problem when you pass a string and it is unexpectedly iterated upon)
The rest of your code is OK and has nothing to do with the problem.
Do this instead:
outcomes=set([tuple(start),])
now set iterates through a list of 1 tuple, effectively creating tuple elements.
You could do that also, maybe simpler:
outcomes=set()
outcomes.add(tuple(start))
there's no ambiguity since you're adding 1 element. It's not iterated through.

Related

How can we account for elements that may move inside a 2D list/vector?

I got this question from a programming challenge event but didn’t pass all the test cases. Here’s the question:
Imagine you’re playing Minecraft and using sand blocks to build stuff, you can add one block to any row horizontally (from left to right) and the block will fall vertically (from top to bottom) until it hits the bottom or another block. Your goal is to find the number of minimum and maximum moves to fill the first column, i.e., [min, max].
For example, we have a 4x4 space like this:
[[0, 0, 0, 1, 0],
[0, 1, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 1]], where 1 represents a stone block that cannot be moved.
The answer for the above example is [10, 12]. First you need to add 1 sand block to the last row and fill (3, 0). Then we add 2 blocks to the 1st and 3rd row respectively and fill (3, 2), (2, 2), (1, 2), and (0, 2) in order. After that, we add 2 blocks, 1 block, and 2 blocks to 3rd/2nd/1st row and fill the first column, which gives us 10. To get the number of maximum move, we add two more blocks to 3rd row and fill the space at (2, 3) and (2, 4).
I tried to use a counter to count how many zeros we meet before meeting a stone block and keeped comparing with current_min/current_max to find the answer. However, this approach won't work for cases when we need to account for falling sand blocks.
Using the same example, my approach would get [10, 10] instead of [10, 12]. I was thinking if we can check if the row below is 0, add 1 to the answer and modify the space in place to account for the falling sand blocks. Can anyone provide some ideas on potential solution and possbily code as well? Thanks!

Find sum of number in multiple sets using exactly one number of each set

Background
Hi, I'm trying to solve a programming problem and I'm stuck on the following problem:
Assume you have multiple lists of numbers. All are sorted in decreasing order.
You now have to take exactly one number from each list to make the biggest possible sum.
So far so easy, to solve this you could just take the first number of each list and you're done.
But now, I need the second-largest sum while still using exactly one number from each list.
To achieve this, I would take the first element in each list but for the list which has the least difference between the first and second number the second number will be used.
This is still pretty doable.
The Problem
But I need an Iterator over every possible sum using exactly one number of each list sorted in decreasing order.
For performance reasons, it isn't possible to just compute every sum and then sort it. The algorithm must already provide the sums in decreasing order. If there are multiple combinations for a sum then the sum must be returned multiple times.
Additional Requirements
The Iterator should be lazy (only calculate the next sum when required).
The Lists are already lazy, which means you should require as few values as possible to calculate the fitting sum.
Example
For the Lists:
List 1: [5, 2, 1]
List 2: [10, 2]
List 3: [6, 1]
The Iterator then should return:
[5, 10, 6] = 21
[2, 10, 6] = 18
[1, 10, 6] = 17
[5, 10, 1] = 16
[5, 2, 6] = 13
[2, 10, 1] = 13
[1, 10, 1] = 12
[2, 2, 6] = 10
[1, 2, 6] = 9
[5, 2, 1] = 8
[2, 2, 1] = 5
[1, 2, 1] = 4
Comment
I don't need code as an answer to my question (you're still welcome to provide it if it helps to explain). What I'm looking for are ideas to solve this, or solutions that I can implement myself.
Thanks in advance!
First of all, Thanks to wlui155 for the help.
For Anyone interested, I coded a BFS algorithm that acts as follows:
Definitions:
Entry: Struct containing indices of used numbers and sum
BSet: Ordered set which can only contain unique Entries
Algorithm:
Pop Entry with biggest sum from BSet
Create a clone for each list
Advance in each clone a different index by one
Put new entries in BSet
Print current Entry
Goto 1.
Now you only have to ensure that no entry appears again after you've popped it. This can be ensured with a separate set containing all combinations for the current sum. Once the current sum gets smaller this set can be cleared.
If you have ideas to improve this, you're welcome to tell me.

Python 3 list with range and other individual numbers

I need to make a list of numbers. These numbers represent binary masks. The first 100 or so masks are all included in this range. In the next group of masks only certain masks are included. I need a list similar to the following.
[1,2,3,5,6,7,8,9,10,30,34,48,53,62]
Can I do something like [range(1,10),30,34,48,53,62]
or do I need to create my list using range(1,10) and then append the next list to it?
Thanks
Python 3 actually allow you to build a list literal prepending an * to any iterable objects - which are in turn expanded in place:
>>> [1,2, *range(10), *range(2)]
[1, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
If you need this n older Pythons, or if you'd prefer to keep readability for people not too proeficient in Python who might have to walk through your code, an option is just to concatenate your different list fragments using the + operator:
a = list(range(1,10)) + [ 30,34,48,53,62]
Looks like I had to use the list(range(1,10)+[47,34,57]
solution

np.delete and np.s_. What's so special about np_s?

I don't really understand why regular indexing can't be used for np.delete. What makes np.s_ so special?
For example with this code, used to delete the some of the rows of this array..
inlet_names = np.delete(inlet_names, np.s_[1:9], axis = 0)
Why can't I simply use regular indexing and do..
inlet_names = np.delete(inlet_names, [1:9], axis = 0)
or
inlet_names = np.delete(inlet_names, inlet_names[1:9], axis = 0)
From what I can gather, np.s_ is the same as np.index_exp except it doesn't return a tuple, but both can be used anywhere in Python code.
Then when I look into the np.delete function, it indicates that you can use something like [1,2,3] to delete those specific indexes along the entire array. So whats preventing me from using something similar to delete certain rows or columns from the array?
I'm simply assuming that this type of indexing is read as something else in np.delete so you need to use np.s_ in order to specify, but I can't get to the bottom of what exactly it would be reading it as because when I try the second piece of code it simply returns "invalid syntax". Which is weird because this code works...
inlet_names = np.delete(inlet_names, [1,2,3,4,5,6,7,8,9], axis = 0)
So I guess the answer could possibly be that np.delete only accepts a list of the indexes that you would like to delete. And that np._s returns a list of the indexes that you specify for the slice.
Just could use some clarification and some corrections on anything I just said about the functions that may be wrong, because a lot of this is just my take, the documents don't exactly explain everything that I was trying to understand. I think I'm just overthinking this, but I would like to actually understand it, if someone could explain it.
np.delete is not doing anything unique or special. It just returns a copy of the original array with some items missing. Most of the code just interprets the inputs in preparation to make this copy.
What you are asking about is the obj parameter
obj : slice, int or array of ints
In simple terms, np.s_ lets you supply a slice using the familiar : syntax. The x:y notation cannot be used as a function parameter.
Let's try your alternatives (you allude to these in results and errors, but they are buried in the text):
In [213]: x=np.arange(10)*2 # some distinctive values
In [214]: np.delete(x, np.s_[3:6])
Out[214]: array([ 0, 2, 4, 12, 14, 16, 18])
So delete with s_ removes a range of values, namely 6 8 10, the 3rd through 5th ones.
In [215]: np.delete(x, [3:6])
File "<ipython-input-215-0a5bf5cc05ba>", line 1
np.delete(x, [3:6])
^
SyntaxError: invalid syntax
Why the error? Because [3:4] is an indexing expression. np.delete is a function. Even s_[[3:4]] has problems. np.delete(x, 3:6) is also bad, because Python only accepts the : syntax in an indexing context, where it automatically translates it into a slice object. Note that is is a syntax error, something that the interpreter catches before doing any calculations or function calls.
In [216]: np.delete(x, slice(3,6))
Out[216]: array([ 0, 2, 4, 12, 14, 16, 18])
A slice works instead of s_; in fact that is what s_ produces
In [233]: np.delete(x, [3,4,5])
Out[233]: array([ 0, 2, 4, 12, 14, 16, 18])
A list also works, though it works in different way (see below).
In [217]: np.delete(x, x[3:6])
Out[217]: array([ 0, 2, 4, 6, 8, 10, 14, 18])
This works, but produces are different result, because x[3:6] is not the same as range(3,6). Also the np.delete does not work like the list delete. It deletes by index, not by matching value.
np.index_exp fails for the same reason that np.delete(x, (slice(3,6),)) does. 1, [1], (1,) are all valid and remove one item. Even '1', the string, works. delete parses this argument, and at this level, expects something that can be turned into an integer. obj.astype(intp). (slice(None),) is not a slice, it is a 1 item tuple. So it's handled in a different spot in the delete code. This is TypeError produced by something that delete calls, very different from the SyntaxError. In theory delete could extract the slice from the tuple and proceed as in the s_ case, but the developers did not choose to consider this variation.
A quick study of the code shows that np.delete uses 2 distinct copying methods - by slice and by boolean mask. If the obj is a slice, as in our example, it does (for 1d array):
out = np.empty(7)
out[0:3] = x[0:3]
out[3:7] = x[6:10]
But with [3,4,5] (instead of the slice) it does:
keep = np.ones((10,), dtype=bool)
keep[[3,4,5]] = False
return x[keep]
Same result, but with a different construction method. x[np.array([1,1,1,0,0,0,1,1,1,1],bool)] does the same thing.
In fact boolean indexing or masking like this is more common than np.delete, and generally just as powerful.
From the lib/index_tricks.py source file:
index_exp = IndexExpression(maketuple=True)
s_ = IndexExpression(maketuple=False)
They are slighly different versions of the same thing. And both are just convenience functions.
In [196]: np.s_[1:4]
Out[196]: slice(1, 4, None)
In [197]: np.index_exp[1:4]
Out[197]: (slice(1, 4, None),)
In [198]: np.s_[1:4, 5:10]
Out[198]: (slice(1, 4, None), slice(5, 10, None))
In [199]: np.index_exp[1:4, 5:10]
Out[199]: (slice(1, 4, None), slice(5, 10, None))
The maketuple business applies only when there is a single item, a slice or index.

Generate Random Number from fix Set of numbers in iphone

Suppose I have One set of numbers i.e
{1, 6, 3, 5, 7, 9}
I want to Generate Random number from this set of number only i.e. a Generated number should be random and should be from these number({1, 6, 3, 5, 7, 9}) only.
standard C/C++ function will also do...
arc4random%(set count) = a random index.
What they are telling you is this. Generate a random number from 0-5. Then use that as an index into the array. Eg if the random # is 2, look at element #2 (the third one since you start at 0) of your list of numbers, which is 3. If the random # is 5, you get 9.
MSalters' comment shows you how to do it in a single expression.