Jump to content

Longest string challenge: Difference between revisions

Adding Quackery.
m (keeping all REM statements to their own line)
(Adding Quackery.)
Line 2,134:
 
</pre>
 
=={{header|Quackery}}==
 
The quandary here is, is a dynamic array a list? If yes, then nothing is allowed, as the only composite structure in Quackery is the dynamic array ("nest" in Quackery jargon), including code, and Quackery is one of those languages where code is data, and data is code. If no, then everything is allowed and the exercise is trivial. So, in the spirit of the task we will differentiate between strings (in reality nests of numbers interpreted as ascii characters) and other nests, and use only strings where the context is clearly data.
 
We note that implicit arithmetic is allowable, so the use of <code>witheach</code> to iterate over a string is permitted.
 
'''Method'''
 
<code>comparison</code> compares the length of two strings on the top of second on stack, designated A and B here, without using comparisons.
 
From A we construct a string A' of the same length as A consisting entirely of 0s (i.e. the control code "nul"), which will later be taken to indicate that A is the longer string of the two. From B we construct a string B' of the same length consisting entirely of 1s (i.e. the control code "soh"), which will later be taken to indicate that A is the longer string of the two. 2 ("stx" will indicate that the two strings are the same length.)
 
We reduce the length of A' by the number of characters in B' by repeatedly removing the last character from A' using <code>-1 split drop</code>, once for each character in B'. If B' is longer than A' this will leave an empty string. (The way <code>split</code> operates means that attempting to separate the last character from an empty string will return two empty strings, and not raise an error.
 
Then we do the same but reducing the length of B' by the length of A' (the original A', before its length was reduced.)
 
At this point, either A' or B' will be an empty string if one was longer than the other, or both will be empty strings if they were the same length initially. We concatenate them, then concatenate a 2, and return the first character in the resultant string, i.e. 0, 1, or 2.
 
<code>task</code> prompts the user to input strings, using the result of <code>comparison</code> to determine when the user ends inputting, by indexing into an embedded lookup table and performing the specified action. It also constructs a result string consisting of the longest input strings separated by carriage returns in the same manner. Finally it prints the result string.
 
<syntaxhighlight lang="Quackery"> [ 0 ] is alonger
[ 1 ] is blonger
[ 2 ] is a&bsame
 
[ [] swap witheach
[ drop blonger join ]
[] rot witheach
[ drop alonger join ]
over dip dup
witheach [ drop -1 split drop ]
unrot
witheach [ drop -1 split drop ]
join
a&bsame join
0 peek ] is comparison ( $ $ --> c )
 
[ say "Enter an empty string to end."
cr cr
$ "" $ ""
[ $ "Enter a string: " input
dup $ "" comparison
[ table
true true false ] do while
carriage join
2dup comparison
[ table
[ drop ]
[ dip [ 2drop $ "" ] ]
[ dip join ] ]
do again ]
cr say "Result:" cr
drop join echo$ ] is task ( --> )</syntaxhighlight>
 
{{out}}
 
Aa a dialogue in the Quackery shell (REPL):
<pre>/O> task
...
Enter an empty string to end.
 
Enter a string: a
Enter a string: bb
Enter a string: ccc
Enter a string: ddd
Enter a string: ee
Enter a string: f
Enter a string: ggg
Enter a string:
 
Result:
ccc
ddd
ggg
</pre>
 
 
=={{header|Racket}}==
1,496

edits

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