-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample1.py
44 lines (37 loc) · 1.55 KB
/
sample1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import fileinput
def inputNumbers():
print("Please input your numbers, separated by commas (and spaces): ")
numberListString = iter(fileinput.input()).__next__()
numberListStringComponents = numberListString.split(",")
numberList = list(map(int, numberListStringComponents))
return numberList
def computeNumberOfPairs(numberList):
numberOfElements = len(numberList)
return numberOfElements * (numberOfElements - 1) / 2
def mapIncreasingPairTo1(numberTuple):
if numberTuple[0] < numberTuple[1]:
return 1
return 0
def generatePairList(numberList):
pairs = []
numberOfElements = len(numberList)
for indexLeft in range(0, numberOfElements):
for indexRight in range(indexLeft + 1, numberOfElements):
numberLeft = numberList[indexLeft]
numberRight = numberList[indexRight]
pairs.append((numberLeft, numberRight))
return pairs
def computeNumberOfIncreasingPairs(numberList):
pairs = generatePairList(numberList)
counts = map(mapIncreasingPairTo1, pairs)
numberOfIncreasingPairs = sum(counts)
return numberOfIncreasingPairs
def computeIncreasingPairRatio(numberList):
numberOfIncreasingPairs = computeNumberOfIncreasingPairs(numberList)
numberOfAllPairs = computeNumberOfPairs(numberList)
increasingPairRatio = float(numberOfIncreasingPairs) / float(numberOfAllPairs)
return increasingPairRatio
if __name__=="__main__":
numberList = inputNumbers()
increasingPairRatio = computeIncreasingPairRatio(numberList)
print(str(increasingPairRatio))