Description
1 Assignment 7
1.1 Question 1
A palindrome is a word, phrase, or sequence that is the same spelled forward as it is backwards. Write a function using a for-loop to determine if a string is a palindrome. Your function should only have one argument.
1.2 Question 2
Write a function using a while-loop to determine if a string is a palindrome. Your function should only have one argument.
[3]: # your code here
1.3 Question 3
Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2]
Example 3: Input: nums = [3,3], target = 6 Output: [0,1]
Constraints: 2 <= nums.length <= 104 –109 <= nums[i] <= 109 –109 <= target <= 109
Only one valid answer exists.
1
1.4 Question 4
How is a negative index used in Python? Show an example
1.5 Question 5
Check if two given strings are isomorphic to each other. Two strings str1 and str2 are called isomorphic if there is a one-to-one mapping possible for every character of str1 to every character of str2. And all occurrences of every character in ‘str1’ map to the same character in ‘str2’.
Input: str1 = “aab”, str2 = “xxy”
Output: True
‘a’ is mapped to ‘x’ and ‘b’ is mapped to ‘y’.
Input: str1 = “aab”, str2 = “xyz”
Output: False
One occurrence of ‘a’ in str1 has ‘x’ in str2 and other occurrence of ‘a’ has ‘y’.
A Simple Solution is to consider every character of ‘str1’ and check if all occurrences of it map to the same character in ‘str2’. The time complexity of this solution is O(n*n).
An Efficient Solution can solve this problem in O(n) time. The idea is to create an array to store mappings of processed characters.
[ ]:
2
Reviews
There are no reviews yet.