2017년 6월 8일 목요일

How does AI handle strings?


I am trying to implement an algorithm my class came up with after learning about the ceasar cipher. It seemed so simple, but now I am bogged down in AI and can't quite figure out how to handle strings. We have a variable that holds the alphabet, we ask the user for a word to encrypt and an offset number. All we want to do is find the index of each letter in the word to be encrypted and add the offset to it, then find the letter at that index.

Does anyone have a simple explanation or sample code that finds the index of a single letter from one string in another string, then finds the letter at a particular index?

--
Make a list of the 25 letters of the alphabet in order.  Call it Alphabet.
Suppose you want to shift by 4.
Then, given a letter find the index of the letter in Alphabet.
Add 4 tindex, and find the element of Alphabet at that incremented index.

You'll ned to deal with the case where the incremented index is greater than 26. (HInt:  Use modulo).

The basic thing is that you want to use a list, not a string, to represent the alphabet.

--
OK, I can do that. Do I then also have to make the word they gave me into a list so I can process each letter?

--
You can do that -- convert the word to a list of characters -- but you don't need to.   You can just use the text Segment commnd to extract each character as you need it.

--
I think you can also do Ceasar cipher using just Text and string operators -- i.e., no need to convert to lists.  Basically you make a Text variable containing "abcdefghijklmnopqrstuvwxyz".  Then you split it at the Caesar shift and join the two segments to create the caesaralphabet.  For example, for a shift of 3, you would get    "defghijklmnopqrstuvewzy + "abc"   as the Caesar alphabet.  Here's a picture.  To encrypt, you find the index of the plaintext letter in the alphabet (using the startsat(alphabet, letter)  block) and take the letter at that index from the caesaralphabet.  
Inline image 1

--

댓글 없음:

댓글 쓰기