-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode Challenge
More file actions
157 lines (114 loc) · 4.88 KB
/
Code Challenge
File metadata and controls
157 lines (114 loc) · 4.88 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
All code Challanges can be found on leetcode and there id and name will be added along with my solutions to the problem
43. Multiply Strings medium:
def multiply(self, num1: str, num2: str) -> str:
num1AsInt = int(num1)
num2AsInt = int(num2)
aboutToBeStr = num1AsInt * num2AsInt
finalStr = str(aboutToBeStr)
return finalStr
747. Largest Number At Least Twice of Others easy:
def dominantIndex(self, nums: List[int]) -> int:
maxValue = max(nums)
newList = [x for x in nums if x != 0 and x != maxValue]
for x in newList:
if maxValue / x < 2:
return -1
return nums.index(maxValue)
1108. Defaning an IP address:
def defangIPaddr(self, address: str) -> str:
defanged_IP = address.replace(".", "[.]")
return defanged_IP
1342 Number of steps till zero:
def numberOfSteps (self, num: int) -> int:
stepCount = 0
while num != 0:
if num %2 != 0:
num -= 1
stepCount += 1
elif num % 2 == 0:
num = num / 2
stepCount += 1
return stepCount
1287 Element Appearing More than 25% in Sorted Array:
def findSpecialInteger(arr: [int]) -> int:
counter = 0
num = arr[0]
for x in arr:
curr_num = arr.count(x)
if curr_num > counter:
counter = curr_num
num = x
return num
1323. Maximum 69 Number: def maximum69Number (self, num: int) -> int:
# this is so we can call replace() on our nums and change the first 6 with 9 and then turn that back to int
string_num = str(num)
# I will use the replace() function to change the first six we find in our string
# and turn it into a 9 in the highest place to always get the highest possible number
final_number = string_num.replace("6", "9",1)
# casting the final number back into an int
final_as_int = int(final_number)
return final_as_int
CODESIGNLE CHALLENGES:
# CodeChallenges
Challange #1
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.
Example
For inputArray = [3, 6, -2, -5, 7, 3], the output should be
adjacentElementsProduct(inputArray) = 21.
7 and 3 produce the largest product.
func adjacentElementsProduct(inputArray: [Int]) -> Int {
var holdingArray: [Int] = []
let count = inputArray.count
var i = 0
var maxNum = 0
-----------------------
while i < count - 1 {
holdingArray.append(inputArray[i] * inputArray[i + 1])
i = i + 1
maxNum = holdingArray.max() ?? 0
}
return maxNum
}
-----------------------
Challange #2
Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.
A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.
func shapeArea(n: Int) -> Int {
var area: Int
if n != 0 {
area = ((n - 1) * 2 * n) + 1
print(area)
return area
}
return n
}
-----------------------
Challange #3
Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.
func makeArrayConsecutive2(statues: [Int]) -> Int {
var output = 0
var maxNum = statues.max() ?? 0
var minNum = statues.min() ?? 0
var count = statues.count ?? 0
output = maxNum - minNum - count + 1
/*
the comments here show a diffrent train of thought for getting the correct output
back but it would fail do to the fact it would take to long to get the test to pass
I do keep return output at the bottom however since there was no need to change the variable name
*/
// var sorted = statues.sorted()
// let amount = sorted.count
// for count in 0...amount {
// while sorted[count] + 1 != sorted[count + 1] {
// print("we are in the while loop")
// sorted.append(sorted[count] + 1)
// output += 1
// print("output is \(output)")
// sorted = sorted.sorted()
// print(sorted)
// }
// }
// print(output)
// print("this is going to fail")
return output
}