PYTHON PROGRAM: read from txt file and output (THIS PROGRAM SHOULD BE USING DICT
ID: 3592106 • Letter: P
Question
PYTHON PROGRAM: read from txt file and output (THIS PROGRAM SHOULD BE USING DICTIONARIES)
Note: None value is not a string but a special value.
men0.txt
m1;w3;w2;w1
m2;w3;w1;w2
m3;w2;w1;w3
women0.txt
w1;m1;m2;m3
w2;m2;m1;m3
w3;m3;m2;m1
Functions:
def read_match_preferences(open_file : open) -> {str:[str,[str]]}:
def dict_as_str(d : {str:[str,[str]]}, key : callable=None, reverse : bool=False) -> str:
My attempted functions:
My Main Function:
Read files of men and women and their rankings of all members of the opposite gender (highest to lowest preference), separated by semicolons, building a dictionary like the ones above (where each match is initially the special value None). As described above, we annotate the structure of this dictionary as {str:[str,[str) In the file, the person's name appears first, followed by the names of all members of the opposite gender in highest to lowest preference, separated by one semicolon character. For example, the input file men0.txt contains the following lines: these line could appear in this order, or any other, but the each man's preferences must appear in decleasing order of preference The first line means, ml ranks the members of the opposite gender in the order of preference from w3, w2, and wl in decreasing order of preference Each line is guaranteed to start with a unique name, which is guaranteed to be followed by all the names of all members of the opposite gender, each appearing once; and all names are separated by semicolons When you print such information, print each person on a separate line, followed by his/her match and preferences. For example, the file above would print as m1 - [None, ['w3', W2', m2 [None, ['w3' m3[None, ['w2', 'i', w3']] Note that the names on the lines must be sorted in alphabetical order, the list of preferences must appear in the same order they appeared in the file. There are multiple pairs of data files for this program, all named like men0.txt and women0.txt; Test/debug your program on the first file; when you are done, test it on the remaining files.Explanation / Answer
Public:
Patient ( const char *, const char * , const char *, Date, int);
~Patient();
Patient & setID ( const char * ); //check if length of name string is < 32 if not, shorten to 32 letters.
Patient & setFirstName ( const char *); //check if length of name string is <15, if not, shorten to 14 letters.
Patient & setLastName ( const char *); //check if length of name string is <15, if not, shorten to 14 letters.
Patient & setBirthDate ( Date);
Patient & setPrimaryDoctorID (int);
const char * getID();
const char * getFirstName();
const char * getLastName();
Date getBirthDate();
int getPrimaryDoctorID();
bool enterProcedure(Date procedureDate, int procedureID,int procedureProviderID);
void printAllProcedures();
private:
char ID[33];
char firstName[15];
char lastName [15];
Date birthdate;
int primaryDoctorID;
procedure record[100];
int currentCountOfProcedures; // keeps track of how many procedures have been recorded if it reaches 500, no new procedures // can be entered.
};
~~~~
For the Date class, you may copy what is at the end of this assignment with the following changes:
void operator+=(int); // Does not return anything
bool leapYear( ) const; // is the year for the date object a leap year?
bool endOfMonth( ) const; // is the date of the object the last day of the month?
~~~
Date class (Before changes asked for above)
// Definition of class Date in date.h
#ifndef DATE1_H
#define DATE1_H
#include
#include
using namespace std;
class Date {
friend ostream &operator<<( ostream &, const Date & ); // allows easy output to a ostream
public:
Date( int m = 1, int d = 1, int y = 1900 ); // constructor, note the default values
void setDate( int, int, int ); // set the date
const Date &operator+=( int ); // add days, modify object
bool leapYear( int) const; // is this a leap year?
bool endOfMonth( int ) const; // is this end of month?
int getMonth ( ) const; // You need to implement this
int getDay ( ) const; // You need to implement this
int getYear ( ) const; // You need to implement this
string getMonthString( ) const; // You need to implement this
private:
int month;
int day;
int year;
static const int days[]; // array of days per month
static const string monthName[]; // array of month names
void helpIncrement(); // utility function
};
#endif
// Member function definitions for Date class in separate date.cpp file
#include
#include "date.h"
#include
//Initialize static members at file scope; one class-wide copy.
const int Date::days[] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
const string Date::monthName[] = { "", "January",
"February", "March", "April", "May", "June",
"July", "August", "September", "October",
"November", "December" };
Date::Date( int m, int d, int y ) { setDate( m, d, y ); } //Date constructor
void Date::setDate( int mm, int dd, int yy ) //Set the date
{
month = ( mm >= 1 && mm <= 12 ) ? mm : 1;
year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900;
if ( month == 2 && leapYear(year ) ) //test for a leap year
day = ( dd >= 1 && dd <= 29 ) ? dd : 1;
else
day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1;
}
const Date &Date::operator+=( int additionalDays ) //Add a specific number of days to a date
{
for ( int i = 0; i < additionalDays; i++ )
helpIncrement();
return *this; //enables cascading
}
bool Date::leapYear( int testYear ) const //If the year is a leap year, return true;otherwise, return false
{
if ( testYear % 400 == 0 || ( testYear % 100 != 0 && testYear % 4 == 0 ) )
return true; //a leap year
else
return false; // not a leap year
}
bool Date::endOfMonth(int testDay ) const //Determine if the day is the end of the month
{
if ( month == 2 && leapYear( year ) )
return (testDay == 29); //last day of Feb. in leap year
else
return (testDay == days[ month ]);
}
void Date::helpIncrement() //Function to help increment the date
{
if ( ! endOfMonth( day )) { //date is not at the end of the month
day++;
}
else if (month < 12 ) { //date is at the end of the month, but month<12
day = 1;
++month;
}
else //end of month and year: last day of the year
{
day = 1;
month = 1;
++year;
}
}
ostream &operator<<( ostream &output, const Date &d ) //Overloaded output operator
{
output << d.monthName[ d.month ] << ' '<< d.day << ", " << d.year;
return output; //enables cascading
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.