meeting problem

I was given this programming problem to try and figure out, i wasn't able to complete. Also the way my tutor was trying to help me with I had a hard time following their logic and implementation. I also thought that my calculations for the minutes was on point but I kept getting a much highere number than what the example gave
The problem goes something like this. a guy has appointments all throughout the week and at different times on different days.

[content removed]

any help would be great and appreciated,
thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  #include <string>
#include <fstream>
#include <sstream>
#include <iostream>

using namespace std;

int solution(string &ss);
int calcdif(string, string);
int toDay(string);
int main()
{

 string week = "Mon 01:00-23:00\nTue 01:00-23:00\nWed 01:00-23:00\nThu 01:00-23:00\nFri 01:00-23:00\nSat 01:00-23:00\nSun 01:00-21:00";

 cout << solution(week);

 return 0;

}

enum Day {Mon, Tue, Wed, Thu, Fri, Sat, Sun};



int solution(string &ss)
{
    string s = ss; //reading the content of the reference
    istringstream f(s);
    string m ;
    string p_end = "Mon 00:00"; //previous meeting end
    string m_start;
    string m_end;
    int diff;
    int max_diff;

    //"Sun 15:30-20:15"  //format
    while (getline(f,m))
    {
        m_start = m.substr(0,9);
        m_end = m.substr(0,3) + m.substr(10,5); //"Sun 20:15"
        cout << "this is m substring 11,5 " << m.substr(10,5)<< endl; //changed from (11,5)
        cout << "this is m start " << m_start << endl;
        cout << "this is m end " << m_end <<endl << endl;

        diff = calcdif(p_end, m_start);
        if (diff>max_diff) max_diff = diff;
        p_end = m_end;

    }

    //time between last meeting and Sun 23:59
    diff = calcdif(m_start,"Sun 23:59");
        if (diff>max_diff) max_diff = diff;


        return max_diff;

}

//"Mon 01:00"
int calcdif(string s1, string s2) //s2-s1

{
    int d1 = toDay(s1.substr(0,3));
    int d2 = toDay(s2.substr(0,3));
    int diff_days = d2-d1;
    istringstream convert(s1.substr(5,2));
    int h1;
    convert>>h1;
    istringstream convert2(s2.substr(5,2));
    int h2 ;
    convert2>>h2;
    istringstream convert3(s1.substr(6,2));
    int m1 ;
    convert2>>m1;
    istringstream convert4(s2.substr(6,2));
    int m2 ;
    convert2>>m2;
    int tot1 = d1*60*24+h1*60+m1; //difference to start of the week
    int tot2 = d2*60*24+h2*60+m2; //difference to start of the week
    return tot2-tot1;

    //21:25 - 19:17
    //02:08 -> 128 mins
    //21*60+25 - 19*60+17 = 60(21-19) + (25-17) = 60*(2) + 8 = 12

}

int toDay(string s)
{
    if(s=="Mon")return 0;
    if(s=="Tue")return 1;
    if(s=="Wed")return 2;
    if(s=="Thu")return 3;
    if(s=="Fri")return 4;
    if(s=="Sat")return 5;
    if(s=="Sun")return 6;
    return -1;
}
Last edited on by admin
I'm not sure what's wrong with your code, but it can be simplified.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
using namespace std;

int day_to_num(const string& s) {
    static unordered_map<string,int> m {
        {"Mon",0},{"Tue",1},{"Wed",2},{"Thu",3},{"Fri",4},{"Sat",5},{"Sun",6}
    };
    auto it = m.find(s);
    return it == m.end() ? -1 : it->second;
}

int main() {
    string week =
        "Mon 01:00-23:00\n"
        "Tue 01:00-23:00\n"
        "Wed 01:00-23:00\n"
        "Thu 01:00-23:00\n"
        "Fri 01:00-23:00\n"
        "Sat 01:00-23:00\n"
        "Sun 01:00-21:00\n";

    istringstream ss(week);
    string day;
    int h1, m1, h2, m2;
    char ch;
    int now = 0, start = 0, longest = 0;

    while (ss >> day >> h1 >> ch >> m1 >> ch >> h2 >> ch >> m2) {
        start = (day_to_num(day) * 24 + h1) * 60 + m1;
        int end = (day_to_num(day) * 24 + h2) * 60 + m2;
        int len = start - now;
        if (len > longest) longest = len;
        now = end;
    }
    int len = (7*24*60) - now;
    if (len > longest) longest = len;

    cout << longest << '\n';
}

I guess maybe i'm just not understanding the logic and the steps that was needed, looking at your code, I was going about it in a long way. I however have not had too much experience just practice problems in a book. I tried putting your code in a new project to check it out, I use code blocks, but it returned errors c++0x_warning.h, plus a few others that i will have to look up. I'm going to try and run it in visual studio just in case it was a compiler issue. if you don't mind me asking what your thought process was behind it to figure it out? thank you
I don't know how code blocks works, but there should be somewhere where you can set the compiler to the C++11 standard (or higher, like C++14 or C++17). You definitely shouldn't be using pre-C++11.

The logic is, I believe, the same as yours, just simplified. The "end" of the last meeting starts at minute 0 of the week. Subtracting the previous end from the latest start gives the free time. I think that's what you were trying to do, too.

If you have a question about a specific part, just ask.
yes, and I will have to look into it, I have never heard of unordered_map and not clear on your implementation of it. is this to allow if the list was not ordered, if so would that also work if there were multiple meetings on the same day? or would I need to create another variable to hold the 2nd time of that day?
The unordered_map adds nothing over your toDay function. It's just another way to do exactly the same thing.

That code will work for any number of meetings per day, but the days and meetings must be in order (forget the word "unordered" in unordered_map; it has absolutely nothing to do with it).

Actually, I've assumed that the meetings are listed in order, but rereading the instructions, it doesn't seem to say that. So here's a way of doing it even if they aren't in order. I have to say that I like the new result (but then again, I'm Canadian).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>
using namespace std;

struct Meeting {
    int start, end;  // minutes from beginning of week
    Meeting(int s, int e) : start(s), end(e) {}
};

int toDay(string s) {
    for (auto& ch: s) ch = tolower(ch);
    if (s == "mon") return 0;
    if (s == "tue") return 1;
    if (s == "wed") return 2;
    if (s == "thu") return 3;
    if (s == "fri") return 4;
    if (s == "sat") return 5;
    if (s == "sun") return 6;
    return -1;
}

int main() {
    string week =
        "Thu 01:00-23:00\n"
        "Sat 04:00-18:00\n"
        "Mon 01:00-23:00\n"
        "Wed 01:00-03:00\n"
        "Sun 01:00-21:00\n"
        "Sat 21:00-23:00\n"
        "Wed 10:00-19:00\n"
        "Fri 01:00-23:00\n"
        "Tue 01:00-23:00\n";

    istringstream ss(week);
    string day;
    int h1, m1, h2, m2;
    char ch;

    vector<Meeting> meetings;

    while (ss >> day >> h1 >> ch >> m1 >> ch >> h2 >> ch >> m2) {
        int start = (toDay(day) * 24 + h1) * 60 + m1;
        int end   = (toDay(day) * 24 + h2) * 60 + m2;
        meetings.push_back(Meeting(start, end));
    }

    sort(meetings.begin(), meetings.end(),
         [](auto x, auto y){return x.start<y.start;});

    int now = 0, longest = 0;

    for (const auto& m: meetings) {
        int len = m.start - now;
        if (len > longest) longest = len;
        now = m.end;
    }

    int len = (7*24*60) - now;
    if (len > longest) longest = len;

    cout << longest << '\n';
}

wow, I'm a beginner programmer and haven't even seen most of what you coded. I have a lot more to learn, and try different coding problems. Is there any advice and/or websites to work on my programming logic and being able to develop myself to be a competent programmer? Thank you for all your help!!
I don't know what to recommend. Any modern book that covers at least C++11 should be okay. As for websites teaching programming I've never used them so I really don't know. I imagine there must be some good websites, but I don't see how they could beat a decent book.

There isn't really a lot of new things in the code. I created a struct with a constructor, which is a pretty simple concept. I used a vector, but surely you've used a vector before (if not, then you aren't learning modern C++ properly!). I used the sort function along with a so-called "lambda" function to specify how the Meeting structs are to be ordered, although now that I think about it, it would be better to overload operator< like this (which probably looks simpler, too):

1
2
3
4
5
6
7
bool operator<(const Meeting& a, const Meeting& b)
{
    return a.start < b.start;
}

// Then the sort call would just be:
sort(meetings.begin(), meetings.end());

Stroustrup, the creator of the language, has a book called "Programming: Principles and Practice Using C++", which is specifically for beginners, and also the famous "The C++ Programming Language", which is possibly not for beginners.
Last edited on
Topic archived. No new replies allowed.