algorithms icon indicating copy to clipboard operation
algorithms copied to clipboard

Added two optimized pythonic functions and removed unnecessary semicolons.

Open rushil180101 opened this issue 4 years ago • 0 comments

Hello, I have proposed the following changes to include optimized code and resemble pythonic syntax in the strings section.

  1. Added function is_palindrome_pythonic() in strings/is_palindrome.py file. 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.

  2. Added function add_binary_pythonic() in strings/add_binary.py file.

    • The string parameter a is 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 variable decimal_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.
  3. Removed semicolons in strings/int_to_roman.py file to resemble python syntax.

Any suggestions and feedback are welcomed.

rushil180101 avatar Sep 25 '21 13:09 rushil180101