fork(1) download
  1. justWords = {}
  2. words = []
  3.  
  4. justNums = {}
  5. nums = []
  6.  
  7. # Check if value in the input is an integer or a word
  8. def is_number(s):
  9. try:
  10. float(s)
  11. return True
  12. except ValueError:
  13. return False
  14.  
  15. # Replace the values in the dictionary with the correctly sorted words/integers
  16. def go_through(theDict, theList):
  17. counter = 0
  18. for k,v in theDict.iteritems():
  19. theDict[k] = theList[counter]
  20. counter = counter + 1
  21. return theDict
  22.  
  23. # Replace the values in the original input list with correctly sorted values of the word/int dicts
  24. def inject(theDict, theList):
  25. for k,v in theDict.iteritems():
  26. theList[k] = v
  27. return theList
  28.  
  29. if __name__ == "__main__":
  30.  
  31. splitInput = (raw_input("")).split()
  32.  
  33. # Sort the words and numbers into their own lists as tuples
  34. for i,j in enumerate(splitInput):
  35. if is_number(j):
  36. justNums[i] = j
  37. nums.append(j)
  38. elif not is_number(j):
  39. justWords[i] = j
  40. words.append(j)
  41.  
  42. print("%s\n%s\n" % (justWords, justNums))
  43.  
  44. words = sorted(words)
  45. nums = sorted(nums)
  46.  
  47. print("%s\n%s\n" % (words, nums))
  48.  
  49. # Replace the values in the dictionaries with the values in the sorted list
  50. justWords = go_through(justWords, words)
  51. justNums = go_through(justNums, nums)
  52.  
  53. print("%s\n%s\n" % (justWords, justNums))
  54.  
  55. # Inject correctly maped sorted words into the original list
  56. splitInput = inject(justWords, splitInput)
  57. splitInput = inject(justNums, splitInput)
  58.  
  59. print("%s" % (' '.join(splitInput)))
  60.  
Success #stdin #stdout 0.01s 7856KB
stdin
car truck 8 4 bus 6 1
stdout
{0: 'car', 1: 'truck', 4: 'bus'}
{2: '8', 3: '4', 5: '6', 6: '1'}

['bus', 'car', 'truck']
['1', '4', '6', '8']

{0: 'bus', 1: 'car', 4: 'truck'}
{2: '1', 3: '4', 5: '6', 6: '8'}

bus car 1 4 truck 6 8