Skip to content

Commit

Permalink
Merge pull request #3 from russell-lundberg/options1
Browse files Browse the repository at this point in the history
Options1
  • Loading branch information
russell-lundberg authored Jan 17, 2024
2 parents 8002377 + 84c6395 commit 0ff5848
Show file tree
Hide file tree
Showing 17 changed files with 421 additions and 109 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ UNAME_S := $(shell uname -s)
CFLAGS=-g -Wall -Wextra -std=c++17
#CFLAGS += -fsyntax-only
CFLAGS += -fmax-errors=4
CFLAGS += -Wno-unused-variable -Wno-unused-parameter

CCFLAGS=-D BOOST_MATH_STANDALONE
#CCFLAGS=-D BOOST_MATH_STANDALONE
# I always keep my source files in a directory called "src"
SRC=src
SRCS=$(wildcard $(SRC)/*.cpp)
Expand All @@ -28,11 +29,13 @@ ifeq ($(UNAME_S),Darwin)
BOOST_ROOT=/opt/homebrew/Cellar/boost/1.83.0
endif

INCLUDE=-Iinclude -I$(BOOST_ROOT)/include
#INCLUDE=-Iinclude -I$(BOOST_ROOT)/include
INCLUDE=-Iinclude

LDFLAGS=

LDLIBS=-L$(BOOST_ROOT)/lib -lstdc++ -lboost_system -l:libboost_program_options.a
LDLIBS=-lstdc++
#LDLIBS=-lboost_system -l:libboost_program_options.a -L$(BOOST_ROOT)/lib

all:$(BIN)

Expand Down
File renamed without changes.
Binary file modified bin/main
Binary file not shown.
2 changes: 2 additions & 0 deletions include/create_permutations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ std::set<std::string> all_substrings_s( std::string letters );

//std::vector<std::string> all_subpermutations( std::string letters );

//int factorial( int );

}

#endif
32 changes: 32 additions & 0 deletions include/options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef OPTIONS_HPP
#define OPTIONS_HPP
//
// options.hpp
// xcode2
//
// Created by Russell Lundberg on 12/21/23.
//


#include <string>
#include <unordered_map>

void show_usage(std::string );

std::unordered_map<std::string,std::string> process_argv( int, char** );

std::string optionLetter( std::string );

bool isOption( char* );

std::string valuelessOption( std::string );

std::string valuefulOption( std::string );

void Letters_In( std::unordered_map<std::string,std::string>* );

std::string str_tolower(std::string s);

void Option_Switch( int, char**, int*, std::unordered_map<std::string,std::string>* );

#endif /* OPTIONS_HPP */
7 changes: 5 additions & 2 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ std::set<std::string> get_dictionary();
std::map<char,int> LetterScores();
//std::map<std::string,int> LetterScores();

std::unordered_map<std::string,std::string> ingest_args( int, char** );
//std::unordered_map<std::string,std::string> ingest_args( int, char** );

std::map<std::string,int> Score_Word ( std::string );

Expand All @@ -29,5 +29,8 @@ void sort( std::map<std::string, int>& );
std::set<std::pair<std::string,int>> Words_Sorted( std::map<std::string, int>&, std::unordered_map<std::string,std::string> );


}
int factorial( int );

} // close namespace declaration

#endif
Binary file modified obj/LetterScores.o
Binary file not shown.
Binary file modified obj/create_permutations.o
Binary file not shown.
Binary file modified obj/main.o
Binary file not shown.
Binary file added obj/options.o
Binary file not shown.
Binary file modified obj/score_word.o
Binary file not shown.
Binary file modified obj/util.o
Binary file not shown.
31 changes: 1 addition & 30 deletions src/LetterScores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* it uses an unordered_map as an associative array
*
* It might be more sensible to use an ordered map. IDK.
* Russell Lundberg. 20231103
*/

namespace WWF {
Expand Down Expand Up @@ -59,33 +60,3 @@ std::map<char,int> LetterScores() {
} // end main()

} // close namespace declaration
/*
blank => { points => 0, count => 2 },
a => { points => 1, count => 9 },
b => { points => 4, count => 2 },
c => { points => 4, count => 2 },
d => { points => 2, count => 5 },
e => { points => 1, count => 13 },
f => { points => 4, count => 2 },
g => { points => 3, count => 3 },
h => { points => 3, count => 4 },
i => { points => 1, count => 8 },
j => { points => 10, count => 1 },
k => { points => 5, count => 1 },
l => { points => 2, count => 4 },
m => { points => 4, count => 2 },
n => { points => 2, count => 5 },
o => { points => 1, count => 8 },
p => { points => 4, count => 2 },
q => { points => 10, count => 1 },
r => { points => 1, count => 6 },
s => { points => 1, count => 5 },
t => { points => 1, count => 7 },
u => { points => 2, count => 4 },
v => { points => 5, count => 2 },
w => { points => 4, count => 2 },
x => { points => 8, count => 1 },
y => { points => 3, count => 2 },
z => { points => 10, count => 1 },
);
*/
91 changes: 49 additions & 42 deletions src/create_permutations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/math/special_functions/factorials.hpp>
#include "create_permutations.hpp"
#include "util.hpp"
#include <map>
Expand All @@ -18,17 +17,25 @@ bool cmp(const std::string lhs, const std::string rhs) {

namespace WWF {

// std::vector<std::string> create_permutations( std::string letters, int blanks )
//std::vector<std::string> create_permutations( std::string letters,
// std::string blankString)
std::vector<std::string> create_permutations(
std::unordered_map<std::string,std::string> options )
std::vector<std::string> create_permutations( std::unordered_map<std::string,std::string> options )
{
// std::cout << "Create_Permutations(): starting.\n";

// std::cout << "create_Permutations(): starting.\n";

if ( options.empty() ) {
std::cout << "options arg was empty";
exit(1);
}

/*
for ( auto elem : options) {
std::cout << "create_permutations(): Option=" << elem.first << ". Value: " << elem.second << ".\n";
}
*/


std::string letters = options.at("lettersIn");

// std::cout << "Create_Permutations(): lettersIn: " << letters << ".\n";
// std::cout << "Create_Permutations(): lettersIn: " << letters << ".\n";

// container for permutations
// intermidiate storage of permutations in set
Expand All @@ -37,22 +44,27 @@ std::vector<std::string> create_permutations(
// final storage of permutations in set
std::set<std::string> final_s;

// the trailing "_s" indicates return type "set"
// perm_s = WWF::all_substrings_s( letters );

// the map values were type
int blanks = 0;
if ( options.at("blanks") == "blank1") {
//if ( blankString == "blank1") {
blanks = 1;
// std::cout << "main() blanks chk: " << blanks << "\n";
}
// std::cout << "Create_Permutations(): starting tests.\n";
// the map values were type
int blanks = 0;

if ( options.at("blanks") == "blank2") {
//if ( blankString == "blank2") {
blanks = 2;
// std::cout << "main() blanks chk: " << blanks << "\n";
}
// if ( options.at("blanks") == "blank1")
if (auto search = options.find("blanks"); search != options.end()) {
// std::cout << "Found " << search->first << ' ' << search->second << '\n';

if ( options.at("blanks") == "blank1") {
blanks = 1;
// std::cout << "main() blanks chk: " << blanks << "\n";
}

// std::cout << "Create_Permutations(): tested blank1.\n";

if ( options.at("blanks") == "blank2") {
blanks = 2;
// std::cout << "main() blanks chk: " << blanks << "\n";
}
}

// all uppercase to distinguish "blank" letter from letters having
// non-zero value
Expand All @@ -77,20 +89,17 @@ if ( options.at("blanks") == "blank2") {
std::set<std::string> this_time = WWF::all_substrings_s( tmp1 );
for ( auto elem : this_time ) {
perm_s.insert(elem);
// std::cout << "elem: " << elem << "\n";
// std::cout << "elem: " << elem << "\n";
}
// std::cout << "tmp1: " << tmp1 << "\n";
j++;
// std::cout << "tmp1: " << tmp1 << "\n";
}
}
std::cout << j << " loops processed\n.";
// std::cout << j << " loops processed\n.";
}
else {
perm_s = WWF::all_substrings_s( letters );
}



std::cout << perm_s.size() << " substrings generated.\n";

// loop through all substrings feeding each to
Expand All @@ -105,7 +114,7 @@ if ( options.at("blanks") == "blank2") {
}
}

std::cout << final_s.size() << " permutations generated.\n";
// std::cout << final_s.size() << "Create_Permutations(): permutations generated.\n";

// create a map for reporting statistics
std::map<int, int> stats;
Expand All @@ -130,13 +139,9 @@ if ( options.at("blanks") == "blank2") {

// this loop prints the unique permutations by
// descending length, and populates the stats hash
// int i = 1;
for ( auto elem : words) {
// printf() wants a char type for strings. So
// elem must be converted using cstring library
// char temp[ elem.size() +1];
// strcpy(temp, elem.c_str());
// std::printf("%2i: %s\n", i++, temp);
stats[ elem.size() ] += 1;
}

Expand All @@ -152,7 +157,7 @@ if ( options.at("blanks") == "blank2") {
// std::cout << total << " permutations generated.\n";

return words;
}
} // end Create_Permutations()


std::string permutation( std::string word, int perm_idx )
Expand All @@ -162,23 +167,24 @@ std::string permutation( std::string word, int perm_idx )

unsigned long int length = word.size() ;

double factored = boost::math::factorial<double>( length );
if ( perm_idx >= (int) factored || perm_idx < 0 )
// replace Boost::Math::Factorial()
int fact = WWF::factorial( int(length) );

if ( perm_idx >= fact || perm_idx < 0 )
{
// std::cout << "permutations::invalid perm_index: " << perm_idx << "\n";
return "";
}

factored = boost::math::factorial<double>( length - 1 );
fact = WWF::factorial( int ( length - 1 ) );

int current_idx = perm_idx / ( int)factored;
int current_idx = perm_idx / fact;

int rest = perm_idx % (int)factored;
int rest = perm_idx % fact;

std::string first_letter = word.substr(current_idx, 1 );

word = word.replace(current_idx, 1, "");
// word = word.erase( current_idx );

std::string more_letters;
more_letters = WWF::permutation( word, rest);
Expand All @@ -192,7 +198,8 @@ std::vector<std::string> all_permutations( std::string letters )

std::vector<std::string> permutations;

double fact = boost::math::factorial<double>( letters.size() );
int fact = 0;
fact = WWF::factorial( int ( letters.size() ) );

for ( double i = 0 ; i <= fact ; i++ )
{
Expand Down
12 changes: 10 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <unordered_map>
#include <utility>
#include <set>
#include "options.hpp"

// comparison function to sort the valid words by score
/*
Expand All @@ -27,13 +28,20 @@ struct comp {


int main(int argc, char *argv[])
//int main(int argc, std::string* argv)
{
std::unordered_map<std::string,std::string> Options = {};

// ingest_args() returns an unordered map of command line args and values
Options = WWF::ingest_args( argc, argv );
// Options = WWF::ingest_args( argc, argv );
Options = process_argv( argc, argv );

// std::cout << "Ingested arguments.\n";
std::cout << "Ingested arguments.\n";
/*
for ( auto elem : Options) {
std::cout << "Option: " << elem.first << ". Value: " << elem.second << ".\n";
}
*/

std::vector<std::string> Permutations;
Permutations = WWF::create_permutations( Options );
Expand Down
Loading

0 comments on commit 0ff5848

Please sign in to comment.