Provide a context-free grammar that generates odd length language {w = 0*1* : |w| is odd} [closed] - complexity-theory

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
Provide a context-free grammar that generates the following language over
Σ = {0,1}: {w = 0*1* : |w| is odd}
My Solution:
S->AB|0|1
A->0A|^
B->1B|^
But using this grammar we are able to create an even number of string.
I want grammar that produces L = {0,1,000,111,001,011,00000,11111,00001,00011....}

An odd number is the sum of an odd number and an even number, so sentences in the language are either an odd number of 0s followed by an even number of 1s, or an even number of 0s followed by an odd number of ones. Moreover, an odd number is an even number plus one; if we make that substitution in the preceding description we get "an even number of 0s followed by either 0 or 1, followed by an even number of 1s". Since every even number is either 0 or two more than an even number, we end up with.
S -> A 0 B | A 1 B
A -> ε | A 0 0
B -> ε | B 1 1
or
S -> 0 | 1 | 0 0 S | S 1 1

Related

How to derive the big O of this algorithm? [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 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
This is the algorithm:
for i ← 1 to n by 1 do
for j ← 1 to i by 1 do
for k ← 1 to j by 1 do
x = x + 1
end
end
end
The number of times the inner loop iterate, depends on the outer loops... so how can the time complexity be derived?
This algorithm increases 𝑥 with the 𝑛th tetrahedral number. This is because the loops can be seen as sums over a range:
The outer loop: ∑𝑖𝑛= 1
The middle loop: ∑𝑗𝑖= 1
The inner loop: ∑𝑘𝑗= 1
The inner statement: runs in constant time, so that counts as 1 operation.
This means the inner statement runs this many times:
∑𝑖𝑛= 1 ( ∑𝑗𝑖= 1 ( ∑𝑘𝑗= 1
1 ) )
The inner sum really is 𝑗, so we can write:
∑𝑖𝑛= 1 ( ∑𝑗𝑖= 1
𝑗 )
We recognise this expression as the tetrahedral number (see formula at above link to Wikipedia)
This double sum is 𝑛(𝑛 + 1)(𝑛 + 2)/6 = θ(𝑛³)
Another way to see this, is that the loops lead to combinations for 𝑖, 𝑗, and 𝑘 where they appear in non-decreasing order. So the number of times x = x + 1 is executed, corresponds to the number of ways we can select a multiset of three values from the range 1..𝑛, allowing duplicates. This number is "𝑛 multichoose 3", which leads to the same formula.

Count integers up to n that contain the digits 2018 in order [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 4 years ago.
Improve this question
Given an integer n between 0 and 10,0000,0000, count the number of integers smaller than n which contain the digits [2,0,1,8] in order.
So e.g. the number 9,230,414,587 should be counted, because removing the digits [9,3,4,4,5,7] leaves us with [2,0,1,8].
Example input and output:
n = 2018 -> count = 1
n = 20182018 -> count = 92237
My general thought is that: the maximum length of n is 10 and the worst situation is that we have to insert 6 digits into [2,0,1,8] and remove the duplicates and the numbers greater than n.
I don't see any own attempts to solve, so I'll give only clue:
You have 9-digits number (small numbers might be represented as 000002018) containing digit sequence 2,0,1,8.
Name them 'good' ones.
Let denote digit places from 1 to 9 right to left:
number 532705183
digits 5 3 2 7 0 5 1 8 3
index 9 8 7 6 5 4 3 2 1
The most left '2' digit can occupy places from 4 to 9. How many good numbers contain the first 2 at k-th place? Let make function F2(l, k) for quantity of good numbers where 2 refers to digit 2, l is number length, k is place for the most left digit.
. . . . 2 . . . .
^
|
left part k right part should contain 0 1 8 sequence
without 2's
F2(9, k) = 9^(9-k) * Sum(F0(k-1, j) for j=1..k-1)
Overall quantity of good numbers is sum of F2(9, k) for all possible k.
GoodCount = Sum(F2(9, k) for k=4..9)
Explanation:
There are 9-k places at the left. We can put any digit but 2 there, so there are 9^(9-k) possible left parts.
Now we can place 0 at the right part and count possible variants for 018 subsequences. F0(...) will of course depend on F1(...) and F1 will depend on F8(...) for shorter numbers.
So fill tables for values for F8, F0, F1 step-by-step and finally calculate result for digit 2.
Hand-made example for 4-digit numbers containing subsequence 1 8 and k = position of the first '1':
k=2: there are 81 numbers of kind xx18
k=3: there are numbers of kind x1x8 and x18x
there are 9 subnumbers like x8, 10 subnumbers 8x, so (10+9)*9=171
k=4: there are numbers of kind
1xx8 (9*9=81 such numbers),
1x8x (9*10=90 numbers),
18xx (100 numbers),
so 81+90+100=271
Overall: 81+171+271=523
This is actually a relatively small problem set. If the numbers were much bigger, I'd opt to use optimised techniques to just generate all numbers that meet your criteria (those containing the digits in that order) rather than generating all possible numbers and checking each to ensure it meets the criteria.
However, the brute force method does your 20182018 variant in about ten seconds and the full 1,000,000,000 range in a little under eight minutes.
So, unless you need it faster than that, you may find the brute-force method more than adequate:
import re
num = 1000000000 # or 20182018 or something else.
lookfor = re.compile("2.*0.*1.*8")
count = 0
for i in range(num + 1):
if lookfor.search(str(i)) is not None:
count += 1
#print(count, i) # For checking.
print(count)

Solutions to fair distribution programming problems [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 6 years ago.
Improve this question
I have run into a wall since I attended a coding contest. I think this problem falls into the category of fair distribution, but I am not sure.
I am attaching the problem here.
I have come out with an empirical formula so which I tested successfully for 3 iterations attached here.
I am unable to come up with a working code that can solve this programming problem. Any help is appreciated.
Throughout the solution, I use A for hotel and B for house
Let us define 3 types of arrangements:
Arrangement ending with A||B, that is hotel on left and house on right.
Arrangement ending with B||A, that is house on left and hotel on right.
Arrangement ending with B||B, that is house on both sides.
Let us denote the respective arrays:
Array one[i] denotes number of arrangements of type 1 having street length i
Array two[i] denotes number of arrangements of type 2 having street length i
Array three[i] denotes number of arrangements of type 3 having street length i
Any arrangement of type 1, can be extended when the street length increases by 1 as follows:
B||A
A||B [A Type 2 arrangement]
B||B
A||B [A Type 3 arrangement]
Any arrangement of type 2, can be extended when the street length increases by 1 as follows:
A||B
B||A [A Type 1 arrangement]
B||B
B||A [A Type 3 arrangement]
Any arrangement of type 3, can be extended when the street length increases by 1 as follows:
A||B
B||B [A Type 1 arrangement]
B||A
B||B [A Type 2 arrangement]
B||B
B||B [A Type 3 arrangement]
When street length is 1:
one[1] = two[1] = three[1] = 1 (As explained in the sample test case)
When street length is i:
one[i] = two[i-1] + three[i-1]
two[i] = one[i-1] + three[i-1]
three[i] = one[i-1] + two[i-1] + three[i-1]
For a street length of size n, your answer is (one[n] + two[n] + three[n]) % 10^9+7
As n can be very huge, you need to come up with a general formula for the value of (one[n] + two[n] + three[n]) % 10^9+7
For n=1: result = 3
For n=2: result = 7
For n=3: result = 17
For n=4: result = 41
Verify the above results yourself!!!
Edit: You can use Matrix exponentiation to find out the result for different values of n in O(logn) time. Have a look here for more details!!!
You can use a matrix to represent the recurrence relation.
(one[n+1]) = ( 0 1 1 ) (one[n])
(two[n+1]) ( 1 0 1 ) (two[n])
(three[n+1]) ( 1 1 1 ) (three[n])
With this representation, it's feasible to compute values for large n, by matrix exponentation (modulo 10^9+7), using exponentation by squaring. That'll give you the result in O(log n) time.
(one[n]) = ( 0 1 1 )^(n-1) (1)
(two[n]) ( 1 0 1 ) (1)
(three[n]) ( 1 1 1 ) (1)
Happy Coding....

RE: Project Euler 1:Find the sum of all the multiples of 3 or 5 below 1000 comments [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
I see a lot of people saying that the sum of all the multiples of 3 and 5 up to 1000 being one sum and others saying another. Some include the multiples of 15, some don't. So, What is it in reality?
(1..1000).to_a.each do |i|
num = []
if i % 3 == 0
num << i
p num
elsif i % 5 == 0
num << i
p num
end
end
I know there is a better way to code this but when i run this code I don't get repeats. meaning no two 15s
So why would we have to subtract it?
just wondering.
Building upon the original problem statement from project euler which is less ambiguous:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
This means "multiples of either 3 or 5", which naturally includes multiples of 15 one or the other way around. However, your code will not work, because
the num variable is reset to an empty array on every iteration. You should instead initialize the variable outside of the loop
No need to store the numbers in an array, we can directly compute the sum.
1..1000 will iterate up to the number 1000 whereas the problem clearly wants us to find "all the multiples of 3 or 5 below 1000". You can achieve that with an exclusive range 1...1000 (three dots) or by writing 1..999. This should be the primary reason why you get the wrong result.
to_a is not necessary.
you can combine the conditionals with ||, so you dont have to write the identical inner code twice.
All of this leads to
sum = 0
(1...1000).each do |i|
if i % 3 == 0 || i % 5 == 0
sum += i
end
end
For a more functional-ish approach you could rewrite this as follows:
(1...1000).select {|x| x % 3 == 0 || x % 5 == 0 }.inject(:+)

compute The horizontal absolute difference value of a pixel [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
We have the test image with M rows and N columns as
f(x,y), for x∈ [1,M] and y∈ [1,N ]. The horizontal absolute
difference value of a pixel is defined by
D (x, y) = |f (x, y +1) − f (x, y −1)|.
need help in how to implement it in matlab
This will generate same size matrix, that you need:
mat1 = [zeros(2,size(f,2)); f];% adds 2 rows of zeros to begining
mat2 = [f;zeros(2,size(f,2))]; %adds 2 row of zeros to the end
Dd = mat1-mat2;
D = Dd(2:((size(Dd,1)-1)),:);%crop Dd matrix to size(f)
D = abs( f(1:end-1,:) - f(2:end,:) );
check out diff command as well. Note that D has 1 row less than f.
aux = abs(diff(f,[],2));
D = max(aux(:,1:end-1), aux(:,2:end));
For example: given
f = [3 5 6 4
2 5 4 3
8 9 3 1];
the result is
>> D
D =
2 2
3 1
6 6

Resources