Skip to content

Commit f401a42

Browse files
committed
Update baffling-birthdays/.meta/example.sh
Co-authored-by: IsaacG
1 parent 933e642 commit f401a42

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

exercises/practice/baffling-birthdays/.meta/example.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ sharedBirthday () {
1313
for birthdate in "${birthdates[@]}"; do
1414
birthday=${birthdate: -5}
1515

16-
(( birthdays[$birthday] ++ ))
16+
(( birthdays[$birthday]++ ))
1717
done
1818

1919
if (( ${#birthdays[@]} == ${#birthdates[@]} )); then
@@ -59,18 +59,14 @@ estimatedProbabilityOfSharedBirthday () {
5959
# but the more you increase the computing time!
6060
local -i runs=1000
6161
local -i count=0
62-
local result
63-
6462

6563
for ((i = 0; i <= runs; i++)); do
6664
if [[ $(sharedBirthday $(randomBirthdates $group_size)) == "true" ]]; then
6765
(( ++ count ))
6866
fi
6967
done
7068

71-
result="$(LC_NUMERIC=C printf "%.2f\n" "$(echo "scale=2; ($count * 100) / $runs" | bc)")"
72-
73-
echo "$result"
69+
bc <<< "scale=2; $count * 100 / $runs"
7470
}
7571

7672
main "$@"

exercises/practice/baffling-birthdays/baffling_birthdays.bats

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ load bats-extra
9292
@test "random birthdates -> generate requested number of birthdates" {
9393
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
9494
generate=$(shuf -n 1 -i 100-1000)
95-
run bash baffling_birthdays.sh randomBirthdates $generate
95+
run bash baffling_birthdays.sh randomBirthdates "$generate"
9696
assert_success
9797
read -ra output_dates <<< "$output"
98-
assert_equal ${#output_dates[@]} $generate
98+
assert_equal ${#output_dates[@]} "$generate"
9999
}
100100

101101
@test "random birthdates -> years are not leap years" {
102102
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
103103
generate=$(shuf -n 1 -i 100-1000)
104104
seen_leap=0
105-
run bash baffling_birthdays.sh randomBirthdates $generate
105+
run bash baffling_birthdays.sh randomBirthdates "$generate"
106106
assert_success
107107
read -ra output_dates <<< "$output"
108108
for date in "${output_dates[@]}"; do
@@ -111,14 +111,14 @@ load bats-extra
111111
(( ++ seen_leap ))
112112
fi
113113
done
114-
assert_equal $seen_leap 0
114+
assert_equal ${seen_leap} 0
115115
}
116116

117117
@test "random birthdates -> months are random" {
118118
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
119119
declare -A seen_month
120120
generate=$(shuf -n 1 -i 100-1000)
121-
run bash baffling_birthdays.sh randomBirthdates $generate
121+
run bash baffling_birthdays.sh randomBirthdates "$generate"
122122
assert_success
123123
read -ra output_dates <<< "$output"
124124
for date in "${output_dates[@]}"; do
@@ -133,7 +133,7 @@ load bats-extra
133133
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
134134
declare -A seen_day
135135
generate=$(shuf -n 1 -i 100-1000)
136-
run bash baffling_birthdays.sh randomBirthdates $generate
136+
run bash baffling_birthdays.sh randomBirthdates "$generate"
137137
assert_success
138138
read -ra output_dates <<< "$output"
139139
for date in "${output_dates[@]}"; do
@@ -150,29 +150,29 @@ load bats-extra
150150
# expect="0.0" # this is the exercice ref.
151151
run bash baffling_birthdays.sh estimatedProbabilityOfSharedBirthday 1
152152
assert_success
153-
[[ "$output" =~ ^(0\.) ]]
153+
assert_equal "$output" 0
154154
}
155155

156156
@test "estimated probability of at least one shared birthday -> among ten people" {
157157
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
158158
# expect="11.694818" # this is the exercice ref.
159159
run bash baffling_birthdays.sh estimatedProbabilityOfSharedBirthday 10
160160
assert_success
161-
[[ "$output" =~ ^(10\.|11\.|12\.|13\.|14\.) ]]
161+
[[ $output =~ ^(10\.|11\.|12\.|13\.|14\.) ]]
162162
}
163163

164164
@test "estimated probability of at least one shared birthday -> among twenty-three people" {
165165
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
166166
# expect="50.729723" # this is the exercice ref.
167167
run bash baffling_birthdays.sh estimatedProbabilityOfSharedBirthday 23
168168
assert_success
169-
[[ "$output" =~ ^(48\.|49\.|50\.|51\.|52\.|53\.) ]]
169+
[[ $output =~ ^(48\.|49\.|50\.|51\.|52\.|53\.) ]]
170170
}
171171

172172
@test "estimated probability of at least one shared birthday -> among seventy people" {
173173
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
174174
# expect="99.915958" # this is the exercice ref.
175175
run bash baffling_birthdays.sh estimatedProbabilityOfSharedBirthday 70
176176
assert_success
177-
[[ "$output" =~ ^(98\.|99\.|100\.) ]]
177+
[[ $output =~ ^(98\.|99\.|100\.) ]]
178178
}

0 commit comments

Comments
 (0)