Home > Blockchain >  How To Remove Characters from String based on Some Conditions
How To Remove Characters from String based on Some Conditions

Time:10-31

Dears,

How To Remove Characters from String based on Some Conditions ? Knowing that I have one string where I need to :

  1. STEP 01 : Remeove vowels
  2. STEP 02 : Remove Duplicate consonants and keep the 1st appeared one.
  3. STEP 03 : Remove 1st and Last Characters if string starts/Ends with a given letter.

Example : Word : Transmuted

After Step01 ( Removing Vowels) => Trnsmtd After Step02 ( Removing Duplicate Consnants , here "2nd t", "2nd n", "2nd s" ) ==> Trnsmd After Step03 ( Removing 1st and Last Characters if word starts or ends with "t" and "d" => rnsm

Here is my first part of python Script , need the remaining 2 Steps:

string = "Transmuted"

vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
result = ""

for i in range(len(string)):
    if string[i] not in vowels:
    result = result   string[i]

    print("\nAfter removing Vowels: ", result)

OUTPUT : After removing Vowels: Trnsmtd

CodePudding user response:

Here's another version of Step 01:

word = 'THERMOCHEMISTRY'

vowels={'a','e','i','o','u'}
result = ''.join([letter for letter in word if letter.lower() not in vowels])

For step 02, you could do this (there are probably more efficient methods, but this works):

result2 = ""
for i in result:
    if i.lower() not in result2 and i.upper() not in result2:
        result2  = i
       

For step 03

filter_out = {'d','t'}
result3 = result2[1:] if result2[0].lower() in filter_out else result2
result3 = result3[:-1] if result3[-1].lower() in filter_out else result3

print(result   ' ' result2 ' ' result3)
# THRMCHMSTRY THRMCSY HRMCSY

CodePudding user response:

If you want step by step solution, check out this code:

string_ = "Transmuted"

vowels_ = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
states_ = set()
filter_ = {'T', 'D'}

result_step1 = ""
for i in range(len(string_)):
    if string_[i] not in vowels_:
            result_step1 = result_step1   string_[i]

print("\nAfter removing Vowels: ", result_step1)


result_step2 = ""
for i in range(len(result_step1)):
    if result_step1[i].upper() not in states_:
        result_step2  = result_step1[i]
        states_.add(result_step1[i].upper())
    
print("\nAfter removing Duplicate: ", result_step2)

start_ = 0 if result_step2[0].upper() not in filter_ else 1
end_ = len(result_step2)-1 if result_step2[0].upper() not in filter_ else len(result_step2)-2

result_step3 = result_step2[start_:end_]
print("\nAfter removing 1st and Last Char: ", result_step3)

OUPUT:

After removing Vowels:  Trnsmtd

After removing Duplicate:  Trnsmd

After removing 1st and Last Char:  rnsm

And if you want final result you may refer to @Swifty code or this:

string_ = "Transmuted"

vowels_ = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
states_ = {chr(i):0 for i in range(65, 91)}
result = ""

for i in range(len(string_)):
    if string_[i] not in vowels_:
        if states_[string_[i].upper()] < 1:
            result = result   string_[i]
            states_[string_[i].upper()]  = 1

    print("\nAfter removing Vowels: ", result[1:len(result)-1])
  • Related