I want to find the smallest number in this file and I wrote the following program:
function smaller()
small = 0
for line in eachline("file.txt")
if (small == 0)
small = line
elseif (small > line)
small = line
end
end
println(small)
end
smaller()
I am a newbie and I want to know where my problem is and I don’t want to use other methods.
the only case you’d want to update small with line is if small > line, so your if and else statements could be simpler
traditionally, one might expect your smaller function to accept the file name and return the smallest integer (i.e. not print it out).
I don’t know for sure, but I bet that the error messages you got would have helped you in improving this code. Maybe you can match the messages to some of these tips?
What precisely did you change? If you just replaced the two occurrences of small = line by small = parse(Int, line), then the comparison small > line would yield an error, as you are comparing an Int (small) to a String (line).
Hi,
Thank you so much for your note.
I changed the code as follows:
function smaller()
small = 0
for line in eachline("file.txt")
if (small == 0)
small = parse(Int,line)
elseif (small > parse(Int,line))
small = parse(Int,line)
end
end
println(small)
end
smaller()
Another note for style: You can simplify your logic if you initialize small with the greatest possible number. Then you do don’t need the if small == 0 stuff and only need the comparison
You can get the biggest value of a datatype with typemax(datatype) so in your case that’s typemax(Int).
The original code overwrites the initialisation in the first iteration step, which minimum(..., init=...) does not. So this is not equivalent, and the original code (with parse) is correct (though not particularly concise).
You probably meant typemax(Int), cf. @abraemer 's post. Alternatively, you could simply leave out the explicit init.
Independently of the one-liners above, which might be confusing, the simple loop is probably written like this (and it is perfectly fine):
function smaller(file) # provide the file as a parameter to the function
small = typemax(Int)
for line in eachline(file)
small = min(small, parse(Int, line))
end
return small
end
To be run with:
julia> smaller("file.txt")
And there’s nothing wrong in substituting the min(...) by
value = parse(Int,line)
if value < small
small = value
end