Let Σ={a,b,c}. How many languages over Σ are there such that each string in the language has length 2 or less? - finite-automata

First of all I see the number of strings as the following:
1 (epsilon 0 length string) + 3 (pick one letter) + 9 (3 options for first letter, 3 options for second)
For a total of 13 strings. Now as far as I know a language can pick any combination of this for example l1 = {ab,a,ac} l2 = {c}
I'm not sure how to calculate the total number of languages there could be here. Any Help?

So you have a set with 13 elements. A particular language could be any subset of this set. How many subsets does this set have?
This is called the power set of that set, and it has 213 elements.

Cardinality of character set, say d = 3.
Total words possible of length (<= k), say w = (d^(k+1) - 1)/(d-1) = 13.
Total languages possible = Power Set {Each word can be included or not} = 2^w = 8192.

Related

Why does the 'i' need to be divided by 2 in caculating positional encoding?

In this transformer tutorial:
https://www.tensorflow.org/text/tutorials/transformer
def get_angles(pos, i, d_model):
angle_rates = 1 / np.power(10000, (2 * (i//2)) / np.float32(d_model))
return pos * angle_rates
I don't understand why 'i//2' is used, since in the original formula there is no specification of the integer division.
So what's the purpose of i//2?
according to the formula
PE(pos, 2i) = sin(1/100000^(2i /D) , PE(pos, 2i+1) = cos(1/100000^(2i
/D)
As you can see, for odd row, 2i -> 2i, for even row 2i+1 -> 2i, for example, a word embedding with [0,1,2,3,4,5,6,7] should transfer to [0,0,1,1,2,2,3,3], there is where i//2 comes from.

why maximum number of corner points is m+nCn?

In Linear programming we have:
maximum number of corner points for a problem with m constrains and n variable is . n+mCn . (taking a combination of the number of equations plus variables with number of variables )
why this is the case? I have no idea why this is true.
Define:
m = number of rows = number of logical variables (slacks)
n = number of columns = number of structural variables
so the total number of variables is n+m
Further, we have:
number of basic variables = m (solved by linear algebra)
number of non-basic variables = n (temporarily fixed, usually at 0)
The total number of corner points is equal to the number of ways we can choose m basic variables out of n+m total variables.
But we have:
n+m choose m = n+m choose n
Note that in general many of these bases are infeasible.

Is there an existing algorithm in checking password strength? Or re-invent the wheel?

I've been thinking to develop an Android application which will tell the password strength of user-entered password.
In terms of checking password strength, I developed these 2 Algorithms to check it. But I'm having second thought using these Algorithms because I don't think it's efficient.
What do you guys think?
Here is my 2 Algorithms:
Average Method
Sample input = Password12#
1. Count the lowercase, uppercase, digits and special characters in the given String.
Eg.
Lowercase count = 7;
Uppercase count = 1;
Digits count = 2;
SpecialCharacter count = 1;
2. Get the character count and multiply it to the size of given String.
Eg.
(Count * Size)
(7 * 10) = 70
(1 * 10) = 10
(2 * 10) = 20
(1 * 10) = 10
3. Add the following results
Eg.
70 + 10 + 20 + 10 = 110
4. Get the results which is the password strength.
Eg.
The password is 110% strong.
Points Method
Sample input = Password12#
1. Set the points such that for every:
Lowercase = 1 point given
Uppercase = 5 points given
Digits = 10 points given
Special Character = 15 points given
2. Count the lowercase, uppercase, digits and special characters in the given String.
Eg.
Lowercase count = 7;
Uppercase count = 1;
Digits count = 2;
SpecialCharacter count = 1;
3. Get the character count and add it to the given point and multiply the size of the given String.
Eg.
(Count + Point) * size
(7 + 1) * 10 = 80;
(1 + 5) * 10 = 60;
(2 + 10) * 10 = 120;
(1 + 15) * 10 = 160;
4. Add the following results and divide it to the size of given String and divide it by 4.
Eg.
//4 because count={uppercase, lowercase, digits, special character}
80 + 60 + 120 + 160 = 420
420 / 4 = 105
5. Get the result which is the pswword strength.
Eg.
The password strength is 105%.
My questions are:
Which algorithm showed that has a better implementation?
If the 2 given algorithms is inefficient, is there an existing algorithm that I can use to check the strength of the given password. Not like this, re-inventing the wheel.
A link to open source password strength checker:
https://github.com/dropbox/zxcvbn
I didn't use it, just found it on google, check it out.
Your algorithms don't seem to get the job done well.
First one can be expressed as number of characters n^2, the kinds of character don't make a difference.
Second one is similar, it still doens't mean what kind of characters you input as the points only constitute a constant term in the equation:
(d + 10) * 10 = d * 10 + 100 (for digits). It isn't better, it just shows a larger score.
Both algorithm produce a number that is roughly a square of the length of the password, while the time to break it (or strength) depends more on the exponent of length.
Check this article from coding horror: http://blog.codinghorror.com/your-password-is-too-damn-short/
Time to break a random password (from the article):
9 characters 2 minutes
10 characters 2 hours
11 characters 6 days
12 characters 1 year
13 characters 64 years
The random.org generator is "only" uppercase, lowercase, and number
Both of your algorithms have some inherent issues when checking password strengths:
The first algorithm basically just counts the length of the password and multiply with 10. Using this algorithm the password "passwordpassword" would get a rating of 160%. Way too strong for such a simple password.
The second algorithm is a bit more complex and uses weights, based on the type of character. However using this algorithm, the password "1234" would get a rating of a 100%. I am sure this is not what you want.
A general rule of thumb is to test a password strength based on a list of rules, and then weight those rules (together with the number of rules actually enforced by the password):
Password must be atleast 8 characters long (10 pts)
Password must contain atleast a lowercase letter (5 pts)
Password must contain atleast an uppercase letter (5 pts)
Password must contain atleast a digit (5 pts)
Password must contain atleast a symbol (10 pts)
Password must contain atleast 5 unique characters (5 pts)
You could then add the rules enforced together and multiply that number with the number of rules enforced multiplied by a weight. E.g:
"1234" = (5) + 1*10 = 15
"password" = (5+5) + 2*10 = 30
"password1" = (5+5+5) + 3*10 = 45
"password1234" = (5+5+5+10) + 4*10 = 65
"Password1234" = (5+5+5+5+10) + 5*10 = 80
"Passw#rd1234" = (5+5+5+5+10+10) + 6*10 = 100
This is just a simple formulation of how the rule based approach work. Personally I would weight the number of rules used exponentionally instead, and have a wide variety of rules. Basically, the more rules satisfied, the more complex the password, the more secure it is.

How to choose a range for a loop based upon the answers of a previous loop?

I'm sorry the title is so confusingly worded, but it's hard to condense this problem down to a few words.
I'm trying to find the minimum value of a specific equation. At first I'm looping through the equation, which for our purposes here can be something like y = .245x^3-.67x^2+5x+12. I want to design a loop where the "steps" through the loop get smaller and smaller.
For example, the first time it loops through, it uses a step of 1. I will get about 30 values. What I need help on is how do I Use the three smallest values I receive from this first loop?
Here's an example of the values I might get from the first loop: (I should note this isn't supposed to be actual code at all. It's just a brief description of what's happening)
loop from x = 1 to 8 with step 1
results:
x = 1 -> y = 30
x = 2 -> y = 28
x = 3 -> y = 25
x = 4 -> y = 21
x = 5 -> y = 18
x = 6 -> y = 22
x = 7 -> y = 27
x = 8 -> y = 33
I want something that can detect the lowest three values and create a loop. From theses results, the values of x that get the smallest three results for y are x = 4, 5, and 6.
So my "guess" at this point would be x = 5. To get a better "guess" I'd like a loop that now does:
loop from x = 4 to x = 6 with step .5
I could keep this pattern going until I get an absurdly accurate guess for the minimum value of x.
Does anybody know of a way I can do this? I know the values I'm going to get are going to be able to be modeled by a parabola opening up, so this format will definitely work. I was thinking that the values could be put into a column. It wouldn't be hard to make something that returns the smallest value for y in that column, and the corresponding x-value.
If I'm being too vague, just let me know, and I can answer any questions you might have.
nice question. Here's at least a start for what I think you should do for this:
Sub findMin()
Dim lowest As Integer
Dim middle As Integer
Dim highest As Integer
lowest = 999
middle = 999
hightest = 999
Dim i As Integer
i = 1
Do While i < 9
If (retVal(i) < retVal(lowest)) Then
highest = middle
middle = lowest
lowest = i
Else
If (retVal(i) < retVal(middle)) Then
highest = middle
middle = i
Else
If (retVal(i) < retVal(highest)) Then
highest = i
End If
End If
End If
i = i + 1
Loop
End Sub
Function retVal(num As Integer) As Double
retVal = 0.245 * Math.Sqr(num) * num - 0.67 * Math.Sqr(num) + 5 * num + 12
End Function
What I've done here is set three Integers as your three Min values: lowest, middle, and highest. You loop through the values you're plugging into the formula (here, the retVal function) and comparing the return value of retVal (hence the name) to the values of retVal(lowest), retVal(middle), and retVal(highest), replacing them as necessary. I'm just beginning with VBA so what I've done likely isn't very elegant, but it does at least identify the Integers that result in the lowest values of the function. You may have to play around with the values of lowest, middle, and highest a bit to make it work. I know this isn't EXACTLY what you're looking for, but it's something along the lines of what I think you should do.
There is no trivial way to approach this unless the problem domain is narrowed.
The example polynomial given in fact has no minimum, which is readily determined by observing y'>0 (hence, y is always increasing WRT x).
Given the wide interpretation of
[an] equation, which for our purposes here can be something like y =
.245x^3-.67x^2+5x+12
many conditions need to be checked, even assuming the domain is limited to polynomials.
The polynomial order is significant, and the order determines what conditions are necessary to check for how many solutions are possible, or whether any solution is possible at all.
Without taking this complexity into account, an iterative approach could yield an incorrect solution due to underflow error, or an unfortunate choice of iteration steps or bounds.
I'm not trying to be hard here, I think your idea is neat. In practice it is more complicated than you think.

Recognizing when to use the modulus operator

I know the modulus (%) operator calculates the remainder of a division. How can I identify a situation where I would need to use the modulus operator?
I know I can use the modulus operator to see whether a number is even or odd and prime or composite, but that's about it. I don't often think in terms of remainders. I'm sure the modulus operator is useful, and I would like to learn to take advantage of it.
I just have problems identifying where the modulus operator is applicable. In various programming situations, it is difficult for me to see a problem and realize "Hey! The remainder of division would work here!".
Imagine that you have an elapsed time in seconds and you want to convert this to hours, minutes, and seconds:
h = s / 3600;
m = (s / 60) % 60;
s = s % 60;
0 % 3 = 0;
1 % 3 = 1;
2 % 3 = 2;
3 % 3 = 0;
Did you see what it did? At the last step it went back to zero. This could be used in situations like:
To check if N is divisible by M (for example, odd or even)
or
N is a multiple of M.
To put a cap of a particular value. In this case 3.
To get the last M digits of a number -> N % (10^M).
I use it for progress bars and the like that mark progress through a big loop. The progress is only reported every nth time through the loop, or when count%n == 0.
I've used it when restricting a number to a certain multiple:
temp = x - (x % 10); //Restrict x to being a multiple of 10
Wrapping values (like a clock).
Provide finite fields to symmetric key algorithms.
Bitwise operations.
And so on.
One use case I saw recently was when you need to reverse a number. So that 123456 becomes 654321 for example.
int number = 123456;
int reversed = 0;
while ( number > 0 ) {
# The modulus here retrieves the last digit in the specified number
# In the first iteration of this loop it's going to be 6, then 5, ...
# We are multiplying reversed by 10 first, to move the number one decimal place to the left.
# For example, if we are at the second iteration of this loop,
# reversed gonna be 6, so 6 * 10 + 12345 % 10 => 60 + 5
reversed = reversed * 10 + number % 10;
number = number / 10;
}
Example. You have message of X bytes, but in your protocol maximum size is Y and Y < X. Try to write small app that splits message into packets and you will run into mod :)
There are many instances where it is useful.
If you need to restrict a number to be within a certain range you can use mod. For example, to generate a random number between 0 and 99 you might say:
num = MyRandFunction() % 100;
Any time you have division and want to express the remainder other than in decimal, the mod operator is appropriate. Things that come to mind are generally when you want to do something human-readable with the remainder. Listing how many items you could put into buckets and saying "5 left over" is good.
Also, if you're ever in a situation where you may be accruing rounding errors, modulo division is good. If you're dividing by 3 quite often, for example, you don't want to be passing .33333 around as the remainder. Passing the remainder and divisor (i.e. the fraction) is appropriate.
As #jweyrich says, wrapping values. I've found mod very handy when I have a finite list and I want to iterate over it in a loop - like a fixed list of colors for some UI elements, like chart series, where I want all the series to be different, to the extent possible, but when I've run out of colors, just to start over at the beginning. This can also be used with, say, patterns, so that the second time red comes around, it's dashed; the third time, dotted, etc. - but mod is just used to get red, green, blue, red, green, blue, forever.
Calculation of prime numbers
The modulo can be useful to convert and split total minutes to "hours and minutes":
hours = minutes / 60
minutes_left = minutes % 60
In the hours bit we need to strip the decimal portion and that will depend on the language you are using.
We can then rearrange the output accordingly.
Converting linear data structure to matrix structure:
where a is index of linear data, and b is number of items per row:
row = a/b
column = a mod b
Note above is simplified logic: a must be offset -1 before dividing & the result must be normalized +1.
Example: (3 rows of 4)
1 2 3 4
5 6 7 8
9 10 11 12
(7 - 1)/4 + 1 = 2
7 is in row 2
(7 - 1) mod 4 + 1 = 3
7 is in column 3
Another common use of modulus: hashing a number by place. Suppose you wanted to store year & month in a six digit number 195810. month = 195810 mod 100 all digits 3rd from right are divisible by 100 so the remainder is the 2 rightmost digits in this case the month is 10. To extract the year 195810 / 100 yields 1958.
Modulus is also very useful if for some crazy reason you need to do integer division and get a decimal out, and you can't convert the integer into a number that supports decimal division, or if you need to return a fraction instead of a decimal.
I'll be using % as the modulus operator
For example
2/4 = 0
where doing this
2/4 = 0 and 2 % 4 = 2
So you can be really crazy and let's say that you want to allow the user to input a numerator and a divisor, and then show them the result as a whole number, and then a fractional number.
whole Number = numerator/divisor
fractionNumerator = numerator % divisor
fractionDenominator = divisor
Another case where modulus division is useful is if you are increasing or decreasing a number and you want to contain the number to a certain range of number, but when you get to the top or bottom you don't want to just stop. You want to loop up to the bottom or top of the list respectively.
Imagine a function where you are looping through an array.
Function increase Or Decrease(variable As Integer) As Void
n = (n + variable) % (listString.maxIndex + 1)
Print listString[n]
End Function
The reason that it is n = (n + variable) % (listString.maxIndex + 1) is to allow for the max index to be accounted.
Those are just a few of the things that I have had to use modulus for in my programming of not just desktop applications, but in robotics and simulation environments.
Computing the greatest common divisor
Determining if a number is a palindrome
Determining if a number consists of only ...
Determining how many ... a number consists of...
My favorite use is for iteration.
Say you have a counter you are incrementing and want to then grab from a known list a corresponding items, but you only have n items to choose from and you want to repeat a cycle.
var indexFromB = (counter-1)%n+1;
Results (counter=indexFromB) given n=3:
`1=1`
`2=2`
`3=3`
`4=1`
`5=2`
`6=3`
...
Best use of modulus operator I have seen so for is to check if the Array we have is a rotated version of original array.
A = [1,2,3,4,5,6]
B = [5,6,1,2,3,4]
Now how to check if B is rotated version of A ?
Step 1: If A's length is not same as B's length then for sure its not a rotated version.
Step 2: Check the index of first element of A in B. Here first element of A is 1. And its index in B is 2(assuming your programming language has zero based index).
lets store that index in variable "Key"
Step 3: Now how to check that if B is rotated version of A how ??
This is where modulus function rocks :
for (int i = 0; i< A.length; i++)
{
// here modulus function would check the proper order. Key here is 2 which we recieved from Step 2
int j = [Key+i]%A.length;
if (A[i] != B[j])
{
return false;
}
}
return true;
It's an easy way to tell if a number is even or odd. Just do # mod 2, if it is 0 it is even, 1 it is odd.
Often, in a loop, you want to do something every k'th iteration, where k is 0 < k < n, assuming 0 is the start index and n is the length of the loop.
So, you'd do something like:
int k = 5;
int n = 50;
for(int i = 0;i < n;++i)
{
if(i % k == 0) // true at 0, 5, 10, 15..
{
// do something
}
}
Or, you want to keep something whitin a certain bound. Remember, when you take an arbitrary number mod something, it must produce a value between 0 and that number - 1.