forked from ChicoState/UnitTestPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice.cpp
More file actions
139 lines (119 loc) · 2.75 KB
/
Practice.cpp
File metadata and controls
139 lines (119 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "Practice.h"
#include <string>
#include <stdio.h>
using std::string;
// Receive three integers and rearrange their values so that they are in
// descending order from greatest (first) to least (third)
void Practice::sortDescending(int & first, int & second, int & third)
{
int temp;
if( second > first ){
temp = first;
first = second;
second = temp;
}
if( third > first ){
temp = first;
first = third;
third = temp;
}
if( third > second ){
temp = third;
third = second;
second = temp;
}
}
// Receive a string and return whether or not it is strictly a palindrome,
// where it is spelled the same backwards and forwards when considering every
// character in the string, but disregarding case ('x' is the same as 'X')
bool Practice::isPalindrome(string input)
{
if( input.length() == 0 ){
return true;
}
else{
return ( toupper(input[0]) == toupper(input[input.size()-1]) && isPalindrome(input.substr(1, input.length()-2)) );
}
}
// This function receives a string and counts how many times the same character
// is repeated at the beginning of the string, before any other characters. The
// function is case sensative so 'Z' is different than 'z'.
int Practice::count_starting_repeats(string word)
{
int repetition = 1;
char letter;
if( word.length() == 0 )
return 0;
for(unsigned int i=0; i < word.length() - 1; i++){
if( word[i] == word[i+1] ){
repetition++;
}
}
return repetition;
}
// Receives an array that represents the hours someone sleeps each night of the week
// (as an array of seven integers) and returns a pointer to locate the first instance
// of an "all nighter" in the array (a day with 0 hours sleep) and returns the pointer.
// However, if there are no such days found, the function should return nullptr.
/* Implementation A
int* Practice::allnighter(int sleep[7])
{
return nullptr;
}
*/
/* Implementation B
int* Practice::allnighter(int sleep[7])
{
return & sleep[0];
}
*/
/* Implementation C
int* Practice::allnighter(int sleep[7])
{
int i=0;
int *place = &sleep[i];
while(place && place != nullptr)
{
place = &sleep[++i];
}
return place;
}
*/
/* Implementation D
int* Practice::allnighter(int sleep[7])
{
int i=0;
int *place = &sleep[i];
while(place && place != nullptr && i < 7)
{
place = &sleep[++i];
}
return place;
}
*/
/* Implementation E
int* Practice::allnighter(int sleep[7])
{
int *place = nullptr;
for(int i=0; i < 7; i++)
{
if( sleep[i] == 0 ){
place = &sleep[i];
return place;
}
}
return place;
}
*/
/* Implementation F
int* Practice::allnighter(int sleep[7])
{
int *place = nullptr;
for(int i=0; i < 7; i++)
{
if( sleep[i] == 0 )
place = &sleep[i];
}
return place;
}
*/