Skip to content

Commit d1457fa

Browse files
committed
Merge branch 'master' of ssh://github.com/Perilynn/HackerRank-Coding-Challenges
2 parents fcd46b5 + 0b3e565 commit d1457fa

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

Fun Problems/Rohan.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.lang.*;
2+
import java.util.*;
3+
4+
public class FunProblems {
5+
int toInt(String str) {
6+
return Integer.parseInt(str);
7+
}
8+
9+
String toString(double x) {
10+
return String.valueOf(x);
11+
}
12+
13+
boolean isNumber(String str) {
14+
try {
15+
double x = Double.parseDouble(str);
16+
} catch(NumberFormatException e) {
17+
return false;
18+
}
19+
20+
return true;
21+
}
22+
}

Fun Problems/aravind.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,64 @@ void FillSiblingVector(node* root) {
104104
children[i]->siblings.push_back(children[j]);
105105
}
106106
}
107+
}
108+
109+
/*
110+
Convert string to int manually, and convert an int to a string manually
111+
*/
112+
113+
int ToInt(string input) {
114+
int val = 0;
115+
for(int i = 0; i < input.size(); i++) value += (input[i] = '0') * pow(10, input.size() - i - 1);
116+
return value;
117+
}
118+
119+
string ToString(int input) {
120+
bool isNeg = input < 0;
121+
if(input == 0) return "0";
122+
string value = "";
123+
while(input != 0) {
124+
value += pow(-1, negative) * (input % 10) + '0';
125+
input /= 10;
126+
}
127+
if(negative) value += '-';
128+
return Reverse(value);
129+
}
130+
131+
132+
133+
/*
134+
Boolean function isNumber. given a string, return true if it is a
135+
valid number, return false if not. dont forget to account negatives
136+
and decimals
137+
*/
138+
139+
bool IsNumber(string number) // no fractions
140+
{
141+
bool foundDecimal = false;
142+
int i = 0;
143+
if (number[0] == '-')
144+
{
145+
i++;
146+
}
147+
148+
for ( ; i < number.size(); i++)
149+
{
150+
if (number[i] == '.')
151+
{
152+
if (foundDecimal)
153+
{
154+
return false;
155+
}
156+
else
157+
{
158+
foundDecimal = true;
159+
}
160+
}
161+
else if (!isdigit(number[i]))
162+
{
163+
return false;
164+
}
165+
}
166+
return true;
107167
}

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ git push origin master
5252
* insert <5,8> gets <4,8><10,15><20,21>
5353
* insert <16,17> gets <4,8><10,15><16,17><20,21>
5454
* insert <1,30> gets <1,30>
55-
3. In a tree of nodes, where each node is defined as
55+
4. In a tree of nodes, where each node is defined as
5656
```
5757
class node
5858
{
@@ -65,3 +65,24 @@ class node
6565
assume given a tree, all nodes have their children vectors filled.
6666
fill their siblings vector
6767

68+
5.
69+
Convert string to int manually, and convert an int to a string manually
70+
71+
E.x. "123" (type string) to 123 (type int)
72+
73+
and 123 (type int) to "123" (type string)
74+
(boilerplate c++ example)
75+
```
76+
int toInt(string x) {
77+
//your code here
78+
}
79+
80+
string toString(int x) {
81+
//your code here
82+
}
83+
```
84+
85+
6.
86+
Boolean function isNumber. given a string, return true if it is a
87+
valid number, return false if not. dont forget to account negatives
88+
and decimals

0 commit comments

Comments
 (0)