Longest string challenge: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (make all literals INT(VAL("number")))
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(9 intermediate revisions by 6 users not shown)
Line 577:
 
=={{header|BASIC}}==
{{trans|FreeBASIC}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">
DO
READ test$
IF test$ = "~" THEN EXIT DO
IF LEN(test$) > LEN(test1$) THEN
test1$ = test$
test2$ = test1$ + CHR$(10)
ELSEIF LEN(test$) = LEN(test1$) THEN
LET test2$ = test2$ + test$ + CHR$(10)
END IF
LOOP
 
PRINT (test2$)
 
DATA "a", "bb", "ccc", "ddd", "ee", "f", "ggg", "~" : ' la tilde es solo para mantener el código compacto
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
==={{header|Applesoft BASIC}}===
Only Integer and String type variables are used. It is difficult not to use floats as, unfortunately, pretty much all internal operations default to floating point in Applesoft BASIC. Most, if not all, functions in Applesoft BASIC are passed arguments and return floating point types. In the spirit of the task the INT function is used throughout.
Line 627 ⟶ 605:
140 TEXT : HOME : VTAB TWO%: POKE 34,UN%
REM + IS A "STRING" OPERATOR, A NON-ARITHMETIC OPERATOR WHEN USED WITH STRINGS FOR CONCATENATION
150 IT$ = IT$ + ".": REM + MEANS CONCATENATE, A "STRING" OPERATOR, A NON-ARITHMETIC OPERATOR
160 P% = INT ( VAL ("10" + STR$ ( LEN (IT$))))
170 POKE P%, INT ( ASC ( LEFT$ (MSG$,UN%)))
Line 670 ⟶ 648:
620 IF INT ( LEN (IT$)) THEN COMPARE$ = MID$ (COMPARE$,TWO%): GOTO 590"COMPARE AGAIN
630 PRINT RESULT$</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">dim test = {"a", "bb", "ccc", "ddd", "ee", "f", "ggg"}
test1 = ""
 
for c = 0 to test[?]-1
if length(test[c]) > length(test1) Then
test1 = test[c]
test2 = test1 & chr(10)
else
if length(test[c]) = length(test1) then test2 += test[c] & chr(10)
end if
next c
 
print test2
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 data "a","bb","ccc","ddd","ee","f","ggg","~" : ' la tilde es solo para mantener el código compacto
20 restore
30 read test$
40 if test$ = "~" then goto 70
50 if len(test$) > len(test1$) then test1$ = test$ : test2$ = test1$+chr$(10) else if len(test$) = len(test1$) then test2$ = test2$+test$+chr$(10)
60 goto 30
70 print test2$
80 end</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 DATA "a","bb","ccc","ddd","ee","f","ggg","~"
20 RESTORE
30 READ TEST$
40 IF TEST$ = "~" THEN GOTO 80
50 IF LEN(TEST$) > LEN(TEST1$) THEN LET TEST1$ = TEST$ : LET TEST2$ = TEST1$ + CHR$(10) : GOTO 30
60 IF LEN(TEST$) = LEN(TEST1$) THEN LET TEST2$ = TEST2$ + TEST$ + CHR$(10)
70 GOTO 30
80 PRINT TEST2$
90 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{works with|Applesoft BASIC}}
<syntaxhighlight lang="qbasic">10 DATA "a","bb","ccc","ddd","ee","f","ggg","~"
30 READ T$
40 IF T$ = "~" THEN GOTO 80
50 IF LEN(T$) > LEN(T1$) THEN LET T1$ = T$ : LET T2$ = T1$ + CHR$(10) : GOTO 30
60 IF LEN(T$) = LEN(T1$) THEN LET T2$ = T2$ + T$ + CHR$(10)
70 GOTO 30
80 PRINT T2$
90 END</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">
DO
READ test$
IF test$ = "~" THEN EXIT DO
IF LEN(test$) > LEN(test1$) THEN
test1$ = test$
test2$ = test1$ + CHR$(10)
ELSEIF LEN(test$) = LEN(test1$) THEN
LET test2$ = test2$ + test$ + CHR$(10)
END IF
LOOP
 
PRINT (test2$)
 
DATA "a", "bb", "ccc", "ddd", "ee", "f", "ggg", "~" : ' la tilde es solo para mantener el código compacto
</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
 
==={{header|True BASIC}}===
Line 733 ⟶ 788:
ggg
</pre>
 
=={{header|BQN}}==
Most primitives in BQN implicitly use comparison, so this solution avoids the several easy ways out.
 
The namespace approach keeps track of the latest string and the result without needing a list.
 
The recursion and pattern matching lets us avoid comparison operators and arithmetic.
 
The datatype restrictions are, as discussed in the problem statement, not applicable for BQN as it would unfairly rule the language out.
 
<syntaxhighlight lang="bqn">Cmp ← {
⟨⟩ 𝕊 ⟨⟩: 0; · 𝕊 ⟨⟩: 1; ⟨⟩ 𝕊 ·: 2;
𝕨 𝕊○(1⊸↓) 𝕩
}
 
cr←@+10
 
•Out {𝕊:
(•Getline@){
@ Longest 𝕩: 𝕩.str;
𝕨 Longest 𝕩:
(•Getline@) Longest 𝕨 {𝕨Cmp𝕩.curr}◶⟨
{curr⇐𝕨,str⇐𝕩.str∾cr∾𝕨}
{curr⇐𝕨,str⇐𝕨}
⟩ 𝕩
}𝕩
}{curr⇐"",str⇐""}</syntaxhighlight>
 
=={{header|C}}==
Line 1,538 ⟶ 1,621:
[1,3] = ggg
} </pre>
 
=={{header|MiniScript}}==
{{trans|Python}}
<syntaxhighlight lang="miniscript">
string.isLonger = function(s)
return self.hasIndex(s.len)
end function
 
longest = ""
lines = ""
 
current = input
while current
if current.isLonger(longest) then
lines = current
longest = current
else if not longest.isLonger(current) then
lines += current
end if
current = input
end while
 
for i in range(0, lines.len, longest.len)
print lines[i:i + longest.len]
end for
</syntaxhighlight>
{{out}}
<pre>
a
bb
ccc
dd
eee
f
ggg
hh
 
ccc
eee
ggg
</pre>
 
=={{header|Nanoquery}}==
Line 2,134 ⟶ 2,258:
 
</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}}==
Line 2,308 ⟶ 2,508:
ddd
ggg
</pre>
 
=={{header|RPL}}==
This program complies with the list of restrictions:
The main hack is based on the fact that the <code>SUB</code> function, which extracts one or more characters from a string, returns the null character if its arguments exceed the size of the string. It is then easy to compare lengths without any comparison operator.
 
A second hack is to use a string to count the number of compliant strings already in the stack: the <code>+</code> and <code>STO+</code> commands present in the code are not arithmetic operations, but string operations aiming at appending characters.
 
The user stack is both the standard input and output.
{{works with|HP|49}}
≪ "X" → count
≪ 1 CF <span style="color:grey">@ flag 1 set means there are at least 2 strings in the stack</span>
'''WHILE''' "String?" "" INPUT DUP SIZE '''REPEAT'''
'''IF''' 1 FS? '''THEN'''
'''CASE'''
DUP2 SWAP SIZE DUP SUB NUM NOT '''THEN'''
DROP '''END'''
DUP2 SIZE DUP SUB NUM NOT '''THEN'''
count "X" + SIZE ROLLD count SIZE DROPN "X" 'count' STO '''END'''
"X" 'count' STO+
'''END'''
'''ELSE''' 1 SF '''END'''
'''END''' DROP
≫ ≫ '<span style="color:blue">TASK</span>' STO
 
<span style="color:blue">TASK</span>
"a" "bb" "ccc" "ddd" "ee" "f" "ggg" ""
{{out}}
<pre>
3: "ccc"
2: "ddd"
1: "ggg"
</pre>
 
Line 2,538 ⟶ 2,770:
=={{header|Wren}}==
{{trans|D}}
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin
 
// Return a.length - b.length if positive, 0 otherwise.
9,485

edits