Skip to content

Conversation

@rushil180101
Copy link

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.

Used list comprehension to store alphanumeric characters.
Then compared the original and the reversed list and returned the result
of the comparison.
It uses inbuilt type conversions.
Strings are converted to decimal representation, then the decimal
numbers are added and the result is again converted to string. The
required part of the resulting string is returned by the function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant