Longest string challenge: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Adding Quackery.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 4 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 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,384 ⟶ 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,614 ⟶ 2,770:
=={{header|Wren}}==
{{trans|D}}
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin
 
// Return a.length - b.length if positive, 0 otherwise.
9,482

edits