0

I need to print a list of words inputed by the user from the command line. Now, the alplhabetical order works fine but when I print in reverse it does not come out right. I tried a lot of things and I am now out of ideas. Anyone? Here is the code:

import argparse

argparser = argparse.ArgumentParser()
argparser.add_argument("user_string", help = 'User string')
argparser.add_argument("--reverse", "-r", action="store_true", dest="reverseflag")
args = argparser.parse_args()
user_string = args.user_string
words_to_sort = user_string.split()
if len(words_to_sort) < 2:
        args.user_string
        # alerts user to input more than 1 word
        print("Invalid command line arguments to program. Please, supply two or more strings to sort.")

if len(words_to_sort) > 1 and (args.reverseflag == True):
        words_to_sort = sorted(args.user_string, reverse=True)
        print(*words_to_sort)
else:
        words_to_sort.sort()
        for word in words_to_sort:
            print(word)

this is what I get from the command line:

PS C:\Users\desktop\folder> python mysort.py --reverse "all mall ball"
m l l l l l l b a a a
PS C:\Users\desktop\folder> python mysort.py "all mall ball"
all
ball
mall

The reverse should just reverse the array from z to a but sadly it isn't the case.

brokenfoot
  • 11,083
  • 10
  • 59
  • 80
  • **Beware if you (accidentally) call `sorted()` on a string but think it's a list, you get a list back, not a string**: `sorted("abcd", reverse=True)` gives `['d', 'c', 'b', 'a']` not `"dcba"` [What is the difference between `sorted(list)` vs `list.sort()`?](https://stackoverflow.com/questions/22442378/what-is-the-difference-between-sortedlist-vs-list-sort) – smci Sep 23 '19 at 21:37

4 Answers4

5

If reverse is True, your code is treating user input as one giant string:

words_to_sort = sorted(args.user_string, reverse=True)

instead you need to pass in list of strings, which is words_to_sort:

words_to_sort = sorted(words_to_sort, reverse=True)
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
3

Sorted takes an iterable. In your case, it splits up the string to its characters. Perhaps, you should consider this: sorted(text.split(), reverse=True)

as133
  • 31
  • 2
3

You're sorting args.user_string for some reason. You should only be sorting words_to_sort. Because args.reverseflag is already a Boolean, you can simply pass it so .sort. Get rid of your whole if-else block and replace it with

words_to_sort.sort(reverse=args.reverseflag)
for word in words_to_sort:
    print(word)
iz_
  • 15,923
  • 3
  • 25
  • 40
1

You're sorting the input string rather than the list of words

if len(words_to_sort) > 1 and (args.reverseflag == True):
    words_to_sort = sorted(args.user_string, reverse=True)
    print(*words_to_sort)

Sort words_to_sort instead

if len(words_to_sort) > 1 and (args.reverseflag == True):
    words_to_sort = sorted(words_to_sort, reverse=True)
    print(*words_to_sort)

You could actually sort it before the if/else and just return the list reversed

args = argparser.parse_args()
user_string = args.user_string
words = sorted(user_string.split())
if len(words) < 2:
    args.user_string
    # alerts user to input more than 1 word
    print("Invalid command line arguments to program. Please, supply two or more strings to sort.")

if args.reverseflag:
    print(*words[::-1])
else:
    print(*words)
Mikey Lockwood
  • 1,258
  • 1
  • 9
  • 22