How this state set of DFA was retrieved from the given NFA - state-machine

I have this NFA:
1,{2, 3}
2,empty
3,{4}
4,empty
All the arrows in this NFA are epsilon-arrows.
I understand that all possible states that can be reached from each of the states, using only epsilon paths are these:
E(1) = {1,2,3,4}
E(2) = {2}
E(3) = {3,4}
E(4) = {4}
However I don't understand how this state set was achieved:
DFA = {empty, {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}, {1, 2,
3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}}
My understanding is that the transition table that looks like this:
epsilon
1 2, 3
2 -
3 4
4 -
is to be used to determine the DFA, along with the E function.
I tried to do it like this:
Start state = 1 => the Result = {E(1)} = {{1, 2, 3, 4}}
T({1, 2, 3, 4}) = E(transitionTable({1, 2, 3, 4})) = E({2, 3, 4}) = {2, 3, 4}
Result = {{1, 2, 3, 4}, {2, 3, 4}}
T({2, 3, 4}) = E({4}) = {4}
Result = {{1, 2, 3, 4}, {2, 3, 4}, {4}}
T({4}) = E({}) = {}
Result = {{1, 2, 3, 4}, {2, 3, 4}, {4}, {}}
What am I doing wrong here? Any help is appreciated.

Related

How to merge lists value with shared key of two dictionaries?

e.g.
d1 = {'a':[1, 2, 3], 'b': [1, 2, 3]}
d2 = {'a':[4, 5, 6], 'b': [3, 4, 5]}
The output should be like this:
{'a':[1, 2, 3, 4, 5, 6], 'b': [1, 2, 3, 4, 5]}
If the value repeats itself, it should be recorded only once.
Assuming both dictionaries have the same keys and all keys are present in both dictionaries.
One way to achieve could be:
d1 = {'a':[1, 2, 3], 'b': [1, 2, 3]}
d2 = {'a':[4, 5, 6], 'b': [3, 4, 5]}
# make a list of both dictionaries
ds = [d1, d2]
# d will be the resultant dictionary
d = {}
for k in d1.keys():
d[k] = [d[k] for d in ds]
d[k] = list(set([item for sublist in d[k] for item in sublist]))
print(d)
Output
{'a': [1, 2, 3, 4, 5, 6], 'b': [1, 2, 3, 4, 5]}

How to extract the values of 2 nested dictionary and summing their respective values together without using any import functions?

I have a dictionary that looks something like that
{A:{'score': 0, 'throw1': [3, 2, 5, 6], 'throw2': [1, 5, 5, 1]},
'B': {'score': 0, 'throw1': [2, 2, 3, 6], 'throw2': [6, 4, 2, 2]}}
A and B are players in this game and the throw1 and throw2 are their dice rolls. Each player has 4 attempts.
My question is how do i extract both throw1 and throw2 from the dictionary and sum their respective attempts together for each player. For instance, Player A threw 3 and 1 for their first attempt on both throws. I want the answer to be 4
you can use the zip that returns an iterator of tuples
data = {'A': {'score': 0, 'throw1': [3, 2, 5, 6], 'throw2': [1, 5, 5, 1]},
'B': {'score': 0, 'throw1': [2, 2, 3, 6], 'throw2': [6, 4, 2, 2]}}
player_A_1_results = data['A']['throw1']
player_A_2_results = data['A']['throw2']
for f, s in zip(player_A_1_results, player_A_2_results):
print(f + s)
You could iterate each players throw summary like below
data = {'A': {'score': 0, 'throw1': [3, 2, 5, 6], 'throw2': [1, 5, 5, 1]},
'B': {'score': 0, 'throw1': [2, 2, 3, 6], 'throw2': [6, 4, 2, 2]}}
for player, info in data.items():
print("Throw summary of player "+player)
for t1, t2 in zip(info["throw1"], info["throw2"]):
print(t1+t2)

Finding the number of occurrences of various groups in a list

`So,I am given a list-
group = [2,1,3,4]
Each index of this list represents a group.
So group 0 = 2
group 1 = 1
group 2 = 3
group 3 = 4
I am given another list called :
l =[[[0, 0, 3, 3, 3, 3], [0, 0, 1, 3, 3, 3, 3]], [[0, 1]], [[2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3]], [[0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3]]]
the output I want is:
dict = {0:[0,3],1;[1],2:[2,3],3:[0,2]}
If the number of times the element appears in each sublist of l ie if both l[0][0] and l[0][1] have 0's appear 2 times, it is added to the index 0 of the dict. Since both l[0][0] and l[0][1] have 3 appear 4 times(this is because group[3] is 4), it is added to the index 0.
now in l[1][0] and 0 appears just once(instead of twice) so its not added to index 1. However 1 just appears once so it is added to index 1.Thanks!
What I have tried so far:
def tryin(l,groups):
for i in range(len(l)):
count = 0
for j in range(len(l[i])):
if j in (l[i][j]):
count+=1
if count == groups[i]:
print(i,j)
try this code :
input:
group = [2,1,3,4]
l =[[[0, 0, 3, 3, 3, 3], [0, 0, 1, 3, 3, 3, 3]], [[0, 1]], [[2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3]], [[0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3]]]
def IntersecOfSets(list_):
result = set(list_[0])
for s in list_[1:]:
result.intersection_update(s)
return result
def nb_occ(l,group):
d = {}
for i in l:
l2=[]
for j in i:
l1 = []
for x in group:
if j.count(group.index(x)) >= x :
y=group.index(x)
l1.append(y)
l2.append(l1)
if len(l2)>1:
d[str(l.index(i))]= IntersecOfSets(l2)
else:
d[str(l.index(i))]= l2[0]
return d
print(nb_occ(l,group))
output:
{'2': {2, 3}, '1': [1], '0': {0, 3}, '3': {0, 2}}

Python: finding all pallindromic sequences of length k that sum to n

I'm trying to find all palindromic sequences of length k that sum to n. I have a specific example (k=6):
def brute(n):
J=[]
for a in range(1,n):
for b in range(1,n):
for c in range(1,n):
if (a+b+c)*2==n:
J.append((a,b,c,c,b,a))
return(J)
The output gives me something like:
[(1, 1, 6, 6, 1, 1),
(1, 2, 5, 5, 2, 1),
(1, 3, 4, 4, 3, 1),
(1, 4, 3, 3, 4, 1),
(1, 5, 2, 2, 5, 1),
(1, 6, 1, 1, 6, 1),
(2, 1, 5, 5, 1, 2),
(2, 2, 4, 4, 2, 2),
(2, 3, 3, 3, 3, 2),
(2, 4, 2, 2, 4, 2),
(2, 5, 1, 1, 5, 2),
(3, 1, 4, 4, 1, 3),
(3, 2, 3, 3, 2, 3),
(3, 3, 2, 2, 3, 3),
(3, 4, 1, 1, 4, 3),
(4, 1, 3, 3, 1, 4),
(4, 2, 2, 2, 2, 4),
(4, 3, 1, 1, 3, 4),
(5, 1, 2, 2, 1, 5),
(5, 2, 1, 1, 2, 5),
(6, 1, 1, 1, 1, 6)]
The issue is that I have no idea how to generalize this to any values of n and k. I hear that dictionaries would be helpful. Did I mention I was new to python? any help would be appreciated
thanks
The idea is that we simply count from 0 to 10**k, and consider each of these "integers" as a palindrome sequence. We left pad with 0 where necessary. So, for k==6, 0 -> [0, 0, 0, 0, 0, 0], 1 -> [0, 0, 0, 0, 0, 1], etc. This enumerates over all possible combinations. If it's a palindrome, we also check that it adds up to n.
Below is some code that (should) give a correct result for arbitrary n and k, but is not terribly efficient. I'll leave optimizing up to you (if it's necessary), and give some tips on how to do it.
Here's the code:
def find_all_palindromic_sequences(n, k):
result = []
for i in range(10**k):
paly = gen_palindrome(i, k, n)
if paly is not None:
result.append(paly)
return result
def gen_palindrome(i, k, n):
i_padded = str(i).zfill(k)
i_digits = [int(digit) for digit in i_padded]
if i_digits == i_digits[::-1] and sum(i_digits) == n:
return i_digits
to test it, we can do:
for paly in find_all_palindromic_sequences(n=16, k=6):
print(paly)
this outputs:
[0, 0, 8, 8, 0, 0]
[0, 1, 7, 7, 1, 0]
[0, 2, 6, 6, 2, 0]
[0, 3, 5, 5, 3, 0]
[0, 4, 4, 4, 4, 0]
[0, 5, 3, 3, 5, 0]
[0, 6, 2, 2, 6, 0]
[0, 7, 1, 1, 7, 0]
[0, 8, 0, 0, 8, 0]
[1, 0, 7, 7, 0, 1]
[1, 1, 6, 6, 1, 1]
[1, 2, 5, 5, 2, 1]
[1, 3, 4, 4, 3, 1]
[1, 4, 3, 3, 4, 1]
[1, 5, 2, 2, 5, 1]
[1, 6, 1, 1, 6, 1]
[1, 7, 0, 0, 7, 1]
[2, 0, 6, 6, 0, 2]
[2, 1, 5, 5, 1, 2]
[2, 2, 4, 4, 2, 2]
[2, 3, 3, 3, 3, 2]
[2, 4, 2, 2, 4, 2]
[2, 5, 1, 1, 5, 2]
[2, 6, 0, 0, 6, 2]
[3, 0, 5, 5, 0, 3]
[3, 1, 4, 4, 1, 3]
[3, 2, 3, 3, 2, 3]
[3, 3, 2, 2, 3, 3]
[3, 4, 1, 1, 4, 3]
[3, 5, 0, 0, 5, 3]
[4, 0, 4, 4, 0, 4]
[4, 1, 3, 3, 1, 4]
[4, 2, 2, 2, 2, 4]
[4, 3, 1, 1, 3, 4]
[4, 4, 0, 0, 4, 4]
[5, 0, 3, 3, 0, 5]
[5, 1, 2, 2, 1, 5]
[5, 2, 1, 1, 2, 5]
[5, 3, 0, 0, 3, 5]
[6, 0, 2, 2, 0, 6]
[6, 1, 1, 1, 1, 6]
[6, 2, 0, 0, 2, 6]
[7, 0, 1, 1, 0, 7]
[7, 1, 0, 0, 1, 7]
[8, 0, 0, 0, 0, 8]
Which looks similar to your result, plus the results that contain 0.
Ideas for making it faster (this will slow down a lot as k becomes large):
This is an embarrassingly parallel problem, consider multithreading/multiprocessing.
The palindrome check of i_digits == i_digits[::-1] isn't as efficient as it could be (both in terms of memory and CPU). Having a pointer at the start and end, and traversing characters one by one till the pointers cross would be better.
There are some conditional optimizations you can do on certain values of n. For instance, if n is 0, it doesn't matter how large k is, the only palindrome will be [0, 0, 0, ..., 0, 0]. As another example, if n is 8, we obviously don't have to generate any permutations with 9 in them. Or, if n is 20, and k is 6, then we can't have 3 9's in our permutation. Generalizing this pattern will pay off big assuming n is reasonably small. It works the other way, too, actually. If n is large, then there is a limit to the number of 0s and 1s that can be in each permutation.
There is probably a better way of generating palindromes than testing every single integer. For example, if we know that integer X is a palindrome sequence, then X+1 will not be. It's pretty easy to show this: the first and last digits can't match for X+1 since we know they must have matched for X. You might be able to show that X+2 and X+3 cannot be palindromes either, etc. If you can generalize where you must test for a new palindrome, this will be key. A number theorist could help more in this regard.
HTH.

Replacing elements of a list that depend on elements of other lists

I have two lists:
data1 = {0, 1, 1, 0, 0}
data2 = {1, 2, 3, 4, 5}
I want to replace the elements in data2 depending on the value of data1.
For example, if data1=0, i want data2 to replaced with 0, otherwise i want data2 to stay as it is.
The output i am looking for is:
data2 = {1, 0, 0, 4, 5};
For the required output, if data1 = 0, data2 is not replaced with 0.
data1 = {0, 1, 1, 0, 0};
data2 = {1, 2, 3, 4, 5};
data2 = MapThread[If[#1 == 0, #2, 0] &, {data1, data2}]
{1, 0, 0, 4, 5}
also
data2 = UnitStep[-Abs#data1]*data2
{1, 0, 0, 4, 5}
another way..
ReplacePart[data2, Position[data1, 0] -> 0]
{0, 2, 3, 0, 0}
note your example output doesn't agree with the text of your question.

Resources