Site icon Doc Sity

SIT Python Alphanumeric Characters Questions

SIT Python Alphanumeric Characters Questions

Description

   

Unformatted Attachment Preview

You should edit the file “answer.py” and answer the questions asked.
Please don’t try to rename the files. Please don’t try to edit other files.
Make sure your code is bugs free and ready to run.
Complete the code
answer.py
“””
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):
“””
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):
“””
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.
“””
ddef isPalindrome(x):
To test the code in
answer_test.py
import pytest
import answer
class TestAnswer():
__correct__ = 0
__total__ = 0
@classmethod
def setup_class(cls):
print(“Before”)
cls.__correct__ = 0
cls.__total__ = 0
@classmethod
def teardown_class(cls):
print(f”Score:{(cls.__correct__/cls._
_total__)*100}%”)
def test_Q1_normal_list(self):
TestAnswer.__total__ += 1
test_list = [10, 5, 20, 15, 25]
output =
answer.find_maximum(test_list)
assert(output == 25)
TestAnswer.__correct__ += 1
def
test_Q1_same_number_list(self):
TestAnswer.__total__ += 1
test_list = [10, 10, 10, 10]
output =
answer.find_maximum(test_list)
assert(output == 10)
TestAnswer.__correct__ += 1
def test_Q1_empty_list(self):
TestAnswer.__total__ += 1
test_list = []
output =
answer.find_maximum(test_list)
assert(output == None)
TestAnswer.__correct__ += 1
def test_Q2_Input_0(self):
TestAnswer.__total__ += 1
test_num = 0
output =
answer.generateParenthesis(test_n
um)
assert(output == [”])
TestAnswer.__correct__ += 1
def test_Q2_Input_1(self):
TestAnswer.__total__ += 1
test_num = 1
output =
answer.generateParenthesis(test_n
um)
assert(output == [‘()’])
TestAnswer.__correct__ += 1
def test_Q2_Input_2(self):
TestAnswer.__total__ += 1
test_num = 2
output =
answer.generateParenthesis(test_n
um)
assert(output == [‘(())’, ‘()()’])
TestAnswer.__correct__ += 1
def test_Q2_Input_3(self):
TestAnswer.__total__ += 1
test_num = 3
output =
answer.generateParenthesis(test_n
um)
assert(output == [‘((()))’, ‘(()())’, ‘(())()’,
‘()(())’, ‘()()()’])
TestAnswer.__correct__ += 1
def test_Q2_Input_4(self):
TestAnswer.__total__ += 1
test_num = 4
output =
answer.generateParenthesis(test_n
um)
assert(output ==
[‘(((())))’,'((()()))’,'((())())’,'((()))()’,'(()(()))’,'((
)()())’,
‘(()())()’,'(())(())’,'(())()()’,'()((()))’,'()(()())’,'()(
())()’,'()()(())’,'()()()()’])
TestAnswer.__correct__ += 1
def test_Q2_Input_3(self):
TestAnswer.__total__ += 1
test_num = 3
output =
answer.generateParenthesis(test_n
um)
assert(output == [‘((()))’, ‘(()())’, ‘(())()’,
‘()(())’, ‘()()()’])
TestAnswer.__correct__ += 1
def test_Q3_normal_list(self):
TestAnswer.__total__ += 1
test_str= “A man, a plan, a canal:
Panama”
output =
answer.isPalindrome(test_str)
assert(output == True)
TestAnswer.__correct__ += 1
def test_Q3_normal_list_2(self):
TestAnswer.__total__ += 1
test_str= “Was it a car or a cat I
saw?”
output =
answer.isPalindrome(test_str)
assert(output == True)
TestAnswer.__correct__ += 1
def test_Q3_empty_list(self):
TestAnswer.__total__ += 1
test_str= “”
output =
answer.isPalindrome(test_str)
assert(output == True)
TestAnswer.__correct__ += 1
def test_Q3_wrong_list(self):
TestAnswer.__total__ += 1
test_str= “hello”
output =
answer.isPalindrome(test_str)
assert(output == False)
TestAnswer.__correct__ += 1
def test_Q3_wrong_list_2(self):
TestAnswer.__total__ += 1
test_str= “race a car”
output =
answer.isPalindrome(test_str)
assert(output == False)
TestAnswer.__correct__ += 1
def test_Q3_str_num_true(self):
TestAnswer.__total__ += 1
test_str= “1234321”
output =
answer.isPalindrome(test_str)
assert(output == True)
TestAnswer.__correct__ += 1
def test_Q3_str_num_wrong(self):
TestAnswer.__total__ += 1
test_str= “1234321a”
output =
answer.isPalindrome(test_str)
assert(output == False)
TestAnswer.__correct__ += 1

Purchase answer to see full
attachment
Explanation & Answer:
3 Questions
User generated content is uploaded by users for the purposes of learning and should be used following Studypool’s honor code & terms of service.

Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."

Exit mobile version