Solution of Virtual Pharmacy

Sergiu Pușcaș - Python 3

    1 #!/usr/bin/python3
    2 
    3 v = []
    4 for t in range(int(input())):
    5     line = input().split()
    6 
    7     if line[0] == '1':
    8         v.append(line[1])
    9     if line[0] == '2' and len(v) > 0:
   10         v.pop()
   11     if line[0] == '3':
   12         filtered = [x for x in v if int(x) <= int(line[1])]
   13         print("Empty" if filtered == [] else ' '.join(filtered))

Cosmin Rusu - C++11

    1 #include <iostream>
    2 #include <vector>
    3 
    4 using namespace std;
    5 
    6 int main() {
    7     vector <int> v;
    8     int n;
    9 
   10     cin >> n;
   11     while(n--) {
   12         int op, x;
   13         cin >> op;
   14         if(op == 1) {
   15             cin >> x;
   16             v.push_back(x);
   17         }
   18         else if(op == 2) {
   19             if(v.size())
   20                 v.pop_back();
   21         }
   22         else {
   23             cin >> x;
   24             vector <int> aux;
   25             for(auto it : v)
   26                 if(it <= x)
   27                     aux.push_back(it);
   28             if(aux.size() == 0) {
   29                 cout << "Empty";
   30             }
   31             else
   32                 for(auto it : aux)
   33                     cout << it << ' ';
   34             cout << '\n';
   35         }
   36     }
   37 
   38     return 0;
   39 }
Questions?

Sponsors Gold