All possible 10-digit combinations of digits 0-9 without repeats - python-3.3

I'm trying to generate all the possible 10-digit combinations of the digits 0-9 without repeats for a math problem, but I can't seem to get my head around it.
I've tried itertools.combinations, but that gets subsequences.
I've also tried random.shuffle, but that's horribly inefficient with multiple repeats.
Is there an algorithm to solve this?

As it turns out, thanks to user2864740, I found itertools.permutations. This does what I asked.

I think something like
itertools.permutations(range(10), 10)
would give you all 10-digit combinations of the digits 0 - 9.

Related

Generating numbers sequence containing leading zeros

I'm looking for a way to create a sequence of numbers with a number of leading zeros.
For example, starting from 00000001 to 1000000000.
I tried many variations of seq but I was never able to produce a sequence with starting number as indicated to go to the desired end number.
To generate a billion of numbers, seq would likely take 20min on my machine o printf which is a ton slower is likely not an option, I'm looking into achieving this with seq only for starters if possible.
Any ideas?
To generate a sequence with a specified format you have to use -f argument like this:
seq -f "%08g" 1000000000
Although you didn't specify how you want to use so large numbers it would be better to parallelize it to make a final solution efficient.
Edit:
Version without shortest representation:
seq -f "%08.0f" 10000000

Greedy vs Dynamic Programming to find split of even integers

I was solving the common coin change problem and it makes sense to solve using DP, since the greedy approach won't work here. Eg. For amount 12, if coins = {3,5} then greedy algorithm to use minimum coins will use two 5-coins and then fail. So far so good.
Now I came across another problem where we are given an integer which we want to split into a sum of a maximum number of unique positive even integers.
This problem looked a lot like the coin change problem (as in for both problems we have to add upto a sum using the given denominations - repeating is okay in coins problem and not okay in this one), however I found out that this can be solved using a greedy approach.
I can of-course try and write down different use cases and figure this out, but is there any way of intuitively coming to this conclusion? Asking for this problem, and also in general.

KDB: generate random string

How can one generate a random string of a given length in KDB? The string should be composed of both upper and lower case alphabet characters as well as digits, and the first character can not be a digit.
Example:
"i0J2Jx3qa" / OK
"30J2Jx3qa" / bad
Thank you very much for your help!
stringLength: 13
randomString: (1 ? .Q.A,.Q.a) , ((stringLength-1) ? .Q.nA,.Q.a)
If you prefer without the repetitions:
raze(1,stringLength-1)?'10 0_\:.Q.nA,.Q.a
For the purposes of creating random data you can also use ?/deal with a number of characters up to 8 as a symbol(which you could string). This doesn't include numbers though so just an alternative approach to your own answer.
1?`8
,`bghgobnj
There's already a fine answer above which has been accepted. Just wanted somewhere to note that if this is to generate truly random data you need to consider randomising your seed. This can be done in Linux by using $RANDOM in bash or reading up to four bytes from /dev/random (relatively recent versions of kdb can read directly from FIFOs).
Otherwise the seed is set to digits from pi: 314159

Looping through a string to find the first index of a particular character

for i in reversed(bin(n|(n+1))[2:]):#loops through representation of
# integer n, converted to binary, and flips first 0 bit
if i == '0':
print(str(count))
count=0
break
count +=1
If n is an integer, the above sample code flips the first zero bit, then finds and prints the index of what was the second zero bit.
I'm working on CodeFights, to practice my skills and can't seem to figure out how to format an algorithm that accomplishes basically what this one does.
I'm supposed to find the appropriate index and raise 2 to that power, in one line. I'm looked into using generators and llambdas... not sure what to do.
So, specifically how can I can I get
2**index of second zero in integer n
#in one line of code?
def secondRightmostZeroBit(n):
return 2**bin(n|(n+1))[::-1].index('0')
Researched this for days, stumbled upon the answer five minutes after asking on here.
'.replace(0,1,1)' and
'.find(0)' would also have been useful.
The above is what I ended up submitting. As explained in the question n|(n=1), serves to flip the rightmost zero in the binary representation of n. From there I reversed the order of the string/binary representation of that result, and used '.index' to retrieve the first '0' to be found.
Another way to do it has more to do with manipulation of binary numbers than with the way I phrased the original question.
def secondRightmostZeroBit(n):
return ~n & (~n-1) & -(~n & (~n-1))
I haven't found much reason to do this sort of bit manipulation in the past, so I'm always looking at a reference when I interpret code like this. https://www.tutorialspoint.com/python/bitwise_operators_example.htm If you want to understand it, I suggest plugging in a specific number for n, convert to binary, and work it through. Do that a couple of times and it should become clear what's going on. You can also plug individual peices of it into your interactive pain, but that won't do you much good unless you're looking at the binary. bin(n) and format(n, "8b") are good for that.

Shortest Common Superstring algorithm?

I'm trying to code this problem here:
but I'd like to find an algorithm that breaks down the steps for solving the problem. I can't seem to find anything too useful online so I've come here to ask if anyone knows of a resource which I can use to refer to an algorithm that solves this problem.
This is called the shortest common supersequence problem. The idea is that in order for the supersequence to be the shortest, we want to find as many shared bits of a and b as possible. We can solve the problem in two steps:
Find the longest common subsequence of a and b.
Insert the remaining bits of a and b while preserving the order of these bits.
We can solve the longest common subsequence problem using dynamic programming.
I agree with Terry Li: it is only NP-complete to find the SCS of multiple sequences. For 2 sequences (say s is of length n and t is of length m), my solution (doesn't use LCS but uses something similar) is done in O(nm) time:
1) Run a global alignment, in which you disallow mismatches, don't penalize indels, and give a positive score to matches (I did +1 for matches, -10 for mismatches, and 0 for indels, but these can be adjusted). (This is O(nm))
2) Iterate over the global alignment for both output strings v and w. If v[i] isn't a gap, output it. Otherwise, output w[i]. (This is O(n+m)).

Resources