clojure-losangeles

https://www.meetup.com/Los-Angeles-Clojure-Users-Group/
nate 2020-09-10T03:43:46.002900Z

hammered my head against it a bit more, and I got it to work. both functions return lists, so one of them needs to flatten so that it doesn't endlessly keep making nested lists. I pushed a branch called working-code to the github repo if anyone is interested @dorab @jts

2020-09-10T14:52:20.003900Z

i missed the meetup but this looks interesting, do you hvae the problem description handy?

Javier Trevino Saldana 2020-09-10T15:13:14.004200Z

yep, this is it https://edabit.com/challenge/y7xoBP9bgHRNTcELK the test cases were converted to clojure in the repo link above if you want em

2020-09-10T15:21:22.004500Z

thanks thats enough to go on :thumbsup:

dorab 2020-09-10T21:17:10.004700Z

@nate Your solution is likely better than mine. I, too, banged on it last night and realized what you did. However, my solution is hackier than yours. In any case, it passes all the tests, so it MUST be correct 🙂

2020-09-10T21:18:20.004900Z

the tests don't suggest it but i can imagine inputs that would produce multiple solutions, which suggest a solution that backtracks

dorab 2020-09-10T21:18:37.005100Z

I should mention that I pushed my solution under the dorab-working-code branch. I attempted to put that branch off of the la-meetup-september-2020.

dorab 2020-09-10T21:20:49.005300Z

@alandipert When we worked on it together, we did envision multiple solutions. In fact, my solution generates all possible solutions and then takes the first one. Did not add tests to see if it worked.

2020-09-10T21:21:22.005500Z

oh awesome. i'll try not to look at it before making my own 😄

nate 2020-09-10T21:21:34.005700Z

Yes, definitely need a solution that backtracks. The last test case has a dead end that needs to be backed out of.

dorab 2020-09-10T21:22:37.005900Z

One alley we went down was to use a backtracking regex. But eventually stopped pursuing that path because we could not quite figure out the regex.

dorab 2020-09-10T21:23:49.006100Z

This was a surprisingly good challenge.

dorab 2020-09-10T21:25:10.006300Z

The first solution we tried was a greedy longest-match-first solution without backtracking. When it failed on a test we realized we'd need a backtracking solution.

2020-09-10T21:29:39.006500Z

nice! yeah i enjoy this puzzle, i think i'll warm up with it before work tomorrow

2020-09-10T21:30:32.006700Z

about 10 yrs ago i was presented a similar problem as part of an interview at Twitter and i totally blew it

2020-09-10T21:40:08.006900Z

this is a chance for me to redeem myself :simple_smile:

dorab 2020-09-10T22:39:50.007100Z

Thanks to @jts for making it an international virtual meetup and contributing to the discussion and solution.

🙌 2
😄 1
Javier Trevino Saldana 2020-09-10T23:37:14.007700Z

haha anytime. Looking forward to the next one

Mario C. 2020-09-11T18:31:16.008100Z

Seems to work on the 3 examples on the site

Mario C. 2020-09-11T18:44:17.008300Z

does not work

Mario C. 2020-09-11T18:44:30.008500Z

Just found the tests on the github

Mario C. 2020-09-11T19:22:18.008700Z

After some tweaking I ended up with this

Mario C. 2020-09-11T19:22:20.008900Z

(defn first-matches [sentence words]
    (-> (filter (fn [word]
                  (let [reg    (re-pattern word)
                        result (str/split sentence reg)
                        match  (first result)]
                    (zero? (count match))))
                words)))

  (defn match-validator [sentence fms]
    (->> fms
         (remove (fn [ms]
                   (-> (str/split sentence (re-pattern ms) 2)
                       last
                       (first-matches words)
                       empty?)))
         first))

  (defn clean-string [sentence words ms]
    (let [fms (first-matches sentence words)
          fm (match-validator sentence fms)]
      (if (nil? fm)
        "Cleaving stalled: Word not found"
        (let [cs (last (str/split sentence (re-pattern fm) 2))]
          (if (or (empty? cs) (nil? cs))
            (str/join " " (conj ms fm))
            #(clean-string cs words (conj ms fm)))))
      ))

  (trampoline clean-string s7 words [])

Mario C. 2020-09-11T19:22:47.009100Z

this passed all the tests

Mario C. 2020-09-11T19:22:53.009300Z

fun problem

nate 2020-09-11T22:14:07.009700Z

oh man, I always forget about trampoline!

nate 2020-09-11T22:14:20.009900Z

it's like recur, but for mutual recursion

Mario C. 2020-09-11T23:10:42.010100Z

I was thinking about this and figured out a way to simplify it. You could sort the word bank and reverse it. Then "wrap" all the matches starting with the biggest word. Leaving you with something like [[[a]n]d] . All the words in valid sentences will be insides the brackets. If any letters are dangling outside of that, then it is stalled sentence. Then you split the sentence by ][ . Then remove all the brackets.

Mario C. 2020-09-11T23:13:29.010500Z

"[f[or]][a][moment][noth[i]ng][[h[a]ppen]ed][[the]n][[a]fter][a][second][or][so][noth[i]ng][cont[i]nued][to][h[a]ppen]"

nate 2020-09-10T03:45:01.003300Z

I added the flatten to line 15, and changed match to [match] on line 17.

nate 2020-09-10T03:45:35.003500Z

then it was just a matter of handling the case in cleave of filtering out all the non results and defaulting to the "not found" string if nothing was found

nate 2020-09-10T03:45:58.003700Z

very curious if you found other solutions in your tinkering