Coding Questions in Spark SQL, Pyspark, and Python

Write a spark SQL query and pyspark dataframe based script and a coding sollution in python to find Longest Palindromic Substring?

PySpark DataFrame Approach:

from pyspark.sql import SparkSession
from pyspark.sql import functions as F

# Create SparkSession
spark = SparkSession.builder.appName("Longest Palindromic Substring").getOrCreate()

# Input string
s = "babad"

# Generate all substrings
substrings = []
for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        substrings.append((s[i:j], len(s[i:j])))

# Create DataFrame
substr_df = spark.createDataFrame(substrings, ["substr", "len"])

# Filter palindromic substrings
palindromes_df = substr_df.filter(substr_df.substr == F.reverse(substr_df.substr))

# Find longest palindromic substring
longest_palindrome = palindromes_df.orderBy(F.col("len").desc()).first()

print(longest_palindrome.substr)
from pyspark.sql import SparkSession
from pyspark.sql.functions import udf
from pyspark.sql.types import StringType

# Initialize Spark session
spark = SparkSession.builder.appName("LongestPalindrome").getOrCreate()

# Sample data
data = [("babad",), ("cbbd",), ("a",), ("ac",), ("racecar",)]
df = spark.createDataFrame(data, ["input_string"])

# Define the UDF to find the longest palindromic substring
def find_longest_palindrome(s):
    def expand_from_center(left, right):
        while left >= 0 and right < len(s) and s[left] == s[right]:
            left -= 1
            right += 1
        return s[left+1:right]

    longest_palindrome = ""
    for i in range(len(s)):
        odd_palindrome = expand_from_center(i, i)
        even_palindrome = expand_from_center(i, i+1)
        longest_palindrome = max(longest_palindrome, odd_palindrome, even_palindrome, key=len)
    return longest_palindrome

# Register the UDF
find_longest_palindrome_udf = udf(find_longest_palindrome, StringType())

# Apply the UDF on the DataFrame
df_with_palindrome = df.withColumn("longest_palindrome", find_longest_palindrome_udf("input_string"))

# Show the result
df_with_palindrome.show(truncate=False)

Python Approach:

def longest_palindromic_substring(s):
# Function to expand around the center and find the longest palindrome
def expand_from_center(left, right):
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return s[left+1:right]
longest_palindrome = ""

for i in range(len(s)):
    # Odd-length palindrome
    odd_palindrome = expand_from_center(i, i)

    # Even-length palindrome
    even_palindrome = expand_from_center(i, i+1)

    # Update the longest palindrome
    longest_palindrome = max(longest_palindrome, odd_palindrome, even_palindrome, key=len)

return longest_palindrome

Test the function

input_string = “babad”
print(“Longest Palindromic Substring:”, longest_palindromic_substring(input_string))

Pages ( 9 of 11 ): « Previous1 ... 78 9 1011Next »