aboutsummaryrefslogtreecommitdiff
path: root/challenge-098
diff options
context:
space:
mode:
authorCris-HD <crisn7@hotmail.com>2021-02-06 16:38:49 +0100
committerCris-HD <crisn7@hotmail.com>2021-02-06 16:38:49 +0100
commit648113e083baf279daf0d7e1e9b34b216f85557d (patch)
treec99a472703d8cedcc4e9b062b3f47609840732a0 /challenge-098
parent475be2b683ff50041adc741e19abb5e9ed288963 (diff)
downloadperlweeklychallenge-club-648113e083baf279daf0d7e1e9b34b216f85557d.tar.gz
perlweeklychallenge-club-648113e083baf279daf0d7e1e9b34b216f85557d.tar.bz2
perlweeklychallenge-club-648113e083baf279daf0d7e1e9b34b216f85557d.zip
Added challenge 98 solution
Diffstat (limited to 'challenge-098')
-rw-r--r--challenge-098/cristian-heredia/perl/ch-1.pl67
-rw-r--r--challenge-098/cristian-heredia/perl/ch-2.pl58
-rw-r--r--challenge-098/cristian-heredia/python/ch-1.py45
-rw-r--r--challenge-098/cristian-heredia/python/ch-2.py49
4 files changed, 219 insertions, 0 deletions
diff --git a/challenge-098/cristian-heredia/perl/ch-1.pl b/challenge-098/cristian-heredia/perl/ch-1.pl
new file mode 100644
index 0000000000..533376e39d
--- /dev/null
+++ b/challenge-098/cristian-heredia/perl/ch-1.pl
@@ -0,0 +1,67 @@
+=begin
+
+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"
+
+=end
+=cut
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+#Input
+my $number = 4;
+my $text = '1234567890';
+
+#Variable
+my $file_name = "";
+
+#Call function
+askFileName();
+
+
+#Request the name of the file to the user
+sub askFileName {
+
+ print "Please, introduce the name of the file\n";
+ $file_name = <>;
+ $file_name =~ s/\s//g;
+
+ #Check that the file exists, if not, it's created
+ if ( !-f $file_name) {
+ createFile();
+ }
+ #Call the function that returns the first n-characters and moves the pointer to the (n+1)th character
+ readN($file_name, $number);
+}
+
+#Create the file with a specific text
+sub createFile {
+ open (F, ">>$file_name") || die "Could not open file: $!\n";
+ print F "$text";
+ close F;
+}
+
+#Returns the first n-characters and moves the pointer to the (n+1)th character
+sub readN {
+ my $file = shift;
+ my $N = shift;
+
+ open(FILE, $file) or die "Could not read from $file\n";
+
+ #Read the file, groups the characters n by n
+ while (read FILE, my $char, $N) {
+ print "print readN(\"$file_name\", $number); # returns \"$char\"\n";
+ }
+}
diff --git a/challenge-098/cristian-heredia/perl/ch-2.pl b/challenge-098/cristian-heredia/perl/ch-2.pl
new file mode 100644
index 0000000000..ff993db7ce
--- /dev/null
+++ b/challenge-098/cristian-heredia/perl/ch-2.pl
@@ -0,0 +1,58 @@
+=begin
+
+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.
+
+=end
+=cut
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+#Inputs
+my @NList = (11, 13, 15, 17);
+my $N = 19;
+
+#Call the function to search in the array
+findNumber();
+
+#If the target isn't in the array, the target will be added into the array and the "new array" will be sorted. Then, call the search function (with the new array)
+if (findNumber() == -1 ) {
+ push @NList, $N;
+ my @sorted = sort { $a <=> $b } @NList;
+ @NList = @sorted;
+ findNumber();
+}
+
+#Print the position of target inside the array
+print (findNumber()."\n");
+
+#Function to check if the target is in the array
+sub findNumber{
+
+ #walk through the array
+ foreach (my $i=0; $i <@NList; $i++){
+ #If the target is in the array, it will return its position
+ if ($NList[$i] == $N) {
+ return $i;
+ }
+ }
+ return -1;
+}
diff --git a/challenge-098/cristian-heredia/python/ch-1.py b/challenge-098/cristian-heredia/python/ch-1.py
new file mode 100644
index 0000000000..8d7cf9bd41
--- /dev/null
+++ b/challenge-098/cristian-heredia/python/ch-1.py
@@ -0,0 +1,45 @@
+'''
+
+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"
+
+'''
+
+import os
+
+number = 4
+text = 1234567890
+
+#Returns the first n-characters and moves the pointer to the (n+1)th character
+def readN(fileName, N):
+ #The file is opened and copy its content in the variable "contents"
+ with open (fileName, 'rt') as myfile:
+ contents = myfile.read()
+ #Read the file's content and groups the characters n by n
+ for line in ([contents[i:i+N] for i in range(0, len(contents), N)]):
+ print("print readN(\"{}\", {}); # returns \"{}\"".format(fileName, N, line))
+
+#Request the name of the file to the user
+def askFileName():
+ file_name = input("Please, introduce the name of the file\n")
+
+ #We check that the file exists, if not, the file is created with a specific text
+ if (os.path.isfile(file_name) == False):
+ f = open(file_name, 'w')
+ f.write(str(text))
+ f.close()
+ readN(file_name, number)
+
+#Request the name of the file to the user
+askFileName()
+ \ No newline at end of file
diff --git a/challenge-098/cristian-heredia/python/ch-2.py b/challenge-098/cristian-heredia/python/ch-2.py
new file mode 100644
index 0000000000..f54487fb69
--- /dev/null
+++ b/challenge-098/cristian-heredia/python/ch-2.py
@@ -0,0 +1,49 @@
+'''
+
+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.
+
+'''
+
+#Input
+NList = (1, 2, 3, 4)
+N = 3
+
+#Function to check if the target is in the list
+def findNumber():
+ #walk through the list
+ for num in range(len(NList)):
+ #If the target is in the list, it will return its position
+ if NList[num] == N:
+ return num
+ return -1
+
+#Call the function to search in the list
+findNumber()
+
+#If the target isn't in the list, the target will be added into the list and the "new list" will be sorted. Then, call the search function (with the new list)
+if (findNumber() == -1 ):
+ numbers = list(NList)
+ numbers.append(N)
+ NList = sorted(numbers)
+ findNumber()
+
+#Print the position of target inside the list
+print (findNumber())
+