0
Your task is to sort a given string. Each word in the String will contain a single number. This number is the position the word
Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0). If the input String is empty, return an empty String. The words in the input String will only contain valid consecutive numbers. For an input: "is2 Thi1s T4est 3a" the function should return "Thi1s is2 3a T4est"
4 odpowiedzi
0
Is it good? https://code.sololearn.com/c5Zo3Rs3b1Ji/?ref=app
0
0
I m writing this code by considering 3 important case:
case 1)program should not print anything if it encounters an empty string
case 2)program should terminates if one of the word in the sentence does not contains any number
case 3)program should run successfully if some words contains duplicate numbers. e.g
is2 thi1s t4est 4a
import sys
import re
def get_string():
a=input()
return(a)
def main():
a=get_string()
pattern='[1 2 3 4 5 6 7 8 9]'
if(a==''):
sys.exit()
else:
lst=a.split()
if(len(lst)==1):
x=len(re.findall(pattern,lst[0]))
#print(x)
if(x!=1):
sys.exit()#because more than 1 number is present in a word
else:
print(lst[0])
sys.exit()
else:
d={}
h=0.1
for i in range(0,len(lst)):
y=(re.findall(pattern,lst[i]))
x=len(y)
if(x!=1):
sys.exit()
else:
if(int(y[0]) in d):
d[float(y[0])+h]=i
h=h+0.1
else:
d[int(y[0])]=i
#d[int(y[0])]=i
#print(d)
key=d.keys()
key=list(key)
key.sort()
for i in range(0,len(key)):
print(lst[d[key[i]]],end=" ")
main()
0
using System;
namespace ConsoleApp2
{
class Program
{
public static string Order(string text)
{
if (text==string.Empty)
{
return text;
}
string[] words = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
text = string.Empty;
for(int i=1; i<10;i++)
foreach (var word in words)
{
if (word.Contains(i.ToString()))
{
text +=" "+ word;
}
}
return text;
}
public static void Main()
{
string s = "is2 Thi1s T4est 3a";
Order(s);
}
}
}