From d329d9f116ca59f1733f18509130dc7461782ecf Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 16 Jun 2021 23:11:52 +0100 Subject: Add Perl solution to challenge 117 --- challenge-117/paulo-custodio/perl/ch-1.pl | 35 +++++++++++++++++ challenge-117/paulo-custodio/perl/ch-2.pl | 61 ++++++++++++++++++++++++++++++ challenge-117/paulo-custodio/t/test-1.yaml | 25 ++++++++++++ challenge-117/paulo-custodio/t/test-2.yaml | 15 ++++++++ challenge-117/paulo-custodio/test.pl | 7 ++++ 5 files changed, 143 insertions(+) create mode 100644 challenge-117/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-117/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-117/paulo-custodio/t/test-1.yaml create mode 100644 challenge-117/paulo-custodio/t/test-2.yaml create mode 100755 challenge-117/paulo-custodio/test.pl diff --git a/challenge-117/paulo-custodio/perl/ch-1.pl b/challenge-117/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..8d2ae98cfb --- /dev/null +++ b/challenge-117/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +# Challenge 117 +# +# TASK #1 - Missing Row +# Submitted by: Mohammad S Anwar +# You are given text file with rows numbered 1-15 in random order but there +# is a catch one row in missing in the file. +# +# 11, Line Eleven +# 1, Line one +# 9, Line Nine +# 13, Line Thirteen +# 2, Line two +# 6, Line Six +# 8, Line Eight +# 10, Line Ten +# 7, Line Seven +# 4, Line Four +# 14, Line Fourteen +# 3, Line three +# 15, Line Fifteen +# 5, Line Five +# Write a script to find the missing row number. + +use Modern::Perl; +my @rows; +while (<>) { + chomp; + my($nr, $text) = split /,\s*/, $_; + $rows[$nr] = $text; +} +say join(",", (map {$_->[0]} + grep {!defined $_->[1]} + map {[$_ => $rows[$_]]} 1..15)); diff --git a/challenge-117/paulo-custodio/perl/ch-2.pl b/challenge-117/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..6baa2d50d3 --- /dev/null +++ b/challenge-117/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl + +# Challenge 117 +# +# TASK #2 - Find Possible Paths +# Submitted by: E. Choroba +# You are given size of a triangle. +# +# Write a script to find all possible paths from top to the bottom right +# corner. +# +# In each step, we can either move horizontally to the right (H), or move +# downwards to the left (L) or right (R). +# +# BONUS: Try if it can handle triangle of size 10 or 20. +# +# Example 1: +# Input: $N = 2 +# +# S +# / \ +# / _ \ +# /\ /\ +# /__\ /__\ E +# +# Output: RR, LHR, LHLH, LLHH, RLH, LRH +# Example 2: +# Input: $N = 1 +# +# S +# / \ +# / _ \ E +# +# Output: R, LH + +use Modern::Perl; +my $N = shift || 1; +say join(', ', paths($N)); + +sub paths { + my($size) = @_; + my @paths; + find_paths(\@paths, $size, '', 0, 0); + return @paths; +} + +sub find_paths { + my($paths, $size, $path, $row, $col) = @_; + if ($row == $size && $col == $size) { # reached end + push @$paths, $path; + } + else { + if ($row < $size) { + find_paths($paths, $size, $path.'L', $row+1, $col); + find_paths($paths, $size, $path.'R', $row+1, $col+1); + } + if ($col < $row) { + find_paths($paths, $size, $path.'H', $row, $col+1); + } + } +} diff --git a/challenge-117/paulo-custodio/t/test-1.yaml b/challenge-117/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..c473de96c2 --- /dev/null +++ b/challenge-117/paulo-custodio/t/test-1.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: + input: | + 1, Line one + output: 2,3,4,5,6,7,8,9,10,11,12,13,14,15 +- setup: + cleanup: + args: + input: | + 11, Line Eleven + 1, Line one + 9, Line Nine + 13, Line Thirteen + 2, Line two + 6, Line Six + 8, Line Eight + 10, Line Ten + 7, Line Seven + 4, Line Four + 14, Line Fourteen + 3, Line three + 15, Line Fifteen + 5, Line Five + output: 12 diff --git a/challenge-117/paulo-custodio/t/test-2.yaml b/challenge-117/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..512e041cca --- /dev/null +++ b/challenge-117/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 + input: + output: LH, R +- setup: + cleanup: + args: 2 + input: + output: LLHH, LRH, LHLH, LHR, RLH, RR +- setup: + cleanup: + args: 3 + input: + output: LLLHHH, LLRHH, LLHLHH, LLHRH, LLHHLH, LLHHR, LRLHH, LRRH, LRHLH, LRHR, LHLLHH, LHLRH, LHLHLH, LHLHR, LHRLH, LHRR, RLLHH, RLRH, RLHLH, RLHR, RRLH, RRR diff --git a/challenge-117/paulo-custodio/test.pl b/challenge-117/paulo-custodio/test.pl new file mode 100755 index 0000000000..cf1ced98e0 --- /dev/null +++ b/challenge-117/paulo-custodio/test.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.030; + +require '../../challenge-001/paulo-custodio/test.pl'; -- cgit From a87d8dbe5ea94e4f78c20b9c9d25b48c3f5c16e3 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 17 Jun 2021 23:43:49 +0100 Subject: Add C and C++ solutions to challenge 004 --- .gitignore | 2 + challenge-004/paulo-custodio/c/ch-1.c | 20 +++++++++ challenge-004/paulo-custodio/c/ch-2.c | 71 +++++++++++++++++++++++++++++++ challenge-004/paulo-custodio/cpp/ch-1.cpp | 20 +++++++++ challenge-004/paulo-custodio/cpp/ch-2.cpp | 61 ++++++++++++++++++++++++++ 5 files changed, 174 insertions(+) create mode 100644 challenge-004/paulo-custodio/c/ch-1.c create mode 100644 challenge-004/paulo-custodio/c/ch-2.c create mode 100644 challenge-004/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-004/paulo-custodio/cpp/ch-2.cpp diff --git a/.gitignore b/.gitignore index db4cc30c24..43ef1dae98 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ *.o *.obj *.ali +ch-1 +ch-2 # Rust languageoutput directory target/ diff --git a/challenge-004/paulo-custodio/c/ch-1.c b/challenge-004/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..ab42e85d3d --- /dev/null +++ b/challenge-004/paulo-custodio/c/ch-1.c @@ -0,0 +1,20 @@ +/* +Challenge 004 +Challenge #1 +Write a script to output the same number of PI digits as the size of your script. +Say, if your script size is 10, it should print 3.141592653. +https://crypto.stanford.edu/pbc/notes/pi/code.html +*/ +#include +#include +#include +char buf[1024]; +int a=10000,b,c=2800,d,e,f[2801],g; +int main(int argc,char**argv){ +for(;b-c;)f[b++]=a/5; +for(;d=0,g=c*2;c-=14,sprintf(buf+strlen(buf),"%.4d",e+d/a),e=d%a) +for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b); +a=argc==2?atoi(argv[1]):800;if(buf[a]>='5'){for(b=a-1;b>0;b--){ +buf[b]++;if(buf[b]<='9')break;buf[b]--;}};buf[a]=0; +printf("%c.%s\n",buf[0],buf+1); +} diff --git a/challenge-004/paulo-custodio/c/ch-2.c b/challenge-004/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..3bd1dfb317 --- /dev/null +++ b/challenge-004/paulo-custodio/c/ch-2.c @@ -0,0 +1,71 @@ +/* +Challenge 004 +Challenge #2 +You are given a file containing a list of words (case insensitive 1 word per +line) and a list of letters. Print each word from the file that can be made +using only letters from the list. You can use each letter only once (though +there can be duplicates and you can use each of them once), you don’t have to +use all the letters. (Disclaimer: The challenge was proposed by Scimon Proctor) +*/ + +#include +#include +#include +#include +#include + +#define MAX_WORD 1024 + +void strtolower(char* str) { + for (char* p = str; *p; p++) + *p = tolower(*p); +} + +bool strisalpha(const char* str) { + for (const char* p = str; *p; p++) + if (!isalpha(*p)) + return false; + return true; +} + +void trim(char* str) { + int len = strlen(str); + while (len > 0 && isspace(str[len-1])) + str[--len] = '\0'; +} + +bool matches(const char* word, const char* letters) { + char pending[MAX_WORD]; + strcpy(pending, word); + for (const char* p = letters; *p; p++) { + char* found = strchr(pending, *p); + if (found) *found = ' '; + } + for (char* p = pending; *p; p++) + if (*p != ' ') return false; + return true; +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + + char letters[MAX_WORD]; + strcpy(letters, argv[1]); + strtolower(letters); + + FILE* fp = fopen("words.txt", "r"); + if (!fp) return EXIT_FAILURE; + + char word[MAX_WORD]; + while (fgets(word,sizeof(word),fp)) { + trim(word); + if (strlen(word) >= 2 && + strisalpha(word) && + matches(word, letters) + ) + printf("%s\n", word); + } + + fclose(fp); + return EXIT_SUCCESS; +} diff --git a/challenge-004/paulo-custodio/cpp/ch-1.cpp b/challenge-004/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..6fcb14df32 --- /dev/null +++ b/challenge-004/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,20 @@ +/* +Challenge 004 +Challenge #1 +Write a script to output the same number of PI digits as the size of your script. +Say, if your script size is 10, it should print 3.141592653. +https://crypto.stanford.edu/pbc/notes/pi/code.html +*/ +#include +#include +#include +using namespace std; +ostringstream buf;int a=10000,b,c=2800,d,e,f[2801],g; +int main(int argc,char**argv){ +for(;b-c;)f[b++]=a/5; +for(;d=0,g=c*2;c-=14,(buf<='5'){for(b=a-1;b>0;b--){ +s[b]++;if(s[b]<='9')break;s[b]--;}};s.resize(a); +cout< +#include +#include +#include +using namespace std; + +void strtolower(string& str) { + for (size_t i = 0; i < str.size(); i++) + str[i] = tolower(str[i]); +} + +bool strisalpha(string& str) { + for (size_t i = 0; i < str.size(); i++) + if (!isalpha(str[i])) + return false; + return true; +} + +bool matches(const string& word, const string& letters) { + string pending = word; + for (size_t i = 0; i < letters.size(); i++) { + size_t found = pending.find(letters[i]); + if (found != string::npos) + pending.erase(found, 1); + } + if (pending.size() == 0) + return true; + else + return false; +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + + string letters = argv[1]; + strtolower(letters); + + ifstream ifs("words.txt"); + if (!ifs.is_open()) return EXIT_FAILURE; + + string word; + while (getline(ifs, word)) { + if (word.size() >= 2 && + strisalpha(word) && + matches(word, letters) + ) + cout << word << endl; + } + + return EXIT_SUCCESS; +} -- cgit