Skip to content

Commit dbd0bb0

Browse files
author
Carlos Leonard
committed
update
1 parent 04ea1ea commit dbd0bb0

24 files changed

+622
-8
lines changed

61_LargestFour.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// For this challenge you will need to find the four largest elements in an array.
2+
/*
3+
have the function LargestFour(arr) take the array of integers stored in arr, and find the four largest elements and return their sum. For example: if arr is [4, 5, -2, 3, 1, 2, 6, 6] then the four largest elements in this array are 6, 6, 4, and 5 and the total sum of these numbers is 21, so your program should return 21. If there are less than four numbers in the array your program should return the sum of all the numbers in the array.
4+
*/
5+
6+
#include <iostream>
7+
#include <string>
8+
using namespace std;
9+
10+
/*
11+
Check the size of the input
12+
If less than or equal to 4 find the sum
13+
Order the numbers
14+
Get the last 4
15+
find the sum
16+
*/
17+
18+
void bubbleSort(int arr[], int size)
19+
{
20+
bool swap;
21+
do
22+
{
23+
swap = false;
24+
int temp;
25+
26+
for (int x = 0; x < size-1; x++)
27+
{
28+
if (arr[x] > arr[x + 1])
29+
{
30+
temp = arr[x];
31+
arr[x] = arr[x + 1];
32+
arr[x + 1] = temp;
33+
swap = true;
34+
}
35+
}
36+
} while (swap);
37+
}
38+
39+
int LargestFour(int arr[], int size)
40+
{
41+
int total = 0;
42+
if (size <= 4)
43+
{
44+
for (int x = 0; x < size; x++)
45+
{
46+
total += arr[x];
47+
}
48+
return total;
49+
}
50+
else
51+
{
52+
// Sort our input
53+
bubbleSort(arr, size);
54+
55+
int count = 0;
56+
for (int x = size - 1; count < 4; count++, x--)
57+
{
58+
total += arr[x];
59+
}
60+
return total;
61+
}
62+
}
63+
64+
int main()
65+
{
66+
int A[] = { 4, 5, -2, 3, 1, 2, 6, 6 };
67+
int B[] = { 1, 1, 1, -5 };
68+
int C[] = { 0, 0, 2, 3, 7, 1 };
69+
cout << LargestFour(A, sizeof(A)/sizeof(A[0])) << endl; // 21
70+
cout << LargestFour(B, sizeof(B) / sizeof(B[0])) << endl; // -2
71+
cout << LargestFour(C, sizeof(C) / sizeof(C[0])) << endl; // 13
72+
return 0;
73+
}
74+
75+

62_QuestionMarks.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// For this challenge you will be determining if a string follows a particular pattern.
2+
/*
3+
have the function QuestionsMarks(str) take the str string parameter, which will contain single digit numbers, letters, and question marks, and check if there are exactly 3 question marks between every pair of two numbers that add up to 10. If so, then your program should return the string true, otherwise it should return the string false. If there aren't any two numbers that add up to 10 in the string, then your program should return false as well.
4+
5+
For example: if str is "arrb6???4xxbl5???eee5" then your program should return true because there are exactly 3 question marks between 6 and 4, and 3 question marks between 5 and 5 at the end of the string.
6+
*/
7+
8+
#include <iostream>
9+
#include <string>
10+
using std::cout;
11+
using std::string;
12+
using std::cin;
13+
using std::endl;
14+
15+
/*
16+
locate any digits
17+
analyze if the pair of number add up to 10
18+
Keep track of their index if they do
19+
Check to see if there are exactly 3 question marks in between them
20+
*/
21+
22+
string QuestionsMarks(string str)
23+
{
24+
bool correctPair = false;
25+
26+
for (int x = 0; x < str.size()-1; x++)
27+
{
28+
if (str[x] >= '0' && str[x] <= '9')
29+
{
30+
for (int y = x + 1; y < str.size(); y++)
31+
{
32+
if (str[y] >= '0' && str[y] <= '9')
33+
{
34+
int total = int(str[x]) - 48 + (int(str[y]) - 48);
35+
36+
if (total == 10)
37+
{
38+
correctPair = true;
39+
40+
// Count number of question marks
41+
int count = 0;
42+
for (int i = x; i < y; i++)
43+
{
44+
if (str[i] == '?')
45+
{
46+
count++;
47+
}
48+
}
49+
50+
51+
if (count != 3)
52+
{
53+
return "false";
54+
}
55+
else
56+
{
57+
break;
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
65+
if (correctPair)
66+
{
67+
return "true";
68+
}
69+
else
70+
{
71+
return "false";
72+
}
73+
74+
}
75+
76+
int main()
77+
{
78+
cout << QuestionsMarks("arrb6???4xxbl5???eee5") << endl; // true
79+
cout << QuestionsMarks("acc?7??sss?3rr1??????5") << endl; // true
80+
cout << QuestionsMarks("aa6?9") << endl; // false
81+
return 0;
82+
}

63_CamelCase.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// For this challenge you will be converting a string into camel case format.
2+
/*
3+
have the function CamelCase(str) take the str parameter being passed and return it in proper camel case format where the first letter of each word is capitalized (excluding the first letter). The string will only contain letters and some combination of delimiter punctuation characters separating each word.
4+
5+
For example: if str is "BOB loves-coding" then your program should return the string bobLovesCoding.
6+
*/
7+
8+
#include <iostream>
9+
#include <string>
10+
using namespace std;
11+
12+
/*
13+
iterate through string
14+
make the first word lowercase
15+
locate any punctuations as a signal to Capitalize the first letter of each word
16+
*/
17+
18+
string CamelCase(string str)
19+
{
20+
for (int x = 0; x < str.size(); x++)
21+
{
22+
if (x == 0)
23+
{
24+
str[x] = tolower(str[x]);
25+
continue;
26+
}
27+
28+
if (int(str[x-1]) >= 32 && int(str[x-1]) <= 47)
29+
{
30+
str[x] = toupper(str[x]);
31+
str.erase(str.begin() + x-1);
32+
x--;
33+
34+
}
35+
else
36+
{
37+
str[x] = tolower(str[x]);
38+
}
39+
}
40+
41+
return str;
42+
}
43+
44+
int main()
45+
{
46+
cout << CamelCase("BOB loves-coding") << endl; //bobLovesCoding
47+
cout << CamelCase("cats AND*Dogs-are Awesome") << endl; // catsAndDogsAreAwesome
48+
cout << CamelCase("a b c d-e-f%g") << endl; // aBCDEFG
49+
return 0;
50+
}

64_SumMultiplier.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// For this challenge you will determine if two numbers can be multiplied to some specific number.
2+
/*
3+
have the function SumMultiplier(arr) take the array of numbers stored in arr and return the string true if any two numbers can be multiplied so that the answer is greater than double the sum of all the elements in the array. If not, return the string false. For example: if arr is [2, 5, 6, -6, 16, 2, 3, 6, 5, 3] then the sum of all these elements is 42 and doubling it is 84. There are two elements in the array, 16 * 6 = 96 and 96 is greater than 84, so your program should return the string true.
4+
*/
5+
6+
#include <iostream>
7+
#include <string>
8+
using namespace std;
9+
10+
/*
11+
find the total sum
12+
double the sum
13+
analyze if any 2 numbers are greater than double sum
14+
*/
15+
16+
string SumMultiplier(int arr[], int size)
17+
{
18+
int total = 0;
19+
20+
for (int x = 0; x < size; x++)
21+
{
22+
total += arr[x];
23+
}
24+
25+
total *= 2;
26+
27+
for (int x = 0; x < size - 1; x++)
28+
{
29+
for (int y = x + 1; y < size; y++)
30+
{
31+
if (arr[x]*arr[y] > total)
32+
{
33+
return "true";
34+
}
35+
}
36+
}
37+
38+
return "false";
39+
}
40+
41+
42+
int main()
43+
{
44+
int A[] = { 2, 5, 6, -6, 16, 2, 3, 6, 5, 3 };
45+
int B[] = { 2, 2, 2, 2, 4, 1 };
46+
int C[] = { 1, 1, 2, 10, 3, 1, 12 };
47+
cout << SumMultiplier(A, sizeof(A) / sizeof(A[0])) << endl; // true
48+
cout << SumMultiplier(B, sizeof(B) / sizeof(B[0])) << endl; // false
49+
cout << SumMultiplier(C, sizeof(C) / sizeof(C[0])) << endl; // true
50+
return 0;
51+
}

65_StringMerge.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// For this challenge you will be merging two different strings together.
2+
/*
3+
have the function StringMerge(str) read the str parameter being passed which will contain a large string of alphanumeric characters with a single asterisk character splitting the string evenly into two separate strings. Your goal is to return a new string by pairing up the characters in the corresponding locations in both strings. For example: if str is "abc1*kyoo" then your program should return the string akbyco1o because a pairs with k, b pairs with y, etc. The string will always split evenly with the asterisk in the center.
4+
*/
5+
6+
#include <iostream>
7+
#include <string>
8+
using namespace std;
9+
10+
/*
11+
split the string into 2
12+
after combine the 2 strings into one doing a step by step merge alternating between the 2
13+
*/
14+
15+
string StringMerge(string str)
16+
{
17+
int middle = str.find("*");
18+
19+
string first = str.substr(0, middle);
20+
string second = str.substr(middle+1, str.size() - middle);
21+
string result;
22+
23+
for (int x = 0; x < middle; x++)
24+
{
25+
result.push_back(first[x]);
26+
result.push_back(second[x]);
27+
}
28+
29+
return result;
30+
}
31+
32+
int main()
33+
{
34+
cout << StringMerge("abc1*kyoo") << endl; // akbyco1o
35+
cout << StringMerge("aaa*bbb") << endl; // ababab
36+
cout << StringMerge("123hg*aaabb") << endl; // 1a2a3ahbgb
37+
return 0;
38+
39+
}

66_SimpleEvens.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// For this challenge you will be checking whether a number is even.
2+
/*
3+
have the function SimpleEvens(num) check whether every single number passed in parameter is even. If so, return the string true, otherwise return the string false. For example: if num is 4602225 your program should return the string false because 5 is not an even number.
4+
*/
5+
6+
#include <iostream>
7+
#include <string>
8+
using namespace std;
9+
10+
11+
/*
12+
take the input number at the start and check if is even
13+
If so continue breaking that number down by multiplying by .1
14+
After each iteration check if is even
15+
This will check all the digits of the input
16+
*/
17+
18+
19+
string SimpleEvens(long long num)
20+
{
21+
while (num > 0)
22+
{
23+
if (num % 2 == 0)
24+
{
25+
num *= .1;
26+
}
27+
else
28+
{
29+
return "false";
30+
}
31+
}
32+
return "true";
33+
}
34+
35+
int main()
36+
{
37+
cout << SimpleEvens(4602225) << endl; // false
38+
cout << SimpleEvens(2222220222) << endl; // true
39+
cout << SimpleEvens(20864646452) << endl; // false
40+
return 0;
41+
42+
}

67_OneDecremented.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// For this challenge you will be counting how many times a specific pattern occurs in a large number.
2+
/*
3+
have the function OneDecremented(num) count how many times a digit appears that is exactly one less than the previous digit. For example: if num is 5655984 then your program should return 2 because 5 appears directly after 6 and 8 appears directly after 9. The input will always contain at least 1 digit.
4+
*/
5+
6+
#include <iostream>
7+
#include <string>
8+
#include <sstream>
9+
using namespace std;
10+
11+
int OneDecremented(long long num)
12+
{
13+
stringstream convert;
14+
convert << num;
15+
string number = convert.str();
16+
17+
int count = 0;
18+
for (int x = 0; x < number.size(); x++)
19+
{
20+
if (x > 0)
21+
{
22+
if (int(number[x-1]) - int(number[x]) == 1)
23+
{
24+
count++;
25+
}
26+
}
27+
}
28+
29+
return count;
30+
}
31+
32+
int main()
33+
{
34+
cout << OneDecremented(5655984) << endl; // 2
35+
cout << OneDecremented(9876541110) << endl; // 6
36+
cout << OneDecremented(56) << endl; // 0
37+
return 0;
38+
39+
}

0 commit comments

Comments
 (0)