TI Basic Index Error when looping through List - sorting

I'm trying to implement insertion sort in TI-BASIC on the TI-84 and have nearly been successful. My solution sorts all but the first element of the list. In order to work properly, I believe this line:
While C>1 and L1(C)>V
needs to be
While C>0 and L1(C)>V
However this creates an error because apparently TI-BASIC is checking L1(0) even though we are exiting the loop before getting there.
Could someone please explain how to avoid this problem and make the code work?
This is my code:
0->dim(L1
randIntNoRep(1,10,10)->L1
For(I,2,10)
L1(I)->V
I-1->C
While C>1 and L1(C)>V
L1(C)->L1(C+1)
C-1->C
End
V->L1(C+1)
End
Disp L1

After a lot of trial and error, i found the solution. The trick is if C=1 you can't have it in a while loop you have to have it in an If statement. Place this block of code after your while loop:
If C=1 and L1(C)>V
Then
L1(C)->L1(C+1)
C-1->C
End
The whole thing:
SetUpEditor L1
0->dim(L1
randIntNoRep(1,10,10)->L1
For(I,2,10)
L1(I)->V
I-1->C
While C>1 and L1(C)>V
L1(C)->L1(C+1)
C-1->C
End
If C=1 and L1(C)>V
Then
L1(C)->L1(C+1)
C-1->C
End
V->L1(C+1)
End
Disp L1

Related

Merge Sort confusion, stack level too deep?

So Im learning merge sort (In Ruby) and I understand mostly of how it works, so I was just writing the beginning of the "mergeSort" functionality and splitting the arrays up:
array = [5,1,8,3,4,6,11,2]
def mergeSort(arr,beginIndex,endIndex)
if endIndex > beginIndex
mid = (beginIndex+endIndex)/2
puts mid
puts "#{arr[0..mid]}"
puts "#{arr[mid+1..-1]}"
mergeSort(arr,0,mid) #comment out here?
mergeSort(arr,mid+1,arr.length-1) #or here?
end
end
mergeSort(array,0,array.length-1)
So I understand whats happening, and whats printing out works correctly. However the strange thing is...if I comment out either one of the secondary nested mergeSort's (where I say "#comment out here? or here?) I get a printout like I would think. However if I leave both it'll go on forever until I get a "stack level too deep". Which is weird because commenting out either one it'll stop when the array is at 1 length.
Why is this happening?
The second call should be mergeSort(arr,mid+1,endIndex).
It works fine if you leave out the second call because you narrow in to the beginning. It works fine if you leave out the first call because you narrow in to the end. It fails if you have both because you go to sort the first half and find yourself on the second call still trying to sort the whole array...recursively.

Loop collapsing in Ruby - iterating through combinations of two ranges

I have a code that iterates through two ranges. Please see the example below:
(0..6).each do |wday|
(0..23).each do |hour|
p [wday,hour]
end
end
Although this seems very concise and readable, sometimes 5 lines can be too much. One might want to write a more vertically compact code.
(0..6).to_a.product((0..23).to_a).each do |wday,hour|
p [wday, hour]
end
Above was my try, but the code looks very artificial to me. Am I missing something? Does ruby have a preferred way for this type of loop collapsing? If not, are there other alternatives to this workaround?
The following is slightly cleaner version of your loop:
[*0..6].product([*0..23]).each do |wday,hour|
p [wday, hour]
end
This approach does have the disadvantage of expanding the ranges into memory.
I think my preferred way of "collapsing" loops though, especially if the specific loop structure occurs in multiple places, is to turn the nested loops into a method that takes a block and yields to it. E.g.
def for_each_hour_in_week
(0..6).each do |wday|
(0..23).each do |hour|
yield wday,hour
end
end
end
for_each_hour_in_week do |wday,hour|
p [wday,hour]
end
This keeps the deep nesting out of the way of your logic, and makes your intent clear.

"Translating" a strange pseudocode into python

I'm doing an assignment and in one of the questions, my professor put some strange pseudo code as a condition and frankly I'm not sure if I understood it correctly.
This is what he gave us:
LOOP if S>0 then {S:=S-1; exit} end_if;
END_LOOP
can I understand this as
while True:
if S>0:
S = S - 1
break
if I were to rewrite it in Python?
Or, should it be like this?
while S>0:
S = S -1
break
It's not 100% clear, but given that the first version will either go around once, or go around forever, it's likely to be the second one.

Why won't finding polynomial whole number routes work?

So I want this to find me the roots of a polynomial. However, everytime I run it, it never gives me any roots, even if I use an obvious one like 2x-2. Why won't it work?
Input "Degree?",θ
Disp "Left to right"
Disp "coefficients"
1→V
For(Z,0,θ)
Input A
Q→R
P→Q
O→P
N→O
M→N
L→M
K→L
J→K
I→J
H→I
G→H
F→G
E→F
D→E
C→D
B→C
A→B
If V=1
Then
A→S
V=0
End
End
For(T,–A,A)
For(U,–W,W)
If T≠0
U/T→X
RX+Q→Y
YX+P→Z
ZX+O→Y
YX+N→Z
ZX+M→Y
YX+L→Z
ZX+K→Y
YX+J→Z
ZX+I→Y
YX+H→Z
ZX+G→Y
YX+F→Z
ZX+E→Y
YX+D→Z
ZX+C→Y
YX+B→Z
If Z=0
Then
Disp X
End
End
End
prgmRESET
RESET just resets the variable values. What is wrong with it?
Request: I have absolutely no idea what operation you are working off of, if you could please state that
Observation: You're using a lot of variables that haven't had any value assigned to them or initially cleared, I can see that you're trying to create a 'stream' of variables to work with, but if you do this without clearing the variables ahead of time then you create problems in your later calculations.
Coding Recommendations:
You state V=0, which does nothing in this context, instead of assigning it a value
You can change 'If T≠0' into just 'If T'
In your third 'For()' statement, "W" is undefined in the code.
You can change 'If Z=0:Then:Disp X:End', near the end of your code, into just 'If not(Z:Disp X'
Move prgmRESET to the top of your program
To be honest, I'm not entirely sure how you code is supposed to find the routes of a polynomial. Your error is most likely occurring somewhere in your mess of variable assigning/reassigning/swapping. I would redo your code using lists instead of basic variables.
If all you want to do is find the routes of a polynomial, I can give you a program for that.
:Prompt L1,X
:Repeat 1=dim(L1
:dim(L1->dim(L3
:seq(L1(A)(Ans-A),A,1,Ans-1->L2
:Repeat abs(Ans)<10^(-7
:L1(1->L3(1
:For(A,2,dim(L1
:XL3(A-1)+L1(A->L3(A
:End
:Ans->B
:L2(1->L3(1
:For(A,2,dim(L2
:XL3(A-1)+L2(A->L3(A
:End
:Ans^-1(AnsX-B->X
:B
:End
:Disp X
:L1(1->L2(1
:For(A,2,dim(L1)-1
:XL2(A-1)+L1(A->L2(A
:End
:L2->L1
:End
I'm not quite sure what you're trying to do here. You use a whole lot of variables without ever clearing or defining them, which probably means that all of your values will be 0.
Also, recommendation for future TI-BASIC questions:
PLEASE explain your variables. There's nothing worse than having a mess of variables and expecting the reader to do detective work to find out what they're supposed to do. Plus, it's helpful for you as well when you decide to come back to it for troubleshooting.

Trying to write sort method

Trying to sort an array by writing my own sort method using recursion (Pine's book). Saw some other examples on stackoverflow, but my code looks different from them. Two things I don't understand so far:
What is a wrapper method, and why do I need one? (I put on in the code, I think).
How to fix the "stack level too deep" error.
EDIT: New code updated, working but not correct.
Here's what I have so far:
def word_sorter unsorted, sorted
if unsorted[1] == nil
sorted.push unsorted[0]
words_put(sorted)
elsif unsorted[0] <= unsorted[1]
sorted.push unsorted[0]
unsorted.shift
word_sorter(unsorted, sorted)
else
unsorted.push unsorted[0]
unsorted.shift
word_sorter(unsorted, sorted)
end
end
def words_put sorted
puts 'these are your words now organized.'
sorted.compact!
puts sorted.join(', ')
Process.exit
end
unsorted = Array.new
sorted = Array.new
puts 'list as many words as you want. I will sort them... I think'
while unsorted.last != ''
unsorted.push gets.chomp
if unsorted.last == ''
unsorted.pop
word_sorter(unsorted, sorted)
end
end
Thanks!
1) There is nothing special going on here. We are using plain English (albeit metaphorically). A wrapper method is a method which is a wrapper. A wrapper is a thing which wraps. You are wrapping the word_sorter method with the sort method. You "need" it for convenience: it would be strange for the sort method to expect an empty list for its second parameter when you call it from outside. The wrapping takes into account the fact that the obvious interface for the recursion differs from the obvious interface for the outside world.
2) Take a close look at how the code for handling unsorted[0] >= unsorted[1] differs from the else case (i.e. when unsorted[0] < unsorted[1]).
3) Try describing your algorithm in English first. And then try putting out a few playing cards and testing your algorithm by following it, to the letter.
4) A working sort algorithm will only need to be called once. So work out a proper sorting algorithm, and then only call it once - outside the loop, after you've read in all the values to sort. You might also want to actually call words_put.
You should first try your code by some simple examples. E.g. use the list [3,2,1] as input.
In the first call it will match the 3>=2 condition. Thus now sorted=[2].
There are two issues with this one already.
2 isn't the first entry in the sorted list. There must be an issue with your algorithm being not able to sort any input.
The array unsorted isn't changed at all and thus it will loop with this one forever, yielding sorted=[2,2,2,2,2.....].
"Stack level too deep" implies that you have infinite recursion going on. It doesn't look like the unsorted list gets shorter in any of your branches in word_sorter, so it will keep running forever.

Resources