How to resolve the algorithm Cheryl's birthday step by step in the uBasic/4tH programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Cheryl's birthday step by step in the uBasic/4tH 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 uBasic/4tH programming language

Source code in the ubasic/4th programming language

Dim @d(30)
Dim @m(13)

Push 5,15,1, 5,16,1, 5,19,1, 6,17,1 ,6,18,1, 7,14,1, 7,16,1, 8,14,1, 8,15,1, 8,17,1
For x = 29 To 0 Step -1 : @d(x) = Pop() : Next

Push  "ERR", "Jan", "Feb", "Mar", "Apr", "May"
Push  "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
For x = 12 To 0 Step -1 : @m(x) = Pop() : Next

Proc _printRemaining                   ' the month cannot have a unique day
Proc _firstPass

Proc _printRemaining                   ' the day must now be unique
Proc _secondPass

Proc _printRemaining                   ' the month must now be unique
Proc _thirdPass

Proc _printAnswer
End

_printRemaining
  Local (2)
  
  b@ = 0
  For a@ = 0 To 29 Step 3
    If @d(a@+2) Then b@ = b@ + 1
  Next

  Print b@; " remaining."
Return

_printAnswer
  Local (1)

  For a@ = 0 To 29 Step 3
    If @d(a@+2) Then Print Show (@m(@d(a@))); ", "; @d(a@+1)
  Next
Return

_firstPass                             ' the month cannot have a unique day
  Local (3)
  
  For a@ = 0 To 29 Step 3
    c@ = 0
    
    For b@ = 0 To 29 Step 3
      If @d(b@+1) = @d(a@+1) Then c@ = c@ + 1
    Next
    
    If c@ = 1 Then
      For b@ = 0 To 29 Step 3
        If @d(b@+2) = 0 Then
          Continue
        EndIf
      
        If @d(b@) = @d(a@) Then
          @d(b@+2) = 0
        EndIf
      Next
    EndIf
  Next
Return

_secondPass                             ' the day must now be unique
  Local (3)
  
  For a@ = 0 To 29 Step 3
    If @d(a@+2) = 0 Then Continue
    c@ = 0
    
    For b@ = 0 To 29 Step 3
      If @d(b@+2) = 0 Then Continue
      If @d(b@+1) = @d(a@+1) Then c@ = c@ + 1
    Next
    
    If c@ > 1 Then
      For b@ = 0 To 29 Step 3
        If @d(b@+2) = 0 Then
          Continue
        EndIf
      
        If @d(b@+1) = @d(a@+1) Then
          @d(b@+2) = 0
        EndIf
      Next
    EndIf
  Next
Return   

_thirdPass                             ' the month must now be unique
  Local (3)
  
  For a@ = 0 To 29 Step 3
    If @d(a@+2) = 0 Then Continue
    c@ = 0
    
    For b@ = 0 To 29 Step 3
      If @d(b@+2) = 0 Then Continue
      If @d(b@) = @d(a@) Then c@ = c@ + 1
    Next
    
    If c@ > 1 Then
      For b@ = 0 To 29 Step 3
        If @d(b@+2) = 0 Then
          Continue
        EndIf
      
        If @d(b@) = @d(a@) Then
          @d(b@+2) = 0
        EndIf
      Next
    EndIf
  Next
Return

  

You may also check:How to resolve the algorithm Monty Hall problem step by step in the Nim programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Clojure programming language
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the APL programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Wren programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the EchoLisp programming language