How can I generate all possible words with a specified set of characters? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I play to HackNet game and i have to guess a word to bypass a firewall.
The key makes 6 characters long and contains the letters K,K,K,U,A,N.
What is the simplest way to generate all possible combinations either in bash or in python ? (bonus point for bash)

Here is a backtracking-based solution in Python:
db = {"K" : 3, "U" : 1, "A" : 1, "N" : 1}
N = 6
def possibilities(v):
l = []
for k in db.keys():
if v.count(k) < db[k]:
l.append(k)
return l
def generateImpl(a):
if len(a) < N:
lst = []
for c in possibilities(a):
lst += generateImpl(a+[c])
return lst
else:
return [''.join(a)]
def generate():
return generateImpl([])
Just run generate() to obtain a list of possible words.

You have 6 letters and need to find a combination of 6 letters. If you are not using the same character in ['K','K','K','U','A','N'] again and again, then there is only 1 permutation.
If you can use the same character again and again, you can use the following code to generate all possible combinations.
import itertools
y = itertools.combinations_with_replacement(['K','U','A','N'],6)
for i in list(y):
print(i)

Related

Tuple into list to find max value and if there is a duplicate [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am creating a function that takes any quantity of numbers, and tell you what is the max value or if there is a tie for largest. I am wondering what I could do to simplify what I have.
def max_num(*args):
nums = []
nums_1 = []
nums.append(args)
i = 0
while i < len(nums[0]):
nums_1.append(nums[0][i])
i += 1
c = max(nums_1)
nums_1.remove(c)
if c in nums_1:
print("It's a tie!")
else:
print(c)
max_num(-10, 0, 10, 10)
So when I initially make a list with the arguments given, it gives me a tuple inside the list. This is why I create a new list to dissect the tuple into separate values. I have the feeling that wasn't necessary, and that there is a much simpler way to do this. Any advice would be great.
Just get the max, and count how many times it appears in your data:
def max_num(*args):
maxi = max(args)
if args.count(maxi) == 1:
print(maxi)
else:
print('Tie')
max_num(2, 5, 1)
#5
max_num(2, 5, 1, 5)
#Tie

In Python how do I extract all substrings that cross a certain index in a longer string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Say I have a string (mystring). I want to extract all possible substrings of mystring so long as the substrings are lengths 8-15. I've been able to do that with no problem (see code below). However, what if I want to only extract these substrings if they overlap a certain part of mystring? The overlap is defined by the position in mystring rather than a certain letter of mystring, as the letters are not unique across mystring.
In the example below, I might want my substrings to include zero-based index 11.
mystring = "JACKANDJILLRANUPTHEHILLFORWATER"
substrings = set()
for i in range(0, len(mystring)):
for length in range(8,16):
ss = mystring[i:i+length]
if len(ss) == length:
substrings.add(ss)
Simple answer
You could check that 11 is included in [i, i + length) by checking i <= 11 < i + length:
mystring = "JACKANDJILLRANUPTHEHILLFORWATER"
substrings = set()
for i in range(0, len(mystring)):
for length in range(8,16):
ss = mystring[i:i+length]
if len(ss) == length and i <= 11 < i + length:
substrings.add(ss)
As set comprehension
You could do it like this:
substrings = {mystring[i:j]
for i in range(0, len(mystring))
for j in range(i + 8, min(i + 16, len(mystring)))
if i <= 11 < j}

How to form a given list of elements in python to create another set of lists from it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
List1 =['000095', '000094', '000092', '000101', '000099', '000096', '000095']
def makecycle(list, startElement):
A loop which forms the bottom list which is made from the upper one's elements!
if i pass that function the start element and the list it shoul print like this:
makecycle(list1, 000094) it should print:
['000094', '000092', '000101', '000099', '000096', '000095', '000094']
and if pass
makecycle(list1, 000101) it should print:
['000101', '000099', '000096', '000095', '000094', '000092', '000101']
and if pass
makecycle(list1, 000092) it should print:
['000092', '000101', '000099', '000096', '000095', '000094', '000092']
i know its kinda not clear enough but thats all i can point!
def makecycle(list1,startElement):
ind = list1.index(startElement)
l = len(list1)
i = ind
list2 = []
while ind < (l-1):
list2.append(list1[ind])
ind = ind + 1
for x in range(i):
if list1[x] not in list2:
list2.append(list1[x])
print(list2)

Python: how to display all possible cases to place brackets [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Given a number of brackets pairs. I want to display all correct combinations of these brackets. By correct I mean each bracket should be opened before closing in each combination. For example, if the number of brackets is 2 the output should be:
(())
()()
For 3:
((()))
()()()
(()())
(())()
()(())
The order of the output lines doesn't matter.
How can I do it with Python?
Try this code, please:
def __F(l, r, pref):
if r < l or l < 0 or r < 0:
return
if r == 0 and l == 0:
print(pref)
return
__F(l - 1, r, pref + "(")
__F(l, r - 1, pref + ")")
def F(n):
__F(n, n, "")
F(2)
F(3)

Generate all possible combinations in dictionary [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
mydict ={1:'All',
2:'Web',
4:'Iphone',
8:'Ipad',
16:'Android',
32:'Iphone-Web',
64:'Ipad-Web',
128:'Android-Web',
256:'Android-Web-Desktop',
512:'Iphone-Web-Desktop',
1024:'Ipad-Web-Desktop'}
This is my dictionary. How to print all possible combinations like:
1:'All'
2:'Web'
4:'Iphone'
8:'Ipad'
16:'Android'
32:'Iphone-Web'
64:'Ipad-Web'
128:'Android-Web'
256:'Android-Web-Desktop'
512:'Iphone-Web-Desktop'
1024:'Ipad-Web-Desktop'
3:'All-Web'
5: 'All-Iphone'
6: 'Web-Iphone'
7: 'All-Web-Iphone'
and so on. The combination needs to be created using this idea,
sum(keys): 'values(key1)-value(key2)'
.Also there are few combinations already, please assume them as new device. The maximum length of combinations is len(mydict). I need it in Python. Thanks.
Printing all single result and then combinations of two keys using combinations.
from itertools import combinations
for key in mydict:
print "{}: '{}'".format(key, mydict[key])
for x, y in combinations(mydict, 2):
print "{}: '{}'".format(x+y, '-'.join((mydict[x], mydict[y])))
UPDATED
Print all possible combinations.
from itertools import combinations
for n in range(len(mydict)):
for combs in combinations(sorted(mydict), n+1):
print "{}: '{}'".format(sum(combs), '-'.join([mydict[key] for key in combs]))
We don't know what you are actually looking for, but if it it is the powerset you are looking for:
from copy import deepcopy
def powerset (id_base=0, prefix="", dictionary=dict(), length=0):
if length < 0:
return dict()
if length == 0:
return dict((id_base,prefix))
if len(prefix):
prefix = prefix + "-"
this_powerset = dict()
for key, item in dictionary.items():
smaller_dictionary = deepcopy(dictionary)
del smaller_dictionary[key]
this_powerset = dict(
powerset( id_base + key,
prefix + item,
smaller_dictionary,
length - 1 ).items()
+ this_powerset.items()
return this_powerset
run as
the_set = powerset(dictionary = mydict, length = len(mydict))
(Comes with all the exectution time problems of recursive functions)

Categories