Amb: Difference between revisions
→{{header|J}}: re-implement task
Cyril Nocton (talk | contribs) m (→4. Execution) |
(→{{header|J}}: re-implement task) |
||
Line 1,898:
=={{header|J}}==
Here, we interpret the specification such that ambassert picks an arbitrary valid example. In other words, it discards all unnecessary ambiguity (it arbitrarily picks a "single future" from among the valid "potential futures").
Since this task specifies an O(n^m) complexity problem, we'll also assume that the user knows to limit its use to small examples.
Implementation:
+-----------------------+</syntaxhighlight>▼
<syntaxhighlight lang=J>ambsel=: <^:99@,
ambassert=: {{
anames=. (#~ 99<:{{L.do y}}@>) (#~ 0=nc)/:~~.;:m
+-----------------------+</syntaxhighlight>▼
avalues=. {{>^:98 do y}}every anames
limit=. */alimits=. #@> avalues
<syntaxhighlight lang="j"> NB. Dynamic programming method...▼
j=. 0 while. do.
ainds=. alimits#:j
(anames)=. test=. ainds {each avalues
try. do m
anames {{(x)=:y}}&> test
break.
catch. end.
if. limit<:j=.j+1 do. assert.'no valid solution' end.
end.
EMPTY
Task example:
s2=: ambsel;:'frog elephant thing'
s3=: ambsel;:'walked treaded grows'
s4=: ambsel;:'slowly quickly'
edgematch=: {{ x,y assert. ({:;x)={.;y }}
{{)n edgematch/ s1,s2,s3,s4 }} ambassert
echo s1,s2,s3,s4
┌────┬─────┬─────┬──────┐
│that│thing│grows│slowly│
└────┴─────┴─────┴──────┘
=={{header|JavaScript}}==
|