# Need help writing some recursive code please! Full code explanation and snapshot included

**URL:** <https://discourse.julialang.org/t/need-help-writing-some-recursive-code-please-full-code-explanation-and-snapshot-included/39306>\
**Category:** General Usage\
**Created:** [May 11, 2020, 8:00pm UTC](https://discourse.julialang.org/t/need-help-writing-some-recursive-code-please-full-code-explanation-and-snapshot-included/39306 "2020-05-11T20:00:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [May 11, 2020, 8:00pm UTC](https://discourse.julialang.org/t/need-help-writing-some-recursive-code-please-full-code-explanation-and-snapshot-included/39306/1 "2020-05-11T20:00:14Z")

</div>

Hi how’s it going?

I’m trying to write this recursive code out and can’t get it to work. Can someone help me with a solution? Here is a code of the scenario.

```julia
y = [2,2,2,2,3,3,4]
x = [1,2,3,3,2,2,2]
df = DataFrame(:column_1=>x,:column_2=>y)
df[!,:column_3] = string.(df[!,:column_1]).*string.(df[!,:column_2])

countmap(df[!,:column_3])

Dict{String,Int64} with 5 entries:
  "32" => 2
  "24" => 1
  "12" => 1
  "23" => 2
  "22" => 1

```

What I’m looking for is that no value of column\_3 has a count of 1 in the dataframe. If it is the case, then it should look either forward or backwards to rename to another value of :column\_3, based on the two values that it was concatenated from. What I’m doing now is sorting by column 1, then identifying which values of column 3 has a count of 1, then trying to run a loop and replacing values then calling the function recursively, but I can’t get it.

For example, in the image, “12” only has 1 value, it should then look upwards to see if there’s a “22”, and since there is, it should rename to “22”. Similarly, “24” only has one value. It should look backwards and find that there is no “14” value so it checks the second part of the string concatenation and finds that there is a “23” value so it renames it to that.

A little complicated but essentially I just dont want any values of column\_3 to have a count of 1 in the dataframe and it should adjust based on the closest numerical values pre-string concatenation.

Thank you!

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 11, 2020, 8:37pm UTC](https://discourse.julialang.org/t/need-help-writing-some-recursive-code-please-full-code-explanation-and-snapshot-included/39306/2 "2020-05-11T20:37:54Z")

</div>

Please post code that people can copy and paste, not a screenshot.

---

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [May 11, 2020, 8:41pm UTC](https://discourse.julialang.org/t/need-help-writing-some-recursive-code-please-full-code-explanation-and-snapshot-included/39306/3 "2020-05-11T20:41:00Z")

</div>

Ok done, sorry bout that.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 11, 2020, 8:41pm UTC](https://discourse.julialang.org/t/need-help-writing-some-recursive-code-please-full-code-explanation-and-snapshot-included/39306/4 "2020-05-11T20:41:44Z")

</div>

Thanks. Please quote your code as well: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [May 11, 2020, 8:43pm UTC](https://discourse.julialang.org/t/need-help-writing-some-recursive-code-please-full-code-explanation-and-snapshot-included/39306/5 "2020-05-11T20:43:30Z")

</div>

Cool thanks for the link. Done.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 11, 2020, 8:47pm UTC](https://discourse.julialang.org/t/need-help-writing-some-recursive-code-please-full-code-explanation-and-snapshot-included/39306/6 "2020-05-11T20:47:08Z")

</div>

Thanks for the post.

Can you give a more complete MWE?

First, please quote your code using triple back ticks

````julia
```
like this
```

````

It isn’t super clear what you want from this. It would be helpful if your MWE could include information about what your desired output should be.

Here is a super basic implementation

```julia

julia> function change_all!(v)
       unique_items = unique(v)
       i = 1
       while no_uniques(v) && i <= length(unique_items)
           change!(v, unique_items[i])
           i += 1
       end
       return v
       end

julia> function no_uniques(v)
           !(1 in values(countmap(v)))
       end

julia> function change!(v, current)
       if count(t -> t == current, v) == 1
           similars = filter(t -> startswith(t, first(current)), v)
           similars = setdiff(similars, [current])
           sim1 = first(similars)
           v[v .== current] .= sim1
       end
       return v
       end

julia> v = ["21", "22", "22"]
julia> change_all!(v)

```

---

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [May 11, 2020, 9:00pm UTC](https://discourse.julialang.org/t/need-help-writing-some-recursive-code-please-full-code-explanation-and-snapshot-included/39306/7 "2020-05-11T21:00:23Z")

</div>

Cool thanks a lot for the reply. I will try implementing this. It’s a bit difficult for me to describe it any further, tried to give an extremely simple example. But if I’m not able to make it work with the code you provided, I will figure out a way to describe it better.
