-
-
Notifications
You must be signed in to change notification settings - Fork 72
Factory Sensors concept exercise #953
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
Conversation
I can see if I can try to write some instructions and hints to complement this (but that might have to wait for tomorrow). Of course, if that simply isn't enough in the end, we shouldn't hesitate to pull back and go with a simpler implementation, either without |
I've realized that we could put the If we do that, I'd probably rename the functions to something like Anyway... Brain is done for the day! :D |
Good suggestions, I'm happy to change tasks 1 & 2 if it helps task 3. My brain feels like it's done for the month, if not the year. |
Okay, here's an update on what I've got. Let me know if you think this is sufficient, if we should add more instruction/hints, or if we should just simplify Task 4. Notes:
Hinted solution: function humiditycheck(pct_humidity)
pct_humidity > 70 && error("humidity level too high: $pct_humidity%")
@info "humidity level check passed: $pct_humidity%"
end
function temperaturecheck(temperature)
isnothing(temperature) && throw(ArgumentError("sensor is broken"))
temperature > 500 && throw(DomainError(temperature))
@info "temperature check passed: $temperature °C"
end
struct MachineError <: Exception end
function machinemonitor(pct_humidity, temperature)
errors = 0
try
humiditycheck(pct_humidity)
catch humidity_error
errors += 1
@error "humidity level check failed: $pct_humidity%"
end
try
temperaturecheck(temperature)
catch temp_error
errors += 1
temp_error isa ArgumentError ? (@warn "sensor is broken") : (@error "overheating detected: $temperature °C")
end
iszero(errors) || throw(MachineError())
end Testset: using Test
include("factory-sensors.jl")
@testset verbose = true "tests" begin
@testset "1. Monitor the humidity level of the room" begin
@testset "Passing" begin
@test_logs (:info, "humidity level check passed: 53%") humiditycheck(53)
end
@testset "Failing" begin
@test_throws ErrorException humiditycheck(80)
@test_throws "80" humiditycheck(80)
end
end
@testset "2. Check for overheating" begin
@testset "Passing" begin
@test_logs (:info, "temperature check passed: 200 °C") temperaturecheck(200)
end
@testset "Failing" begin
@test_throws ArgumentError temperaturecheck(nothing)
@test_throws DomainError temperaturecheck(501)
@test_throws "501" temperaturecheck(501)
end
end
@testset "3. Monitor the machine" begin
@testset "Passing" begin
@test_logs (:info, "humidity level check passed: 53%") (:info, "temperature check passed: 200 °C") machinemonitor(53, 200)
end
@testset "Failing" begin
@test_logs (:error, "humidity level check failed: 80%") (:info, "temperature check passed: 220 °C") begin
@test_throws MachineError machinemonitor(80, 220)
end
@test_logs (:info, "humidity level check passed: 52%") (:warn, "sensor is broken") begin
@test_throws MachineError machinemonitor(52, nothing)
end
@test_logs (:info, "humidity level check passed: 21%") (:error, "overheating detected: 540 °C") begin
@test_throws MachineError machinemonitor(21, 540)
end
@test_logs (:error, "humidity level check failed: 100%") (:warn, "sensor is broken") begin
@test_throws MachineError machinemonitor(100, nothing)
end
@test_logs (:error, "humidity level check failed: 93%") (:error, "overheating detected: 521 °C") begin
@test_throws MachineError machinemonitor(93, 521)
end
end
end
end InstructionsElena is the new quality manager of a newspaper factory. 1. Check the humidity level of the roomYour first mission is to write a piece of software to monitor the humidity level of the production room. There is already a sensor connected to the software of the company that returns periodically the humidity percentage of the room. You need to implement a function in the software that will throw an error if the humidity percentage is too high. You should halt with an ErrorException (the exact message is not important, but must contain the measured humidity level) if the percentage exceeds 70%. julia> humiditycheck(60)
[ Info: humidity level check passed: 60% julia> humiditycheck(100)
ERROR: humidity check failed: 100% 2. Check for overheatingElena is very pleased with your first assignment and asks you to deal with the monitoring of the machines' temperature. The machine is equipped with a sensor that measures its internal temperature. Your job is to implement a function
julia> temperaturecheck(nothing)
ERROR: ArgumentError: sensor is broken
julia> temperaturecheck(800)
ERROR: DomainError with 800:
"overheating detected"
julia> temperaturecheck(500)
[ Info: temperature check passed: 500 °C 3. Define custom errorFor the next task, you will need to define a more general, catch-all error. 4. Monitor the machineNow that your machine can detect errors and you have a custom machine error, you add a wrapper function that can report how everything is working.
Implement a function julia> machinemonitor(42, 450)
[ Info: humidity level check passed: 42%
[ Info: temperature check passed: 450 °C
julia> machinemonitor(42, 550)
[ Info: humidity level check passed: 42%
┌ Error: overheating detected: 550 °C
└ @ Main # output truncated
Error: MachineError
julia> machinemonitor(82, 521)
┌ Error: humidity level check failed: 82%
└ @ Main # output truncated
┌ Error: overheating detected: 521 °C
└ @ Main # output truncated
Error: MachineError
julia> machinemonitor(42, nothing)
[ Info: humidity level check passed: 42%
┌ Warning: sensor is broken
└ @ Main # output truncated
Error: MachineError Hints1. Check the humidity level of the room
2. Check for overheating
3. Define custom error
4. Monitor the machine
|
This looks great! I'll go ahead and commit these changes, then make the PR ready for review (when you get chance). I found some small wording issues, copied from the JS version and carried through all my changes then your changes. For example, "as a parameter" should be "as an argument" for consistency within or syllabus. |
Given that this is now mostly your exercise, I tend to see you as the author who should get the credit. At the risk of reopening an old argument, are you still refusing this, or can we edit |
tweaked wording
I've added a couple comments on some other small things I've found.
I don't need credit for stuff unless there's some technical reason for needing to ascribe it, so we can just leave things as they are if you're okay with that. |
fixed function names in stubs, added comment about MachineError
Thanks for approving. I'll hold off merge for now, and look through it again later. I'm not even sure whether the GH-Exercism link is repaired yet. |
I just checked and it looks like they've found the cause, but I didn't see if it's actually been resolved. I was hoping to merge the test sync PR, so I could work on |
added indentation to function skeletons
Apologies, I'd have done this earlier if I'd known it was important to you. FWIW, it's merged and live on Exercism now. |
As best I can tell, new merges are succeeding. Things merged yesterday will need to wait for Jeremy to manually resync the tracks with the website. |
No worries! It wasn't important. I had quite literally just sat down to look at GH when you started working on this, so it was just as well to be able to use the time I had to help here :) |
I fixed a couple of typos and a formatting glitch. I'll merge when the checks finish, to see what it looks like on the website. Someone will need to propagate the small typo-fixes to the concept documents, sometime. I'll put a PR in, but it's not urgent. |
Encouraging news... The first solution by a a student seems to have effectively found the example solution. |
That makes me wonder who it is. Seems to be a new user with only 13 rep. |
This is what I've got so far. Maybe it's enough?
I've left in the residues of previous attempts in runtests and exemplar. They will all need removing before we merge.