Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions exercises/solved_notebooks/create_unsolved.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
create_unsolved(file::String; output_path::String = homedir() * "/Downloads")

Make the unsolved version of a notebook.

Cells that contain a line that starts with `#` and contains a "missing" will be changed:
all uncommented lines are removed and all commented lines will be then uncommented.
"""
function create_unsolved(file::String; output_path::String = homedir() * "/Downloads")
lines = readlines(file)
cell_start_idxs = findall(occursin.("# ╔═╡", lines))
delete_idxs = Int64[]
for cell_nr in 1:(length(cell_start_idxs)-1)
cell_idxs = (cell_start_idxs[cell_nr]+1):(cell_start_idxs[cell_nr+1]-1)
needs_replacement = occursin.(r"^(\s*)\#.*missing", lines[cell_idxs]) |> any

needs_replacement && for cell_idx in cell_idxs
isempty(lines[cell_idx]) && continue
if lines[cell_idx][1] == '#'
println(lines[cell_idx])
lines[cell_idx] = lines[cell_idx][3:end]
else
push!(delete_idxs, cell_idx)
end
end
end
deleteat!(lines, delete_idxs)

output_filename = replace(splitpath(file)[end], "_sol" => "")
write(output_path * '/' * output_filename, *(lines .* '\n'...))

return nothing
end

file = "P7_sens_uncert/sens_intro_sol.jl"
create_unsolved(file)
Loading