Skip to content

Commit ccc55d4

Browse files
committed
Update example.sh and baffling_birthdays.bats
* Co-authored-by: IsaacG * Co-authored-by: glennj
1 parent 933e642 commit ccc55d4

3 files changed

Lines changed: 67 additions & 65 deletions

File tree

config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,10 @@
661661
"uuid": "c3fbce6a-61ce-439c-8c67-fad93561326f",
662662
"practices": [],
663663
"prerequisites": [],
664-
"difficulty": 4
664+
"difficulty": 4,
665+
"topics": [
666+
"dates"
667+
]
665668
},
666669
{
667670
"slug": "binary-search",
Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
#!/usr/bin/env bash
22

3+
current_year=$(date -d "now" +%Y)
4+
35
main() {
46
local function=$1; shift
57

6-
$function "$@"
8+
"$function" "$@"
79
}
810

9-
sharedBirthday () {
11+
shared_birthday () {
1012
local -a birthdates=( "$@" )
1113
local -A birthdays
1214

1315
for birthdate in "${birthdates[@]}"; do
1416
birthday=${birthdate: -5}
1517

16-
(( birthdays[$birthday] ++ ))
18+
(( birthdays[$birthday]++ ))
1719
done
1820

1921
if (( ${#birthdays[@]} == ${#birthdates[@]} )); then
@@ -23,54 +25,51 @@ sharedBirthday () {
2325
fi
2426
}
2527

26-
randomBirthdates () {
28+
random_int() {
29+
echo $(( RANDOM % $1 ))
30+
}
31+
32+
random_birthdates () {
2733
local -i number=$1
28-
local -i actual_year random_year random_month random_day
34+
local -i random_year random_month random_day
2935
local -a birthdates
3036

31-
actual_year=$(date -d "now" +%Y)
32-
33-
for ((i = 0; i < number; i++)); do
34-
random_year=$(shuf -n 1 -i 1900-${actual_year})
35-
random_month=$(shuf -n 1 -i 1-12)
37+
for (( i = 0; i < number; i++ )); do
38+
random_year=$(( 1900 + $(random_int $(( current_year - 1900 + 1 ))) ))
39+
random_month=$(( 1 + $(random_int 12) ))
3640

3741
if (( random_year % 400 == 0 || (random_year % 4 == 0 && random_year % 100 != 0) )); then
3842
(( random_year ++ ))
3943
fi
4044

41-
if (( random_month == 2 )); then
42-
random_day=$(shuf -n 1 -i 1-28)
43-
elif [[ $random_month =~ ^(4|6|9|11)$ ]]; then
44-
random_day=$(shuf -n 1 -i 1-30)
45-
else
46-
random_day=$(shuf -n 1 -i 1-31)
47-
fi
45+
case "$random_month" in
46+
2) random_day=$(( 1 + $(random_int 28) )) ;;
47+
4|6|9|11) random_day=$(( 1 + $(random_int 30) )) ;;
48+
*) random_day=$(( 1 + $(random_int 31) )) ;;
49+
esac
4850

49-
birthdates+=( "$(date -d "${random_year}-${random_month}-${random_day}" +%F)" )
51+
birthdates+=( "$(printf '%d-%02d-%02d' "$random_year" "$random_month" "$random_day")" )
5052
done
5153

5254
echo "${birthdates[@]}"
5355
}
5456

55-
estimatedProbabilityOfSharedBirthday () {
57+
estimated_probability_of_share_birthday () {
5658
local -i group_size=$1
57-
# the more you increase the 'runs' value,
59+
# the more you increase the 'runs' value,
5860
# the more the result is approching the expected one
5961
# but the more you increase the computing time!
60-
local -i runs=1000
62+
local -i runs=2000
6163
local -i count=0
62-
local result
63-
6464

65-
for ((i = 0; i <= runs; i++)); do
66-
if [[ $(sharedBirthday $(randomBirthdates $group_size)) == "true" ]]; then
67-
(( ++ count ))
65+
for (( i = 0; i <= runs; i++ )); do
66+
read -ra birthdates < <(random_birthdates "$group_size")
67+
if [[ "$(shared_birthday "${birthdates[@]}")" == "true" ]]; then
68+
(( count++ ))
6869
fi
6970
done
7071

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

7675
main "$@"

exercises/practice/baffling-birthdays/baffling_birthdays.bats

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,47 @@ load bats-extra
44
@test "shared birthday -> one birthdate" {
55
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip
66
birthdates=("2000-01-01")
7-
run bash baffling_birthdays.sh sharedBirthday "${birthdates[@]}"
7+
run bash baffling_birthdays.sh shared_birthday "${birthdates[@]}"
88
assert_success
99
assert_output "false"
1010
}
1111

1212
@test "shared birthday -> two birthdates with same year, month, and day" {
1313
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
1414
birthdates=("2000-01-01" "2000-01-01")
15-
run bash baffling_birthdays.sh sharedBirthday "${birthdates[@]}"
15+
run bash baffling_birthdays.sh shared_birthday "${birthdates[@]}"
1616
assert_success
1717
assert_output "true"
1818
}
1919

2020
@test "shared birthday -> two birthdates with same year and month, but different day" {
2121
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
2222
birthdates=("2012-05-09" "2012-05-17")
23-
run bash baffling_birthdays.sh sharedBirthday "${birthdates[@]}"
23+
run bash baffling_birthdays.sh shared_birthday "${birthdates[@]}"
2424
assert_success
2525
assert_output "false"
2626
}
2727

2828
@test "shared birthday -> two birthdates with same month and day, but different year" {
2929
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
3030
birthdates=("1999-10-23" "1988-10-23")
31-
run bash baffling_birthdays.sh sharedBirthday "${birthdates[@]}"
31+
run bash baffling_birthdays.sh shared_birthday "${birthdates[@]}"
3232
assert_success
3333
assert_output "true"
3434
}
3535

3636
@test "shared birthday -> two birthdates with same year, but different month and day" {
3737
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
3838
birthdates=("2007-12-19" "2007-04-27")
39-
run bash baffling_birthdays.sh sharedBirthday "${birthdates[@]}"
39+
run bash baffling_birthdays.sh shared_birthday "${birthdates[@]}"
4040
assert_success
4141
assert_output "false"
4242
}
4343

4444
@test "shared birthday -> two birthdates with different year, month, and day" {
4545
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
4646
birthdates=("1997-08-04" "1963-11-23")
47-
run bash baffling_birthdays.sh sharedBirthday "${birthdates[@]}"
47+
run bash baffling_birthdays.sh shared_birthday "${birthdates[@]}"
4848
assert_success
4949
assert_output "false"
5050
}
@@ -57,7 +57,7 @@ load bats-extra
5757
"2001-12-25"
5858
"1980-11-10"
5959
)
60-
run bash baffling_birthdays.sh sharedBirthday "${birthdates[@]}"
60+
run bash baffling_birthdays.sh shared_birthday "${birthdates[@]}"
6161
assert_success
6262
assert_output "false"
6363
}
@@ -70,7 +70,7 @@ load bats-extra
7070
"2001-07-29"
7171
"1980-11-10"
7272
)
73-
run bash baffling_birthdays.sh sharedBirthday "${birthdates[@]}"
73+
run bash baffling_birthdays.sh shared_birthday "${birthdates[@]}"
7474
assert_success
7575
assert_output "true"
7676
}
@@ -84,95 +84,95 @@ load bats-extra
8484
"1980-07-29"
8585
"2019-02-12"
8686
)
87-
run bash baffling_birthdays.sh sharedBirthday "${birthdates[@]}"
87+
run bash baffling_birthdays.sh shared_birthday "${birthdates[@]}"
8888
assert_success
8989
assert_output "true"
9090
}
9191

9292
@test "random birthdates -> generate requested number of birthdates" {
9393
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
94-
generate=$(shuf -n 1 -i 100-1000)
95-
run bash baffling_birthdays.sh randomBirthdates $generate
94+
generate=500
95+
run bash baffling_birthdays.sh random_birthdates "$generate"
9696
assert_success
97-
read -ra output_dates <<< "$output"
98-
assert_equal ${#output_dates[@]} $generate
97+
num_output_dates=$( wc -w <<< "$output" )
98+
assert_equal "$num_output_dates" "$generate"
9999
}
100100

101101
@test "random birthdates -> years are not leap years" {
102102
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
103-
generate=$(shuf -n 1 -i 100-1000)
103+
generate=500
104104
seen_leap=0
105-
run bash baffling_birthdays.sh randomBirthdates $generate
105+
run bash baffling_birthdays.sh random_birthdates "$generate"
106106
assert_success
107107
read -ra output_dates <<< "$output"
108108
for date in "${output_dates[@]}"; do
109109
year=${date:0:4}
110110
if (( year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) )); then
111-
(( ++ seen_leap ))
111+
(( ++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
120-
generate=$(shuf -n 1 -i 100-1000)
121-
run bash baffling_birthdays.sh randomBirthdates $generate
120+
generate=500
121+
run bash baffling_birthdays.sh random_birthdates "$generate"
122122
assert_success
123123
read -ra output_dates <<< "$output"
124124
for date in "${output_dates[@]}"; do
125125
month=${date:5:2}
126126

127127
seen_month[$month]=true
128128
done
129-
assert_equal ${#seen_month[@]} 12
129+
assert_equal "${#seen_month[@]}" 12
130130
}
131131

132132
@test "random birthdates -> days are random" {
133133
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
134134
declare -A seen_day
135-
generate=$(shuf -n 1 -i 100-1000)
136-
run bash baffling_birthdays.sh randomBirthdates $generate
135+
generate=500
136+
run bash baffling_birthdays.sh random_birthdates "$generate"
137137
assert_success
138138
read -ra output_dates <<< "$output"
139139
for date in "${output_dates[@]}"; do
140140
day=${date:8}
141141

142142
seen_day[$day]=true
143143
done
144-
assert_equal ${#seen_day[@]} 31
144+
assert_equal "${#seen_day[@]}" 31
145145
}
146146

147147
# "The expected probability values should be compared using some tolerance to allow for small deviations."
148148
@test "estimated probability of at least one shared birthday -> for one person" {
149149
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
150-
# expect="0.0" # this is the exercice ref.
151-
run bash baffling_birthdays.sh estimatedProbabilityOfSharedBirthday 1
150+
# The expected probability is 0.0
151+
run bash baffling_birthdays.sh estimated_probability_of_share_birthday 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
158-
# expect="11.694818" # this is the exercice ref.
159-
run bash baffling_birthdays.sh estimatedProbabilityOfSharedBirthday 10
158+
# The expected probability is 11.694818
159+
run bash baffling_birthdays.sh estimated_probability_of_share_birthday 10
160160
assert_success
161-
[[ "$output" =~ ^(10\.|11\.|12\.|13\.|14\.) ]]
161+
(( output >= 10 && output <= 13 ))
162162
}
163163

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

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

0 commit comments

Comments
 (0)