2017년 5월 4일 목요일

How do you delete duplicate letters in a string?


I'm trying to take a string and delete duplicate letters in said string. My teacher can't figure it out either, and suggested I try posting here. There was another (old) topic that was asking pretty much the same thing, but it never got solved (or if it did, I wasn't able to figure out how to translate the last reply's pseudo code into the blocks format).

--
It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

-- 
That's pretty easy!

Check out in Thunkable community here:

use for each loop and add items to list from string, also check if it exists in the list you are putting into before adding. This way you will have a list of unique characters.

See also:
Adding up specific words in text string

--
You have to build up a new variable from empty a letter at a time from the input string.

Use a for each number from 1 to length(inputString) by 1 loop:

outputString = empty string
for each number from 1 to length(inputString) by 1 
set global nextLetter to segment(inputString at pos number length 1)
If not(outputString contains nextLetter) THEN
   set outputString to JOIN(outputStringnextLetter)
end for each number

-- 
By relevant blocks, do you mean my variables and my components? I'll upload screenshots in my next replies, thanks.

-- 
I tried to replicate your suggestion in the blocks editor like so:


I couldn't figure out what you meant by "end for each number" and building up variables from an empty letter at a time, so I doubt I got everything. I'm a visual learner, so if you gave me a screenshot with an example to learn from, that would help me a lot more, thanks.

-- 
It occurred to me that "duplicate letters" is ambiguous.

For example, if the input word is "Mississippi",
should the output be "Misp" or should it be "Misisipi" ?

In other words, do you only want to delete consecutive duplicates?

-- 
I meant to delete all duplicate letters, so in your example, would be "Misp". Your suggestion worked! (I had screenshotted what I tried doing following Pavitra's advice instead of yours) I guess I did it right this time, because last time I tried yours it didn't work but now it does! I appreciate all your help.

Here's my resulting blocks:
 (Your reply didn't show up in this thread when it showed up in my gmail inbox, so I assumed it was an email, and replied with an email... bluh. I don't use forums so I'm only learning how to use one right now.)

--
Thanks for posting the answer.

By the way, the alternate question, deleting consecutive duplicates,
is close but a bit harder, having to check for text with length < 2.

-- 
A little optimization and formatted as a procedure.
-- 

댓글 없음:

댓글 쓰기