Turing Machine - Generate number sequences - numbers

http://morphett.info/turing/turing.html
How would I create a looping number sequence such as:
01011011101111011111...
So basically adding a zero, then adding 1, then a zero, then 1 on top of the previous number of ones.

Write 01 on the tape. Move one space to the right. If you're looking at a zero, scan back to the left until you see a zero. Move one space to the right. If you're looking at a one, replace it with a two and move to the right until you see a zero; then keep moving right until you another zero. Replace this zero with one. Then, move back until you see a two. Replace the two with a one. Move one to the right; if you're looking at a one, repeat the process of replacing with two and back again. Eventually, you'll exhaust the previous supply of 1s, so you're looking at a zero when you move one to the right. In that case, move right to the next zero, and replace it with one. Loop on this entire process (minus the "write 01 part) to get longer strings of ones.
The intuition behind this is straightforward. If you move to the right and see a zero, move two zeroes to the left, copy all the one's between the last and second-to-last zero after the zero you found, and then add one more one. The two is used as a way to keep track of your position in the string of ones you're copying. The basic idea is sound, but you should try to write out states and transitions for this to make it rigorous.
Example:
>
^
>0
^
>01
^
>010
^
>010
^
>010
^
>020
^
>0200
^
>0201
^
>0201
^
>0101
^
>0101
^
>01010
^
>010110
^

Related

How to demonstrate the gamblers fallacy

I'm beginning to learn Matlab and having trouble with a routine exercise which reads as follows
set ff to a null set, set x to equal 10,000 random numbers between 0 and 1 (make sure the numbers are rounded to the nearest integer)
{Think of 1’s as Heads and 0’s at Tails}
start a for loop where n goes to 9,999 coin flips (starting with the 4th flip) check if the nth flip == Heads and if the previous three flips were also Heads if so, save the result of the very next flip in your expanding vector ff end the if statement
end the for loop develop a way to check if half (or more than half) of those “fifth-flips” are tails.
so far I have managed to get the beginning of the code seen below
clear
ff=[]
x=round(rand(1,10000)
for n=4:9999
if n==1
not sure where to go from here, hoping for code with a thorough description of the reasoning behind command choices

slicing assignment with negative index

I am having some problems regarding slicing assignment:
As i understand that general syntax of slicing is l[start:stop:step]
when we use positive step then we transverse forward and when we use negative step we transverse backward:
l=[1,2,3,4]
l[3:1:1]=[5]
when i use the above assignment then it inserts the element 5 at the index 3 like insert operation
but when i use
l[-3:-1:-1]=[5]
then it shows me value error....
i m totally confused..
please explain it.
Assuming you are asking about slices in Python,
the 'step' part will make the slice an extended slice.
Assigning to extended slices is only possible if the list on the right
hand side is of the same size as the extended slice.
see
https://docs.python.org/2.3/whatsnew/section-slices.html
So the confusing thing actually is that your l[3:1:1] = [5] does not raise
a ValueError, because the left and right size differ (0 and 1; note
that both your l[3:1:1] and l[-3:-1:-1] evaluate to empty lists).
I think that can be explained by the fact that a step of 1 is no different
from the original slice syntax [start:end], and may therefore be handled
as a normal slice.
If your goal is inserting, just don't use the step.

How to merge two lists(or arrays) while keeping the same relative order?

For example,
A=[a,b,c,d]
B=[1,2,3,4]
my question is: how to generate all possible ways to merge A and B, such that in the new list we can have a appears before b, b appears before c,etc., and 1 appears before 2, 2 appears before 3,etc.?
I can think of one implementation:
We choose 4 slots from 8,then for each possible selection, there are 2 possible ways--A first or B first.
I wonder is there a better way to do this?
EDIT:
I've just learned a more intuitive way--use recursion.
For each spot, there are two possible cases, either taken from A or taken from B; keep recursing until A or B is empty, and concatenate the remaining.
If the relative order is different than what constitutes a sorted list (I assume it is, because otherwise it would not be a problem), then you need to formalize the initial order. Multiple ways to do that. the easiest being remembering the index of each element in each list. Example: valid position for a is 1 in the first array [...]
Then you could just go ahead and join the lists, then generate all the permutations of elements. Any valid permutation is one that keeps the order relationship of the new indexes with the order you have stored
Example of one valid permutation array
a12b3cd4
You can know and check that this is valid permutation because the index of element 'a' is smaller than the index of b, and so on. and you know the indexes must be smaller because this is what you have formulated at the first step
Similarly an invalid permutation array is
ba314cd2
same way of checking

Quick sort with middle element as pivot

My understanding of quick sort is
Choose a pivot element (in this case I am choosing middle element as
pivot)
Initialize left and right pointers at extremes.
Find the first element to the left of the pivot which is greater than pivot.
Similarly find the first element to the right of the pivot which is smaller than pivot
Swap elements found in 3 and 4.
Repeat 3,4,5 unless left >= right.
Repeat the whole thing for left and right subarray as pivot is now placed at its place.
I am sure I am missing something here and being very stupid. But above does not seems to be working fot this array:
8,7,1,2,6,9,10,2,11 pivot: 6 left pointer at 8, right pointer at 11
2,7,1,2,6,9,10,8,11 swapped 2,8 left pointer at 7, right pointer at 10
Now what ? There is no element smaller than 6 on it's right side.
How 7 is going to go to the right of 6 ?
There is no upfront division between the left and the right side. In particular, 6 is not the division. Instead, the division is the result of moving the left and right pointer closer to each other until they meet. The result might be that one side is considerably smaller than the other.
Your description of the algorithm is fine. Nowhere does it say you have to stop at the middle element. Just continue to execute it as given.
BTW.: The pivot element might be moved during the sorting. Just continue to compare against 6, even if it has been moved.
Update:
There are indeed a few minor problems in your description of the algorithm. One is that either step 3 or step 4 need to include elements that are equal to the pivot. Let's rewrite it like this:
My understanding of quick sort is
Choose a pivot value (in this case, choose the value of the middle element)
Initialize left and right pointers at extremes.
Starting at the left pointer and moving to the right, find the first element which is greater than or equal to the pivot value.
Similarly, starting at the right pointer and moving to the left, find the first element, which is
smaller than pivot value
Swap elements found in 3 and 4.
Repeat 3,4,5 until left pointer is greater or equal to right pointer.
Repeat the whole thing for the two subarrays to the left and the right of the left pointer.
pivot value: 6, left pointer at 8, right pointer at 11
8,7,1,2,6,9,10,2,11 left pointer stays at 8, right pointer moves to 2
2,7,1,2,6,9,10,8,11 swapped 2 and 8, left pointer moves to 7, right pointer moves to 2
2,2,1,7,6,9,10,8,11 swapped 2 and 7, left pointer moves to 7, right pointer moves to 1
pointers have now met / crossed, subdivide between 1 and 7 and continue with two subarrays
Quick Sort Given an array of n elements (e.g., integers):
-If array only contains one element, return
-Else
pick one element to use as pivot.
Partition elements into two sub-arrays:
Elements less than or equal to pivot
Elements greater than pivot
Quicksort two sub-arrays
Return results
Let i and j are the left and right pivots, then code for one array will look like this:
1) While data[i] <= data[pivot]
++i
2) While data[j] > data[pivot]
--j
3) If i < j
swap data[i] and data[j]
4) While j > i, go to 1.
5) Swap data[j] and data[pivot_index]
Position of index j is where array is to-be partitioned in two half and then same steps are applied to them recursively.
At last you gets an sorted array.
Your confusion is because you think the partition should be the landmark separating the two. This is not true (for middle element pivot)!
Lomuto's partition (pivot = most right partition).
Left: (lo ... p-1) (note the pivot is not included)
Right: (p+1 ... high)
middle element as the pivot. The segment is partitioned:
Left: (lo ... p)
Right: (p+1 ... high)
[https://en.wikipedia.org/wiki/Quicksort]

TI-BASIC tab character / output formatting

I've written a program on my TI-nspire CAS calculator that outputs something similar to the following...
intercepts
x = 2 and x = 4
y = 0
derivative
2x - 4
turning points
(2, -4)
This is obviously a fair bit of information to output over multiple lines in a calculator.
I was wondering how I might go about outputting this a little better, say, placing the x and y intercepts on the same line seperated by a tab, or having the turning points printed on the same line (if more than one were printed. They're currently kept in a list and iterated through, Disp 'ing each).
Does TI-Basic have a TAB character, or is there a method that allows things to be printed on the same line (through multiple statements. Like Disp but without a trailing newline suffix).
There is no TAB character in TI-BASIC.
I would suggest using spaces and determining the length of the first line if you want the spacing to be consistent.