June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,8 +1,6 @@
|
|||
;Background:
|
||||
This "longest string challenge" is inspired by a problem that used to be given to students learning Icon. Students were expected to try to solve the problem in Icon and another language with which the student was already familiar. The basic problem is quite simple; the challenge and fun part came through the introduction of restrictions. Experience has shown that the original restrictions required some adjustment to bring out the intent of the challenge and make it suitable for Rosetta Code.
|
||||
|
||||
The original programming challenge and some solutions can be found at [https://tapestry.tucson.az.us/twiki/bin/view/Main/LongestStringsPuzzle Unicon Programming TWiki / Longest Strings Puzzle]. (See notes on the talk page if you have trouble with the site).
|
||||
|
||||
|
||||
;Basic problem statement
|
||||
Write a program that reads lines from standard input and, upon end of file, writes the longest line to standard output.
|
||||
|
|
@ -54,7 +52,7 @@ The guiding principle here should be to be creative in demonstrating some of the
|
|||
Now having said that, the restrictions require some elaboration.
|
||||
|
||||
* In general, the restrictions are meant to avoid the explicit use of these features.
|
||||
* "No comparison operators may be used" - At some level there must be some test that allows the solution to get at the length and determine if one string is longer. Comparison operators, in particular any less/greater comparison should be avoided. Representing the length of any string as a number should also be avoided. Various approaches allow for detecting the end of a string. Some of these involve implicitly using equal/not-equal; however, explicitly using equal/not-equal should be acceptable.
|
||||
* "No comparison operators may be used" - At some level there must be some test that allows the solution to get at the length and determine if one string is longer. Comparison operators, in particular any less/greater comparison should be avoided. <u>Representing the length of any string as a number should also be avoided.</u> Various approaches allow for detecting the end of a string. Some of these involve implicitly using equal/not-equal; however, explicitly using equal/not-equal should be acceptable.
|
||||
* "No arithmetic operations" - Again, at some level something may have to advance through the string. Often there are ways a language can do this implicitly advance a cursor or pointer without explicitly using a +, - , ++, --, add, subtract, etc.
|
||||
* The datatype restrictions are amongst the most difficult to reinterpret. In the language of the original challenge strings are atomic datatypes and structured datatypes like lists are quite distinct and have many different operations that apply to them. This becomes a bit fuzzier with languages with a different programming paradigm. The intent would be to avoid using an easy structure to accumulate the longest strings and spit them out. There will be some natural reinterpretation here.
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
module Main where
|
||||
|
||||
import System.Environment
|
||||
|
||||
cmp :: String -> String -> Ordering
|
||||
cmp [] [] = EQ
|
||||
cmp [] (_:_) = LT
|
||||
cmp (_:_) [] = GT
|
||||
cmp (_:xs) (_:ys) = cmp xs ys
|
||||
|
||||
longest :: String -> String
|
||||
longest = longest' "" "" . lines
|
||||
where
|
||||
longest' acc l [] = acc
|
||||
longest' [] l (x:xs) = longest' x x xs
|
||||
longest' acc l (x:xs) = case cmp l x of
|
||||
LT -> longest' x x xs
|
||||
EQ -> longest' (acc ++ '\n':x) l xs
|
||||
GT -> longest' acc l xs
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
(file:_) <- getArgs
|
||||
contents <- readFile file
|
||||
putStrLn $ longest contents
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
function longer(a, b)
|
||||
try
|
||||
b[endof(a)]
|
||||
catch
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function printlongest(io::IO)
|
||||
lines = longest = ""
|
||||
while !eof(io)
|
||||
line = readline(io)
|
||||
if longer(line, longest)
|
||||
longest = lines = line
|
||||
elseif !longer(longest, line)
|
||||
lines *= "\n" * line
|
||||
end
|
||||
end
|
||||
println(lines)
|
||||
end
|
||||
printlongest(str::String) = printlongest(IOBuffer(str))
|
||||
|
||||
printlongest("a\nbb\nccc\nddd\nee\nf\nggg")
|
||||
|
|
@ -1,14 +1,16 @@
|
|||
import fileinput
|
||||
|
||||
# originally, return len(a) - len(b) if positive, 0 otherwise.
|
||||
# Observing that longer is used for its Boolean result,
|
||||
# and that '' is False, while any other string is True,
|
||||
# longer need only to return a after removing len(b) characters,
|
||||
# which is done without resorting to len().
|
||||
# This returns True if the second string has a value on the
|
||||
# same index as the last index of the first string. It runs
|
||||
# faster than trimming the strings because it runs len once
|
||||
# and is a single index lookup versus slicing both strings
|
||||
# one character at a time.
|
||||
def longer(a, b):
|
||||
while a and b:
|
||||
a, b = a[1:], b[1:]
|
||||
return a
|
||||
try:
|
||||
b[len(a)-1]
|
||||
return False
|
||||
except:
|
||||
return True
|
||||
|
||||
longest, lines = '', ''
|
||||
for x in fileinput.input():
|
||||
|
|
|
|||
|
|
@ -1,17 +1,19 @@
|
|||
/*REXX pgm reads a file & prints the longest [widest] record(s)/line(s).*/
|
||||
!.='' /*initialize stemmed array to nul*/
|
||||
fileID='LONGEST1.TXT' /*point to the input file. */
|
||||
m=0
|
||||
|
||||
do while min(lines(fileID),1); _=linein(fileID); w=length(_)
|
||||
say 'input =' _ /*display the input to terminal. */
|
||||
!.w=!.w || '0a'x || _ /*build a stemmed array element. */
|
||||
m=max(m,w) /*find the maximum width so far. */
|
||||
end /*while min(lines(... */
|
||||
|
||||
do j=m for m /*handle the case of no input. */
|
||||
say center(' longest record(s): ',79,'═')
|
||||
say substr(!.m,2)
|
||||
say center(' list end ',79,'═')
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
end /*j*/
|
||||
/*REXX program reads a file and displays the longest [widest] record(s) [line(s)]. */
|
||||
signal on notReady /*when E-O-F is reached, jump/branch. */
|
||||
iFID= 'LONGEST.TXT' /*the default file identifier for input*/
|
||||
parse arg fid . /*obtain optional argument from the CL.*/
|
||||
do #=1 to length(fid); iFID=fid /*Specified? Then use what's given. */
|
||||
end /*#*/
|
||||
!= /*the maximum width (so far). */
|
||||
do forever; _=linein(iFID); ?=_ /*read a line from the input file. */
|
||||
t=0 /*don't do the initialization next time*/
|
||||
do #=t for t; !=?; ?=; $=. || _; end /*just do 1st time.*/
|
||||
do #=length(!' ') to length(?) for 1; $=; end /*found widest rec.*/
|
||||
do #=length(!) to length(?) for 1; $=$'a0d'x || _; end /*append it to $. */
|
||||
/* [↑] variable # isn't really used.*/
|
||||
!=left(., max( length(!), length(?) ) ) /*!: is the max length record, so far.*/
|
||||
end /*forever*/
|
||||
/* [↓] comes here when file gets E─O─F*/
|
||||
notReady: do j=length(!) to length(!) for length(!) /*handle the case of no input*/
|
||||
say substr($, 2) /*display (all) the longest records. */
|
||||
end /*j*/ /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,18 +1,44 @@
|
|||
/*REXX pgm reads a file & prints the longest [widest] record(s)/line(s).*/
|
||||
!.='' /*initialize stemmed array to nul*/
|
||||
fileID='LONGEST2.TXT' /*point to the input file. */
|
||||
signal on notready /*when E-O-F is reached, jump. */
|
||||
m=0 /*maximum width line so far. */
|
||||
|
||||
do forever; _=linein(fileID); w=length(_) /*read a line. */
|
||||
say 'input =' _ /*display the input to terminal. */
|
||||
!.w=!.w || '0a'x || _ /*build a stemmed array element. */
|
||||
m=max(m,w) /*find the maximum width so far. */
|
||||
end /*forever*/
|
||||
|
||||
notready: do j=m for m /*handle the case of no input. */
|
||||
say center(' longest record(s): ',79,'═')
|
||||
say substr(!.m,2)
|
||||
say center(' list end ',79,'═')
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
end /*j*/
|
||||
/* REXX ***************************************************************
|
||||
* 27.10.2010 Walter Pachl
|
||||
**********************************************************************/
|
||||
Parse Arg fid
|
||||
If fid='' Then Do
|
||||
"ALLOC FI(IN) DA('N561985.PRIV.V100(LL)') SHR REUSE"
|
||||
'EXECIO * DISKR IN (STEM L. FINIS' /* read all lines */
|
||||
'FREE FI(IN)'
|
||||
End
|
||||
Else Do
|
||||
Do i=1 By 1 While lines(fid)>0
|
||||
l.i=linein(fid)
|
||||
End
|
||||
l.0=i-1
|
||||
End
|
||||
maxl = 0 /* initialize maximum length */
|
||||
Do i=1 To l.0 /* loop through all lines */
|
||||
linl=length(l.i) /* length of current line */
|
||||
Select
|
||||
When linl>maxl Then Do /* line longer than preceding */
|
||||
maxl=linl /* initialize maximum length */
|
||||
mem.0=1 /* memory has one entry */
|
||||
mem.1=l.i /* the current line */
|
||||
lin.1=i /* its line number */
|
||||
End
|
||||
When linl=maxl Then Do /* line as long as maximum */
|
||||
z=mem.0+1 /* new memory index */
|
||||
mem.z=l.i /* the current line */
|
||||
lin.z=i /* its line number */
|
||||
mem.0=z /* memory size */
|
||||
End
|
||||
Otherwise /* line is shorter than max. */
|
||||
Nop /* ignore */
|
||||
End
|
||||
End
|
||||
If mem.0>0 Then Do
|
||||
Say 'Maximum line length='maxl
|
||||
Say ' Line Contents'
|
||||
Do i=1 To mem.0
|
||||
Say right(lin.i,5) mem.i
|
||||
End
|
||||
End
|
||||
Else
|
||||
Say 'No lines in input file or file does not exist'
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
# Project : Longest string challenge
|
||||
# Date : 2017/10/11
|
||||
# Author : Gal Zsolt (~ CalmoSoft ~)
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
load "stdlib.ring"
|
||||
|
||||
test = ["a", "bb", "ccc", "ddd", "ee", "f", "ggg"]
|
||||
test1 = []
|
||||
test2 = []
|
||||
|
||||
for n = 1 to len(test)
|
||||
add(test1, [test[n], len(test[n])])
|
||||
next
|
||||
sortFirstSecond(test1, 2)
|
||||
|
||||
for n = len(test1) to 2 step -1
|
||||
if test1[n][2] = test1[n-1][2]
|
||||
add(test2, test1[n][1])
|
||||
else
|
||||
add(test2, test1[n][1])
|
||||
exit
|
||||
ok
|
||||
next
|
||||
test2 = sort(test2)
|
||||
see test2 + nl
|
||||
Loading…
Add table
Add a link
Reference in a new issue