I have a list in python of integers like this:
myList = [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
I want to get Z3 to output various sets or lists of numbers which are all members of myList... Essentially, I want to use Z3 to get additional lists of numbers which all exist in myList but are in various different orders. Put differently, I want to get various outputs from Z3 which contain numbers in the set myList above.
I am having trouble doing this with Z3py because I do not know how to have z3 return a list or a set as the model when I call s.model(), assuming s = Solver().
from z3 import *
myList = [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
s = Solver ()
pick = []
for (i, v) in enumerate(myList):
b = Bool ('pick_%d' % i)
s.add(b == b)
pick += [(b, v)]
while (s.check() == sat):
m = s.model()
chosen = []
block = []
for (p, v) in pick:
if m.eval(p, model_completion=True):
chosen += [v]
block.append(Not(p))
else:
block.append(p)
print chosen
s.add(Or(block))
Note that this will print 2^n solutions where n is the number of elements in your list; so it'll take a while to finish if n is large!
Related
My Data This picture shows my data. See this: [1, 0,list([32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110,....], I want this data to be like : [1,0,32,64,117,115......]. I want data out of the list and become a member of the array. How can I do this easily and quickly? I have tried a long method but there are many errors. The method I have tried: Take data out of the list one by one and append in the particular row then move to the next data row and do the same thing there for the list.
you can try something like this :
`for i in range(len(data)) :
data[i][2].insert(0,data[i][1])
data[i][2].insert(0,data[i][0])
data[i].pop(1)
data[i].pop(0)`
You could do something like below.
We iterate through the given list, checking each type. If an element is a type list we will iterate through that and append each of those elements to a list called new. If an element we have iterated through in the given list is a type != list then we can directly append the element to new.
x = [1, 0,list([32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110])]
new = []
for num in x:
if type(num) == list:
for num2 in num:new.append(num2)
else:new.append(num)
You can flatten this jagged nested array with numpy.hstack() as follows:
import numpy as np
original = [1, 0,list([32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110])]
f = np.hstack(np.array(original, dtype=object)).tolist()
print(f)
#[1, 0, 32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110]
Let first consider this example
# lets take this example value for data
data = [1, 0,list([32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110])]
print("data before modification:", data)
print()
extract_items = data.pop(2) # Since in the picture, the inner list is always the third element we can use the index 2
for item in extract_items: # loop through the inner list and append items to outer
data.append(item)
print("extract_items", extract_items)
print("data", data)
**output for example case: **
data before modification: [1, 0, [32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110]]
extract_items: [32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110]
data: [1, 0, 32, 64, 117, 115, 101, 114, 32, 119, 104, 101, 110]
Which is what we would like.
Thus, the Final Solution:
for element in data:
extract_items = element.pop(2)
for item in extract_items:
element.append(item)
I have defined a function to generate a list of prime numbers less than or equal to a given input which you can specify, based on the sieve of eratosthenes.
def eratosthenes(max_num):
primes = list(range(2,max_num+1))
for i in primes:
j=2
while i*j<= primes[-1]:
if i*j in primes:
primes.remove(i*j)
j=j+1
return primes
Now, if I wanted to find out which prime would be the 50th in the list, such that
len(primes) = 50
How would I go about this? Is it possible to reverse-engineer a defined function in this way?
You will get the 50th value from the list just with eratosthenes(300)[49]:
def eratosthenes(max_num):
primes = list(range(2,max_num+1))
for i in primes:
j=2
while i*j<= primes[-1]:
if i*j in primes:
primes.remove(i*j)
j=j+1
return primes
print(eratosthenes(300))
print(eratosthenes(300)[49])
which will yield
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
229
The counting of indices starts from zero, therefore 50th value can be extracted with list index value 49.
Then if you would like to limit then length of the list to 50, you may reverse-engineer by calling the function many times until the condition is met:
i = 2
while True:
primes = eratosthenes(i)
if len(primes) >= 50:
break
i = i + 1
print(i)
print(len(primes))
print(primes[49])
which yields
229
50
229
Why don't you call the function in a loop with increasingly large max numbers until the length of the output is 50, then print out the last item in the output?
I've been writing Python for a while. What are the opinions on what is the best way to make these three lines concise?
my_range = range(97, 123) # I want a-z
my_range.append(32) # I want space as well
my_range = sorted(my_range) # I want it sorted (whilst not needed)
~~~~~~~~~~~~~~~~~~~~~
import random
count = 0
string = ""
my_range = range(97, 123)
my_range.append(32)
my_range = sorted(my_range)
while len(string) != 27:
string = string + chr(random.choice(my_range))
print string
~~~~~~~~~~~~~~~~
In Python 2, you can do:
>>> my_range = sorted([32]+range(97,123))
>>> my_range
[32, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]
Single Line Solution
In Python 2.7, in order to achieve this in single line, you may simply do:
>>> sorted(range(97, 123) + [32])
[32, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]
whereas in Python 3.x, you need to type-cast range to list as it returns object of range type (which doesn't have + operator defined for it):
>>> sorted(list(range(97, 123)) + [32])
[32, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]
Performance Efficient Solution (using bisect.bisect)
However, the performance efficient way to achieve this will be using bisect.bisect which gives the same result without performing sorting on the list. This solution is based on the fact that range list is already sorted. Hence, insert the new number at the correct position in the sorted list maintaining the sortedness (without explicitly sorting the list). Below are the example to illustrate the behavior:
from bisect import bisect
# Example 1: when new number falls inside the range
my_range = list(range(31, 35))
my_range.insert(bisect(my_range, 32), 32)
# where `my_range` will hold `[31, 32, 32, 33, 34]`
# Example 2: when new number falls before the range
my_range = list(range(97, 102))
my_range.insert(bisect(my_range, 32), 32)
# where `my_range` will hold `[32, 97, 98, 99, 100, 101]`
# Example 3: when new number falls after the range
my_range = list(range(25, 30))
my_range.insert(bisect(my_range, 32), 32)
# where `my_range` will hold `[25, 26, 27, 28, 29, 32]`
Performance Comparison
Below is the timeit comparison of both the above solution in Python 3. As the size of range will increase, the time difference between both the solution will increase proportionally.
using sorted(...) (1.91 usec per loop)
mquadri$ python3 -m timeit "sorted(list(range(97, 123)) + [32])"
100000 loops, best of 3: 1.91 usec per loop
using bisect.bisect(...) (1.18 usec per loop)
mquadri$ python3 -m timeit -s "from bisect import bisect" "my_range = list(range(97, 123)); my_range.insert(bisect(my_range, 32), 32)"
1000000 loops, best of 3: 1.18 usec per loop
I am trying to understand how struct work?
I have two confusions :
how struct is different from * and ** or *args and **kwargs ? where should i use struct and where should i use * and ** ?
My code is :
list(b'stackoverflow')
[115, 116, 97, 99, 107, 111, 118, 101, 114, 102, 108, 111, 119]
second doubt is suppose if someone send me this code :
[115, 116, 97, 99, 107, 111, 118, 101, 114, 102, 108, 111, 119]
i have to read it after unpacking it so general way is :
>>> struct.pack(b'bbbbbbbbbbbbb',115, 116, 97, 99, 107, 111, 118, 101, 114, 102, 108, 111, 119)
but i suppose i sentence is too long and i don't want to add many "bbbbbb" as argument so how can i use something which automatically detect the length of sentence ? something like
struct.pack(b'*magic',115, 116, 97, 99, 107, 111, 118, 101, 114, 102, 108, 111, 119)
can we do it?
The struct module has nothing to do with argument passing, so I simply can't guess what you're asking about in the first part.
To your second question, you shouldn't be using struct at all for that purpose. What you want is a bytes object corresponding to the list of little integers, and that's very easy to get:
>>> somelist = [115, 116, 97, 99, 107, 111, 118, 101, 114, 102, 108, 111, 119]
>>> bytes(somelist)
b'stackoverflow'
If for some reason you're determined to use struct.pack instead for this purpose, then this is the easiest way:
>>> struct.pack("b" * len(somelist), *somelist)
b'stackoverflow'
I have created a bingo game where random numbers are generated and called for a list.
bingo_num = random.randint(1,100)
How would I stop a random number being generated more than once
I would suggest, random.shuffle
from random import shuffle
my_list = range(100)
shuffle(my_list)
print my_list
But if you need only specific amount of unique numbers, you can use random.sample, like this
from random import sample
my_list = range(100)
print sample(my_list, 10)
You can take a sample of a range from the random library
>>> import random
>>> nums = random.sample(range(0,200),100)
>>> nums
[143, 149, 52, 183, 161, 179, 180, 155, 163, 157, 139, 15, 154, 181, 56, 29, 31,
14, 77, 82, 165, 32, 35, 92, 109, 172, 69, 99, 54, 3, 88, 76, 11, 126, 78, 162,
198, 145, 124, 75, 114, 174, 136, 100, 190, 193, 148, 153, 167, 113, 38, 17, 16
8, 0, 196, 73, 47, 164, 184, 6, 140, 30, 58, 74, 4, 79, 147, 178, 191, 21, 112,
13, 27, 57, 199, 116, 28, 104, 111, 71, 23, 85, 170, 25, 141, 156, 91, 7, 182, 1
34, 94, 169, 175, 166, 137, 160, 129, 36, 67, 135]
Just do it the same way they do it in a real Bingo game. They do not roll dices, but put all the numbers in a big bag, shake it, and pull out numbers one at a time, until all the numbers are used up.
numbers = list(range(1, 101)) # all the numbers in the bag, from 1 to 100
random.shuffle(numbers) # shake the bag
bingo_num = numbers.pop() # pull out next number (inside your loop)
You could first create a list containing all possible numbers. Then pick a random number from that list, add that to a result list and finally remove it from the list of possible numbers.
For example if you want 5 different numbers from 0-9:
possible_numbers = range(10)
numbers = []
for i in range(5):
index = random.randint(0, len(possible_numbers) - 1)
numbers.append(possible_numbers[index])
del possible_numbers[index]
Something like this should work:
numbers = []
while len(numbers) < 100:
bingo_num = random.randint(1,100)
if not bingo_num in numbers:
numbers.append(bing_num)
You could just create a new number if already in use
advantage: easy code
disadvantage: will run long if nearly all numbers are use (there are better solutions)
Sample code:
bingo_num_list = []
# init num
bingo_num = random.randint(1,100)
# create new numbers till "find" a not used one
while bingo_num in bingo_num_list:
bingo_num = random.randint(1,100)
bingo_num_list.append(bingo_num)
Build list of all the numbers then select a random 1, remove it from the list then select the next one:
#!/usr/bin/python
import random
myNumz=[]
xIdx=1
while xIdx<101:
myNumz.insert(xIdx,xIdx)
xIdx+=1
xIdx=100
while xIdx>0:
xIdx-=1
baseNum=random.randint(0,xIdx)
print myNumz[baseNum]
myNumz.remove(myNumz[baseNum])