Post Reply 
Programming puzzles: processing lists!
04-27-2017, 02:11 PM
Post: #50
RE: Programming puzzles: processing lists!
I made TI-89 versions of some of these just to be obnoxious. Wink (Well, and also to get some use out of this thing; it's nice to program, even if it's a bit clumsy for basic number crunching.)

Most are implemented as functions, which can return a value. Some are implemented as a program, since you can't use SortA or SortD in a function, even on local variables. Lazy compiler design, I guess. (I implemented quicksort as a function in pure Basic, but it's a lot slower than the built-in operations.)

Note that I've used != in place of the "not-equals" character, <= and >= for the inequalities, and -> in place of the "STO" arrow.

#1
Code:
p1(x)
Func
Local i,m
dim(x)->m
For i,1,m
  If x[i]!=3 and x[i]!=5:Return "Invalid"
EndFor
Return x
EndFunc
Storing the dim(x) in a variable seemed to be faster than putting it right in the For loop condition. I'm guessing it evaluates the ending value expression at each pass. I could use seq() to process the list, but this allows an early exit on failure.

#2
Code:
p2(x)
Func
Return seq(when(x[i]=3,4,7),i,1,dim(x))
EndFunc
Storing the dim(x) in a variable made no difference here.

#3
Code:
p3(x)
Func
Local d,p
dim(x)->d
iPart(d/3)->p
Return augment(right(x,p),left(x,d-p))
EndFunc

#4
Code:
p4(x)
Func
Local c,i,d
dim(x)->d
1->c
For i,d,1,-1
  If x[i]=1 Then
    0->x[i]
  Else
    1->x[i]
    0->c
    Exit
  EndIf
EndFor
If c=1:augment({1},x)->x
Return x
EndFunc

#5
Code:
p5(x)
Func
Local i,d
dim(x)->d
For i,d,1,-1
  If x[i]=0 Then
    1->x[i]
  Else
    0->x[1]
  Exit
EndIf
EndFor
Return x
EndFunc

#6
Code:
p6(x)
Prgm
Local y,c,l,i,d,t
If dim(x)=0:Goto e
x->y
0->c
SortA y
y[1]->l
dim(y)->d
0->t
For i,1,d
  If y[i]=l Then
    c+1->c
  Else
    If t=0 Then
      c->t
    ElseIf c!=t Then
      Goto i
    EndIf
    1->c
    y[i]->l
  EndIf
EndFor
If c!=t and t!=0:Goto i
Lbl e
Disp x
Return

Lbl i
Disp "Invalid"
EndPrgm

#7
Code:
p7(x)
Func
Local d,h
dim(x)->d
iPart(d/2)->h
Return augment(augment(left(x,h),{-1}),right(x,d-h))
EndFunc

#8
Code:
p8(x)
Func
Local d
dim(x)->d
If mod(d,2)=1:Goto i
If d=0:Return x
If left(x,d/2)=right(x,d/2):Return x
Lbl i
Return "Invalid"
EndFunc
[code]

#9
[code]p9(x)
Func
Local k,i,d
dim(x)->d
If d=0:Return x
If x[1]!=3:Goto i
1->i

Loop
  If x[i]!=3 Then
    i-1->k
    Exit
  EndIf
  i+1->i
  If i>d:Goto i
EndLoop

Loop
  If x[i]!=5 Then
    If (i-1)/2!=k:Goto i
    Exit
  EndIf
  i+1->i
  If i>d:Goto i
EndLoop

Loop
  If x[i]!=3:Goto i
  i+1->i
  If i>d Then
    If (i-1)/3!=k:Goto i
    Exit
  EndIf
EndLoop

Return x

Lbl i
Return "Invalid"
EndFunc

#10
Code:
p10(x)
Prgm
Local y,n,c,d,i
x->y
SortA y
dim(y)->d
If d=0:Goto v
0->c
y[1]->n
If n!=iPart(n) or n<1:Goto i

1->i
While i<=d
  If y[i]!=n Then
    If c!=n:Goto i
    1->c
    y[i]->n
    If n!=iPart(n) or n<1:Goto i
  Else
    c+1->c
  EndIf
  i+1->i
EndWhile

If c!=n:Goto i

Lbl v
Disp x
Return

Lbl i
Disp "Invalid"
EndPrgm

Might attempt a few more of these later.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - Dave Britten - 04-27-2017 02:11 PM



User(s) browsing this thread: 7 Guest(s)