Kernighans large earthquake problem: Difference between revisions

m
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 3 users not shown)
Line 1:
{{task}}
 
[https[w://en.wikipedia.org/wiki/Brian_KernighanBrian Kernighan|Brian Kernighan]], in a [https://www.youtube.com/watch?v=Sg4U4r_AgJU lecture] at the University of Nottingham, described a [https://youtu.be/Sg4U4r_AgJU?t=50s problem] on which this task is based.
 
;Problem:
Line 1,007:
First, with a data file. This adds a fair amount of verbosity to COBOL. For something this one-off, a simpler cut using ACCEPT from standard in is shown.
 
<syntaxhighlight lang="cobolcobolfree">*>
*> Kernighan large earthquake problem
*>
*> Tectonics: cobc -xj kernighan-earth-quakes.cob
*> Kernighan large earthquake problem
*> *> Tectonics: cobc -xj kernighan-earth-quakes.cobtxt with the 3 sample lines
*> ./kernighan-earth-quakes.txt with the 3 sample lines
*>
*> ./kernighan-earth-quakes
>>SOURCE FORMAT IS FREE
*>
IDENTIFICATION DIVISION.
>>SOURCE FORMAT IS FREE
PROGRAM-ID. quakes.
identification division.
program-id. quakes.
 
ENVIRONMENT DIVISION.
environment division.
CONFIGURATION SECTION.
configuration section.
REPOSITORY.
repository.
FUNCTION ALL INTRINSIC.
function all intrinsic.
 
INPUT-OUTPUT SECTION.
input-output section.
FILE-CONTROL.
file-control.
selectSELECT quake-data
ASSIGN assign toTO command-filename
ORGANIZATION IS organizationLINE is line sequentialSEQUENTIAL
STATUS status isIS quake-fd-status.
.
 
DATA DIVISION.
data division.
FILE SECTION.
file section.
fd FD quake-data recordRECORD varyingVARYING dependingDEPENDING onON line-length.
01 data-line pic x PICTURE IS X(32768).
 
WORKING-STORAGE SECTION.
working-storage section.
01 quake-fd-status 01 quake-fd-statusPICTURE picIS xxXX.
88 ok 88 ok values VALUES ARE "00", "01", "02", "03", "04",
"05", "06", "07", "08", "09".
88 no-more 88 no-more value VALUE IS "10".
88 io-error 88 io-error value high VALUE IS HIGH-valueVALUE.
 
01 line-length USAGE IS BINARY-LONG.
01 line-length usage binary-long.
01 date-time PICTURE IS X(10).
01 quake PICTURE IS X(20).
01 magnitude PICTURE IS 99V99.
 
01 command-filename 01 date-timePICTURE picIS xX(1080).
01 quake pic x(20).
01 magnitude pic 99v99.
 
PROCEDURE DIVISION.
01 command-filename pic x(80).
show-big-ones.
procedure division.
show-big-ones.
 
acceptACCEPT command-filename fromFROM commandCOMMAND-lineLINE
ifIF command-filename equalIS EQUAL TO spacesSPACES thenTHEN
moveMOVE "data.txt" toTO command-filename
endEND-ifIF
 
OPEN open inputINPUT quake-data
performPERFORM status-check
ifIF io-error thenTHEN
DISPLAY display trimTRIM(command-filename) " not found" uponUPON syserrSYSERR
gobackGOBACK
endEND-ifIF
 
readREAD quake-data
performPERFORM status-check
PERFORM perform untilUNTIL no-more orOR io-error
unstringUNSTRING data-line delimitedDELIMITED byBY allALL spacesSPACES
intoINTO date-time quake magnitude
endEND-unstringUNSTRING
 
IF magnitude if magnitudeIS greaterGREATER thanTHAN 6
displayDISPLAY date-time spaceSPACE quake spaceSPACE magnitude
endEND-ifIF
 
readREAD quake-data
performPERFORM status-check
endEND-performPERFORM
 
closeCLOSE quake-data
performPERFORM status-check
gobackGOBACK.
*> *> ****
 
status-check.
IF if notNOT ok andAND notNOT no-more thenTHEN *> not normal status, bailing
displayDISPLAY "io error: " quake-fd-status uponUPON syserrSYSERR
setSET io-error toTO trueTRUE
endEND-ifIF
EXIT PARAGRAPH.
 
END end programPROGRAM quakes.</syntaxhighlight>
 
{{output}}
Line 1,110 ⟶ 1,108:
<syntaxhighlight lang="cobol"> *>
*> Tectonics: ./kerighan-earth-quakes <quakes.txt
identificationIDENTIFICATION divisionDIVISION.
programPROGRAM-idID. quakes.
 
dataDATA divisionDIVISION.
 
workingWORKING-storageSTORAGE sectionSECTION.
01 data-line pic x PICTURE IS X(32768).
88 no-more value high VALUE IS HIGH-valuesVALUES.
 
01 date-time pic x PICTURE IS X(10).
01 quake pic x PICTURE IS X(20).
01 magnitude pic 99v99 PICTURE IS 99V99.
 
procedurePROCEDURE divisionDIVISION.
show-big-ones.
 
acceptACCEPT data-line onON exceptionEXCEPTION setSET no-more toTO trueTRUE endEND-acceptACCEPT
performPERFORM untilUNTIL no-more
unstringUNSTRING data-line delimitedDELIMITED byBY allALL spacesSPACES
intoINTO date-time quake magnitude
endEND-unstringUNSTRING
 
ifIF magnitude greaterIS thanGREATER THAN 6
displayDISPLAY date-time spaceSPACE quake spaceSPACE magnitude
endEND-ifIF
 
acceptACCEPT data-line onON exceptionEXCEPTION setSET no-more toTO trueTRUE endEND-acceptACCEPT
endEND-performPERFORM
 
gobackGOBACK.
endEND programPROGRAM quakes.</syntaxhighlight>
 
That cut would be used as <pre>prompt$ ./kernighans-large-earthquakes <quakes.txt</pre>
Line 1,240 ⟶ 1,238:
5/18/1980 MountStHelens 7.6
1/25/4567 EdgeCase3 6.1</pre>
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Controls,SysUtils,Classes,StdCtrls,ExtCtrls}}
This code takes advantage of the standard Delphi "TStringGrid" object to do most of the heavy lifting. It is initially used to read the earthquake file into memory, breaking it up into of individual lines as it goes. Then the individual fields are stored in structurs/records attached to the data. finally, the data is sorted by magnitude so the earthquakes of magnitudes greater than six can be extracted. Because the data is now neatly organized in memory, all kinds of other process could be done, including sorting it by date or location. To make the problem more realistic, I extracted actual earthquake data from the first few months of 2023. I've post the data for other people to test here: [https://fountainware.com/download/EarthQuakes.txt EarthQuakes.txt]
 
<syntaxhighlight lang="Delphi">
{Structure used to contain all the earthquake data}
 
type TQuakeInfo = record
Date: TDate;
Name: string;
Mag: double;
end;
type PQuakeInfo = ^TQuakeInfo;
 
{Used to contain individual fields of the earthquake data}
 
type TStringArray = array of string;
 
 
function SortCompare(List: TStringList; Index1, Index2: Integer): Integer;
{Custom sort routine to sort data by magnitude }
var QI1,QI2: TQuakeInfo;
begin
QI1:=PQuakeInfo(List.Objects[Index1])^;
QI2:=PQuakeInfo(List.Objects[Index2])^;
Result:=Round(QI2.Mag*10)-Round(QI1.Mag*10);
end;
 
procedure GetFields(S: string; var SA: TStringArray);
{Extract the three fields from each row of data}
var I,F: integer;
begin
SetLength(SA,3);
for I:=0 to High(SA) do SA[I]:='';
F:=0;
for I:=1 to Length(S) do
if S[I] in [#$09,#$20] then Inc(F)
else SA[F]:=SA[F]+S[I];
end;
 
procedure AnalyzeEarthQuakes(Filename: string; Memo: TMemo);
{Read earhtquake data from specified file}
{Extract the individual fields and sort and display it}
var SL: TStringList;
var I: integer;
var S: string;
var FA: TStringArray;
var QI: PQuakeInfo;
begin
SL:=TStringList.Create;
try
{Read file, separating it into rows}
SL.LoadFromFile(Filename);
{Process each row}
for I:=0 to SL.Count-1 do
begin
S:=SL[I];
{Separate row into fields}
GetFields(S,FA);
{Store data as objects in TStringList}
New(QI);
QI.Date:=StrToDate(FA[0]);
QI.Name:=FA[1];
QI.Mag:=StrToFloat(FA[2]);
SL.Objects[I]:=TObject(QI);
end;
{Sort data by magnitude}
SL.CustomSort(SortCompare);
{Display sorted data}
for I:=0 to SL.Count-1 do
begin
if PQuakeInfo(SL.Objects[I]).Mag<6 then break;
S:=FormatDateTime('dd/mm/yyyy', PQuakeInfo(SL.Objects[I]).Date);
S:=S+Format(' %-34s',[PQuakeInfo(SL.Objects[I]).Name]);
S:=S+Format(' %5f',[PQuakeInfo(SL.Objects[I]).Mag]);
Memo.Lines.Add(S);
end;
{Dispose of memory}
finally
for I:=0 to SL.Count-1 do Dispose(PQuakeInfo(SL.Objects[I]));
SL.Free;
end;
end;
 
 
procedure ShowEarthQuakes(Memo: TMemo);
begin
AnalyzeEarthQuakes('EarthQuakes.txt',Memo);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
06/02/2023 Turkey_Kahramanmaras 7.80
09/01/2023 Indonesia_Maluku 7.60
06/02/2023 Turkey_Kahramanmaras 7.50
02/04/2023 Papua_New_Guinea_East_Sepik 7.00
16/03/2023 New_Zealand_Kermadec_Islands 7.00
18/01/2023 Indonesia_North_Maluku 7.00
14/04/2023 Indonesia_East_Java 7.00
08/01/2023 Vanuatu_Sanma 7.00
04/03/2023 New_Zealand_Kermadec_Islands 6.90
23/02/2023 Tajikistan_Gorno-Badakhshan 6.90
18/03/2023 Ecuador_Guayas 6.80
20/01/2023 Argentina_Santiago_del_Estero 6.80
18/04/2023 South_of_theFiji_Islands 6.70
06/02/2023 Turkey_Gaziantep 6.70
01/03/2023 Papua_New_Guinea_West_New_Britain 6.60
02/03/2023 Vanuatu_Sanma 6.50
03/04/2023 Russia_Kamchatka_Krai 6.50
22/03/2023 Argentina_Jujuy 6.50
21/03/2023 Afghanistan_Badakhshan 6.50
24/01/2023 Argentina_Santiago_del_Estero 6.40
19/04/2023 Papua_New_Guinea_West_New_Britain 6.30
16/01/2023 Japan_Bonin_Islands 6.30
20/02/2023 Turkey_Hatay 6.30
23/02/2023 Indonesia_North_Maluku 6.30
21/04/2023 Indonesia_Southeast_Sulawesi 6.30
14/03/2023 Papua_New_Guinea_Madang 6.30
30/03/2023 Chile_Maule 6.30
04/04/2023 Panama_Chiriqu- 6.30
25/02/2023 Papua_New_Guinea_West_New_Britain 6.20
04/04/2023 Philippines_Bicol 6.20
27/03/2023 Solomon_Islands_Isabel 6.10
03/04/2023 Indonesia_North_Sumatra 6.10
17/02/2023 Indonesia_Maluku 6.10
15/02/2023 Philippines_Bicol 6.10
13/02/2023 New_Zealand_Kermadec_Islands 6.10
20/01/2023 France_Guadeloupe 6.10
15/01/2023 Indonesia_Aceh 6.10
28/03/2023 Japan_Hokkaido 6.00
18/01/2023 Indonesia_Gorontalo 6.00
01/02/2023 Philippines_Davao 6.00
05/01/2023 Afghanistan_Badakhshan 6.00
26/01/2023 New_Zealand_Kermadec_Islands 6.00
06/02/2023 Turkey_Kahramanmaras 6.00
06/02/2023 Turkey_Malatya 6.00
25/02/2023 Japan_Hokkaido 6.00
13/04/2023 Canada_British_Columbia 6.00
</pre>
 
=={{header|D}}==
Line 2,198 ⟶ 2,340:
=={{header|Wren}}==
{{libheader|Wren-pattern}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "os" for Process
import "./pattern" for Pattern
 
var args = Process.arguments
9,476

edits