How to design a DFA for the following language? - theory

How do I go about designing a DFA for:
Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
set of decimal digits.
L = {w| The decimal number represented by w leaves an odd remainder when divided by seven.}
So far, I have (hand) drawn out seven states (q0 - q6), with the odd number of q states being accepting.
Where do I go from here?

I would construct this in two steps:
Construct a DFA whose states keep track of the remainder of w when divided by seven. You can do this by constructing states 0, 1, 2, 3, ..., 6 and linking them as follows: if w leaves a remainder of r when divided by 7 and the next digit is d, then you want to end up in the state corresponding to 10r + d (mod 7). This will give ten outgoing links from each state. It will be annoying to compute these links, but you only have to do it once.
Mark states 1, 3, and 5 as accepting and everything else as rejecting.
Hope this helps!

Related

Generate a random number without a given digit

I want to generate a random number without a specific single digit like random numbers without 5 are 240,237,112, 198 etc. I want to do this in C programming.
Set up an array of digits without the missing digit: [0, 1, 2, 3, 4, 6, 7, 8, 9].
Pick digits from the array to assemble the number.

Finding the square of a number in a sequential array of odds

I have a sequential odd array starting at 3. So x = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13...}.
I am wondering if there is a quick way to find at what index the square of a number n is at. So if n was 5, I am looking for where 25 is in the array. Right now I have ((n) * (n - 1)) which I add to the current i index. Is there anything faster?
Your array is made of consecutive numbers and it's sorted, Because of this it forms a mathematical arithmetic progression with difference 1 and first element as 3, so at index i we have a[i]=i+3 and so i=a[i]-3.
So to find the index of the square of n let nsqr be n*n, nsqr index is simply nsqr-3, that's an O(1) algorithm.
To make it general whenever we have consecutive sorted numbers which start with a0 and differ by d, to find where is the square of n we do (nsqr-a0)/d.

Modifying a numpy index during assignment

I am unsure of the correct terminology to search for to find the correct optimisation. I want to simplify the final four lines of code below into two lines, whereby, the addition of +/- 1 is done during the assignment to plus and minus variables respectively.
# generic params to simulate loop conditions
position = np.arange(10)
axis = 2
# actual code to optimise
plus = np.asarray(position)
plus[axis] += 1
minus = np.asarray(position)
minus[axis] -= 1
To clarify this is an iteration problem: Any solutions that don't take generic position or axis variables are wrong i.e. explicitly the following are not solutions:
plus = np.asarray([0,1,3,3,4,5,6,7,8,9])
plus = np.asarray(range(axis)+[position[axis]+1]+range(axis+1,len(position)))
Here's an approach using np.in1d to condense those four lines to two -
mask = np.in1d(np.arange(position.size),axis)
plus, minus = position + mask, position - mask
Sample run
Let's test it out for a generic position array with another index 6 -
In [60]: position
Out[60]: array([1, 0, 6, 8, 1, 7, 1, 3, 1, 6])
In [61]: axis = 6
In [62]: mask = np.in1d(np.arange(position.size),axis)
In [63]: plus, minus = position + mask, position - mask
In [64]: plus
Out[64]: array([1, 0, 6, 8, 1, 7, 2, 3, 1, 6]) # Change at 6th index
In [65]: minus
Out[65]: array([1, 0, 6, 8, 1, 7, 0, 3, 1, 6]) # Change at 6th index

Removing numbers from a large range of numbers

I've got the following problem that I'm trying to find a more optimal solution for.
Let's say you have a range of numbers between 0 and 9:
Values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Index: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Now, let's say you "remove" 1, 4, 5, and 7:
Values: 0, -, 2, 3, -, -, 6, -, 8, 9
Index: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Where there is no value, all subsequent values are shifted to the left:
Values: 0, 2, 3, 6, 8, 9
Index: 0, 1, 2, 3, 4, 5
The value at index 1 has now become 2 (was 1), the value at index 2 is now 3 (was 2), the value at index 3 is now 6 (was 3), etc.
Here's the problem. I need to manage this on a larger scale, up to tens of thousands of values. A random number of those values will be removed from the original contiguous range, and potentially added back afterwards (but not in the same order they were removed). The starting state will always be a complete sequence of numbers between 0 and MAX_VAL.
Things I've tried:
1) Maintaining an array of values, removing values from that array, and shifting everything over by one. This fails because you're iterating through all the values after the one you've just removed, and it's too slow as a result. Getting the value for a given index afterwards is really fast though.
2) Maintaining a linked list of values, and removing the value by pulling it out of the list. This seems to be slow both adding/removing values and getting the value at a given index, since I need to walk through the list first.
3) Keeping track of the "removed" values, rather then maintaining a giant array/list/etc of values from 0 to MAX_VAL. If the removed values are stored in an ordered array, then it becomes trivial to calculate how many values have been removed before and after a given index, and just return an offset index instead. This kinda works, except it's slow to maintain the ordered array of removed values and iterate through that instead, especially if the number of removed values approaches MAX_VAL.
Is there some sort of algorithm or technique that can handle this kind of problem more quickly and efficiently?
Is there some sort of algorithm or technique that can handle this kind of problem more quickly and efficiently?
The answer very much depends on typical use cases:
Is the set of numbers typically sparse or dense?
How often do you do insertions vs. removals vs. lookups?
In which patterns are numbers inserted or removed (random, continuous, from the end or start)?
What are there any memory constraints?
Here are some ideas for a generic solution:
Create a structure that stores ranges instead of numbers.
Start with a single entry: 0 - MAX_VAL.
A range can have subranges. This resulting graph of ranges forms a tree.
Removing a number splits a leaf range into two, creating two new leafs.
This algorithm would perform quite well when the set is dense (because there are few ranges). It would still perform somewhat fast when the graph grows (O(log n) for lookups) when you keep the tree balanced.
Now, let's say you "remove" 1, 4, 5, and 7:
Values: 0, -100, 2, 3, -100, -100, 6, -100, 8, 9// use a unique value that doesn't used in array
Index: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

How Can I Creat Method do This Job?

I want create method that return an array which contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4.
Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3 or 4, and a 3 appears in the array before any 4.
Example:
problem({1, 3, 1, 4, 4, 3, 1}) → {1, 3, 4, 1, 1, 3, 4}
problem({3, 2, 2, 4}) → {3, 4, 2, 2}
thanks .
Set i = 0, j = 0. Then you repeat the following:
Find the first 3 at an index ≥ i which is not followed by a 4. If none are found, you succeeded. If the 3 is the last number in the array or followed by a 3, you failed. Now find the first 4 at an index ≥ j which is not preceded by a 3. If none are found, you fail. Otherwise set i = location of the 3, j = location of the 4, exchange the objects at positions i+1 and j, set i = i + 2 and j = j + 1, and repeat.
I don't like writing code that depends on promises about the data that I don't verify myself, so this will work whatever is in the array.

Resources