Turing Machine to Accept String from a 3 character alphabet - discrete-mathematics

I need to create a turing machine that accepts the language a^1 b^j c^k where i >= j >= k, but I am not even really sure how to start. Turing machines in this context are a hard concept for me to grasp for some reason.

Turing machines can read and write to the tape and move back and forth over it. If you had a line of three colors of marbles how would you see if they arranged like the strings in your language? You could verify they're in order and then separately count each color and make sure the relationships hold. "greater than or equal to" is a binary relation so you'd probably check both pairs separately. This is really easy to envision using three extra tapes:
scan from left to right to make sure a comes first, then b, then c, then go back to the beginning
scan right counting the a's, writing one a to extra tape #1 for each a you read on the input tape
continue scanning using extra tape #2 to count b
continue scanning using extra tape #3 to count c
reset all tape heads
scan right to make sure extra tape #1 has more stuff in it than extra tape #2
reset all tape heads
scan right to make sure extra tape #2 has more stuff in it than extra tape #3
If we don't want to use extra tapes, how can we proceed? Well, we can go ahead and make sure the symbols are in the right order first... makes the rest neater. Then, we can "cross off" pairs of a and b until all b are exhausted (if we exhaust all a first, then halt_reject); then, un-cross the b and cross off pairs of b and c until you run out of c (if you run out of b first, halt_reject). Something like...
q t q' t' d
q0 # q1 # right //
q1 a q1 a right //
q1 b q2 b right //
q1 # q4 # left //
q2 b q2 b right // verify subset of
q2 c q3 c right // a*b*c*
q2 # q4 # left //
q3 c q3 c right //
q3 # q4 # left //
q4 a q4 a left //
q4 b q4 b left // reset input
q4 c q4 c left // tape to start
q4 # q5 # right //
q5 a q5 a right //
q5 A q5 A right // change susbtring a^j b^j
q5 b q6 B left // into substring A^j b^j
q5 B q5 B right // if run out of a, crash
q5 c q7 C left // if run out of b and no c, accept
q5 # h_a # left // if run out of b and c, continue
q6 a q5 A right //
q6 A q6 A left //
q6 B q6 B left //
q7 B q8 D right //
q7 C q7 C left // change substring B^k c^k
q7 D q7 D left // to substring D^k c^k
q8 D q8 D right // if run out of B, crash
q8 C q8 C right // if run out of c, accept
q8 c q7 C left //
q8 # h_a # left //
Example 1: aaabbc
(q0, [#]aaabbc#) -> (q1, #[a]aabbc#) -> (q1, #a[a]abbc#) //
-> (q1, #aa[a]bbc#) -> (q1, #aaa[b]bc#) -> (q2, #aaab[b]c#) // a*b*c*
-> (q2, #aaabb[c]#) -> (q3, #aaabbc[#]) -> (q4, #aaabb[c]#) //
-> (q4, #aaab[b]c#) -> (q4, #aaa[b]bc#) -> (q4, #aa[a]bbc#) //
-> (q4, #a[a]abbc#) -> (q4, #[a]aabbc#) -> (q4, [#]aaabbc#) // reset
-> (q5, #[a]aabbc#) //
-> (q5, #a[a]abbc#) -> (q5, #aa[a]bbc#) -> (q5, #aaa[b]bc#) //
-> (q6, #aa[a]Bbc#) -> (q5, #aaA[B]bc#) -> (q5, #aaAB[b]c#) // a^j b^j
-> (q6, #aaA[B]Bc#) -> (q6, #aa[A]BBc#) -> (q6, #a[a]ABBc#) // A^j B^j
-> (q5, #aA[A]BBc#) -> (q5, #aAA[B]Bc#) -> (q5, #aAAB[B]c#) //
-> (q5, #aAABB[c]#) -> (q7, #aAAB[B]C#) //
-> (q8, #aAABD[C]#) -> (q8, #aAABDC[#]) -> (ha, #aAABD[C]#) // B^k c^k
// D^k C^k

Related

Plotting ATR as hline

i want two horizontal lines on chart with marks day highest and lowest according to ATR.
Let’s say ATR (14) is 1$ and market opens at 10$.
So, one line at day highest 11$ (market open + ATR -> 10$ + 1$)
Other line at day lowest 9$ (market open – ATR -> 10$ - 1$)
If I use plot(l_atr') it works, but if I use hline I get an error: line 13: Cannot call 'hline' with argument 'price'='l_atr'. An argument of 'series float' type was used but a 'input float' is expected.
I’m not sure what it means with “input float”.
How can I fix this?
//#version=5
indicator("Arego ATR", overlay=true)
o = request.security("", "D", open, lookahead=barmerge.lookahead_on)
atr = request.security("", "D", ta.atr(14))
l = request.security("", "D", low, lookahead=barmerge.lookahead_on)
h = request.security("", "D", high, lookahead=barmerge.lookahead_on)
l_atr = o + (h - o) - atr
h_atr = o + (l - o) + atr
hline(l_atr)
//plot(h_atr)
//plot(l_atr)

Qlik Sense Set Analysis (A or B) and (C or D)

Trying to write a function to pull counts for Items sold in (Region A or Region B) AND (Region C or Region D). An intersection between these 2 sets
count({<Item={'Region A', 'Region B'} * {'Region C', 'Region D'}>} Item)
This is not working.
Example:
[O,R,S]
[O1,A,10]
[O2,B,20]
[O2,D,20]
[O1,C,10]
[O3,A,25]
[O4,B,35]
Expected result:
Only count: O1 since it was sold in region A and C and O2 since it was sold in region B and D. Do not count O3, O4
Final count would be 2
You could try:
=count({<Order={'Region A', 'Region B'}>} Orders)
+
count({<Order={'Region C', 'Region D'}>} Orders)
That way you are just adding results of both.
Asked the same question on the Qlik community site and got a correct answer:
sum(aggr(count({< Region = {'A', 'B'}>} Region) * count({< Region = {'C', 'D'}>} Region), Item))

q/KDB - nprev function to get all the previous n elements

I am struggling to write a nprev function in KDB; xprev function returns the nth element but I need all the prev n elements relative to the current element.
q)t:([] i:1+til 26; s:.Q.a)
q)update xp:xprev[3;]s,p:prev s from t
Any help is greatly appreciated.
You can achieve the desired result by applying prev repeatedly and flipping the result
q)n:3
q)select flip 1_prev\[n;s] from t
s
-----
" "
"a "
"ba "
"cba"
"dcb"
"edc"
..
If n is much smaller than the rows count, this will be faster than some of the more straightforward solutions.
The xprev function basically looks like this :
xprev1:{y til[count y]-x} //readable xprev
We can tweak it to get all n elements
nprev:{y til[count y]-\:1+til x}
using nprev in the query
q)update np: nprev[3;s] , xp1:xprev1[3;s] , xp: xprev[3;s], p:prev[s] from t
i s np xp1 xp p
-------------------
1 a " "
2 b "a " a
3 c "ba " b
4 d "cba" a a c
5 e "dcb" b b d
6 f "edc" c c e
k equivalent of nprev
k)nprev:{$[0h>#y;'`rank;y(!#y)-\:1+!x]}
and similarly nnext would look like
k)nnext:{$[0h>#y;'`rank;y(!#y)+\:1+!x]}

q - apply function on table rowwise

Given a table and a function
t:([] c1:1 2 3; c2:`a`b`c; c3:13:00 13:01 13:02)
f:{[int;sym;date]
symf:{$[x=`a;1;x=`b;2;3]};
datef:{$[x=13:00;1;x=13:01;2;3]};
r:int + symf[sym] + datef[date];
r
};
I noticed that when applying the function f onto columns of t, then the entire columns are passed into f and if they can be operated on atomically then the output will be of the same length as the inputs and a new column is produced. However in our example this wont work:
update newcol:f[c1;c2;c3] from t / 'type error
because the inner functions symf and datef cannot be applied to the entire column c2, c3, respectively.
If I dont want to change the function f at all, how can I apply it row by row and collect the values into a new column in t.
What's the most q style way to do this?
EDIT
If not changing f is really inconvenient one could workaround like so
f:{[arglist]
int:arglist 0;
sym:arglist 1;
date:arglist 2;
symf:{$[x=`a;1;x=`b;2;3]};
datef:{$[x=13:00;1;x=13:01;2;3]};
r:int + symf[sym] + datef[date];
r
};
f each (t`c1),'(t`c2),'(t`c3)
Still I would be interested how to get the same result when working with the original version of f
Thanks!
You can use each-both for this e.g.
q)update newcol:f'[c1;c2;c3] from t
c1 c2 c3 newcol
------------------
1 a 13:00 3
2 b 13:01 6
3 c 13:02 9
However you will likely get better performance by modifying f to be "vectorised" e.g.
q)f2
{[int;sym;date]
symf:3^(`a`b!1 2)sym;
datef:3^(13:00 13:01!1 2)date;
r:int + symf + datef;
r
}
q)update newcol:f2[c1;c2;c3] from t
c1 c2 c3 newcol
------------------
1 a 13:00 3
2 b 13:01 6
3 c 13:02 9
q)\ts:1000 update newcol:f2[c1;c2;c3] from t
4 1664
q)\ts:1000 update newcol:f'[c1;c2;c3] from t
8 1680
In general in KDB, if you can avoid using any form of each and stick to vector operations, you'll get much more efficiency

Easy68K IF-ELSE branching

writing my first assembly language program for class using Easy68K.
I'm using an if-else branching to replicate the code:
IF (P > 12)
P = P * 8 + 3
ELSE
P = P - Q
PRINT P
But I think I have my branches wrong because without the first halt in my code the program runs through the IF branch anyway even after the CMP finds a case that P < 12. Am I missing something here or would this be a generally accepted way of doing this?
Here is my assembly code:
START: ORG $1000 ; Program starts at loc $1000
MOVE P, D1 ; [D1] <- P
MOVE Q, D2 ; [D2] <- Q
* Program code here
CMP #12, D1 ; is P > 12?
BGT IF ;
SUB D2, D1 ; P = P - Q
MOVE #3, D0 ; assign read command
TRAP #15 ;
SIMHALT ; halt simulator
IF ASL #3, D1 ; P = P * 8
ADD #3, D1 ; P = P + 3
ENDIF
MOVE #3, D0 ; assign read command
TRAP #15 ;
SIMHALT ; halt simulator
* Data and Variables
ORG $2000 ; Data starts at loc $2000
P DC.W 5 ;
Q DC.W 7 ;
END START ; last line of source
To do if..else, you need two jumps; one at the start, and one at the end of the first block.
While it doesn't affect correctness, it is also conventional to retain source order, which means negating the condition.
MOVE P, D1 ; [D1] <- P
MOVE Q, D2 ; [D2] <- Q
* Program code here
CMP #12, D1 ; is P > 12?
BLE ELSE ; P is <= 12
IF
ASL #3, D1 ; P = P * 8
ADD #3, D1 ; P = P + 3
BRA ENDIF
ELSE
SUB D2, D1 ; P = P - Q
ENDIF
MOVE #3, D0 ; assign read command
TRAP #15 ;
SIMHALT ; halt simulator
EASy68K supports structured assembly.
OPT SEX
IF.L P <GT> #12 THEN
ELSE
ENDI
Add the option SEX to expand the structured code during assembly if you wish to view the compare and branch instructions used to implement the structured code.