I want to make a lexical analyzer that detects numbers divisible by 4.
Sample code -
%%
16(divisible by 4) {printf("divisible by 4 %s\n",yytext);}
%%
main()
{
yylex();
}
Divisibility by 4
The single-digit numbers which are divisible by 4 are 0, 4, and 8.
The two-digit numbers which are divisible by 4 can be divided into two groups:
12, 16, 32, 36, 52, 56, 72, 76, 92, 96
20, 24, 28, 40, 44, 48, 60, 64, 68, 80, 84, 88
Any number which is three or more digits and ends in any of these two-digit numbers is divisible by four.
Therefore, the regular expression should search for numbers of the form \d*[02468][048], or the form \d*[13579][26], or the single digit numbers 0, 4, and 8.
The Regex
This regular expression matches all numbers, positive or negative, which are divisible by 4:
-?(?:\d*[02468][048]|\d*[13579][26]|[048])
Note that this could match part of a number, such as 24 in 1245. If you want to make sure that you only match an entire number, you could add negative look-around expressions:
(?<!\d)-?(?:\d*[02468][048]|\d*[13579][26]|[048])(?!\d)
or you could use word boundaries:
\b-?(?:\d*[02468][048]|\d*[13579][26]|[048])\b
%%
[0-9]+ {int number = atoi(yytext); if((number % 4) == 0) printf("Div_4 %d\n", number);}
%%
main()
{
yylex();
}
As lex/flex support C, so you can save the string as integer and then check it in C.
Related
Input: list = [23, 23, 25, 34, 34, 32, 34]
Here 34 occurs 3 times, whereas 23 occurs 2 times.
I need to print the second lowest value. In this case output should be 23
I'm not able to figure out how I can pass the list as an argument and get the required output.
Suppose I have a list of 17 objects, out of these 17 objects some have a certain property say P1. I separate them and say they are n in number where n < 17. Out of these n objects some have another property say P2. I separate them and say they are m in number where m < n. Out of these m objects some have another property say P3. I separate them and say they are k in number where k < m. I want to print these k objects only.
I was thinking of a long way that is I separate n, m and k objects all from 17 objects according to their respective property and then look for common index, the index that appear in all of three calculations.
Either I need to derive this common index or I do what I have written in the first paragraph that is to filter through and through according to the three properties.
Example:
list_1 = [17, 23, 15, 37, 43, 52, 57, 93, 55, 85, 11, 13, 7, 22, 24]
list_odd = [17, 23, 15, 37, 43, 57, 93, 55, 85, 11, 13, 7] #P1 is a number is odd
list_odd_div3 = [15, 57, 93] #P2 is a number divisible by 3
list_odd_div5 = [15, 55, 85] #P3 is a number divisible by 5
required_list = [15] #A number having P1, P2 and P3
How do we write these statements and their respective values below using the IF() conditions?
If 0 to equal or less than 8, $0.00
If 9 to equal or less than 18, $10.00
If 19 to equal or less than - 37, $20.00
If 38 to equal or less than - 51, $30.00
If equal to or more than 53, $60.00
I can't wrap my head around it after a couple of days of trials. Hope someone can clear this up for me.
#player0's answer is the answer I personally would use.
If you must use IF-conditions, then the following might be useful:
=IF(ISBETWEEN(A1;0;8);0;IF(ISBETWEEN(A1;9;18);10;IF(ISBETWEEN(A1;19;37);20;IF(ISBETWEEN(A1;38;52);30;IF(A1>=53;60;0)))))
use:
=VLOOKUP(A1, {0, 0; 9, 10; 19, 20; 38, 30; 53, 60}, 2, 1)
see: https://webapps.stackexchange.com/q/123729/186471
I've written a similar question which was closed I would like to ask not the code but an efficiency tip. I haven't coded but if I can't find any good hint in here I'll go and code straightforward. My question:
Suppose you have a function listNums that take a as lower bound and b as upper bound.
For example a=120 and b=400
I want to print numbers between these numbers with one rule. 120's permutations are 102,210,201 etc. Since I've got 120 I would like to skip printing 201 or 210.
Reason: The upper limit can go up to 1020 and reducing the permutations would help the running time.
Again just asking for efficiency tips.
I am not sure how you are handling 0s (eg: after outputting 1 do you skip 10, 100 etc since technically 1=01=001..).
The trick is to select a number such that all its digits are in increasing order (from left to right).
You can do it recursively. AT every recursion add a digit and make sure it is equal to or higher than the one you recently added.
EDIT: If the generated number is less than the lower limit then permute it in such a way that it is greater than or equal to the lower limit. If A1A2A3..Ak is your number and it is lower than limit), then incrementally check if any of A2A1A3...Ak, A3A1A2...Ak, ... , AkA1A2...Ak-1 are within limit. If need arises, repeat this step to with keeping Ak as first digit and finding a combination of A1A2..Ak-1.
Eg: Assume we are selecting 3 digits and lower limit is 99. If the combination is 012, then the lowest permutation that is higher than 99 is 102.
When the lower bound is 0, an answer is given by the set of numbers with non-decreasing digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 27, 28, 29, 33, 34, 35, 36, 37, 38, 39, 44, 45, 46, 47, 48, 49, 55, 56, 57, 58, 59, 66, 67, 68, 69, 77, 78, 79, 88, 89, 99, 111, 112...) that fall in the requested range.
This sequence is easily formed by incrementing an integer, and when there is a carry, replicate the digit instead of carrying. Exemple: 73 is followed by 73+1 = 74 (no carry); 79 is followed by 79+1 = 80 (carry), so 88 instead; 22356999 is followed by 22356999+1 = 22357000, hence 22357777.
# Python code
A= 0 # CAUTION: this version only works for A == 0 !
B= 1000
N= A
while N < B:
# Detect zeroes at the end
S= str(N)
P= S.find('0')
if P > 0:
# Replicate the last nonzero digit
S= S[:P] + ((len(S) - P) * S[P-1])
N= eval(S)
# Next candidate
print N
N+= 1
Dealing with a nonzero lower bound is a lot more tricky.
A character (1 byte) can represent 255 characters but how do i actually find it?
(answering the comment)
There are 256 different combinations of 8 0s and 1s.
This is true because 256 = 28.
Each digit that you add doubles the number of combinations.
In a fixed width binary number, there are two choices for the first bit, two choices for the second bit, two choices for the third, and so on. The total number of combinations for an 8-bit byte is:
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 28 = 256
do you mean
for (char c = " "; c <= "~"; c++) std::cout << c << std::endl;
?
This should show you printable characters in ASCII proper. To see all characters in your font, try c = 0 and c < 255 (be careful with 255 and infinite loop) - but this won't work with your terminal, most probably.
8 bits can represent permutations of ones and zeros from binary 00000000 to 11111111. Just like 3 decimal digits can represent permutations of decimal numbers (0-9) from decimal 000 to 999.
You just start counting: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and then after you reach the digit maximum, you carry over a 1 and start from 0: ..., 8, 9, 10. Right? And then continue this until you fill up all your digits with nines: ..., 997, 998, 999.
It's the same thing in binary: 0, 1 then carry over 1 and start from 0: 0, 1, 10. Continue: 10, 11, 100, 101, 110, 111, 1000, 1001 etc.
Simply counting from 0 to the maximum value than can be represented by your digits gives you all the permutations.