05-21-2025 01:08 AM
Hi, I am trying to write a simple program that extracts all elements containing a given element (the element to extract will be hardcoded) and the element before it. I have attached a screenshot containing example input and output. In this example code, the elements I want to extract are all elements containing 'A'. My data will be structured that all desired elements (in this case 'A') will be squeezed between unwanted elements (in this case C, D and E), but the desired element can also be in the first index. Does anybody know how to implement this in an easy way?
05-21-2025 01:32 AM - edited 05-21-2025 01:33 AM
Hi Asasa,
@Asasafuchi wrote:
Hi, I am trying to write a simple program that extracts all elements containing a given element (the element to extract will be hardcoded) and the element before it. I have attached a screenshot containing example input and output. In this example code, the elements I want to extract are all elements containing 'A'. My data will be structured that all desired elements (in this case 'A') will be squeezed between unwanted elements (in this case C, D and E), but the desired element can also be in the first index. Does anybody know how to implement this in an easy way?
Why do you request for an "easy" way? Why not ask for generic suggestions?
Your requirement isn't well-defined!
You want to read the "given element and the element before it": in your example there are three consecutive "A" elements - according to your requirement you should result in 5 consecutive "A" elements (with an "E" in front) in the output array!!!
So please write down the requirement WITH ALL considerations/"border effects"!
What have you tried so far?
Where are you stuck?
05-21-2025 03:36 AM
Going by your picture: Instead of looking at the element before, look at the next one. If either the current or next element is an A, keep the current element.
05-21-2025 09:46 AM
From your picture, B is the first element and the resulting array is with B removed. Is that what you're after?
05-21-2025 10:34 AM
05-21-2025 11:15 AM
Are all strings guaranteed to be single characters?
05-22-2025 01:04 AM
no, but the strings that I want to extract will be identical and they can be hardcoded as they will always be the same.
05-22-2025 03:06 AM
Hi, yes almost.
the goal is to extract all strings containing a given element, in this case A. and the two adjacent elements that are NOT A.
I have implemented something that I think works. It is pretty messy so if anybody has some suggestions on an easier way to implement this I would be happy to see it.
05-22-2025 04:35 AM
Hi Asasa,
@Asasafuchi wrote:
the goal is to extract all strings containing a given element, in this case A.
and the two adjacent elements that are NOT A.
In your first message you gave as input:
BCAAADABEAAA.
The desired result was given as:
CAAADAEAAA
When you want "A and two adjacent elements" then why is there no "B" in the output???
05-22-2025 01:55 PM
I think the title gives it away. The character "B" is never followed by the Character "A" in the string sequence given; therefore, B is never present in the output.
Now the title is different from the instructions that you have provided later - Please Clarify. What is the actual requirement?