HackersInside
HackersInside

Reputation: 307

I'm trying to perform some manual encoding of strings using python

Probelm Description:

sms_encoding() which accepts a sentence and converts it into an abbreviated sentence to be sent as SMS and returns the abbreviated sentence.

Rules are as follows:

a. Spaces are to be retained as is

b. Each word should be encoded separately

If a word has only vowels then retain the word as is If a word has a consonant (at least 1) then retain only those consonants

My Code:

#PF-Assgn-50

def sms_encoding(data):
    #start writing your code here
    vowels=set("aeiouAEIOU")
    v_list=[]
    c_list=[]
    final_list=[]
    new_string=''
    word=data.split()
    word2=[]
    for i in range(0,len(word)):
       ch=word[i]
       #print(ch)
       if ch in vowels: 
           v_list.append(ch)
       for letter in word[i]:
       if letter not in vowels:
           c_list.append(letter)
       c_list.append(" ")


    new_string=''.join(v_list)+''.join(c_list)
    final_list.append(new_string)

    #print(v_list)

    return ' '.join(final_list)


data="Have a Nice Day"
print(sms_encoding(data))

My Output:

aHv **<2spaces>** Nc **<1space>** Dy

Expected Output:

Hv a Nc Dy (contains only 1 space)

Upvotes: 2

Views: 3074

Answers (5)

Akanksha Rastogi
Akanksha Rastogi

Reputation: 11

This will work for all cases... retains all except vowels when even 1 character in a string is not vowel.

def sms_encoding(data):

    vowels = set("aeiouAEIOU")
    
    words = data.split()
    
    encoded_words = []
    
    for i in range(0,len(words)):
        vow_count = 0
        cons_word = []
        for x in words[i]:
            if x in vowels:
                vow_count =vow_count+1
            elif x not in vowels:
                cons_word.append(x)
        if vow_count == len(words[i]):
            encoded_words.append(words[i])
        elif vow_count != len(words[i]):
            encoded_words.append("".join(cons_word))

 
    encoded_msg = " ".join(encoded_words)
    
    return encoded_msg

data=input("Kindly enter your message for sms encoding : ")

print(sms_encoding(data))

Try and let me know!

Upvotes: 1

Anushka Bhadra
Anushka Bhadra

Reputation: 1

def sms_encoding(new_s):
    encrypt_string = []
    consonant_list = []
    vowel_set = set("aeiouAEIOU")

    for word in range(0, len(new_s)):
        v_letter = new_s[word]
        if  v_letter in vowel_set:
            encrypt_string.append(v_letter)

        for letter in v_letter:
            if letter not in vowel_set:
                consonant = " ".join(letter)
                encrypt_string.append(consonant)
        encrypt_string.append(" ")     
       
    encrypt_string = "".join(encrypt_string)
    print(encrypt_string)

s = input("Enter a string ")
new_s = s.split()           
sms_encoding(new_s)

Upvotes: -1

RISHABH CHATURVEDI
RISHABH CHATURVEDI

Reputation: 1

def sms_encoding(data):

    data=data.lower()
    a=data.split()
    v1=""
    for i in range(0,len(a)):
        z=a[i]
        v=""
        c1=0
        for j in z:
            if j not in ('a','e','i','o','u'):
                v=v+j
            elif j in ('a','e','i','o','u'):
                c1=c1+1
        if(c1!=len(z)):    
            v1=v1+v+" "
        elif(c1==len(z)):
            v1=v1+z+" "
    
    word=v1[0:len(v1)-1]
    return word

data="I love Python"
print(sms_encoding(data))

Upvotes: 0

Nitesh Nehra
Nitesh Nehra

Reputation: 29

def sms_encoding(data):
    vowel = "aeiouAEIOU"
    list1 = data.split()
    list2 = []

    for i in list1:
        length=len(i)
        if length == 1:
            list2.append(i)
            list2.append(" ")#to add spaces between the words
        else:
            count=0
            for a in i:
                if a in vowel:
                    count+=1 
            if count==length: #to check if all the letters are vowels
                list2.append(i)
                list2.append(" ")
            for a in i:
                if a not in vowel:
                    list2.append(a)

            list2.append(" ")
    list2.pop() #to remove the extra space at the end of the whole sentence
    q="".join(list2)
return q

data = "I love Python aeio"
print(sms_encoding(data))

try this code it will work the question is from infytq

Upvotes: 0

Austin
Austin

Reputation: 26039

You could iterate over words in the sentence taking only consonants only if the word contains atleast one consonant:

data = "Have a Nice Day"

splitted = data.split()
for i, x in enumerate(splitted):
    if not all(y in 'aeiou' for y in x.lower()):
        splitted[i] = ''.join([y for y in x if y.lower() not in 'aeiou'])

print(' '.join(splitted)) 
# Hv a Nc Dy

Upvotes: 3

Related Questions