Julia
Online Julia Compiler
Your script runs with julia main.jl on Julia 1.12, on one thread, with Julia’s standard libraries. readline() waits for you to type in the terminal.
Try it aboveHow Julia runs here
- Versions
- Julia 1.12
- Run
julia --threads=1 main.jl- Limits per run
- 512 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A Julia script that reads input
This main.jl reads temperatures, then uses broadcasting (the dots) to convert the whole vector at once:
using Printf print("How many temperatures? ") n = parse(Int, readline()) temps = Float64[] for i in 1:n print("Temperature $i: ") push!(temps, parse(Float64, readline())) end avg = sum(temps) / n @printf("Average: %.1f\n", avg) println("Above average: ", filter(t -> t > avg, temps)) println("In Fahrenheit: ", round.(temps .* 9 ./ 5 .+ 32; digits = 1))
$ julia main.jl How many temperatures? 3 Temperature 1: 18.5 Temperature 2: 24 Temperature 3: 30.5 Average: 24.3 Above average: [30.5] In Fahrenheit: [65.3, 75.2, 86.9]
The first call of each function is compiled, so even a short script takes a moment before it starts printing.
Reading input in Julia
- readline() returns the line without its newline. Convert with parse(Int, s) or parse(Float64, s).
- For several numbers on one line, split first: parse.(Int, split(readline())). parse(Int, "3 4") throws.
- readlines() and eachline(stdin) wait for the end of input, which this terminal never sends. Read a count first, or stop on an empty line.
What isn’t available here
- Registered packages. Pkg.add() needs the network, so DataFrames, CSV, Plots and JuMP can’t be installed. Standard libraries such as Printf, LinearAlgebra, Random and Dates load with using.
- Plots. Output is text in the terminal.
- Threads. Julia starts with --threads=1, so Threads.@threads runs on a single thread.
Errors you’ll see
- A MethodError saying there’s no method matching, for example +(::Int64, ::String): you combined a number with text from readline(). parse() it first.
- An ArgumentError from parse() about an invalid digit: the line had a space or a letter in it.
- A BoundsError: Julia arrays start at index 1, so x[0] is out of range.
Questions
Which Julia version does this use?
Julia 1.12, started with julia --threads=1 main.jl.
Can I install packages like DataFrames or Plots?
No. Runs have no network, so Pkg.add() fails. The standard libraries that ship with Julia work.
How do I read a number from input in Julia?
n = parse(Int, readline()). Type the number in the terminal while the script runs.