Skip to content

Clock #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 27, 2025
Merged

Clock #304

merged 8 commits into from
May 27, 2025

Conversation

rabestro
Copy link
Member

Closes #303

Introduce comprehensive Bats tests to verify the clock's functionality, including time calculations, addition, subtraction, and comparisons. Also, add the required Bats support and assertion library for testing utilities.
@rabestro rabestro requested review from glennj and IsaacG May 23, 2025 18:27
@rabestro rabestro self-assigned this May 23, 2025
The "largest-product" and "binary-search-tree" exercises were removed as they are no longer in use. This reduces clutter and ensures the configuration file stays lean and focused on current content.
Copy link
Member

@IsaacG IsaacG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if there might be a more structured approach to the input data.
+cc @glennj for thoughts on that.

Jegors Cemisovs added 2 commits May 24, 2025 08:57
Added a newline at the end of the file to adhere to standard POSIX file formatting conventions. This helps maintain consistency and avoids potential issues with tools expecting a newline at EOF.
Added a newline at the end of the file to adhere to POSIX standards and maintain consistency with other files. This change improves compatibility and avoids potential issues with tools expecting proper file formatting.
@rabestro
Copy link
Member Author

Hi colleagues,

How about to add .editorconfig file to the repo root to prevent issues with a newline ending?
https://editorconfig.org/

@rabestro
Copy link
Member Author

rabestro commented May 24, 2025

I'm wondering if there might be a more structured approach to the input data. +cc @glennj for thoughts on that.

In Bash track equals is not used at all.

Since subtract and add operations use only positive numbers the other approach is to not use keywords at all.

  1. Create - we use two numbers
  2. Add/Subtract - we use three numbers with +/- for each
  3. Equals - we use four numbers

In the program we can use only NF to distinguish between the different cases:

BEGIN {
    MinutesInHour = 60
    MinutesInDay = MinutesInHour * 24
}
NF == 2 {
    printClock(clock($1, $2))
}
NF == 3 {
    printClock(clock($1, $2 + $3))
}
NF == 4 {
    print clock($1, $2) == clock($3, $4) ? "true" : "false"
}
function clock(hours, minutes) {
    return ((hours * MinutesInHour + minutes) % MinutesInDay + MinutesInDay) % MinutesInDay
}
function printClock(minutes) {
    printf "%02d:%02d\n", int(minutes / MinutesInHour), minutes % MinutesInHour
}

@rabestro rabestro requested review from IsaacG and kotp May 24, 2025 08:48
@IsaacG
Copy link
Member

IsaacG commented May 24, 2025

In the program we can use only NF to distinguish between the different cases:

I'm thinking more along the lines of not having to depend on NF or empty fields. What if $1 always contained the operation?

@rabestro
Copy link
Member Author

rabestro commented May 24, 2025

In the program we can use only NF to distinguish between the different cases:

I'm thinking more along the lines of not having to depend on NF or empty fields. What if $1 always contained the operation?

This thought also crossed my mind. So, input data can be like:

create 8 15 
create -4 75
add 8 15 45
subtract 23 67 81
equals 21 15 45 15

$1 like a function or property name.

Compare with the current approach:

8 15 
-4 75
8 15 add 45
23 67 subtract 81
21 15 equals 45 15

50%/50% for me :)

@glennj, @kotp what do you think about this?

@kotp
Copy link
Member

kotp commented May 24, 2025

In the program we can use only NF to distinguish between the different cases:

@glennj, @kotp what do you think about this?

I like the add clock time time pattern. It is easy to remember. But the operator in the middle makes it easier to "normalize" inputs, in case there are only minutes given and not hours before or after that operator, rather than both hour and minute values given for one or both.

Do what you want to show and teach.

@IsaacG
Copy link
Member

IsaacG commented May 25, 2025

I like the add clock time time pattern. It is easy to remember. But the operator in the middle makes it easier to "normalize" inputs, in case there are only minutes given and not hours before or after that operator, rather than both hour and minute values given for one or both.

Do what you want to show and teach.

We could also do something like have multi-line programs, though that breaks with the standard paradigm.

add
HH MM
MM

The add line could indicate that two lines should be read.

@rabestro
Copy link
Member Author

I like the add clock time time pattern. It is easy to remember. But the operator in the middle makes it easier to "normalize" inputs, in case there are only minutes given and not hours before or after that operator, rather than both hour and minute values given for one or both.
Do what you want to show and teach.

We could also do something like have multi-line programs, though that breaks with the standard paradigm.

add
HH MM
MM

The add line could indicate that two lines should be read.

The AWK for processing records and fields.
If we Introduce some specific format, this will increase the complexity of easy exercise.

Three options are equal from my point of view:

  1. The current implementation
  2. The property as the first field and arguments in the next fields
  3. Without property names, just numeric fields.

Let's keep this exercise simple.

@kotp
Copy link
Member

kotp commented May 25, 2025

The add line could indicate that two lines should be read.

The "two lines" being "two records" and since we are already processing a variable number of fields in the records, this may not increase complexity, really. But think of lines (which may be the case, but the idea is fields and records) and that changes the problem.

Let's keep this exercise simple. - @rabestro

Let's add the minimum complexity necessary.

There is no reason that this exercise is inherently simple and must be constrained to that, but also, let's add only the minimal possible to make it "good".

We have already introduced variable fields in a record, by suggestion of

create 8 15 
create -4 75
add 8 15 45
subtract 23 67 81
equals 21 15 45 15

The data suggested is adding complexity since there is no indication that the number of fields is fixed. So we must figure out what happens if we have only 3, what if there are 4, what must happen if there is 5? The 5 is the simplest case, really, as there is no less doubt as to what represents 0 for minutes or is that hours, and for which time?"

@rabestro
Copy link
Member Author

rabestro commented May 26, 2025

Hi team,

Do we agree on this format:

create 8 15 
create -4 75
add 8 15 45
subtract 23 67 81
equals 21 15 45 15

The exemplar solution will be:

BEGIN {
    MinutesInHour = 60
    MinutesInDay = MinutesInHour * 24
}
$1 == "create" {
    printClock(clock($2, $3))
}
$1 == "add" {
    printClock(clock($2, $3 + $4))
}
$1 == "subtract" {
    printClock(clock($2, $3 - $4))
}
$1 == "equal" {
    print clock($2, $3) == clock($4, $5) ? "true" : "false"
}
function clock(hours, minutes) {
    return ((hours * MinutesInHour + minutes) % MinutesInDay + MinutesInDay) % MinutesInDay
}
function printClock(minutes) {
    printf "%02d:%02d\n", int(minutes / MinutesInHour), minutes % MinutesInHour
}

@IsaacG, @kotp, @glennj - is it fine?

@glennj
Copy link
Contributor

glennj commented May 26, 2025

Hi folks. I like the command hh mm delta format. I'd suggest adding a detailed comment in the test script, just so that it's clear.

Updated test cases and example.awk to use explicit command keywords (create, add, subtract, equal) as input for better clarity and consistency. This improves readability and aligns the code with a more structured format for future extensibility.
@rabestro
Copy link
Member Author

rabestro commented May 26, 2025

Hi folks. I like the command hh mm delta format. I'd suggest adding a detailed comment in the test script, just so that it's clear.

I've changed the example solution and tests. For generating tests, I'm using a custom Gem (Gemini)

@rabestro rabestro requested review from a team and kotp and removed request for kotp May 26, 2025 18:43
@@ -0,0 +1,4 @@
BEGIN {
print "Implement this solution" > "/dev/stderr"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, I would use "Create this solution" or "Write this solution". The word "implement" means too many things, including having the meaning "Use this solution" which is confusing if this solution does not exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have too many people (like me) that uses a dictionary to figure out what people are saying, and having that many choices to choose from, and seeing people correctly use the word as it means, and in all the ways that this word means, is very confusing. Better to have a "English as a not first language" friendly statement is probably a good thing to aspire to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this exact line in every awk exercise. I'm all for discussing a change, and the change ought to apply to all exercises. But I'd prefer that discussion happen in a dedicated "discuss stub" discussion and not as part of the "implement clock exercise" discussion.

@@ -0,0 +1,4 @@
BEGIN {
print "Implement this solution" > "/dev/stderr"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this exact line in every awk exercise. I'm all for discussing a change, and the change ought to apply to all exercises. But I'd prefer that discussion happen in a dedicated "discuss stub" discussion and not as part of the "implement clock exercise" discussion.

Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com>
rabestro and others added 2 commits May 27, 2025 12:31
Corrected a misplaced closing brace in the config.json file. This fix ensures proper JSON structure and prevents parsing issues.
@rabestro rabestro merged commit 5393a0e into main May 27, 2025
2 checks passed
@rabestro rabestro deleted the clock branch May 27, 2025 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Clock exercise
4 participants