Skip to content

Commit 5848d42

Browse files
solves magic spell, solves all inheritence provblems
1 parent 248fc16 commit 5848d42

File tree

2 files changed

+140
-4
lines changed

2 files changed

+140
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Hackerrank C++
22

33
![made-with-cpp](https://img.shields.io/badge/Made%20with-C++-1f425f.svg)
4-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-27/44-1abc9c.svg)
4+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-44/44-1abc9c.svg)
55
![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)
66
[![cp](https://img.shields.io/badge/also%20see-competitve%20programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
77

@@ -68,9 +68,9 @@ The C++ Domain is further Divided into the following sub-domains.
6868
| [Rectangle Area](https://www.hackerrank.com/challenges/rectangle-area) | Easy | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](inheritance/rectangle_area.cpp) |
6969
| [Multi Level Inheritance](https://www.hackerrank.com/challenges/multi-level-inheritance-cpp) | Easy | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](inheritance/multi_level_inheritance.cpp) |
7070
| [Accessing Inherited Functions](https://www.hackerrank.com/challenges/accessing-inherited-functions) | Medium | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](inheritance/accessing_inherited_functions.cpp) |
71-
| [Magic Spells](https://www.hackerrank.com/challenges/magic-spells) | Hard | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](inheritance) |
71+
| [Magic Spells](https://www.hackerrank.com/challenges/magic-spells) | Hard | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)](inheritance/magic_spell.cpp) |
7272

73-
### Debugging ![problems-solved](https://img.shields.io/badge/Problems%20Solved-0/4-1abc9c.svg)
73+
### Debugging ![problems-solved](https://img.shields.io/badge/Problems%20Solved-4/4-1abc9c.svg)
7474

7575
| Problem | Difficulty | Solution |
7676
|---------|------------|----------|
@@ -79,7 +79,7 @@ The C++ Domain is further Divided into the following sub-domains.
7979
| [Overloading Ostream Operator](https://www.hackerrank.com/challenges/overloading-ostream-operator) | Medium | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)]()
8080
| [Messages Order](https://www.hackerrank.com/challenges/messages-order) | Medium | [![cpp](https://img.icons8.com/color/35/000000/c-plus-plus-logo.png)]()
8181

82-
### Other Concepts ![problems-solved](https://img.shields.io/badge/Problems%20Solved-0/8-1abc9c.svg)
82+
### Other Concepts ![problems-solved](https://img.shields.io/badge/Problems%20Solved-8/8-1abc9c.svg)
8383

8484
| Problem | Difficulty | Solution |
8585
|---------|------------|----------|

inheritance/magic_spell.cpp

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
using namespace std;
5+
6+
class Spell {
7+
private:
8+
string scrollName;
9+
public:
10+
Spell(): scrollName("") { }
11+
Spell(string name): scrollName(name) { }
12+
virtual ~Spell() { }
13+
string revealScrollName() {
14+
return scrollName;
15+
}
16+
};
17+
18+
class Fireball : public Spell {
19+
private: int power;
20+
public:
21+
Fireball(int power): power(power) { }
22+
void revealFirepower(){
23+
cout << "Fireball: " << power << endl;
24+
}
25+
};
26+
27+
class Frostbite : public Spell {
28+
private: int power;
29+
public:
30+
Frostbite(int power): power(power) { }
31+
void revealFrostpower(){
32+
cout << "Frostbite: " << power << endl;
33+
}
34+
};
35+
36+
class Thunderstorm : public Spell {
37+
private: int power;
38+
public:
39+
Thunderstorm(int power): power(power) { }
40+
void revealThunderpower(){
41+
cout << "Thunderstorm: " << power << endl;
42+
}
43+
};
44+
45+
class Waterbolt : public Spell {
46+
private: int power;
47+
public:
48+
Waterbolt(int power): power(power) { }
49+
void revealWaterpower(){
50+
cout << "Waterbolt: " << power << endl;
51+
}
52+
};
53+
54+
class SpellJournal {
55+
public:
56+
static string journal;
57+
static string read() {
58+
return journal;
59+
}
60+
};
61+
string SpellJournal::journal = "";
62+
63+
void counterspell(Spell *spell) {
64+
65+
/* Enter your code here */
66+
if (Fireball *fb=dynamic_cast<Fireball*>(spell)) {
67+
fb->revealFirepower();
68+
}
69+
else if (Frostbite *fz=dynamic_cast<Frostbite*>(spell)) {
70+
fz->revealFrostpower();
71+
}
72+
else if (Thunderstorm *ts=dynamic_cast<Thunderstorm*>(spell)) {
73+
ts->revealThunderpower();
74+
}
75+
else if (Waterbolt *wb=dynamic_cast<Waterbolt*>(spell)) {
76+
wb->revealWaterpower();
77+
}
78+
else { // generic spell
79+
string spellN = spell->revealScrollName();
80+
string spellJ = SpellJournal::read();
81+
int m=spellN.length();
82+
int n=spellJ.length();
83+
int array[m+1][n+1];
84+
85+
// solve for LCS
86+
for (int i=0;i<=m;i++) array[i][0]=0;
87+
for (int j=0;j<=n;j++) array[0][j]=0;
88+
for (int i=1;i<=m;i++) {
89+
for (int j=1;j<=n;j++) {
90+
if (spellN[i-1] == spellJ[j-1])
91+
array[i][j]=array[i-1][j-1]+1;
92+
else
93+
array[i][j]=max(array[i][j-1],array[i-1][j]);
94+
}
95+
}
96+
cout << array[m][n] << endl;
97+
}
98+
99+
}
100+
101+
class Wizard {
102+
public:
103+
Spell *cast() {
104+
Spell *spell;
105+
string s; cin >> s;
106+
int power; cin >> power;
107+
if(s == "fire") {
108+
spell = new Fireball(power);
109+
}
110+
else if(s == "frost") {
111+
spell = new Frostbite(power);
112+
}
113+
else if(s == "water") {
114+
spell = new Waterbolt(power);
115+
}
116+
else if(s == "thunder") {
117+
spell = new Thunderstorm(power);
118+
}
119+
else {
120+
spell = new Spell(s);
121+
cin >> SpellJournal::journal;
122+
}
123+
return spell;
124+
}
125+
};
126+
127+
int main() {
128+
int T;
129+
cin >> T;
130+
Wizard Arawn;
131+
while(T--) {
132+
Spell *spell = Arawn.cast();
133+
counterspell(spell);
134+
}
135+
return 0;
136+
}

0 commit comments

Comments
 (0)