From ac93068a90b83de448d144893311d9a5e95c72f8 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 5 Feb 2021 07:39:25 +0000 Subject: - Tidied up file name for Ada. --- challenge-098/paulo-custodio/ada/ch-1.adb | 86 +++++++++++++++++++++++++ challenge-098/paulo-custodio/ada/ch-2.adb | 100 ++++++++++++++++++++++++++++++ challenge-098/paulo-custodio/ada/ch_1.adb | 86 ------------------------- challenge-098/paulo-custodio/ada/ch_2.adb | 100 ------------------------------ 4 files changed, 186 insertions(+), 186 deletions(-) create mode 100644 challenge-098/paulo-custodio/ada/ch-1.adb create mode 100644 challenge-098/paulo-custodio/ada/ch-2.adb delete mode 100644 challenge-098/paulo-custodio/ada/ch_1.adb delete mode 100644 challenge-098/paulo-custodio/ada/ch_2.adb diff --git a/challenge-098/paulo-custodio/ada/ch-1.adb b/challenge-098/paulo-custodio/ada/ch-1.adb new file mode 100644 index 0000000000..47c6decd43 --- /dev/null +++ b/challenge-098/paulo-custodio/ada/ch-1.adb @@ -0,0 +1,86 @@ +-- Challenge 098 +-- +-- TASK #1 › Read N-characters +-- Submitted by: Mohammad S Anwar +-- You are given file $FILE. +-- +-- Create subroutine readN($FILE, $number) returns the first n-characters and +-- moves the pointer to the (n+1)th character. +-- +-- Example: +-- Input: Suppose the file (input.txt) contains "1234567890" +-- Output: +-- print readN("input.txt", 4); # returns "1234" +-- print readN("input.txt", 4); # returns "5678" +-- print readN("input.txt", 4); # returns "90" + + +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Command_Line; +with Ada.Strings.Bounded; + +procedure ch_1 is + -- command line arguments + package CL renames Ada.Command_Line; + + -- strings for filenames + package SB is new Ada.Strings.Bounded.Generic_Bounded_Length(Max => 100); + use SB; + + -- maximum number of different files + Max_Files : constant := 100; + + -- arrays of already seen files and their File_Type's + Filenames : array (1 .. Max_Files) of SB.Bounded_String; + File_IDs : array (1 .. Max_Files) of File_Type; + Last_ID : Integer := 0; + + -- either open file and append to array, or return index if already open + function open_file(Filename : SB.Bounded_String) + return Integer is + begin + -- search already open file + for I in 1 .. Last_ID loop + if Filename = Filenames(I) then + return I; + end if; + end loop; + + -- open new file + Last_ID := Last_ID + 1; + Filenames(Last_ID) := Filename; + Open(File_IDs(Last_ID), In_File, To_String(Filename)); + return Last_ID; + end open_file; + + -- read the next N chars from the given file + function readN(Filename : SB.Bounded_String; Number_Chars : Integer) + return SB.Bounded_String is + ID : Integer; + C : Character; + Text : SB.Bounded_String; + begin + ID := open_file(Filename); + for I in 1 .. Number_Chars loop + if not End_Of_File(File_IDs(ID)) then + Get(File_IDs(ID), C); + SB.Append(Text, C); + end if; + end loop; + return Text; + end readN; + + -- local variables + Filename : SB.Bounded_String; + Number_Chars : Integer := 0; + I : Integer := 1; + Text : SB.Bounded_String; +begin + while I+1 <= CL.Argument_Count loop + Filename := SB.To_Bounded_String(CL.Argument(I)); + Number_Chars := Integer'Value(CL.Argument(I+1)); + Text := readN(Filename, Number_Chars); + Put_Line(To_String(Text)); + I := I + 2; + end loop; +end ch_1; diff --git a/challenge-098/paulo-custodio/ada/ch-2.adb b/challenge-098/paulo-custodio/ada/ch-2.adb new file mode 100644 index 0000000000..7cf52f9fb0 --- /dev/null +++ b/challenge-098/paulo-custodio/ada/ch-2.adb @@ -0,0 +1,100 @@ +-- Challenge 098 +-- +-- TASK #2 › Search Insert Position +-- Submitted by: Mohammad S Anwar +-- You are given a sorted array of distinct integers @N and a target $N. +-- +-- Write a script to return the index of the given target if found +-- otherwise place the target in the sorted array and return the index. +-- +-- Example 1: +-- Input: @N = (1, 2, 3, 4) and $N = 3 +-- Output: 2 since the target 3 is in the array at the index 2. +-- Example 2: +-- Input: @N = (1, 3, 5, 7) and $N = 6 +-- Output: 3 since the target 6 is missing and should be placed at +-- the index 3. +-- Example 3: +-- Input: @N = (12, 14, 16, 18) and $N = 10 +-- Output: 0 since the target 10 is missing and should be placed at +-- the index 0. +-- Example 4: +-- Input: @N = (11, 13, 15, 17) and $N = 19 +-- Output: 4 since the target 19 is missing and should be placed at +-- the index 4. + +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Command_Line; +with Ada.Containers.Vectors; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; + +procedure ch_2 is + -- command line arguments + package CL renames Ada.Command_Line; + + -- vector of integers + package IV is new Ada.Containers.Vectors + (Index_Type => Natural, + Element_Type => Integer); + use IV; + + -- print a vector + procedure print_vector(V : Vector) is + begin + Put("("); + for I in V.First_Index .. V.Last_Index loop + if I /= V.First_Index then + Put(", "); + end if; + Put(Trim(Integer'Image(V(I)), Ada.Strings.Left)); + end loop; + Put(")"); + end print_vector; + + -- search for index of element, insert in array if not found + function search_insert(V : in out Vector; N : Integer) return Integer is + B, T, M : Integer; + begin + B := V.First_Index; + T := V.Last_Index; + if T < B then -- vector empty + V.Append(N); + return 0; + elsif N < V(B) then -- before first + V.Prepend(N); + return 0; + elsif N > V(T) then -- after last + V.Append(N); + return V.Last_Index; + else -- do binary search + M := (T+B)/2; + while B+1 100); - use SB; - - -- maximum number of different files - Max_Files : constant := 100; - - -- arrays of already seen files and their File_Type's - Filenames : array (1 .. Max_Files) of SB.Bounded_String; - File_IDs : array (1 .. Max_Files) of File_Type; - Last_ID : Integer := 0; - - -- either open file and append to array, or return index if already open - function open_file(Filename : SB.Bounded_String) - return Integer is - begin - -- search already open file - for I in 1 .. Last_ID loop - if Filename = Filenames(I) then - return I; - end if; - end loop; - - -- open new file - Last_ID := Last_ID + 1; - Filenames(Last_ID) := Filename; - Open(File_IDs(Last_ID), In_File, To_String(Filename)); - return Last_ID; - end open_file; - - -- read the next N chars from the given file - function readN(Filename : SB.Bounded_String; Number_Chars : Integer) - return SB.Bounded_String is - ID : Integer; - C : Character; - Text : SB.Bounded_String; - begin - ID := open_file(Filename); - for I in 1 .. Number_Chars loop - if not End_Of_File(File_IDs(ID)) then - Get(File_IDs(ID), C); - SB.Append(Text, C); - end if; - end loop; - return Text; - end readN; - - -- local variables - Filename : SB.Bounded_String; - Number_Chars : Integer := 0; - I : Integer := 1; - Text : SB.Bounded_String; -begin - while I+1 <= CL.Argument_Count loop - Filename := SB.To_Bounded_String(CL.Argument(I)); - Number_Chars := Integer'Value(CL.Argument(I+1)); - Text := readN(Filename, Number_Chars); - Put_Line(To_String(Text)); - I := I + 2; - end loop; -end ch_1; diff --git a/challenge-098/paulo-custodio/ada/ch_2.adb b/challenge-098/paulo-custodio/ada/ch_2.adb deleted file mode 100644 index 7cf52f9fb0..0000000000 --- a/challenge-098/paulo-custodio/ada/ch_2.adb +++ /dev/null @@ -1,100 +0,0 @@ --- Challenge 098 --- --- TASK #2 › Search Insert Position --- Submitted by: Mohammad S Anwar --- You are given a sorted array of distinct integers @N and a target $N. --- --- Write a script to return the index of the given target if found --- otherwise place the target in the sorted array and return the index. --- --- Example 1: --- Input: @N = (1, 2, 3, 4) and $N = 3 --- Output: 2 since the target 3 is in the array at the index 2. --- Example 2: --- Input: @N = (1, 3, 5, 7) and $N = 6 --- Output: 3 since the target 6 is missing and should be placed at --- the index 3. --- Example 3: --- Input: @N = (12, 14, 16, 18) and $N = 10 --- Output: 0 since the target 10 is missing and should be placed at --- the index 0. --- Example 4: --- Input: @N = (11, 13, 15, 17) and $N = 19 --- Output: 4 since the target 19 is missing and should be placed at --- the index 4. - -with Ada.Text_IO; use Ada.Text_IO; -with Ada.Command_Line; -with Ada.Containers.Vectors; -with Ada.Strings.Fixed; use Ada.Strings.Fixed; - -procedure ch_2 is - -- command line arguments - package CL renames Ada.Command_Line; - - -- vector of integers - package IV is new Ada.Containers.Vectors - (Index_Type => Natural, - Element_Type => Integer); - use IV; - - -- print a vector - procedure print_vector(V : Vector) is - begin - Put("("); - for I in V.First_Index .. V.Last_Index loop - if I /= V.First_Index then - Put(", "); - end if; - Put(Trim(Integer'Image(V(I)), Ada.Strings.Left)); - end loop; - Put(")"); - end print_vector; - - -- search for index of element, insert in array if not found - function search_insert(V : in out Vector; N : Integer) return Integer is - B, T, M : Integer; - begin - B := V.First_Index; - T := V.Last_Index; - if T < B then -- vector empty - V.Append(N); - return 0; - elsif N < V(B) then -- before first - V.Prepend(N); - return 0; - elsif N > V(T) then -- after last - V.Append(N); - return V.Last_Index; - else -- do binary search - M := (T+B)/2; - while B+1