Skip to content

Instantly share code, notes, and snippets.

@RaMSFT
Created January 22, 2022 17:29
Show Gist options
  • Select an option

  • Save RaMSFT/cfaf29354d4285a71bc0afa6441771c0 to your computer and use it in GitHub Desktop.

Select an option

Save RaMSFT/cfaf29354d4285a71bc0afa6441771c0 to your computer and use it in GitHub Desktop.
From a given list of Integers, calculate the percentage (up to 4 decimals) of elements that are positive, negative and zeros
def positive_negative(input_list):
# Write your code here
zeros = input_list.count(0)
total = len(input_list)
if zeros == 0:
input_list.append(0)
input_list.sort()
neg_list = input_list[:input_list.index(0)]
negatives = len(neg_list)
positives = total - negatives - zeros
return f"{(positives/total)*100:7.4f}\n{(negatives/total)*100:7.4f}\n{(zeros/total)*100:7.4f}"
if __name__ == '__main__':
out_put = positive_negative([1, -2, 6, 8, -4, 5, 3])
print(out_put)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment