Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solutions Task1 and Task2 #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 63 additions & 13 deletions src/Task1/task1.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,76 @@
#include <bits/stdc++.h>
using namespace std;

map<string, int> variables;

int solve(int n, vector<string> arr){
int answer=0;
// Start your code here
vector<string> instruction ;

for (int i=0; i<n; i++){
stringstream ss(arr[i]);
string word;
while (ss >> word) {
instruction.push_back(word);
}

for (int j=1; j<instruction.size()-1; j++){
instruction[j].pop_back();
}

if (instruction[0] == "li"){
variables[instruction[1]] = stoi(instruction[2]) ;
}
else if (instruction[0] == "add"){
variables[instruction[1]] = variables[instruction[2]] + variables[instruction[3]];
}
else if(instruction[0] == "addi"){
variables[instruction[1]] = variables[instruction[2]] + stoi(instruction[3]);
}
else if(instruction[0] == "sub"){
variables[instruction[1]] = variables[instruction[2]] - variables[instruction[3]];
}
else if(instruction[0] == "mul"){
variables[instruction[1]] = variables[instruction[2]] * variables[instruction[3]];
}
else if(instruction[0] == "and"){
variables[instruction[1]] = variables[instruction[2]] & variables[instruction[3]];
}
else if (instruction[0] == "or"){
variables[instruction[1]] = variables[instruction[2]] | variables[instruction[3]];
}
else if(instruction[0] == "andi"){
variables[instruction[1]] = variables[instruction[2]] & stoi(instruction[3]);
}
else if(instruction[0] == "ori"){
variables[instruction[1]] = variables[instruction[2]] | stoi(instruction[3]);
}
else if(instruction[0] == "sw"){
variables[instruction[2]] = variables[instruction[1]];
}

instruction.clear();
ss.clear();
}
answer = variables["answer"];

// End your code here
return answer;
}

int main(){
// int n;
// cin>>n;
// cin.ignore();
// vector<string> arr(n);
// for(int i=0;i<n;i++){
// getline(cin,arr[i]);
// cout<<arr[i]<<endl;
// }
// cout<<solve(n,arr);
return 0;
}
// int main(){
// // int n;
// // cin>>n;
// // cin.ignore();
// // vector<string> arr(n);
// // for(int i=0;i<n;i++){
// // getline(cin,arr[i]);
// // cout<<arr[i]<<endl;
// // }
// // cout<<solve(n,arr);
// return 0;
// }

// ==> NOTE: Comment main function and uncomment below line to verify your code
// #include "../../include/test1_cases.h"
#include "../../include/test1_cases.h"
111 changes: 98 additions & 13 deletions src/Task2/task2.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,131 @@
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
using namespace std;

bool secondSort(const pair<int,int> &p1, const pair<int,int> &p2)
{
if (p1.second == p2.second){
return (p1.first < p2.first) ;
}
else{
return (p1.second > p2.second);
}
}

class Solution {
private:
int n; // n is size that user inputs
vector<unsigned int>v,ans1,ans2,ans3; // v is the vector that user inputs
void solve_task1(){
// solve task1 and save the answer in ans1
set<unsigned int> a;
for (auto i: v){
a.insert(i);
}
for (auto i: a){
ans1.push_back(i);
}
}
void solve_task2(){
// solve task2 and save the answer in ans2
vector<pair<unsigned int, unsigned int>> a ;
for (int i=0; i<v.size(); i = i+2){
a.push_back(make_pair(v[i], v[i+1]));
}
sort(a.begin(), a.end(), secondSort);
for (auto i: a){
ans2.push_back(i.first);
}
}
void solve_task3(){
// solve task3 and save the answer in ans3
queue<unsigned int> q;
vector<pair<unsigned int, unsigned int>> a ;
for (int i=0; i<v.size(); i = i+2){
a.push_back(make_pair(v[i], v[i+1]));
}
for (auto i: a){
if (i.first % 2 == 0){
q.push(i.second);
q.pop();
}
else{
q.push(i.second);
}
}
while (!q.empty()){
ans3.push_back(q.front());
q.pop();
}
}

public:
// create a constructor to take input
Solution(size_t length, vector<unsigned int> a){
n = length;
v = a;
solve_task1();
solve_task2();
solve_task3();
}

string FINDMATCH(string path){
// complete this function to read file, compare with ans1, ans2, ans3
vector<unsigned int> binFile ;
int length = 0;
ifstream binaryFile;
binaryFile.open("../../" + path, ios::in | ios::binary) ;
binaryFile.seekg(0, ios::end) ;
length = (int)binaryFile.tellg();
binaryFile.seekg(0, ios::beg) ;
while (binaryFile.tellg() < length){
unsigned int x;
binaryFile.read((char*)&x, sizeof(unsigned int)) ;
binFile.push_back(x);
}
binaryFile.close();

// and return the answer
if (ans1 == binFile){
binFile.clear() ;
return ("TASK1");
}
else if (ans2 == binFile && v.size()%2 == 0){
binFile.clear() ;
return ("TASK2");
}
else if (ans3 == binFile && v.size()%2 == 0){
binFile.clear() ;
return ("TASK3");
}
else{
binFile.clear() ;
return ("NOTFOUND");
}
}
};



int main(){
vector<unsigned int>a{5,7,6,5,2,1,4,0,1,3};
string paths[]={
"missing_files/missing1.bin",
"missing_files/missing2.bin",
"missing_files/missing3.bin",
"missing_files/missing4.bin",
};
// Intialize your solution object here
// int main(){
// vector<unsigned int>a{5,7,6,5,2,1,4,0,1,3};
// string paths[]={
// "missing_files/missing1.bin",
// "missing_files/missing2.bin",
// "missing_files/missing3.bin",
// "missing_files/missing4.bin",
// };
// // Intialize your solution object here
// Solution solution(a.size(), a) ;
// // Make a for loop to go through paths array and call FINDMATCH function

// Make a for loop to go through paths array and call FINDMATCH function
// for (auto path: paths){
// cout<<solution.FINDMATCH(path)<<endl;
// }

return 0;
}
// return 0;
// }

// ==> NOTE: Comment main function and uncomment below line to verify your code
// #include "../../include/test2_cases.h"
#include "../../include/test2_cases.h"