algorithms
algorithms copied to clipboard
Added two optimized pythonic functions and removed unnecessary semicolons.
Hello, I have proposed the following changes to include optimized code and resemble pythonic syntax in the strings section.
-
Added function
is_palindrome_pythonic()instrings/is_palindrome.pyfile. Used list comprehension to store alphanumeric characters in a list, then compared it with its reverse (using the pythonic way) and finally return the result of the comparison. -
Added function
add_binary_pythonic()instrings/add_binary.pyfile.- The string parameter
ais concatenated with a prefix'0b'. This string (e.g. '0b1101') represents binary number 1101 in python.'0b' + a - Then the string is passed to
int()function of python with second parameter as 2, which represents the base of the first parameter string (binary = base 2).int('0b' + a, 2) int()function converts the string (e.g. '0b1101') to the decimal value and stores in the variabledecimal_a. (e.g. '0b1101' converts to 13)decimal_a = int('0b' + a, 2)- Same thing happens with parameter
b.decimal_b = int('0b' + b, 2) - Then both the decimal values are added and again converted to binary value string using python's
bin()function.bin(decimal_a + decimal_b) - This generates a string with prefix
'0b'(e.g. '0b10110'), which is sliced to keep the part after'0b'.[2:] - The resultant string representing the binary value is returned.
- The string parameter
-
Removed semicolons in
strings/int_to_roman.pyfile to resemble python syntax.
Any suggestions and feedback are welcomed.