Jump to content

Solve hanging lantern problem: Difference between revisions

J entry
(→‎{{header|J}}: remove J from Basic implementations)
(J entry)
Line 391:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|J}}==
 
Translation of [[#APL|APL]]:
 
<lang J>lanterns=: {{ (!+/y) % */!y }}<</lang>
 
Example use:
 
<lang J> lanterns 1 2 3
60
lanterns 1 3 3
140
</lang>
 
Also, a pedantic version where we must manually count how many values we are providing the computer:
 
<lang J>pedantic=: {{
assert. ({. = #@}.) y
lanterns }.y
}}</lang>
 
And, in the spirit of providing unnecessary but perhaps pleasant (for some) overhead, we'll throw in an unnecessary comma between this count and the relevant values:
 
<lang J> pedantic 3, 1 2 3
60
pedantic 3, 1 3 3
140</lang>
 
If we wanted to impose even more overhead, we could insist that the numbers be read from a file where tabs, spaces and newlines are all treated equivalently. For that, we must specify the file name and implement some parsing:
 
<lang J>yetmoreoverhead=: {{
pedantic ({.~ 1+{.) _ ". rplc&(TAB,' ',LF,' ') fread y
}}</lang>
 
Examples of this approach are left as an exercise for the user.
 
=={{header|Julia}}==
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.