Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/542205276469071874
added 25 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Improving code on sorting string Sorting strings with certain restrictionrestrictions


 

I am trying to find comments on improve the code or looking for a different approach to the problem if there is a better one.

Currently, what I am doing is splitting the input string on spaces, putting the words and integers into their own lists, sorting them appropriately by mapping to a different list, and then injecting the sorted words/integers back into the original input list.

I am trying to find comments on improve the code or looking for a different approach to the problem if there is a better one. Does anyone have any comments?

Input:


 
car truck 8 4 bus 6 1
INPUT:
car truck 8 4 bus 6 1

OUTPUT:
{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

Output:

{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

Improving code on sorting string with certain restriction


 

I am trying to find comments on improve the code or looking for a different approach to the problem if there is a better one.

Currently, what I am doing is splitting the input string on spaces, putting the words and integers into their own lists, sorting them appropriately by mapping to a different list, and then injecting the sorted words/integers back into the original input list.

Does anyone have any comments?


 
INPUT:
car truck 8 4 bus 6 1

OUTPUT:
{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

Sorting strings with certain restrictions

Currently, what I am doing is splitting the input string on spaces, putting the words and integers into their own lists, sorting them appropriately by mapping to a different list, and then injecting the sorted words/integers back into the original input list.

I am trying to find comments on improve the code or looking for a different approach to the problem if there is a better one. Does anyone have any comments?

Input:

car truck 8 4 bus 6 1

Output:

{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
edited title
Link
Ozera
  • 175
  • 6

Comment Improving code on improving or different approachsorting string with certain restriction

Source Link
Ozera
  • 175
  • 6

Comment on improving or different approach

I want to sort a string which contains words and integers with the restriction that if the nth element in the list is an integer it must remain an integer and if it is a word, it must remain a word.


I am trying to find comments on improve the code or looking for a different approach to the problem if there is a better one.

Currently, what I am doing is splitting the input string on spaces, putting the words and integers into their own lists, sorting them appropriately by mapping to a different list, and then injecting the sorted words/integers back into the original input list.

Does anyone have any comments?

http://ideone.com/X2pPfg

justWords   = {}
words       = []

justNums    = {}
nums        = []

# Check if value in the input is an integer or a word
def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

# Replace the values in the dictionary with the correctly sorted words/integers       
def go_through(theDict, theList):
    counter = 0
    for k,v in theDict.iteritems():
        theDict[k]   = theList[counter]
        counter      = counter + 1
    return theDict

# Replace the values in the original input 
 # list with correctly sorted values of the word/int dicts
def inject(theDict, theList):
    for k,v in theDict.iteritems():
        theList[k] = v
    return theList
    
if __name__ == "__main__":
    
    splitInput  = (raw_input("")).split()
    
    # Sort the words and numbers into their own lists as tuples
    for i,j in enumerate(splitInput):
        if is_number(j):
            justNums[i]  = j
            nums.append(j)
        elif not is_number(j):
            justWords[i] = j
            words.append(j)
        
    print("%s\n%s\n" % (justWords, justNums))

    words = sorted(words)
    nums  = sorted(nums)

    print("%s\n%s\n" % (words, nums))

    # Replace the values in the dictionaries with the values in the sorted list
    justWords = go_through(justWords, words)
    justNums  = go_through(justNums, nums)

    print("%s\n%s\n" % (justWords, justNums))
    
    # Inject correctly maped sorted words into the original list
    splitInput = inject(justWords, splitInput)
    splitInput = inject(justNums, splitInput)

    print("%s" % (' '.join(splitInput)))

INPUT:
car truck 8 4 bus 6 1

OUTPUT:
{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