diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-06-18 08:55:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-18 08:55:49 +0100 |
| commit | 5a48d2f7ff6b19c198ffd69ff5e34f18eca84bee (patch) | |
| tree | 66156640f10a6718e2dab47b676dff04dedc2e37 | |
| parent | 2961fc6e2d4ebf1089ec25afe2319ab14af9ce6d (diff) | |
| parent | a87d8dbe5ea94e4f78c20b9c9d25b48c3f5c16e3 (diff) | |
| download | perlweeklychallenge-club-5a48d2f7ff6b19c198ffd69ff5e34f18eca84bee.tar.gz perlweeklychallenge-club-5a48d2f7ff6b19c198ffd69ff5e34f18eca84bee.tar.bz2 perlweeklychallenge-club-5a48d2f7ff6b19c198ffd69ff5e34f18eca84bee.zip | |
Merge pull request #4286 from pauloscustodio/paulo-custodio
Paulo custodio
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | challenge-004/paulo-custodio/c/ch-1.c | 20 | ||||
| -rw-r--r-- | challenge-004/paulo-custodio/c/ch-2.c | 71 | ||||
| -rw-r--r-- | challenge-004/paulo-custodio/cpp/ch-1.cpp | 20 | ||||
| -rw-r--r-- | challenge-004/paulo-custodio/cpp/ch-2.cpp | 61 |
5 files changed, 174 insertions, 0 deletions
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 <stdio.h> +#include <string.h> +#include <stdlib.h> +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 <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <ctype.h> + +#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 <iostream> +#include <iomanip> +#include <sstream> +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<<setw(4)<<setfill('0')<<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;string s=buf.str();if(s[a]>='5'){for(b=a-1;b>0;b--){ +s[b]++;if(s[b]<='9')break;s[b]--;}};s.resize(a); +cout<<s[0]<<'.'<<s.c_str()+1; +} diff --git a/challenge-004/paulo-custodio/cpp/ch-2.cpp b/challenge-004/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..3be014d043 --- /dev/null +++ b/challenge-004/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,61 @@ +/* +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 <iostream> +#include <fstream> +#include <string> +#include <cctype> +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; +} |
