How to resolve the algorithm Cheryl's birthday step by step in the zkl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Cheryl's birthday step by step in the zkl programming language
Table of Contents
Problem Statement
Albert and Bernard just became friends with Cheryl, and they want to know when her birthday is. Cheryl gave them a list of ten possible dates: Cheryl then tells Albert the month of birth, and Bernard the day (of the month) of birth.
Write a computer program to deduce, by successive elimination, Cheryl's birthday.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Cheryl's birthday step by step in the zkl programming language
Source code in the zkl programming language
dates:=T(T("May", 15), T("May", 16), T("May", 19),
T("June", 17), T("June", 18),
T("July", 14), T("July", 16),
T("August",14), T("August",15), T("August",17) );
mDs:=dates.pump(Dictionary().appendKV); // "June":(15,16,19), ...
dMs:=dates.pump(Dictionary().appendKV,"reverse"); // 15:"May", 16:"May", 19:"May", ...
// remove unique days (18,19) --> "July":(14,16),"August":(14,15,17)
dMs.values.apply2('wrap(ms){ if(ms.len()==1) mDs.del(ms[0]) });
// find intersection of above days --> (14)
fcn intersection(l1,l2){ l1.pump(List,l2.holds,'==(True),Void.Filter) }
badDs:=mDs.values.reduce(intersection);
// --> July:(16),August:(15,17) --> ( ("July",(16)) )
theDay:=mDs.filter('wrap([(m,ds)]){ ds.removeEach(badDs).len()==1 });
// print birthday such that muliples are shown, if any
println("Cheryl's birthday is ",theDay.flatten().flatten().concat(" "));
You may also check:How to resolve the algorithm Address of a variable step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the PHP programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Monads/List monad step by step in the AppleScript programming language