-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflipCoinCombination.sh
executable file
·207 lines (179 loc) · 6.28 KB
/
flipCoinCombination.sh
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
print_winner ()
{
#Inside the function, using local -n ref=$1 to declare a nameref to the variable named by $1, meaning it's not a reference to $1 itself, but rather to a variable whose name $1 holds
local -n keys=$1
local -n values=$2
max_value=0
max_value_index=0
for(( i = 0; i < ${#values[@]}; i++ ))
do
if(( values[i] >= max_value ))
then
max_value=${values[i]}
max_value_index=$i
fi
done
echo "The winning combination is ${keys[$max_value_index]} with ${max_value}%"
}
read -p "Enter number of times to flip a combination:" no_of_flips
echo
echo "---------------------------SINGLET-COMBINATION---------------------------"
#for singlet there 2^1=2 possible outcomes
declare -A singlet_frequency=(
[H]=0
[T]=0
)
for(( flip = 1; flip <= no_of_flips; flip++ ))
do
echo -n "Flip-$flip is "
(( coin = RANDOM % 2 ))
case $coin in
0)
echo "Heads"
(( singlet_frequency[H]++ ))
;;
1)
echo "Tails"
(( singlet_frequency[T]++ ))
esac
done
for combination in ${!singlet_frequency[@]}
do
#update count values with corresponding percentage
percentage=$(( ${singlet_frequency[$combination]} * 100 / no_of_flips ))
singlet_frequency[$combination]=$percentage
echo "percentage of $combination is ${singlet_frequency[$combination]}%"
done
echo "---------------------------DOUBLET-COMBINATION---------------------------"
#for doublelet there 2^2=4 possible outcomes
declare -A doublet_frequency=(
[HH]=0
[HT]=0
[TH]=0
[TT]=0
)
for(( flip = 1; flip <= no_of_flips; flip++ ))
do
echo -n "Flip-$flip is "
(( coin_1 = RANDOM % 2 ))
(( coin_2 = RANDOM % 2 ))
#0 is mapped as heads
#1 is mappes as tails
case $coin_1$coin_2 in
00)
echo "Heads Heads"
(( doublet_frequency[HH]++ ))
;;
01)
echo "Heads Tails"
(( doublet_frequency[HT]++ ))
;;
10)
echo "Tails Heads"
(( doublet_frequency[TH]++ ))
;;
11)
echo "Tails Tails"
(( doublet_frequency[TT]++ ))
;;
esac
done
for combination in ${!doublet_frequency[@]}
do
#update count values with corresponding percentage
percentage=$(( ${doublet_frequency[$combination]} * 100 / no_of_flips ))
doublet_frequency[$combination]=$percentage
echo "percentage of $combination is ${doublet_frequency[$combination]}%"
done
echo "---------------------------TRIPLET-COMBINATION---------------------------"
#for triplet there 2^3=8 possible outcomes
declare -A triplet_frequency=(
[HHH]=0
[HHT]=0
[HTH]=0
[HTT]=0
[THH]=0
[THT]=0
[TTH]=0
[TTT]=0
)
for(( flip = 1; flip <= no_of_flips; flip++ ))
do
echo -n "Flip-$flip is "
(( coin_1 = RANDOM % 2 ))
(( coin_2 = RANDOM % 2 ))
(( coin_3 = RANDOM % 2 ))
#0 is mapped as heads
#1 is mappes as tails
case $coin_1$coin_2$coin_3 in
000)
echo "Heads Heads Heads"
(( triplet_frequency[HHH]++ ))
;;
001)
echo "Heads Heads Tails"
(( triplet_frequency[HHT]++ ))
;;
010)
echo "Heads Tails Heads"
(( triplet_frequency[HTH]++ ))
;;
011)
echo "Heads Tails Tails"
(( triplet_frequency[HTT]++ ))
;;
100)
echo "Tails Heads Heads"
(( triplet_frequency[THH]++ ))
;;
101)
echo "Tails Heads Tails"
(( triplet_frequency[THT]++ ))
;;
110)
echo "Tails Tails Heads"
(( triplet_frequency[TTH]++ ))
;;
111)
echo "Tails Tails Tails"
(( triplet_frequency[TTT]++ ))
;;
esac
done
for combination in ${!triplet_frequency[@]}
do
#update count values with corresponding percentage
percentage=$(( ${triplet_frequency[$combination]} * 100 / no_of_flips ))
triplet_frequency[$combination]=$percentage
echo "percentage of $combination is ${triplet_frequency[$combination]}%"
done
#extracting keys and values
singlet_combinations=( ${!singlet_frequency[@]} )
singlet_combination_percentages=( ${singlet_frequency[@]} )
doublet_combinations=( ${!doublet_frequency[@]} )
doublet_combination_percentages=( ${doublet_frequency[@]} )
triplet_combinations=( ${!triplet_frequency[@]} )
triplet_combination_percentages=( ${triplet_frequency[@]} )
#appending all 3 arrays and assigning into other array
total_combinations+=( ${singlet_combinations[@]} ${doublet_combinations[@]} ${triplet_combinations[@]} )
total_combination_percentages+=( ${singlet_combination_percentages[@]} ${doublet_combination_percentages[@]} ${triplet_combination_percentages[@]} )
echo "---------------------------SORTING---------------------------"
echo "sorted singlet percentages:"
echo "$( printf "%s\n" "${singlet_combination_percentages[@]}" | sort -n ) "
echo "sorted doublet percentages:"
echo "$( printf "%s\n" "${doublet_combination_percentages[@]}" | sort -n ) "
echo "sorted triplet percentages:"
echo "$( printf "%s\n" "${triplet_combination_percentages[@]}" | sort -n ) "
echo "---------------------------WINNERS---------------------------"
echo "For Singlet Combination"
print_winner singlet_combinations singlet_combination_percentages
echo
echo "For Doublet Combination"
print_winner doublet_combinations doublet_combination_percentages
echo
echo "For Triplet Combination"
print_winner triplet_combinations triplet_combination_percentages
echo
echo "For All Combinations"
print_winner total_combinations total_combination_percentages