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