Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -6,20 +6,28 @@ procedure Longest_String_Challenge is
Separator: constant Character := Ada.Characters.Latin_1.NUL;
procedure Funny_Stuff(B, L: in out Unbounded_String; N: Unbounded_String) is
-- B holds a list of all longest strings, separated by Separator
-- L holds longest string so far
-- N is the next string to be considered
Nat: Natural;
begin
Nat := Length(N) - Length(L);
-- (1) this raises exception if L longer then N
declare
Pos: Positive;
begin
Pos := Nat;
Pos := Nat; -- (2) this raises exception if L at least as long as N
-- at this point, we know N is longer then L
B := N;
L := N;
exception
when Constraint_Error => B := B & Separator & N;
when Constraint_Error -- come from (2)
-- at this point, we know L and N are of the same length
=> B := B & Separator & N; -- add N to the set of solutions
end;
exception
when Constraint_Error => null;
when Constraint_Error => null; -- come from (1)
-- at this point, we know L is longer then N
end Funny_Stuff;
Buffer: Unbounded_String := +"";
@ -29,10 +37,11 @@ procedure Longest_String_Challenge is
begin
while True loop
Next := + Ada.Text_IO.Get_Line;
-- (3) raises exception when trying to read beyond the end of file
Funny_Stuff(Buffer, Longest, Next);
end loop;
exception
when others =>
when others => -- come from (3)
for I in To_String(Buffer)'Range loop
if To_String(Buffer)(I) = Separator then
Ada.Text_IO.New_Line;

View file

@ -11,21 +11,26 @@ procedure Longest_String_Challenge is
S, T: String) is
C: Character;
begin
if S = T then
B := B & Separator & N;
else
C:= T(T'First); -- raises Constraint_Error if T is empty
begin
C := S(S'First); -- same if S is empty
Funny_Stuff(B,L,N,S(S'First+1 .. S'Last), T(T'First+1..T'Last)); --
exception
when Constraint_Error =>
B := N;
L := N;
end;
end if;
C:= T(T'First); -- (1) raises Constraint_Error if T is empty
begin
C := S(S'First); -- (2) raises Constraint_Error if S is empty
-- at this point, we know that neither S nor T are empty
Funny_Stuff(B,L,N,S(S'First+1 .. S'Last), T(T'First+1..T'Last));
exception
when Constraint_Error => -- come from (2), S is empty, T is not empty!
B := N;
L := N;
end;
exception
when Constraint_Error => null;
when Constraint_Error => -- come from (1), T is empty
begin
C := S(S'First); -- (3) raises Constraint_Error if S is empty
-- at this point, we know that T is empty and S isn't
null;
exception
when Constraint_Error => -- come from (3); both S and T are empty
B := B & Separator & N;
end;
end Funny_Stuff;
Buffer: Unbounded_String := +"";
@ -35,10 +40,11 @@ procedure Longest_String_Challenge is
begin
while True loop
Next := + Ada.Text_IO.Get_Line;
-- (4) raises exception when trying to read beyond end of file
Funny_Stuff(Buffer, Longest, Next, -Longest, -Next);
end loop;
exception
when others =>
when others => -- come from (4)
for I in To_String(Buffer)'Range loop
if To_String(Buffer)(I) = Separator then
Ada.Text_IO.New_Line;

View file

@ -0,0 +1,28 @@
import java.io.File;
import java.util.Scanner;
public class LongestStringChallenge {
public static void main(String[] args) throws Exception {
String lines = "", longest = "";
try (Scanner sc = new Scanner(new File("lines.txt"))) {
while(sc.hasNext()) {
String line = sc.nextLine();
if (longer(longest, line))
lines = longest = line;
else if (!longer(line, longest))
lines = lines.concat("\n").concat(line);
}
}
System.out.println(lines);
}
static boolean longer(String a, String b) {
try {
String dummy = a.substring(b.length());
} catch (StringIndexOutOfBoundsException e) {
return true;
}
return false;
}
}

View file

@ -0,0 +1,26 @@
function longer(s1, s2)
while true do
s1 = s1:sub(1, -2)
s2 = s2:sub(1, -2)
if s1:find('^$') and not s2:find('^$') then
return false
elseif s2:find('^$') then
return true
end
end
end
local output = ''
local longest = ''
for line in io.lines() do
local islonger = longer(line, longest)
if islonger and longer(longest, line) then
output = output .. line .. '\n'
elseif islonger then
longest = line
output = line .. '\n'
end
end
print(output)

View file

@ -0,0 +1,7 @@
function longer(s1, s2)
if s1:sub(#s2):find('^$') then
return false
else
return true
end
end

View file

@ -0,0 +1 @@
puts open("test.txt").each_line.group_by(&:size).max.last

View file

@ -0,0 +1,2 @@
val longest = scala.io.Source.fromFile(args.head).getLines.toIterable.groupBy(_.length).max._2
println(longest mkString "\n")