Author Topic: TI-BASIC equi challenge  (Read 3457 times)

0 Members and 1 Guest are viewing this topic.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
TI-BASIC equi challenge
« on: August 19, 2011, 12:29:23 am »
While I was taking a small Java programming test (I don't know even Java :P), I came across an interesting problem called the equilibrium index. Basically, given a list such as {-7, 1, 5, 2, -4, 3, 0}, there is an index into the list for which the sum of the elements up to that index is equal to the sum of the elements after. For this particular data set, it happens to be at the indices 6 and 7.

Your challenge is to write a program in TI-BASIC (or Axe if you wish) that will compute the equilibrium index of any list. If it doesn't exist, then the program must return -1. You are allowed to assume that the list is in L1 and that the dimension of the list is in N.

NOTE: Don't use Google. As it turns out, a lot of people have unwittingly made a mistake in declaring the problem such that the example list has indices at 3 and 6. The index of 3 is actually only true when L1(4) is eliminated from the list.

Timing: It took me about three minutes to write the program and a further five minutes to notice the error everyone had made wasn't actually a bug in my program, but their mistake. It shouldn't take longer than half an hour max to do.

« Last Edit: August 19, 2011, 12:29:43 am by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: TI-BASIC equi challenge
« Reply #1 on: August 19, 2011, 09:57:25 am »
My attempt:
Code: [Select]
:augment({0},augment(L1,{0->L1
:For(A,1,N)
:If sum(L1,1,A)=sum(L1,A+1,N+2
:Disp A
:End

Let me know if this works.  I'm not sure if I quite understand the teaser. ;)

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: TI-BASIC equi challenge
« Reply #2 on: August 19, 2011, 10:00:35 am »
Nearly forgot how fun BASIC was...

Done

Took me two minutes in TI-BASIC, 30 bytes. Takes input in Ans and outputs as Ans. It doesn't even destroy any variables :D

Code: [Select]
sum(Ans)/2=cumSum(Ans
If not(sum(Ans
-1

EDIT: ztrumpet, that doesn't work, but take out the augment({0}, in the first line and change N+2 to N+1 in the third and it almost works -- now to figure out how to output -1 if there was no match :)
« Last Edit: August 19, 2011, 10:11:27 am by Deep Thought »




Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: TI-BASIC equi challenge
« Reply #3 on: August 19, 2011, 10:14:00 am »
Code: [Select]
sum(L1->S
0
For(A,1,N
Ans+L1(A
If 2Ans=S
Disp A
End
This acts like ztrumpet's, but is better, I think. For a version that meets your requirements (assuming that "return" means put into Ans, and you only want one equilibrium index):
Code: [Select]
sum(L1->S
0
For(A,1,N
Ans+L1(A
If 2Ans=S
A+N->A
End
A-N-1
Ans-not(Ans
Since TI-BASIC lists are 1-indexed, though, wouldn't it make more sense to have it return 0 if nothing is found? (For my program, just cut off the last line to have it return 0 if nothing's found)
Edit: Deep Thought: that returns a list of 1's and 0's...
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: TI-BASIC equi challenge
« Reply #4 on: August 19, 2011, 10:18:09 am »
Edit: Deep Thought: that returns a list of 1's and 0's...
Fine, but that adds another 13 bytes :(

EDIT:

Since TI-BASIC lists are 1-indexed, though, wouldn't it make more sense to have it return 0 if nothing is found? (For my program, just cut off the last line to have it return 0 if nothing's found)
That's a good point. It even cuts my code down to a nice, even 42 ;D

Code: [Select]
sum(Ans)/2=cumSum(Ans
AnscumSum(binomcdf(dim(Ans)-1,0
If not(sum(Ans
0
« Last Edit: August 19, 2011, 10:29:09 am by Deep Thought »




Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: TI-BASIC equi challenge
« Reply #5 on: August 19, 2011, 10:21:11 am »
Now it returns a list of various numbers and 0's :P
I'd think that, if you're returning a list, you should get rid of the 0's.
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: TI-BASIC equi challenge
« Reply #6 on: August 19, 2011, 10:30:59 am »
If we're allowed to just return a single number, like your second one, I could use this (36 bytes):

Code: [Select]
sum(Ans)/2=cumSum(Ans
max(AnscumSum(binomcdf(dim(Ans)-1,0
« Last Edit: August 19, 2011, 10:31:11 am by Deep Thought »