-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOther_IQ+Answers.py
210 lines (146 loc) · 4.8 KB
/
Other_IQ+Answers.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# List: Remove Element
def remove_element(nums, val):
i = 0
while i < len(nums):
if nums[i] == val:
nums.pop(i)
else:
i += 1
return len(nums)
def remove_element(nums, val):
# Initialize the index variable to 0
i = 0
# Iterate through the array using a while loop
while i < len(nums):
# Check if the current element is equal to the given value
if nums[i] == val:
# If equal, remove the element in-place using pop()
nums.pop(i)
else:
# If not equal, increment the index to move to the next element
i += 1
# Return the new length of the modified array
return len(nums)
# List: Find Max Min
def find_max_min(myList):
maximum = minimum = myList[0]
for num in myList:
if num > maximum:
maximum = num
elif num < minimum:
minimum = num
return maximum, minimum
def find_max_min(myList):
# Initialize the maximum and minimum variables
# to the first element of the list
maximum = minimum = myList[0]
# Traverse the list and update the
# maximum and minimum variables
for num in myList:
if num > maximum:
maximum = num
elif num < minimum:
minimum = num
# Return the maximum and minimum variables
return maximum, minimum
# List: Find Longest String
def find_longest_string(string_list):
longest_string = ""
for string in string_list:
if len(string) > len(longest_string):
longest_string = string
return longest_string
def find_longest_string(string_list):
# Initialize the variable to store the longest string to an empty string
longest_string = ""
# Loop through each string in the list of strings
for string in string_list:
# Check if the length of the current string is greater than the
# length of the current longest string
if len(string) > len(longest_string):
# If so, update the longest string to be the current string
longest_string = string
# Return the longest string
return longest_string
# List: Remove Duplicates
def remove_duplicates(nums):
if not nums:
return 0
write_pointer = 1
for read_pointer in range(1, len(nums)):
if nums[read_pointer] != nums[read_pointer - 1]:
nums[write_pointer] = nums[read_pointer]
write_pointer += 1
return write_pointer
def remove_duplicates(nums):
# Return 0 if input list is empty
if not nums:
return 0
# Initialize write_pointer at index 1
write_pointer = 1
# Loop through list starting from index 1
for read_pointer in range(1, len(nums)):
# Check if current element is unique
if nums[read_pointer] != nums[read_pointer - 1]:
# Move unique element to write_pointer
nums[write_pointer] = nums[read_pointer]
# Increment write_pointer for next unique element
write_pointer += 1
# Return new length of list with unique elements
return write_pointer
# List: Max Profit
def max_profit(prices):
min_price = float('inf')
max_profit = 0
for price in prices:
min_price = min(min_price, price)
profit = price - min_price
max_profit = max(max_profit, profit)
return max_profit
def max_profit(prices):
# Initialize min_price to positive infinity
min_price = float('inf')
# Initialize max_profit to 0
max_profit = 0
# Iterate through the list of stock prices
for price in prices:
# Update min_price with the lowest price so far
min_price = min(min_price, price)
# Calculate profit by selling at the current price
profit = price - min_price
# Update max_profit with the highest profit so far
max_profit = max(max_profit, profit)
# Return the maximum profit after iterating
return max_profit
# List: Rotate
def rotate(nums, k):
k = k % len(nums)
nums[:] = nums[-k:] + nums[:-k]
def rotate(nums, k):
# Calculate the effective number of steps to rotate
k = k % len(nums)
# Rearrange the elements in the rotated order
nums[:] = nums[-k:] + nums[:-k]
# List: Max Sub Array
def max_subarray(nums):
if not nums:
return 0
max_sum = current_sum = nums[0]
for num in nums[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
def max_subarray(nums):
# Return 0 if input list is empty
if not nums:
return 0
# Initialize max_sum and current_sum
max_sum = current_sum = nums[0]
# Iterate through the remaining elements
for num in nums[1:]:
# Update current_sum
current_sum = max(num, current_sum + num)
# Update max_sum if current_sum is larger
max_sum = max(max_sum, current_sum)
# Return the maximum subarray sum
return max_sum