| 1 |
/* |
| 2 |
* @progname bday_cover.ll |
| 3 |
* @version 1 of 1994-11-02 |
| 4 |
* @author Andrew Deacon |
| 5 |
* @category |
| 6 |
* @output Text |
| 7 |
* @description |
| 8 |
|
| 9 |
A novelty report that lists on which days of the year people were born |
| 10 |
and how many people share the same birthday. All valid birthdays |
| 11 |
are considered. A valid birthday is one where the extracted birthday, |
| 12 |
performed using extractdate(), has a month in the range 1-12 and a day |
| 13 |
within that month. |
| 14 |
|
| 15 |
This program works only with LifeLines. |
| 16 |
|
| 17 |
The output is not sorted. The following are examples of |
| 18 |
how to sort the output using UNIX sort: |
| 19 |
# sort by frequency |
| 20 |
sort +2n +0M bday.out |
| 21 |
sort +2nr +0M bday.out |
| 22 |
# sort by month |
| 23 |
sort -M bday.out |
| 24 |
|
| 25 |
*/ |
| 26 |
|
| 27 |
global(julian) |
| 28 |
global(daysinmonth) |
| 29 |
|
| 30 |
proc main () |
| 31 |
{ |
| 32 |
table(day_counts) |
| 33 |
list(day_list) |
| 34 |
|
| 35 |
/* Formats/modes for date functions */ |
| 36 |
monthformat(3) dayformat(1) dateformat(1) |
| 37 |
set(julian, 0) /* change to use Julian dates */ |
| 38 |
|
| 39 |
/* Initialize counters */ |
| 40 |
set(totaldays, 0) set(totalmonths, 0) set(totalbirths, 0) |
| 41 |
|
| 42 |
/* Iterate over whole database */ |
| 43 |
forindi (indi, num) { |
| 44 |
|
| 45 |
/* if birthday recorded for individual */ |
| 46 |
if (bth, birth(indi)) { |
| 47 |
|
| 48 |
/* Extract birthday for individual */ |
| 49 |
extractdate(bth, birthday, birthmonth, birthyear) |
| 50 |
call get_days_in_month(birthday, birthmonth, birthyear) |
| 51 |
|
| 52 |
/* if valid birthday */ |
| 53 |
if (and(gt(birthday, 0), le(birthday, daysinmonth))) { |
| 54 |
|
| 55 |
/* Extract the month name and day */ |
| 56 |
set(bday, concat(substring(stddate(birth(indi)),1,6)," ")) |
| 57 |
|
| 58 |
/* if existing birthday found - just increment */ |
| 59 |
if(nmatch, lookup(day_counts, bday)) { |
| 60 |
set(nmatch, add(nmatch, 1)) |
| 61 |
} |
| 62 |
/* else new birthday - insert */ |
| 63 |
else { |
| 64 |
set(totaldays, add(totaldays,1)) |
| 65 |
enqueue(day_list, bday) |
| 66 |
set(nmatch, 1) |
| 67 |
} |
| 68 |
insert(day_counts, bday, nmatch) |
| 69 |
set(totalbirths, add(totalbirths,1)) |
| 70 |
|
| 71 |
/* Extract the month name */ |
| 72 |
set(bmon, concat(substring(stddate(birth(indi)),1,4),"**")) |
| 73 |
|
| 74 |
/* if existing birth month found - just increment */ |
| 75 |
if(nmatch, lookup(day_counts, bmon)) { |
| 76 |
set(nmatch, add(nmatch, 1)) |
| 77 |
} |
| 78 |
/* else new birth month - insert */ |
| 79 |
else { |
| 80 |
set(totalmonths, add(totalmonths,1)) |
| 81 |
enqueue(day_list, bmon) |
| 82 |
set(nmatch, 1) |
| 83 |
} |
| 84 |
insert(day_counts, bmon, nmatch) |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/* Write report to file - use Unix sort to sort output! */ |
| 90 |
"Distribution of birth days\n\n" |
| 91 |
"Month & day Frequency\n\n" |
| 92 |
forlist(day_list, bday, num) { |
| 93 |
bday |
| 94 |
set(nmatch, lookup(day_counts, bday)) |
| 95 |
col(sub(25, strlen(d(nmatch)))) |
| 96 |
d(nmatch) "\n" |
| 97 |
} |
| 98 |
"Total birthdays in database: " d(totalbirths) "\n" |
| 99 |
"Total days (out of 366) : " d(totaldays) "\n" |
| 100 |
"Total months (out of 12) : " d(totalmonths) "\n" |
| 101 |
} |
| 102 |
|
| 103 |
proc get_days_in_month(birthday, birthmonth, birthyear) |
| 104 |
{ |
| 105 |
/* code from a routine in "dates" by Jim Eggert */ |
| 106 |
/* procedure sets global variable daysinmonth */ |
| 107 |
set(daysinmonth, 31) |
| 108 |
if (or(le(birthmonth, 0), gt(birthmonth, 12))) |
| 109 |
{ set(daysinmonth, 0) } |
| 110 |
elsif (or(or(eq(birthmonth, 9), eq(birthmonth, 4)), |
| 111 |
or(eq(birthmonth, 6), eq(birthmonth, 11)))) |
| 112 |
{ set(daysinmonth, 30) } |
| 113 |
elsif (eq(birthmonth, 2)) { |
| 114 |
if (and(eq(mod(birthyear, 4), 0), |
| 115 |
or(julian, or(ne(mod(birthyear, 100), 0), |
| 116 |
eq(mod(birthyear, 400), 0))))) |
| 117 |
{ set(daysinmonth, 29) } |
| 118 |
else |
| 119 |
{ set(daysinmonth, 28) } |
| 120 |
} |
| 121 |
else |
| 122 |
{ set(daysinmonth, 31) } |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
|
| 127 |
Sample output: |
| 128 |
|
| 129 |
sorted by sort +2nr +0M sample.output and then edited |
| 130 |
|
| 131 |
Distribution of birthdays |
| 132 |
|
| 133 |
Total birthdays in database: 374 |
| 134 |
Total days (out of 366) : 236 |
| 135 |
Total months (out of 12) : 12 |
| 136 |
|
| 137 |
Month & day Frequency |
| 138 |
|
| 139 |
AUG 12 6 |
| 140 |
SEP 12 5 |
| 141 |
FEB 10 4 |
| 142 |
MAR 03 4 |
| 143 |
APR 12 4 |
| 144 |
JUN 17 4 |
| 145 |
JAN 06 3 |
| 146 |
JAN 18 3 |
| 147 |
JAN 29 3 |
| 148 |
........ |
| 149 |
*/ |
| 150 |
|
| 151 |
/* |
| 152 |
Below is a simple C program hack to check if your values |
| 153 |
are similar to those generated randomly by the program. |
| 154 |
Extract the program from these comment to compile and execute. |
| 155 |
Change the RSEED to do different tests; change the ITERATIONS |
| 156 |
to vary accuracy. Can also change the NUM_DAYS_REQUIRED |
| 157 |
to the value obtained for your database and check if the people |
| 158 |
required is similar. |
| 159 |
|
| 160 |
#define RSEED 1576 |
| 161 |
#define NUM_DAYS 365 |
| 162 |
#define NUM_DAYS_REQUIRED NUM_DAYS |
| 163 |
#define ITERATIONS 2000 |
| 164 |
|
| 165 |
#define FALSE 0 |
| 166 |
#define TRUE 1 |
| 167 |
|
| 168 |
static int days[NUM_DAYS]; |
| 169 |
static int num_got; |
| 170 |
static int running_total; |
| 171 |
|
| 172 |
|
| 173 |
int get_day() { |
| 174 |
return rand() % NUM_DAYS; |
| 175 |
} |
| 176 |
|
| 177 |
do_it() { |
| 178 |
int i; |
| 179 |
int j; |
| 180 |
int r; |
| 181 |
|
| 182 |
for (i = 0; i < NUM_DAYS; i++) { |
| 183 |
days[i] = FALSE; |
| 184 |
} |
| 185 |
num_got = 0; |
| 186 |
i = 0; |
| 187 |
|
| 188 |
while (num_got < NUM_DAYS_REQUIRED) { |
| 189 |
i++; |
| 190 |
r = get_day(); |
| 191 |
if (!days[r]) { |
| 192 |
days[r] = TRUE; |
| 193 |
num_got++; |
| 194 |
} |
| 195 |
} |
| 196 |
printf("Required %d people to cover %d days.\n",i, NUM_DAYS_REQUIRED); |
| 197 |
running_total = running_total + i; |
| 198 |
} |
| 199 |
|
| 200 |
main() { |
| 201 |
int i; |
| 202 |
|
| 203 |
running_total = 0; |
| 204 |
srand(RSEED); |
| 205 |
for (i = 0; i < ITERATIONS; i++) { |
| 206 |
do_it(); |
| 207 |
} |
| 208 |
printf("Average was %d.\n",(int)(running_total/ITERATIONS)); |
| 209 |
} |
| 210 |
|
| 211 |
*/ |