You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
string="racecar"is_palindrome=Trueforiinrange(len(string) //2):
ifstring[i] !=string[-(i+1)]:
is_palindrome=False# Use a flag to exit the loop once a mismatch is found# The loop will continue but `is_palindrome` will remain `False`# Final output based on the flagprint(is_palindrome)
B. Check if a substring is repeated consecutively:
string="abcabc"substring="abc"is_repeated=True# Ensure the length of the string is a multiple of the substring lengthiflen(string) %len(substring) !=0:
is_repeated=Falseelse:
# Check if each segment matches the substringforiinrange(0, len(string), len(substring)):
ifstring[i:i+len(substring)] !=substring:
is_repeated=Falseprint(is_repeated)
C. Longest substring without repeating characters:
string="abcabcbb"longest=""foriinrange(len(string)):
substring=""is_unique=Trueforjinrange(i, len(string)):
ifstring[j] insubstring:
is_unique=Falseifis_unique:
substring+=string[j]
else:
# Once a non-unique character is found, stop adding to substringbreakiflen(substring) >len(longest):
longest=substringprint(longest)
D. Find the first non-repeating character:
string="aabbccdde"non_repeating=""found=Falseforiinrange(len(string)):
repeating=Falseforjinrange(len(string)):
ifi!=jandstring[i] ==string[j]:
repeating=True# The inner loop will complete even if repeating is foundifnotrepeating:
non_repeating=string[i]
found=True# If found is True, set a flag to stop processing further# But since we cannot use break, we handle it outside of this loop# After the loop ends, if we have found a non-repeating character, set itiffound:
print(non_repeating)
else:
print("No non-repeating character found")
7a. Check if a string matches a pattern of alternating characters:
string="ababab"alternating=Trueforiinrange(2, len(string)):
ifstring[i] !=string[i%2]:
alternating=False# Instead of using break, use a flag to indicate that the string is not alternating# The loop will complete its iterations# We can then handle the result after the loop# After the loop ends, update alternating if needed# If `alternating` is False, it means we have detected non-alternating charactersprint(alternating)
B. Check if a string matches the pattern abc*:
string="aaabbbccc"valid=Truestate=0# 0 for 'a', 1 for 'b', 2 for 'c'forcharinstring:
ifstate==0:
ifchar=='a':
pass# 'a' is valid in state 0elifchar=='b':
state=1else:
valid=Falseelifstate==1:
ifchar=='b':
pass# 'b' is valid in state 1elifchar=='c':
state=2else:
valid=Falseelifstate==2:
ifchar=='c':
pass# 'c' is valid in state 2else:
valid=False# No need for further checks as loop completes naturallyprint(valid)
7c. Check if a string matches a specific format like "123-456-7890":
string="123-456-7890"valid=True# Check if the length is correctiflen(string) !=12:
valid=Falseelse:
foriinrange(len(string)):
ifiin [3, 7]:
# Check if hyphens are in the correct positionsifstring[i] !='-':
valid=Falseelse:
# Check if characters are digitsifnotstring[i].isdigit():
valid=False# Output the resultprint(valid)
D. Check if a string has balanced parentheses:
string="(())()"balance=0valid=Trueforcharinstring:
ifchar=='(':
balance+=1elifchar==')':
balance-=1# Update valid based on the balance during the loopvalid=validandbalance>=0# Final check to ensure balance is zerovalid=validandbalance==0print(valid)
strings= ["Hello", "world", "from", "Python"]
sorted_strings= []
strings_copy=strings[:] # Create a copy of the original listwhilestrings_copy:
# Find the shortest stringshortest=strings_copy[0]
forsinstrings_copy:
iflen(s) <len(shortest):
shortest=s# Build the sorted list using the + operatorsorted_strings=sorted_strings+ [shortest]
# Create a new list excluding the shortest string# This avoids using break by iterating through the listnew_strings_copy= []
forsinstrings_copy:
ifs!=shortest:
new_strings_copy=new_strings_copy+ [s]
strings_copy=new_strings_copyprint(sorted_strings)
D. Sort characters in a string by their frequency: