Friday, 30 March 2018

python - Extract the number from the string line

Have to build a new string which is of the form numeric_token1  numeric_token2 print this in ascending order. (A single space is the separator) (If no numeric tokens are found you need to print  NONE FOUND)


Input : 

        You need to read a line from STDIN to extract the numeric tokens only

Output :

 The string composed of number1  number2 in ascending order   .  Or  NONE FOUND

Test Cases:


Input: velraj hai 123  789 45 done
Output: 45 123 789

Input: 20 new old 90 67
Output: 20 67 90

Program:

def main():
#b = "vel 444 sdfdf 55 sdfdf 44 sdfdf 22 "
#   value = "vel 444 s 5 sdfdf 10 newver 3 sdfdf 1 sdfdf 2 "
   value = raw_input("Enter value :")
   list_array = value.split()

   digit_list = []
   for digit in list_array:
        if digit.isdigit():
#         print digit
            digit_list.append(int(digit))    # Make list to contain the number as interger type

   digit_list.sort()
   print digit_list

#str_ass = " vel "

   str1 = ' '.join(str(e) for e in digit_list)  # This line is used to convert the list into an string formate with space between digits
   if str1:
      print str1
   else:
      print "NONE FOUND"

#   for lis_to_dig in digit_list:
#      str_ass = ' ' + str(lis_to_dig)
#       print lis_to_dig
#    lis_to_arry = digit_list.split()


main()

Output:

$ python extra-num-from-str.py
Enter value :sdfdf 432 vel 22 hsdf 33 sdfdf 1 sdf 5
[1, 5, 22, 33, 432]
1 5 22 33 432

No comments:

Post a Comment