Maximum number of elems on stack for infix -> postfix translation - stack

I got this as an interview question.
What is the maximum number of elements that can be on stack at a
specific moment while doing a translation from infix form to reversed
postfixed Polish form?
I know that the principle is that on the stack there cannot be elements with higher priority (usually * and /) under the ones with smaller priority (+ and -). I tried making an algorithm keeping track of a global and local maximum number, but I didn`t found out a certain rule.
For example if i have infix: 2 - 3 * 4 * 5 / 1 + 10
Stack 1: - * * / => maxLocal = 4 maxGlobal = 4
Stack 2: (After eliminating /, * and * because + has lower priority) - +
=> maxLocal = 2 maxGlobal = 4
Can you please help me?

I think there is no limit. For example, take the following infix expression: (1 + (1 + (1 + (1 + (1 + (1 + (1 + … It is very deep, every time you push yet another element to the stack. Of course there is usually some limit to the number of parentheses you parser accepts, but such limit is purely practical (to prevent stack overflow), not theoretical.

Related

How do you do alegbra correctly in Lua?

So i have recently been attempting to do alegbra in lua and this was the closet way i could come up with of doing it is this how you are even supposed to do it correctly? An other problem that i find with doing alegbra in lua is that in alegbra there is both a constant and a variable beside eachother well the problem with that is that lua does not like both a Number and a letter beside eachother so it errors is there any way how i could go about doing alegbra inside lua without getting errors?
local a = 5
-- ALEGBRA?
print(((a * 2) / 10) + 15 - 20)
-- 5 * 2 = 10
-- 10/10 = 1
-- 1 + 15 = 16
-- 16 - 20 = -4
-- The Problem lies right here when there is a Variable and a constant together lua does not like that :/
local x = 10
print(5x + 5)
print(5x + 5) will trigger a syntax error; Lua does not allow implicit multiplication. The fix is trivial: Explicitly use the multiplication operator as in your first example: print(5*x + 5) works just fine.

Attention type 'block_sparse' is not possible if sequence_length: 458 <= num global tokens:

I am using the pre-trained google/bigbird-pegasus-large-arxiv model.
But I receive the following update during the forward pass.
Attention type 'block_sparse' is not possible if sequence_length: 458 <= num global tokens: 2 * config.block_size + min. num sliding tokens: 3 * config.block_size + config.num_random_blocks * config.block_size + additional buffer: config.num_random_blocks * config.block_size = 704 with config.block_size = 64, config.num_random_blocks = 3.Changing attention type to 'original_full'...
I understand the update and I am aware of benefit of time and memory it saves while using block_sparse than original_full.
So, how should I go about selecting the suitable block_size and num_random_blocks when I know that there is a lot of variation in the sequence length of my inputs?
I end up setting padding='max_length' in tokenizer.encode(). It will guarantee that the model will always use "block_sparse" attention type.

calculating recurrence relation T(n) = n+ T(n/2) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
T(n) = n + T(n/2)
= n + n/2 + T(n/4)
= n + n/2 + n/4 + T(n/8)
= n + n/2 + n/4 + ... + n/(2^(k-1)) + T(n/2^k)
->>>
and I don't know how to go on to get big Oh formula.
please help me
I'm assuming there's some kind of initial condition like T(1) = 0 that you are not telling us.
If so, the answer is O(log n).
Think about how you would work out T(2), T(4), T(8), T(16) etc. Each one requires just one extra step.
T(1) = T(2^0) calls the method recursively 0 times.
T(2) = T(2^1) calls the method recursively 1 time
T(4) = T(2^2) calls the method recursively 2 times
T(8) = T(2^3) calls the method recursively 3 times
In other words, the number of steps is the power. This means that you have to take logarithms to get the answer.

Modular arithmetic using fractions

I'm stuck on this cryptography problem using multiplication of a whole number and a fraction mod 10.
Here is the equation:
7 * (4/11) mod 10 =?
I know I am supposed to convert this to an integer since the mod operator does not work with fractions, but I cannot figure this one out. Obviously,
7 * (4/11) = 28/11,
but I cannot get the mod 10 of a fraction. The instructor wants the exact answer, not a decimal. Any help would be greatly appreciated!
Have a look here: "Is it possible to do modulo of a fraction" on math.stackexchange.com.
One natural way to define the modular function is
a (mod b) = a − b ⌊a / b⌋
where ⌊⋅⌋ denotes the floor function. This is the approach used in the influential book Concrete Mathematics by Graham, Knuth, Patashnik.
This will give you 1/2(mod3)=1/2.
To work through your problem, you have a = 7 * (4/11) = 28/11, and b = 10.
a / b = (28/11)/10 = 0.25454545...
⌊a/b⌋ = 0
b ⌊a/b⌋ = 0 * 0 = 0
a - b ⌊a/b⌋ = 28/11 - 0 = 28/11
This means your answer is 28/11.
Wolfram Alpha agrees with me and gives 28/11 as the exact result. Google also agrees, but gives it as a decimal, 2.54545454.....
A fraction is an exact answer and not a decimal.
8
8 is the correct answer indeed.
7*4/11 mod 10 means we're looking at 7*4*x mod 10 where x is the modular inverse of 11 modulo 10, which means that 11*x mod 10 = 1.
This is true for x=1 (11*1 mod 10 = 1)
So 7*4*x mod 10 becomes 7*4*1 mod 10 which is 28 mod 10 = 8
I can speculate that the notation is wrong, and that the whole expression is supposed to be evaluated in mod 10 at each intermediate stage. Since ( 11 mod 1 ) is 1, then answer is (7 * 4) mod 10 = 8.
Imagine a calculator with support only for the ones digit.
I'm not saying this is the right answer, I agree 28/11 is the right answer as given, but I am trying to get into the head of the professor. This is common in cryptography, where every calculation is performed mod 2 ^ 256 or so.
This is how the original question probably should have been written, as this has a different meaning. When the (mod 10) is written at the end, it means that each term is evaluated with an implied mod 10 operation.
The problem is a bit weird, as the modulo value of 10 is not general purpose, because it is not prime. For example, the following can not be evaluated because 1/2 mod 10 is not defined, because 2 and 10 are not coprime.
So, here is the correct answer from the instructor. I have no idea how he came up with this:
7 4/11 mod 10 = ((7 4) mod 10)(11−1 mod 10) mod 10
= (28 mod 10)(1 mod 10) mod 10
= (8)(1) mod 10
= 8 mod 10
Using Python:
from fractions import Fraction
from math import fmod
print (fmod(Fraction(28, 11), 10))
The result will be 2.545454545454. So I guess 8 is wrong.

Interpreter with a one-register VM - possible to evaluate all math. expressions?

I'm writing an interpreter. I've done that before but never tried one which can work with expressions like 3 + 4 * 2 / ( 1 − 5 ) ^ 2 ^ 3.
I'm not having a problem with the parsing process, actually it is about my VM which then executes the code.
My goal was a fast interpreter and so I decided not to use a stack-based VM where you would need more than one instruction for a multiplication, for example (push, push, mul)
The "assembly" code for the VM generated by the parser looks as following:
3 + 4 * 2 / ( 1 − 5 ) ^ 2 ^ 3
becomes
sub 1 5
pow result 2
pow result 3
div 2 result
mul 4 result
add 3 result
(The result is correct)
As you can see: Every instruction takes no, one or two arguments. There is the result register which holds the result of the last instruction. And that's it.
Can a VM with a language of this structure and only one register calculate every mathematical expression for example Python or PHP can?
If it is not possible without a stack I'll start over right now!
What do you do about (1 + 2) * (3 + 4), or any other that would require you to calculate more than one intermediate result?

Resources