Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.6 kB
2
Indexable
Never
# A i have done, this is my code

A.Write a function that you will call MinDist() that takes as input a list of integers and returns
the minimum (absolute) difference between any two numbers in that list.
For example, MinDist([5,9,1,3]) should return 2
While, MinDist([3,4,1,1]) should return 0

def MinDist():
    #Input of numbers in integer format,allowing for multiple input for a list and splitting them
    int_list=list(map(int, input("Enter a list of integers separated by space: ").split()))
    #sorting list in ascending order
    int_list.sort()
    #setting the number of checks of differences
    numchecks=len(int_list) -1
    i=0
    # setting a base difference value
    min_diff=abs(int_list[i]-int_list[i+1])
    # one check has been done so fixing accordingly
    i=1
    
    while i<numchecks:
        #absolute difference beween next numbers
        diff=abs(int_list[i]-int_list[i+1])
        #updates smallest difference if old difference is greater than new difference
        if min_diff>diff:
            min_diff=diff
        #
        i+=1
    print( min_diff)  
    
MinDist()


#This bit idk, have so many ways to do and its bugging me 
B. Using the MinDist() function you have created, create another function MaxMinDist() that
takes a list of lists of integers as an input, and it returns the maximum value among all the
minimum distance values calculated on the inner-lists (output of MinDist() for each innerlist).
For example, MaxMinDist([[5,9,1,3],[3,4,1,1]]) should return 2