2

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)

5 Answers 5

3

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
Sign up to request clarification or add additional context in comments.

5 Comments

Both this answer and the original code assume that any word comprised exclusively of vowels has a maximum length of 1. This might be acceptable but words like 'ooo' wouldn't be handled properly.
But that doesn't generalize well to other examples. Say, for the string "Oi, I like you" , you would lose Oi
@Austin. Haha I was just adding the same to mine. My answer is now duplicate so I'll delete.
Thank you so much, the code works perfect.However i was wondering how does enumerate help?
@vishalborana It's not strictly necessary but it allows you to modify the elements of splitted in place.
1

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!

Comments

0
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

Comments

0
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))

Comments

-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)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.