forked from ucsb-cs16-f22/PRACTICE-FINAL-STARTER
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
90 lines (79 loc) · 1.72 KB
/
Copy pathcode.cpp
File metadata and controls
90 lines (79 loc) · 1.72 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
// Sanjay Srikanth
// 7463078
// Do not include a main function in this file
// Only submit this file
// See code.h for the descriptions of each problem
#include "code.h"
/* Problem 1: 10 pts */
TeaPacket* bestPacket(TeaPacket* head){
int value = 0;
if (head->next == nullptr)
{
return head;
}
else
{
if (head->rating * head->rarity >= bestPacket(head->next)->rating * bestPacket(head->next)->rarity)
{
return head;
}
else
{
return bestPacket(head->next);
}
}
}
/* Problem 2: 10 points*/
Node* insert(Node* head, int value){
if (head == nullptr) // if list is empty
{
Node* cur = new Node{value, nullptr};
head = cur;
}
else if (value < head->data) // if lowest value in list is greater than input value
{
Node* cur = new Node{value, head};
head = cur;
}
else
{
Node* cur = head;
Node* prev = head;
while (cur->data < value && cur->next != nullptr)
{
cur = cur->next;
}
if (cur->data < value) // if the last data value is still less than the inserted value
{
Node* insertion = new Node{value, nullptr};
cur->next = insertion;
}
else
{
while (prev->next != cur)
{
prev = prev->next;
}
Node* insertion = new Node{value, cur};
prev->next = insertion;
}
}
return head;
}
/* Problem 3: 10 points*/
bool isBalanced(std::string s){
int aCount = 0;
int bCount = 0;
for (int i=0; i<s.size(); i++)
{
if (s.at(i) == 'A')
{
aCount++;
}
else if (s.at(i) == 'B')
{
bCount++;
}
}
return aCount == bCount;
}