Getting nth element from set - ti-basic

Good day,
I have a set in my program, say nums
{5,6,7}->nums
I have a for loop
For i,1,dim(nums)
EndFor
How would I get the ith element from the set? For example, print 5,6,7 from the for loop?
I've tried calling it like a function, as I've seen in many guides, but that gives an error.
Thanks!

:facepalm:
Somehow I didn't even try brackets. For those with the same issue, it is just
nums[i]

Related

Mathematica - can I define a block of code using a single variable?

It has been a while since I've used Mathematica, and I looked all throughout the help menu. I think one problem I'm having is that I do not know what exactly to look up. I have a block of code, with things like appending lists and doing basic math, that I want to define as a single variable.
My goal is to loop through a sequence and when needed I wanted to call a block of code that I will be using several times throughout the loop. I am guessing I should just put it all in a loop anyway, but I would like to be able to define it all as one function.
It seems like this should be an easy and straightforward procedure. Am I missing something simple?
This is the basic format for a function definition in Mathematica.
myFunc[par1_,par2_]:=Module[{localVar1,localVar2},
statement1; statement2; returnStatement ]
Your question is not entirely clear, but I interpret that you want something like this:
facRand[] :=
({b, x} = Last#FactorInteger[RandomInteger[1*^12]]; Print[b])
Now every time facRand[] is called a new random integer is factored, global variables b and x are assigned, and the value of b is printed. This could also be done with Function:
Clear[facRand]
facRand =
({b, x} = Last#FactorInteger[RandomInteger[1*^12]]; Print[b]) &
This is also called with facRand[]. This form is standard, and allows addressing or passing the symbol facRand without triggering evaluation.

Is there a way to store a list when programming a TI-83+?

Out of curiosity, I'm beginning to learn how to program my TI-83+ calculator. Part of my latest program involves storing numbers in a list. How can I add items to a list on a TI-83+ and how can I loop over them/access them?
Well, if you want to add something to the end, you need the length of the list. Let's say your using L1 as a list and variable A as the value you are trying to add to the list.
Here's what you would do:
:A->L1(1+dim(L1))
Here's how that works. The dim command has 1 parameter. That parameter is a list. When you use the dim command, it returns the length of the list in the parameters. When you want to refer to a specific place in a list, you use the syntax: list_name(location). So This line of code takes the value of variable A and stores it in the location of L1 which is 1 more than the length of L1, therefore appending variable A to the end of L1.
If you want to access a value in list, again use the syntax: list_name(location). On the other hand, if you don't know the location of the value you are looking for, or you are cycling through the list and doing something with each value, you can use a for statement.
Like this:
:FOR(A, 0, dim(L1))
::L1(A)->B
::"do whatever you want with the value of L1(A) here"
:END
Or like this:
:FOR(A, 0, dim(L1))
::if(L1(A) == "insert value being searched for here"):THEN
:::A->B
:::dim(L1)+1->A
::END
:END
The for loop works like this: at the beginning of the loop, 0 is stored to variable A. Then the loop continues until variable A is greater than dim(L1). After each time the loop resets, the value of variable A is increased by 1.
In the first example, the program loops through each value of L1 and does whatever you want to do with each value.
In the second example, the program loops through each value of L1. When the value of L1 matches the value you are looking for, the location of the value is stored in variable B to be used for whatever you want later. Then, the value of variable A is set to 1 more than the length of L1. Since the value of variable A is greater than dim(L1), the for loop is terminated.
An element can be added to the end of a list of unknown length like this:
0→L1(1+dim(L1
Under normal condition, attempting to set the value of an index greater than the length of the list results in ERR: INVALID DIM; however, if the index is only 1 greater than the length of the list, the value is appended to the end of the list.
You could use a list or a matrix, but I would suggest a list. You can find information on lists and their commands from this link.
Lists are also better for saving values in between program executions than just using variables, which may be changes by other programs or math.
You need first to define the size of a list like this :
3->dim(L1
(if you forget, you will have an ERR:Invalid Dim)
Press enter and you get a "10" as answer (don't worry it's normal).
You can find dim( in the [Catalog] and -> is "[STO->].
Then you could fill the list with some data like this :
2->L1(1)
3->L1(3)
Now When you print L1 you get :
{2 0 3 0}
First index is L1(1) not 0 (as usual).
You can delete the list by using DelVar :
DelVar L1
You can fill it with Fill, sort it, convert to matrix ....
Simply go to the List menu (2nd + Stat).
You can iterate on the list using a for loop (no foreach, use dim(L1) for the upper bound).
More informations in the guidebook or you could also ask your questions on this calculator questions stack
Hope this helps =)
You can do what Thibault said, fill it, sort it, convert it (Very well said, by the way). However, you can also do:
3->L1(dim(L1))
This will add 3 to the end of L1.

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.

Correct use of findall/3, especially the first template argument

i know there is a build-in function findall/3 in prolog,
and im trying to find the total numbers of hours(Thrs) and store them in a list, then sum the list up. but it doesnt work for me. here is my code:
totalLecHrs(LN,THrs) :-
lecturer(LN,LId),
findall(Thrs, lectureSegmentHrs(CC,LId,B,E,THrs),L),
sumList(L,Thrs).
could you tell me what's wrong with it? thanks a lot.
You need to use a "dummy" variable for Hours in the findall/3 subgoal. What you wrote uses THrs both as the return value for sumList/2 and as the variable to be listed in L by findall/3. Use X as the first argument of findall and in the corresponding subgoal lectureSegmentHrs/5 as the last argument.
It looks like the problem is that you're using the same variable (Thrs) twice for different things. However it's hard to tell as you've also used different capitalisation in different places. Change the findall line so that the initial variable has the same capitalisation in the lectureSegmentHrs call. Then use a different variable completely to get the final output value (ie the one that appears in sumList and in the return slot of the entire predicate).
You need to use a different variable because Prolog does not support variable reassignment. In a logical language, the notion of reassigning a variable is inherently impossible. Something like the following may seem sensible...
...
X = 10,
X = 11,
...
But you have to remember that , in Prolog is the conjunction operator. You're effectively telling Prolog to find a solution to your problem where X is both 10 and 11 at the same time. So it's obviously going to tell you that that can't be done.
Instead you have to just make up new variable names as you go along. Sometimes this does get a bit annoying but it's just goes with the territory of a logical languages.

Ruby: Parsing a complex hash

I have a hash that goes multiple levels deep: http://gist.github.com/285350
I am trying to loop through each serving but I keep running into multiple nil[] errors even though the hash isn't nil.
For example:
food_hash["food"]["servings"]
Returns nil.[]
It might be because im half asleep but I can't seem to get down to the "serving_description"... could anyone help put me in the right direction?
I can't see get down to the "serving_description"
Note that (1) servings.serving is an array, and (2) food is not a key in the hash. Try this instead:
f["servings"]["serving"][0]["serving_description"]
=> "1 thin slice (yield after cooking)"
where f is the hash.
Is this what you're trying to do?
food_hash["servings"]["serving"][0]["serving_description"]

Resources