Exercise For Python
Exercise For Python
Exercise For Python
1. Palindrome Number
Example 1:
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and
from right to left.
Example 2:
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From
right to left, it becomes 121-. Therefore it is not a
palindrome.
Example 3:
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it
is not a palindrome.
Constraints:
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two ones added together. 12
is written as XII, which is simply X + II. The number 27 is written as XXVII,
which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However,
the numeral for four is not IIII. Instead, the number four is written as IV. Because
the one is before the five we subtract it making four. The same principle applies to the
number nine, which is written as IX. There are six instances where subtraction is
used:
Input: s = "III"
Output: 3
Explanation: III = 3.
Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
• Starting with any positive integer, replace the number by the sum of the
squares of its digits.
• Repeat the process until the number equals 1 (where it will stay), or it loops
endlessly in a cycle which does not include 1.
• Those numbers for which this process ends in 1 are happy.
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
Constraints:
Write a function that reverses a string. The input string is given as an array of
characters ‘s’.
You must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
5. Missing Number
Given an array nums containing n distinct numbers in the range [0, n], return
the only number in the range that is missing from the array.
Example 1:
Example 2:
Constraints:
• n == nums.length
• 1 <= n <= 104
• 0 <= nums[i] <= n
• All the numbers of nums are unique.
6. Bank Account
Write a Python class BankAccount with attributes like account_number, balance,
date_of_opening and customer_name, and methods like deposit, withdraw, and
check_balance.