Skip to content

Commit 49d660a

Browse files
author
Carlos Leonard
committed
update
1 parent 76deb4c commit 49d660a

14 files changed

+213
-8
lines changed

73_ClosestEnemy2.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ For this input your program should return 2 because the closest enemy (2) is 2 s
1616
using namespace std;
1717

1818
/*
19+
NOT FINISHED
20+
21+
1922
locate starting point
2023
check if the matrix has any enemies
2124
*/

75_StringPeriods.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// For this challenge you will need to find the smallest repeating substring.
2+
/*
3+
have the function StringPeriods(str) take the str parameter being passed and determine if there is some substring K that can be repeated N > 1 times to produce the input string exactly as it appears. Your program should return the longest substring K, and if there is none it should return the string -1.
4+
5+
For example: if str is "abcababcababcab" then your program should return abcab because that is the longest substring that is repeated 3 times to create the final string. Another example: if str is "abababababab" then your program should return ababab because it is the longest substring. If the input string contains only a single character, your program should return the string -1.
6+
*/
7+
8+
#include <iostream>
9+
#include <string>
10+
using namespace std;
11+
12+
/*
13+
traverse the string
14+
extract a substring from the string starting from the beginning
15+
keep count of its length only if the substring can produce the input string after being repeated
16+
iterate again but increasing the substring length
17+
*/
18+
string StringPeriods(string str)
19+
{
20+
if (str.length() == 1)
21+
{
22+
return "-1";
23+
}
24+
25+
string substring = "";
26+
string result = "";
27+
int current = 0;
28+
29+
// Loop to traverse input string and compare substring
30+
while (current < str.length() / 2)
31+
{
32+
bool valid = true; // signal checking if the substring can complete the input string after repetitions
33+
substring += str[current]; // With each iteration the substring to check will increase
34+
35+
for (int x = current + 1; x < str.length(); x+=substring.length())
36+
{
37+
// condition comparing the current substring to substring in our input string
38+
// If is not equal than this current substring is invalid
39+
if (substring != str.substr(x, substring.length()))
40+
{
41+
valid = false;
42+
break;
43+
}
44+
}
45+
46+
// Will update the largest substring that is valid
47+
if (valid && substring.length() > result.length())
48+
{
49+
result = substring;
50+
}
51+
52+
current++;
53+
}
54+
55+
if (result.length() >= 1)
56+
{
57+
return result;
58+
}
59+
else
60+
{
61+
return "-1";
62+
}
63+
}
64+
65+
int main()
66+
{
67+
cout << StringPeriods("abcababcababcab") << endl; // abcab
68+
cout << StringPeriods("abcxabc") << endl; // -1
69+
cout << StringPeriods("affedaaffed") << endl; // -1
70+
cout << StringPeriods("abababababab") << endl; // ababab
71+
cout << StringPeriods("gg") << endl; // g
72+
return 0;
73+
}

76_NumberStream.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// For this challenge you will determine if a stream of digits occurs in a string.
2+
/*
3+
have the function NumberStream(str) take the str parameter being passed which will contain the numbers 2 through 9, and determine if there is a consecutive stream of digits of at least N length where N is the actual digit value. If so, return the string true, otherwise return the string false. For example: if str is "6539923335" then your program should return the string true because there is a consecutive stream of 3's of length 3. The input string will always contain at least one digit.
4+
*/
5+
6+
#include <iostream>
7+
#include <string>
8+
using namespace std;
9+
10+
/*
11+
traverse the string
12+
have a temp string that will increase if values are consecutive
13+
if the next value is not the same as current consecutive values
14+
break and analyze the length of the temp string
15+
if the length matches the digit value than return true
16+
else continue until the string has been fully traversed
17+
*/
18+
string NumberStream(string str)
19+
{
20+
if (str.length() == 1)
21+
{
22+
return "false";
23+
}
24+
25+
string temp = "";
26+
temp += str[0];
27+
28+
for (int x = 1; x < str.length(); x++)
29+
{
30+
if (temp.empty())
31+
{
32+
temp.push_back(str[x]);
33+
continue;
34+
}
35+
36+
// If values are the same than add it to temp string
37+
if (str[x] == temp[0])
38+
{
39+
temp += str[x];
40+
}
41+
else
42+
{
43+
// clear the temp string
44+
temp.clear();
45+
}
46+
47+
// condition to analyze length of temp string
48+
// if length matches the consecutive values than it is valid
49+
if (temp.length() > 1 && char(temp.length() + 48) == temp[0])
50+
{
51+
return "true";
52+
}
53+
}
54+
55+
return "false";
56+
}
57+
58+
int main()
59+
{
60+
cout << NumberStream("5556293383563665") << endl; // false
61+
cout << NumberStream("5788888888882339999") << endl; // true
62+
cout << NumberStream("6539923335") << endl; // true
63+
return 0;
64+
}

77_PalindromeSwapper.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// For this challenge you will determine if a palindrome can be created in a string.
2+
/*
3+
have the function PalindromeSwapper(str) take the str parameter being passed and determine if a palindrome can be created by swapping two adjacent characters in the string. If it is possible to create a palindrome, then your program should return the palindrome, if not then return the string -1. The input string will only contain alphabetic characters. For example: if str is "rcaecar" then you can create a palindrome by swapping the second and third characters, so your program should return the string racecar which is the final palindromic string.
4+
*/
5+
6+
#include <iostream>
7+
#include <string>
8+
#include <algorithm>
9+
using namespace std;
10+
11+
/*
12+
traverse the string
13+
continue to swap adjacent characters and check if string is a palindrome
14+
*/
15+
bool palindrome(string value)
16+
{
17+
string temp = value;
18+
19+
reverse(temp.begin(), temp.end());
20+
21+
if (temp == value)
22+
{
23+
return true;
24+
}
25+
26+
return false;
27+
}
28+
29+
string PalindromeSwapper(string str)
30+
{
31+
// in case original input is already a palindrome
32+
if (palindrome(str))
33+
{
34+
return str;
35+
}
36+
37+
// traversing the string
38+
for (int x = 0; x < str.length()-1; x++)
39+
{
40+
// swap current adjacent characters
41+
swap(str[x], str[x + 1]);
42+
43+
// check if string is now a palindrome
44+
if (palindrome(str))
45+
{
46+
return str;
47+
}
48+
else
49+
{
50+
// if not valid swap again to set back to the original
51+
swap(str[x], str[x + 1]);
52+
}
53+
}
54+
55+
return "-1";
56+
}
57+
58+
int main()
59+
{
60+
cout << PalindromeSwapper("anna") << endl; // anna
61+
cout << PalindromeSwapper("kyaak") << endl; // kayak
62+
cout << PalindromeSwapper("rcaecar") << endl; // racecar
63+
cout << PalindromeSwapper("madam") << endl;
64+
return 0;
65+
}

Debug/Fun Practice.log

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
Build started 4/13/2017 1:04:49 PM.
1+
Build started 7/31/2017 5:46:24 PM.
22
1>Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" on node 2 (Build target(s)).
33
1>ClCompile:
4-
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 74_SerialNumber.cpp
5-
74_SerialNumber.cpp
6-
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\74_serialnumber.cpp(35): warning C4018: '<' : signed/unsigned mismatch
4+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 77_PalindromeSwapper.cpp
5+
77_PalindromeSwapper.cpp
6+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\77_palindromeswapper.cpp(38): warning C4018: '<' : signed/unsigned mismatch
77
Link:
8-
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\74_SerialNumber.obj
8+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\77_PalindromeSwapper.obj
99
Fun Practice.vcxproj -> C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe
1010
1>Done Building Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" (Build target(s)).
1111

1212
Build succeeded.
1313

14-
Time Elapsed 00:00:03.87
14+
Time Elapsed 00:00:01.33
49.2 KB
Binary file not shown.
3.24 KB
Binary file not shown.
3.32 KB
Binary file not shown.
20 Bytes
Binary file not shown.
20 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)