# I don't understand this bug

**URL:** <https://discourse.julialang.org/t/i-dont-understand-this-bug/61789>\
**Category:** General Usage\
**Created:** [May 25, 2021, 3:42pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789 "2021-05-25T15:42:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scarlov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scarlov/32/25333_2.png) [@Scarlov](https://discourse.julialang.org/u/Scarlov)\
**Post date:** [May 25, 2021, 3:42pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/1 "2021-05-25T15:42:22Z")

</div>

When I run this code:

```julia
using Printf

function choose_best_sum(t, k, ls)
    ran = range(1, length(ls), step = 1)
    choices = unique!([sort([ls[i], ls[j], ls[m]]) for i in ran for j in ran for m in ran if( i != j && j != m && m != i)])
    println(choices[1])
    distances = sort([sum(i) for i in choices if ( sum(i) <= t)])

    return distances[end]
end
# ls:
towns = [50, 55, 57, 56, 58, 60]

# t:
max_dist = 174

# k - number of towns
num = 3

println(choose_best_sum(max_dist, num, towns))

```

it prints out the value of **choices[1]** and **distances[end]**. However when I delete the **println(choices[1])** line nothing gets printed out. Does anyone know why? I am pretty new to Julia, so sorry in advance if that’s something I should know 😅

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 25, 2021, 3:48pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/2 "2021-05-25T15:48:11Z")

</div>

Which version of Julia are you using? With 1.6.1, after commenting-out the `println(choices[1])` line I still get the output `174`…

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 25, 2021, 3:48pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/3 "2021-05-25T15:48:58Z")

</div>

I tried running your example in the REPL.  
Like in your post:

```julia
julia> println(choose_best_sum(max_dist, num, towns))
[50, 55, 57]
174

```

without `println(choices[1])`:

```julia
julia> println(choose_best_sum(max_dist, num, towns))
174

```

with no `println`:

```julia
julia> choose_best_sum(max_dist, num, towns)
174

```

The return value is printed in the REPL by default.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 25, 2021, 4:44pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/4 "2021-05-25T16:44:51Z")

</div>

You can try to call `flush(stdout)` after printing. This would flush the buffer in case that is the problem.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 25, 2021, 5:10pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/5 "2021-05-25T17:10:23Z")

</div>

> [@Scarlov](#):
>
> `range(1, length(ls), step = 1)`

BTW, Julia has dedicated (and much nicer) syntax for this: `1:length(ls)`. If you want a different steplength than 1, you can write `1:n:length(ls)`.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 25, 2021, 5:16pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/6 "2021-05-25T17:16:21Z")

</div>

There is a [Combinatorics](https://github.com/JuliaMath/Combinatorics.jl) package, which can calculate permutations and a [TravelingSalesmanHeuristics](https://github.com/evanfields/TravelingSalesmanHeuristics.jl) package in case that is your problem.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 25, 2021, 5:25pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/7 "2021-05-25T17:25:00Z")

</div>

> [@Scarlov](#):
>
> `( i != j && j != m && m != i)`

Also, chained comparisons:

```julia
i != j != m != i

```

---

<div class="post-metadata">

**Author:** ![Scarlov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scarlov/32/25333_2.png) [@Scarlov](https://discourse.julialang.org/u/Scarlov)\
**Post date:** [May 25, 2021, 7:04pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/8 "2021-05-25T19:04:25Z")

</div>

my version is also 1.6.1 😕

---

<div class="post-metadata">

**Author:** ![Scarlov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scarlov/32/25333_2.png) [@Scarlov](https://discourse.julialang.org/u/Scarlov)\
**Post date:** [May 25, 2021, 7:06pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/9 "2021-05-25T19:06:06Z")

</div>

Don’t know what to say, when I do it it goes to the next line in the REPL but the place where the output is supposed to be is just empty

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 25, 2021, 7:12pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/10 "2021-05-25T19:12:23Z")

</div>

Can you paste the actual input and output you’re seeing?

---

<div class="post-metadata">

**Author:** ![Scarlov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scarlov/32/25333_2.png) [@Scarlov](https://discourse.julialang.org/u/Scarlov)\
**Post date:** [May 25, 2021, 7:41pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/11 "2021-05-25T19:41:25Z")

</div>

For this input:

```julia
using Printf

function choose_best_sum(t, k, ls)
    ran = range(1, length(ls), step = 1)
    choices = unique!([sort([ls[i], ls[j], ls[m]]) for i in ran for j in ran for m in ran if( i != j != m != i)])
    distances = sort([sum(i) for i in choices if ( sum(i) <= t)])
    return distances[end]
end
# ls:
towns = [50, 55, 57, 56, 58, 60]

# t:
max_dist = 174

# k - number of towns
num = 3

println(choose_best_sum(max_dist, num, towns))

```

I get empty space :  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/1/51e91e7fcbf126bdf09a215b60efbfde5b01025b.png)

---

<div class="post-metadata">

**Author:** ![Scarlov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scarlov/32/25333_2.png) [@Scarlov](https://discourse.julialang.org/u/Scarlov)\
**Post date:** [May 25, 2021, 7:46pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/12 "2021-05-25T19:46:44Z")

</div>

Hey, I’m sorry I think I’m slightly mentally challenged 😂. While running the script still doesn’t work the way I would like it to running the function in the REPL itself seems to resolve that issue. Thanks everyone for your help and useful suggestions!

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 25, 2021, 8:00pm UTC](https://discourse.julialang.org/t/i-dont-understand-this-bug/61789/13 "2021-05-25T20:00:50Z")

</div>

How would you like your script to run?  
If you have a file called script.jl you can do the following:

```julia
julia script.jl

```

or

```julia
julia
julia> include("script.jl")

```
