Minimum pumping length of L= {0^i1^j | i >= j} to prove L is non regular - regular-language

Both Mr. Mithlesh Upadhyay and You play a game:
1. Mr. Mithlesh Upadhyay gives you a constant n.
2. You choose a word w in the language of length at least n.
3. Mr. Mithlesh Upadhyay gives you x, y, and z with xyz = w, |xy|≤n, and y not empty.
4. Now you pick r.
5. Mr. Mithlesh Upadhyay asserts that xyrz is also in the language.
6. If Mr. Mithlesh Upadhyay is wrong, you win.
In case, if you win the game, what is the minimum possible value of
your r for the language {0^i1^j | i >= j} ?
A. 0
B. 2
C. 3
D. Mr. Mithlesh Upadhyay won the game for given language.
Explanation
Given, above game is known as Pumping Lemma for regular languages.
You won the game for the minimum value of r is 0.
Option (A) is correct.
I could not understand the solution. How is it possible to get 0 length min pumping length.If we take
w = 01 and pump 1 , then it does not belong to L.
So, why is 2 not the minimum length ?

According to point number 5 and 6, if you are the winner, it means that Mr. Mithlesh Upadhyay was wrong and xyrz does not belong to L. In your example you have shown that 011 does not belong to L. If you are going to make the minimum possible length of r to be 2, then your example will be wrong. Because you have taken r to be of length 1. Correct me if I'm wrong.

Related

Interview Probability Brain Teaser

This is a question from the test our department gives to recent graduates. We're divided on the correct answer.
A mathematics class is composed of 10 students: 6 boys and 4 girls. 50% of the students are handed Test A to solve and the rest are given Test B. What is the probability that a girl gets Test A?
Should the answer be 0.5 ? Its pretty simple..
Since we answer programming questions here, here's an answer how to compute the probability.
I take that you mean: What is the probability that at least one girl gets Test A?
This can be computed by dividing the number of assignments of tests to persons where a girl gets Test A by the total possible number of permutations. To generate those assignments, it suffices to permute either the class or the tests; e. g. in R:
> library(combinat)
> class = c(rep('b', 6), rep('g', 4))
> sum(unlist(permn(class, function(pt) 'g' %in% pt[1:5])))/factorial(10)
[1] 0.9761905
> tests = c(rep('A', 5), rep('B', 5))
> sum(unlist(permn(tests, function(pt) 'A' %in% pt[7:10])))/factorial(10)
[1] 0.9761905
We see that permuting the class or the tests yields the same result.

Linear Optimization with Categorical Constraints

I am trying to optimize a lineup of players according to the level of competition to play based on maximizing a single statistic(Points in my example below). For a given lineup, 2 players are matched up against a competition level. Here is an example of my DataFrame(This is just an example, my actual DataFrame has a lot more players than this):
Comp Level Points Minutes Played
Player A Elite 10.1 22
Player A Middle -5 22
Player A Low 7.2 22
Player B Elite 0.8 20
Player B Middle 5.6 20
Player B Low 2.2 20
Player C Elite -7.2 18
Player C Middle 3.3 21
Player C Low 6.6 23
Player D Elite -7.2 18
Player D Middle 3.3 21
Player D Low 6.6 23
Player E Elite -7.2 18
Player E Middle 3.3 21
Player E Low 6.6 23
Player F Elite -7.2 18
Player F Middle 3.3 21
Player F Low 6.6 23
The optimized lineup will maximize the total points of the entire lineup (A sum of points from each player). The constraints are:
1. You can't use the same player for more than one competition level (For example, if player A is paired against the elite level, they can't be played against the middle or low levels).
2. Each Lineup Pair has to have a minutes played value that is within 10% of each other (For Example, Player A Could be Paired with Player B at the Elite Level but not Player C).
I'm hoping my output will look something like this:
Line up:
Elite: Player A and B
Middle: Player C and Player D
Low: Player E and Player F
Team Total Points: X
I've started by creating my DataFrame and figured on using PuLP for this optimization. The part where I am struggling with is adding my objective function, and appropriate constraints to the problem.
This is my code thus far:
prob = LpProblem('LineUp Optimization', LpMaximize)
players = list(data['Name'])
# print('The Players under consideration are \n' + '-'*100)
# for x in players:
# print(x, end=',\n ')
costs = dict(zip(players, data['Points']))
comp_levels = dict(zip(players, data['Competition Level']))
Minutes = dict(zip(players, data['Minutes Played']))
ozs = dict(zip(players, data['OZS%']))
player_var = LpVariable.dicts('Players', players, 0, cat='Integer')
Any help/guidance on what my constraints would look like within this PuLP optimization would be greatly appreciated!
If I understood you're problem correctly you need to choose a pair of players for each of the Elite, Middle and lower competition levels.
There are a number of ways you could formulate this problem. The most obvious to me would be to have sets of binary variables which indicate wheter (1) or not (0) each player is chosen for each level of competition:
player_var = LpVariable.dicts('player_var', (players, comp_levels), cat='Binary')
Your objective is to maximise total points of the included players:
prob += lpSum([player_var[p, l]*points[p, l] for p in players for l in comp_levels])
You then need to put in constraints that two players should be chosen for each level:
for l in comp_levels:
prob += lpSum([player_var[p, l] for p in players]) == 2
And that a player can play in at most one competition level:
for p in players:
prob += lpSum([player_var[p, l] for l in comp_levels]) <= 1
And that your 10% rule on minutes played at the chosen level for the pairing is satisfied. This last one is kind of interesting. I'm sure there is a more efficient way of doing this but what I've done below should work... I think.
for l in comp_levels:
for p in players:
prob += minutes[p, l]*player_var[p,l] <= 1.1*lpSum([minutes[p,l]*player_ver[other_p,l] for other_p in players if other_p != p])
This creates a constraints for each competition level, and for each player, however if the player is not chosen to play at the level (player_var[p,l] == 0) then the constraints should have no effect - term on left-hand-side will be 0.
If the player is chosen (player_var[p,l] == 1) then the constaint ensures that the chosen player has no more than 10% more minutes than their oponent - note the right hand sand takes a sum over all other players but only 1 of these will be non-zero.
No check is carried out to ensure the chosen player has at least 90% of the minutes of their oponent - but that case is covered when this constraint is applied to their oponent.
I'm sure someone out there can suggest a much more efficient/elegant way of doing this...

How can I solve this classical dynamic programming problem?

There are N jewellery shop(s). Each jewellery shop has three kinds of coins - Gold, Platinum, and Diamond having worth value A, B, and C respectively. You decided to go to each of N jewellery shop and take coins from each of the shop. But to do so following conditions must satisfy -
You can take at most 1 coin from an individual shop.
You can take at most X coins of Gold type.
You can take at most Y coins of Platinum type.
You can take at most Z coins of Diamond type.
You want to collect coins from shops in such a way that worth value of coins collected is maximised.
Input Format :
The first line contains an integer N. Where N is the number of jewellery shops.
The second line contains three integers X, Y, Z. Where X, Y, Z denotes the maximum number of coins you can collect of type Gold, Platinum, and diamond respectively.
Then N lines contain three space-separated integers A, B, C. Where A, B, C is the worth value of the Gold, Platinum, and diamond coin respectively.
Output Format :
Print a single integer representing the maximum worth value you can get.
Constraints :
1
<=
N
<=
200
1
<=
X
,
Y
,
Z
<=
N
1
<=
A
,
B
,
C
<
10
9
Example : -
4
2 1 1
5 4 5
4 3 2
10 9 7
8 2 9
Answer:-
27(9+9+5+4)
I tried the obvious greedy approach but it failed :-)

recursive exponentiator output and complexity

def exp3(a,b):
if b == 1:
return a
if (b%2)*2 == b:
return exp3(a*a, b/2)
else: return a*exp3(a,b-1)
This is a recursive exponentiator program.
Question 1:
If b is even, it will exceute (b%2)2 == b. If b is odd, it will exceute aexp3(a,b-1). There is no problem in my program. If b is 4, (4%2)*2=0, and 0 is not equal to b. So I can't understand how to calculate b when it's even.
Question 2:
I want to calucate the number of steps in the program. so according to my textbook, I can get the formual as follows.
b even t(b) = 6 + t(b/2)
b odd t(b) = 6 + t(b-1)
Why is the first number 6? How can I get the number 3 in the beginning?
Your (b%2)*2 == b test is never true. I think you want b % 2 == 0 to test if b is even. The code still gets the right answer because the other recursive case (intended only for odd b values) works for even ones too (it's just less efficient).
As for your other question, I have no idea where the 6 is coming from either. It depends a lot on what you're counting as a "step". Usually it's most useful to discuss performance in terms of "Big-O" values rather than specific numbers.

Need Hint for ProjectEuler Problem

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
I could easily brute force the solution in an imperative programming language with loops. But I want to do this in Haskell and not having loops makes it much harder. I was thinking of doing something like this:
[n | n <- [1..], d <- [1..20], n `mod` d == 0] !! 0
But I know that won't work because "d" will make the condition equal True at d = 1. I need a hint on how to make it so that n mod d is calculated for [1..20] and can be verified for all 20 numbers.
Again, please don't give me a solution. Thanks.
As with many of the Project Euler problems, this is at least as much about math as it is about programming.
What you're looking for is the least common multiple of a set of numbers, which happen to be in a sequence starting at 1.
A likely tactic in a functional language is trying to make it recursive based on figuring out the relation between the smallest number divisible by all of [1..n] and the smallest number divisible by all of [1..n+1]. Play with this with some smaller numbers than 20 and try to understand the mathematical relation or perhaps discern a pattern.
Instead of a search until you find such a number, consider instead a constructive algorithm, where, given a set of numbers, you construct the smallest (or least) positive number that is evenly divisible by (aka "is a common multiple of") all those numbers. Look at the algorithms there, and consider how Euclid's algorithm (which they mention) might apply.
Can you think of any relationship between two numbers in terms of their greatest common divisor and their least common multiple? How about among a set of numbers?
If you look at it, it seems to be a list filtering operation. List of infinite numbers, to be filtered based on case the whether number is divisible by all numbers from 1 to 20.
So what we got is we need a function which takes a integer and a list of integer and tells whether it is divisible by all those numbers in the list
isDivisible :: [Int] -> Int -> Bool
and then use this in List filter as
filter (isDivisible [1..20]) [1..]
Now as Haskell is a lazy language, you just need to take the required number of items (in your case you need just one hence List.head method sounds good) from the above filter result.
I hope this helps you. This is a simple solution and there will be many other single line solutions for this too :)
Alternative answer: You can just take advantage of the lcm function provided in the Prelude.
For efficiently solving this, go with Don Roby's answer. If you just want a little hint on the brute force approach, translate what you wrote back into english and see how it differs from the problem description.
You wrote something like "filter the product of the positive naturals and the positive naturals from 1 to 20"
what you want is more like "filter the positive naturals by some function of the positive naturals from 1 to 20"
You have to get Mathy in this case. You are gonna do a foldl through [1..20], starting with an accumulator n = 1. For each number p of that list, you only proceed if p is a prime. Now for the previous prime p, you want to find the largest integer q such that p^q <= 20. Multiply n *= (p^q). Once the foldl finishes, n is the number you want.
A possible brute force implementation would be
head [n|n <- [1..], all ((==0).(n `mod`)) [1..20]]
but in this case it would take way too long. The all function tests if a predicate holds for all elements of a list. The lambda is short for (\d -> mod n d == 0).
So how could you speed up the calculation? Let's factorize our divisors in prime factors, and search for the highest power of every prime factor:
2 = 2
3 = 3
4 = 2^2
5 = 5
6 = 2 * 3
7 = 7
8 = 2^3
9 = 3^2
10 = 2 * 5
11 = 11
12 = 2^2*3
13 = 13
14 = 2 *7
15 = 3 * 5
16 = 2^4
17 = 17
18 = 2 * 3^2
19 = 19
20 = 2^2 * 5
--------------------------------
max= 2^4*3^2*5*7*11*13*17*19
Using this number we have:
all ((==0).(2^4*3^2*5*7*11*13*17*19 `mod`)) [1..20]
--True
Hey, it is divisible by all numbers from 1 to 20. Not very surprising. E.g. it is divisible by 15 because it "contains" the factors 3 and 5, and it is divisible by 16, because it "contains" the factor 2^4. But is it the smallest possible number? Think about it...

Resources