-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistributedSystem1.py
More file actions
132 lines (90 loc) · 3.7 KB
/
Copy pathDistributedSystem1.py
File metadata and controls
132 lines (90 loc) · 3.7 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os, random, time
def Distribute_Fairly(intAmount, lstUnits):
'''
/// Distribute_Fairly()--function to distributed fairly the intAmount to lstUnits items.
/// Parameters:
/// intAmount: main amount number
/// lstUnits: List of units
/// ADDED by Engineer B.Pourtavakoli on 1403/09/13
'''
lstUnits = sorted(lstUnits) # مرتبسازی به ترتیب صعودی (برای توزیع بهتر به واحدهای کوچکتر)
result = {unit: 0 for unit in lstUnits}
intAmount2 = intAmount
while (intAmount2 > 0):
for unit in lstUnits:
if (intAmount2 >= unit):
result[unit] += 1
intAmount2 -= unit
# بررسی جمع نهایی
#total = sum(unit * count for unit, count in result.items())
#if (total != intAmount2):
#print(f"Total: {total}, Main amount: {intAmount2}, Difference: {intAmount2 - total}")
# raise ValueError(f"The total ({total}) is not equal to the original value!")
return (result)
def Distribute_Randomly1(intAmount, lstUnits):
'''
/// Distribute_Randomly1()--function to distributed randomly the intAmount to lstUnits items as descending sort.
/// Parameters:
/// intAmount: main amount number
/// lstUnits: List of units
/// ADDED by Engineer B.Pourtavakoli on 1403/09/13
''' # مرتبسازی واحدها به ترتیب نزولی
lstUnits = sorted(lstUnits, reverse=True)
result = { unit: 0 for unit in lstUnits }
while (intAmount > 0):
# انتخاب تصادفی یک واحد
unit = random.choice(lstUnits)
# اگر عدد اصلی بزرگتر یا مساوی واحد باشد، کاهش میدهیم
if intAmount >= unit:
result[unit] += 1
intAmount -= unit
return (result)
def Distribute_Randomly2(intAmount, lstUnits):
'''
/// Distribute_Randomly2()--function to distributed randomly the intAmount to lstUnits items as descending sort.
/// Parameters:
/// intAmount: main amount number
/// lstUnits: List of units
/// ADDED by Engineer B.Pourtavakoli on 1403/09/13
'''
lstUnits = sorted(lstUnits, reverse=True)
result = { unit: 0 for unit in lstUnits }
while (intAmount > 0):
unit = random.choice(lstUnits)
if intAmount >= unit:
result[unit] += 1
intAmount -= unit
return (result)
#####################################################################################################
# Main Program
dtStartTime = time.time()
if (os.name == "nt"):
_ = os.system("cls")
intAmount = 1000000
lstUnits = [1, 2, 5, 10, 20, 50, 100]
# Part 1
distribution = Distribute_Fairly(intAmount, lstUnits)
print("Fair Distribution:")
for unit, count in distribution.items():
print(f"{unit} $: {count} $")
total = sum(unit * count for unit, count in distribution.items())
print(f"\nTotal: {total}\n")
# Part 2
distribution = Distribute_Randomly1(intAmount, lstUnits)
print("Fair randomly Distribution 1:")
for unit, count in distribution.items():
# print(f"{unit} $: {count} $")
print(f"{unit}: {count}")
total = sum(unit * count for unit, count in distribution.items())
print(f"\nTotal: {total}")
# Part 3
distribution = Distribute_Randomly2(intAmount, lstUnits)
print("Fair randomly Distribution 2:")
for unit, count in distribution.items():
# print(f"{unit} $: {count} $")
print(f"{unit}: {count}")
total = sum(unit * count for unit, count in distribution.items())
print(f"\nTotal: {total}")
dtEndTime = time.time()
dtElapsedTime = dtEndTime - dtStartTime
print("Elapsed time:: %s ms" % round(dtElapsedTime, 3))