Skip to content Skip to sidebar Skip to footer

Command Line Input, Extreme, And Map Patterns Python

I'm doing work for my intro level CS class and I'm not really sure how to solve this task. Here it is: Create a program that takes command line input to obtain the data and proc

Solution 1:

Your list's internal representation is in fact a list of lists, looking like this: ['4', ['2', ['5', ['1', ['0', None]]]]]. You should use the head and tail functions from the List.py module to get the first element of the list:

while numbers:
    num, numbers = head(numbers), tail(numbers)
    if num < smallest:
        smallest = num

Note that given this implementation of a list, a recursive implementation might be more natural:

def get_smallest(lst):
    h, t = head(lst), tail(lst)
    return min(h, get_smallest(t)) if t else h
smallest = get_smallest(numbers)

Solution 2:

It's a shame you have to use your instructor's strangely spelled functions (functions are supposed to be lowercase according to the ubiquitous PEP8 Style Guide in Python).

You could complete this whole assignment in 3 lines of Python 3:

# command.pyimport sys
if __name__ == '__main__':
    print(min(map(int, sys.argv[1:])))

Solution 3:

You don't need to search for the smallest value manually, you can just do this:

smallest = sorted(numbers)[0]

I am not sure what the numbers type is, but if its a List this will work. Otherwise, you can specify a sorting key like this:

smallest = sorted(numbers, key=some_key)[0]

The key is a method of comparation of elements.

Post a Comment for "Command Line Input, Extreme, And Map Patterns Python"