-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path555.py
More file actions
105 lines (88 loc) · 3.15 KB
/
555.py
File metadata and controls
105 lines (88 loc) · 3.15 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
"""
QUESTION 1:
========================================================================================================
Give a list of numbers, write a function to find the maximum number in the list.
Note: For the purpose of this problem, we define empty list will return None.
NOTE: DO NOT USE "MAX" KEYWORD. WRITE algorithm using loop.
Example 1:
========================================
Input: [10, 5, 20, 15, 25]
Output: 25
Example 2:
========================================
Input:[10,10,10]
Output: 10
Example 3:
========================================
Input: []
Output: None
"""
def find_maximum(numbers):
if numbers is None:
return None
max_number = numbers[0]
for i in numbers:
if i > max_number:
max_number = i
return max_number
"""
QUESTION 2:
========================================================================================================
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Write a function named generateParenthesis that takes an integer as an input and returns a list of strings
as an output. Note that you can define a function inside a function if necessary.
Example 1:
========================================
Input: 0
Output: ['']
Example 2:
========================================
Input: 1
Output: ['()']
Example 3:
========================================
Input: 2
Output: ['(())', '()()']
Example 4:
========================================
Input: 3
Output: ['((()))', '(()())', '(())()', '()(())', '()()()'])
"""
def generateParenthesis(n):
def backtrack(res, curr, open_count, close_count, max_pairs):
if len(curr) == max_pairs * 2:
res.append(curr)
return
if open_count < max_pairs:
backtrack(res, curr + '(', open_count + 1, close_count, max_pairs)
if close_count < open_count:
backtrack(res, curr + ')', open_count, close_count + 1, max_pairs)
res = []
backtrack(res, '', 0, 0, n)
return res
"""
QUESTION 3:
========================================================================================================
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
========================================
Input: "A man, a plan, a canal: Panama"
Output: true
Explanation:
After removing non-alphanumeric charactors and ignoring cases, the input is: amanaplanacanalPanama
which reads the same as backward and forward, so it is true.
Example 2:
=========================================
Input: "race a car"
Output: false
Write a function named isPalindrome that takes a string as an input and returns a bool as an output.
Explanation:
After removing non-alphanumeric charactors and ignoring cases, the input is: raceacar
which does not read the same as backward and forward, so it is false.
"""
def isPalindrome(x):
# Convert the string to lowercase and remove non-alphanumeric characters
x = ''.join(e for e in x.lower() if e.isalnum())
# Check if the string is equal to its reverse
return x == x[::-1]