Sunday, May 12, 2024

Python and Calculator Basic: Determine if a Character is in a String

 Python and Calculator Basic: Determine if a Character is in a String

Calculators: Casio fx-CG 50, TI-84 Plus CE


Today’s blog gives a technique on determining whether a character is present in a string.


Problem


We are given the string “a+b*c” and we are determine if the following characters are present in the string: “a”, “b”, “c”, “d”, and “e”.


Let str1 be the string to be searched.


Since the goal is to search single characters, construct a string that would contain all the characters. We can use this string to pick out the character to search for. For our problem, this particular string is “abcde”, which will name this string, str2.


Code: Python


This code was entered on a fx-CG 50, but it should work on all calculators and platforms with Python. No modules are needed.


Script: instring.py


str1=”a+b*c”

str2=”abcde”

l=len(str2)

t=0

print(“\ncounts a-e:”)

for i in range(l):

  s=str2[i]

  c=str1.count(s)

  t+=c

  print(s,c,t)


Notes:


\n: start a new line in a string


string.count(target string): returns the count of when the target string occurs


string[n]: calls the nth character of a string (0 to n – 1)



Basic Code: Casio fx-CG 50


“A+B×C” → Str1

“ABCDE” → Str2

StrLen(Str1) → L

StrLen(Str2) → M

0 → T

For 1 → I To M

0 → C

StrMid(Str2, I, 1) → Str4

For 1 → J To L

StrMid(Str1, J, 1) → Str3

StrCmp(Str3, Str4) = 0 ⇒ C + 1 → C

Next

T + C → T

ClrText

Locate 1, 3, Str4

Locate 1, 4, C

Locate 1, 5, T

“ “◢

Next


Notes:


The alpha variables are upper case.


StrMid(string, starting point, number of characters): extract a sub-string starting with the nth character


StrCmp(string 1, string 2): compares the character value of a string. If the values are equal, the function returns 0. This command works well with strings of one character.


A blank string with one space (“ “), followed by a display character (◢) will pause the screen.


Basic Code: TI-84 Plus CE


“A+B*C” → Str1

“ABCDE” → Str2

length(Str1) → L

length(Str2) → M

0 → T

ClrHome

For(I, 1, M)

0 → C

sub(Str2,I,1) → Str4

For(J,1,L)

sub(Str1,J,1) → Str3

If Str3 = Str4

C +1 → C

End

T + C → T

Disp Str4 + “ “ + toString(C) + “ “ + toString(T)

End


Notes:


The alpha variables are upper case.


sub(string, starting point, number of characters): extract a sub-string starting with the nth character


Strings can be compared directly.


Results


The result of all three algorithms should give:


Character

C (count)

T (accumulative total)

a

1

1

b

1

2

c

1

3

d

0

3

e

0

3


I hope this helpful,


Eddie


All original content copyright, © 2011-2024. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.


TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...