This is my second project with Ti-Basic. I don't know if I'm too ambitious, but I want to finish it. This was partly inspired by a friend.
My project: Translate letters in phone numbers to numbers for the user to have an easier time. For example, if the phone number is "484-GET-FOOD," the number would be 484-438-3663. This project is for me to learn the language better.

EDIT: Right now, I'm having a hard time figuring out how to convert a letter to a digit. I tried assigning a number to a digit and using an if...then statement, but that would take up too much storage. Does anyone have any suggestions?
The number is 10 digits. 123-456-7890
First convert a letter to an index between 1 and 26 using inString("ABC...Z",Str1) where Str1 is the input.

Advanced users would change the lookup string so it's easy to convert this index to the desired digit.
Too ambitious? Nah. A rotary dial might be. Razz

I would start by creating the list of transformations you want:

ABC -> 2
DEF -> 3

...and so on. Continue this for practice and later reference. We're effectively associating every Nth letter of the alphabet from the left column with a numeral on the right. The keyword here is Nth; every letter from A–Z is, for our purposes, a number in disguise. Then it becomes a matter of ranges:

1 through 3 -> 2
4 through 6 -> 3

As you no doubt have noticed, 7 and 9 are special, so we can't get as particularly clean a formula as we'd like, but there is way of adding terms conditionally without resorting to an If statement:

When you wrap a relational test in parentheses, say, (X=6), then this will, at the time of evaluation, turn into a 1 if the relation is true, or 0 if it is false. So the expression 4+(X=6) may parse to 5 (being 4 added to true), but only if X should happen to equal 6 when this line gets assessed by the interpreter. If X were anything else during this period, then the 4 is all you get: not true added to 4 is still 4.

This notion is similar to the Iverson bracket, but don't let that spook you. If you couple it with a smattering of algebra, you should be equipped with everything you need to beat this problem into submission. If algebra isn't your thing, brute-forcing with a bit of ingenuity is within the realm of possibility, and perfectly acceptable in the learning stages.

Give it some real effort. If you get to a point where you're stuck, reply with what you got going, and we'll be happy to nudge you further.

Code:
Input Str1
"?->Str2
For(A,1,length(Str1
sub(Str1,A,1->Str3
inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",Ans
Str2+sub(Str3+"22233344455566677778889999",1+Ansnot(not(Ans)),1->Str2
End
Disp sub(Str2,2,length(Str2)-1
Ok, this is what I have so far.
First, I let the user input their number. Then, I made each digit their own variable. Third, I added the digits all up to form the standard 10 digit number. Fourth, I assigned that number to a variable. So, I effectively created a phone number viewer. Yay!
I tried to store it to a string, but it failed. I also tried to store it to theta, but it also failed. Is there a way to store the number and then convert it? I tried to make an inverse variable store, but there was an error. (ex: A ->2).
Any suggestions?
Schizal wrote:
First, I let the user input their number.

How? Code please.

Code:
ClrHome
Disp "Welcome to the","Phone Number Translator!","This program is for","translating those pesky","words in phone numbers","to numbers.","(Press Enter)
Pause
Menu("Options","Number Decoder",ND,"Help",H,"Quit",Q
Lbl ND
Disp "Input 1st Number
Input A
Disp A
Disp "Input 2nd Number
Input B
Disp 10A+B
Disp "Input 3rd Number
Input C
Disp 100A+10B+C
Disp "Input 4th Number
Input D
Disp 1000A+100B+10C+D
Disp "Input 5th Number
Input E
Disp 10000A+1000B+100C+10D+E
Disp "Input 6th Number
Input F
Disp 100000A+10000B+1000C+100D+10E+F
Disp "Input 7th Number
Input G
Disp 1000000A+100000B+10000C+1000D+100E+10F+G
Disp "Input 8th Number
Input H
Disp 10000000A+1000000B+100000C+10000D+1000E+100F+10G+H
Disp "Input 9th Number
Input I
Disp 100000000A+10000000B+1000000C+100000D+10000E+1000F+100G+10H+I
Disp "Input 10th Number
Input J
Disp 1000000000A+100000000B+10000000C+1000000D+100000E+10000F+1000G+100H+10I+J
SetUpEditor |LNUM
10->dim(|LNUM
1000000000A->R
100000000B->S
10000000C->T
1000000D->U
100000E->V
10000F->W
1000G->X
100H->Y
10I->Z
J->Q
R+S+T+U+V+W+X+Y+Z->X
X->dim(|LNUM
|LNUM(1)->X
Disp |LNUM(1)
Lbl H
Lbl Q
You'll want to store the phone number digits as strings rather than digits. Use Input Str1 to get the whole input string at once, and get the Nth character of the string with sub(Str1,N,1).

The advantage of this is you don't have repeated code (a common programming adage is that repeated code is bad) nor do you need to use math to extract the nth digit. You can also concatenate two strings with + to build the output string. See JWinslow23's code for an example.

Edit: For fun, JWinslow23's code optimized:


Code:
Input Str1
Str1
For(A,1,length(Ans
Ans+sub("-22233344455566677778889999",1+inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",sub(Str1,A,1)),1
End
sub(Ans,A,A-1


And golfed:


Code:
Input Str1
Str1
For(A,1,length(Ans
Ans+sub("23745968-",int(9-.3inString("TUVMNOWXYZJKLGHIPQRSDEFABC",sub(Str1,A,1))),1
End
sub(Ans,A,A-1
lirtosiast wrote:
You'll want to store the phone number digits as strings rather than digits. Use Input Str1 to get the whole input string at once, and get the Nth character of the string with sub(Str1,N,1).

The advantage of this is you don't have repeated code (a common programming adage is that repeated code is bad) nor do you need to use math to extract the nth digit. You can also concatenate two strings with + to build the output string. See JWinslow23's code for an example.

Edit: For fun, JWinslow23's code optimized:


Code:
Input Str1
Str1
For(A,1,length(Ans
Ans+sub("-22233344455566677778889999",1+inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",sub(Str1,A,1)),1
End
sub(Ans,A,A-1


And golfed:


Code:
Input Str1
Str1
For(A,1,length(Ans
Ans+sub("23745968-",int(9-.3inString("TUVMNOWXYZJKLGHIPQRSDEFABC",sub(Str1,A,1))),1
End
sub(Ans,A,A-1

What does "golfed" mean?
In normal golfing, a golfer tries to get their ball into the hole in as few shots as possible.

With code golfing, a programmer tries to get a program to complete a task in as few bytes as possible. This often requires techniques not frequently used in normal programming and is lots of fun once you have gotten relatively good at a language.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement