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

Hackerrank 30 days of code solutions added #9

Open
wants to merge 1 commit into
base: master
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
25 changes: 25 additions & 0 deletions 30 days of code Solutions/Day-0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

//--------------------------------------------------------------------


int main()
{

string input_string;

getline(cin, input_string);

cout << "Hello, World." << endl;

cout << input_string << endl;


return 0;
}
26 changes: 26 additions & 0 deletions 30 days of code Solutions/Day-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

int main() {
int i = 4;
double d = 4.0;
string s = "HackerRank ";

//-----------------------------------------------------------------


int lo;
string lo2;
double lo3;
cin >> lo >> lo3 >> lo2;

cout << lo + i << endl;
cout << lo3 + d << endl;
cout << s << lo2 << endl;

//---------------------------------------------------------------------
return 0;
}
32 changes: 32 additions & 0 deletions 30 days of code Solutions/Day-10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>

using namespace std;

bool desc(int i, int l){return (i > l);}

int main(){
int n;
cin >> n;
vector<int> ones;
bitset<32> m(n);
string ip = m.to_string();
int cnt = 0;
for(int m = 0; m < ip.size(); ++m){
if(ip[m] == '1'){
++cnt;
}
else{
ones.push_back(cnt);
cnt = 0;
}
if(m == ip.size() - 1){
ones.push_back(cnt);
}
}
sort(ones.begin(), ones.end(), desc);
cout << ones.at(0) << endl;
return 0;
}
41 changes: 41 additions & 0 deletions 30 days of code Solutions/Day-11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<vector<int> >lmao;
vector<int> answers;

bool desc(int i, int k){return i > k;}

int TakeInput(int i, int m)
{
for(int f = 0; f < i; ++f){
vector<int> temp;
for(int a = 0; a < m; ++a){
int x;
cin >> x;
temp.push_back(x);
}
lmao.push_back(temp);
}
return 0;
}

void compute()
{
for(int i = 0; i < lmao.size() - 2; ++i){
for(int e = 0; e < lmao[i].size() - 2; ++e){
int sum = lmao[i][e] + lmao[i][e+1] + lmao[i][e+2] + lmao[i+1][e+1] +lmao[i+2][e] + lmao[i+2][e+1] + lmao[i+2][e+2];
answers.push_back(sum);
}
}
}
int main()
{
TakeInput(6, 6);
compute();
sort(answers.begin(), answers.end(), desc);
cout << answers.at(0) << endl;
}
78 changes: 78 additions & 0 deletions 30 days of code Solutions/Day-12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <iostream>
#include <vector>

using namespace std;


class Person{
protected:
string firstName;
string lastName;
int id;
public:
Person(string firstName, string lastName, int identification){
this->firstName = firstName;
this->lastName = lastName;
this->id = identification;
}
void printPerson(){
cout<< "Name: "<< lastName << ", "<< firstName <<"\nID: "<< id << "\n";
}

};

//-------------------------------------------------------------------------

class Student : public Person{
private:
vector<int> testScores;
public:
Student(string firstName, string lastName, int identification, vector<int> scores) : Person(firstName, lastName, identification), testScores(scores) { }

char calculate() {
int average = 0;
for(int i = 0; i < testScores.size(); i++) {
average += testScores[i];
}
average = average / testScores.size();

if(average >= 90) {
return 'O'; // Outstanding
}
else if(average >= 80) {
return 'E'; // Exceeds Expectations
}
else if(average >= 70) {
return 'A'; // Acceptable
}
else if(average >= 55) {
return 'P'; // Poor
}
else if(average >= 40) {
return 'D'; // Dreadful
}
else {
return 'T'; // Troll
}
}
};

//-------------------------------------------------------------------------

int main() {
string firstName;
string lastName;
int id;
int numScores;
cin >> firstName >> lastName >> id >> numScores;
vector<int> scores;
for(int i = 0; i < numScores; i++){
int tmpScore;
cin >> tmpScore;
scores.push_back(tmpScore);
}
Student* s = new Student(firstName, lastName, id, scores);
s->printPerson();
cout << "Grade: " << s->calculate() << "\n";
return 0;
}
46 changes: 46 additions & 0 deletions 30 days of code Solutions/Day-13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
class Book{
protected:
string title;
string author;
public:
Book(string t,string a){
title=t;
author=a;
}
virtual void display()=0;

};

//--------------------------------------------------------------------

class MyBook : public Book {
protected:
int price;
public:
MyBook(string title, string author, int price) : Book(title, author), price(price) { }
void display(){
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Price: " << price << endl;
}
};

//---------------------------------------------------------------------

int main() {
string title,author;
int price;
getline(cin,title);
getline(cin,author);
cin>>price;
MyBook novel(title,author,price);
novel.display();
return 0;
}
48 changes: 48 additions & 0 deletions 30 days of code Solutions/Day-14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class Difference {
private:
vector<int> elements;

public:
int maximumDifference;

//------------------------------------------------------------------------------------

Difference(vector<int> elm) : elements(elm) {}
void computeDifference(){
sort(elements.begin(), elements.end(), greater<int>());
maximumDifference = elements.at(0) - elements.at(elements.size() - 1);
}

//--------------------------------------------------------------------------------------

}; // End of Difference class

int main() {
int N;
cin >> N;

vector<int> a;

for (int i = 0; i < N; i++) {
int e;
cin >> e;

a.push_back(e);
}

Difference d(a);

d.computeDifference();

cout << d.maximumDifference;

return 0;
}
60 changes: 60 additions & 0 deletions 30 days of code Solutions/Day-15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <cstddef>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int d){
data=d;
next=NULL;
}
};
class Solution{
public:

//------------------------------------------------------------

Node* insert(Node *head,int data)
{
if(head == NULL){
head = new Node(data);
return head;
}
else if(head->next == NULL){
Node *temp = new Node(data);
head->next = temp;
return head;
}
else{
insert(head->next, data);
}
return head;
}

//--------------------------------------------------------------

void display(Node *head)
{
Node *start=head;
while(start)
{
cout<<start->data<<" ";
start=start->next;
}
}
};
int main()
{
Node* head=NULL;
Solution mylist;
int T,data;
cin>>T;
while(T-->0){
cin>>data;
head=mylist.insert(head,data);
}
mylist.display(head);

}
18 changes: 18 additions & 0 deletions 30 days of code Solutions/Day-16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>

using namespace std;


int main(){
string S;
cin >> S;
try{
int no;
no = stoi(S);
cout << no << endl;
}
catch(exception e){
cout << "Bad String" << endl;
}
return 0;
}
Loading