0% found this document useful (0 votes)
16 views249 pages

Python Programming Puzzles Solutions

The document contains a series of programming puzzles with Python solutions, each requiring specific conditions to be met in lists of integers or strings. Each puzzle includes a description, sample inputs and outputs, and a Python function to validate the conditions. The document is structured into five distinct puzzles, each with its own set of requirements and examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views249 pages

Python Programming Puzzles Solutions

The document contains a series of programming puzzles with Python solutions, each requiring specific conditions to be met in lists of integers or strings. Each puzzle includes a description, sample inputs and outputs, and a Python function to validate the conditions. The document is structured into five distinct puzzles, each with its own set of requirements and examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Puzzle 1

Question: Last update on May 30 2025 11:48:28 (UTC/GMT +8 hours)


Solution:
Last update on May 30 2025 11:48:28 (UTC/GMT +8 hours)

Check Nineteen and Five Occurrences

Write a Python program to find a list of integers with exactly two occurrences of
nineteen and at least three occurrences of five. Return True otherwise False.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Python Programming Puzzles Exercises [Link]:Check the length and the


fifth element occurs twice in a list.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[19, 19, 15, 5, 3, 5, 5, 2]
Output:
True

Input:
[19, 15, 15, 5, 3, 3, 5, 2]
Output:
False

Input:
[19, 19, 5, 5, 5, 5, 5]
Output:
True

# License: [Link]

# Define a function named 'test' that takes a list 'nums' as input


def test(nums):
# Check if the count of 19 in 'nums' is equal to 2 and the count of 5 is
greater than or equal to 3
return [Link](19) == 2 and [Link](5) >= 3

# Create a list 'nums' with specific elements


nums = [19, 19, 15, 5, 3, 5, 5, 2]

# Print the original list


print("Original list:")
print(nums)

# Print the result of the test function applied to the 'nums' list
print("Check two occurrences of nineteen and at least three occurrences of five in
the said list:")
print(test(nums))

# Create a different list 'nums' with specific elements


nums = [19, 15, 15, 5, 3, 3, 5, 2]

# Print the original list


print("\nOriginal list:")
print(nums)

# Print the result of the test function applied to the modified 'nums' list
print("Check two occurrences of nineteen and at least three occurrences of five in
the said list:")
print(test(nums))

# Create another list 'nums' with specific elements


nums = [19, 19, 5, 5, 5, 5, 5]

# Print the original list


print("\nOriginal list:")
print(nums)

# Print the result of the test function applied to the modified 'nums' list
print("Check two occurrences of nineteen and at least three occurrences of five in
the said list:")
print(test(nums))

Original list:
[19, 19, 15, 5, 3, 5, 5, 2]
Check two occurrences of nineteen and at least three occurrences of five in the
said list:
True

Original list:
[19, 15, 15, 5, 3, 3, 5, 2]
Check two occurrences of nineteen and at least three occurrences of five in the
said list:
False

Original list:
[19, 19, 5, 5, 5, 5, 5]
Check two occurrences of nineteen and at least three occurrences of five in the
said list:
True

===================================================================================
=================

Puzzle 2
Question: Last update on May 30 2025 11:48:29 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:29 (UTC/GMT +8 hours)

Fifth Element and List Length Check

Write a Python program that accepts a list of integers and calculates the length
and the fifth element. Return true if the length of the list is 8 and the fifth
element occurs thrice in the said list.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:List of integers with exactly two occurrences of nineteen and at least


three occurrences of [Link]:Whether an integer greater than 4^4 which is 4 mod
34.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?


Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[19, 19, 15, 5, 5, 5, 1, 2]
Output:
True

Input:
[19, 15, 5, 7, 5, 5, 2]
Output:
False

Input:
[11, 12, 14, 13, 14, 13, 15, 14]
Output:
True

Input:
[19, 15, 11, 7, 5, 6, 2]
Output:
False

# License: [Link]

# Define a function named 'test' that takes a list 'nums' as input


def test(nums):
# Check if the length of 'nums' is 8 and the count of the fifth element in
'nums' is equal to 3
return len(nums) == 8 and [Link](nums[4]) == 3

# Create a list 'nums' with specific elements


nums = [19, 19, 15, 5, 5, 5, 1, 2]

# Print the original list


print("Original list:")
print(nums)

# Print the result of the test function applied to the 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs
thrice in the said list. :")
print(test(nums))

# Create a different list 'nums' with specific elements


nums = [19, 15, 5, 7, 5, 5, 2]

# Print the original list


print("\nOriginal list:")
print(nums)

# Print the result of the test function applied to the modified 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs
thrice in the said list. :")
print(test(nums))
# Create another list 'nums' with specific elements
nums = [11, 12, 14, 13, 14, 13, 15, 14]

# Print the original list


print("\nOriginal list:")
print(nums)

# Print the result of the test function applied to the modified 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs
thrice in the said list. :")
print(test(nums))

# Create one more list 'nums' with specific elements


nums = [19, 15, 11, 7, 5, 6, 2]

# Print the original list


print("\nOriginal list:")
print(nums)

# Print the result of the test function applied to the modified 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs
thrice in the said list. :")
print(test(nums))

Original list:
[19, 19, 15, 5, 5, 5, 1, 2]
Check whether the length of the said list is 8 and fifth element occurs thrice in
the said list. :
True

Original list:
[19, 15, 5, 7, 5, 5, 2]
Check whether the length of the said list is 8 and fifth element occurs thrice in
the said list. :
False

Original list:
[11, 12, 14, 13, 14, 13, 15, 14]
Check whether the length of the said list is 8 and fifth element occurs thrice in
the said list. :
True

Original list:
[19, 15, 11, 7, 5, 6, 2]
Check whether the length of the said list is 8 and fifth element occurs thrice in
the said list. :
False

===================================================================================
=================

Puzzle 3
Question: Last update on May 30 2025 11:48:30 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:30 (UTC/GMT +8 hours)

Integer Greater Than 444^444 and Mod 34


Write a Python program that accepts an integer and determines whether it is greater
than 4^4 and which is 4 mod 34.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Check the length and the fifth element occurs twice in a [Link]:Find
the number of stones in each pile.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
922
Output:
True

Input:
914
Output:
False

Input:
854
Output:
True

Input:
854
Output:
True
# License: [Link]

# Define a function named 'test' that takes an integer 'n' as input


def test(n):
# Check if 'n' is congruent to 4 modulo 34 and greater than 4^4
return n % 34 == 4 and n > 4 ** 4

# Assign a specific integer 'n' to the variable


n = 922

# Print the original integer


print("Original Integer:")
print(n)

# Print the result of the test function applied to the integer 'n'
print("Check whether the said integer greater than 4^4 and which is 7 mod 134 :")
print(test(n))

# Assign a different integer 'n' to the variable


n = 914

# Print the original integer


print("\nOriginal Integer:")
print(n)

# Print the result of the test function applied to the modified integer 'n'
print("Check whether the said integer greater than 4^4 and which is 7 mod 134 :")
print(test(n))

# Assign another integer 'n' to the variable


n = 854

# Print the original integer


print("\nOriginal Integer:")
print(n)

# Print the result of the test function applied to the modified integer 'n'
print("Check whether the said integer greater than 4^4 and which is 7 mod 134 :")
print(test(n))

# Print the original integer again (note: the variable 'n' retains its previous
value)
print("\nOriginal Integer:")
print(n)

# Print the result of the test function applied to the integer 'n' (no modification
to 'n' since the previous assignment)
print("Check whether the said integer greater than 4^4 and which is 7 mod 134 :")
print(test(n))

Original Integer:
922
Check whether the said integer greater than 4^4 and which is 7 mod 134 :
True
Original Integer:
914
Check whether the said integer greater than 4^4 and which is 7 mod 134 :
False

Original Integer:
854
Check whether the said integer greater than 4^4 and which is 7 mod 134 :
True

Original Integer:
854
Check whether the said integer greater than 4^4 and which is 7 mod 134 :
True

===================================================================================
=================

Puzzle 4
Question: Last update on May 30 2025 11:48:30 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:30 (UTC/GMT +8 hours)

Stone Piles Distribution

We are making n stone piles! The first pile has n stones. If n is even, then all
piles have an even number of stones. If n is odd, all piles have an odd number of
stones. Each pile must more stones than the previous pile but as few as possible.
Write a Python program to find the number of stones in each pile.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Whether an integer greater than 4^4 which is 4 mod [Link]:Check the nth-
1string is a proper substring of nthstring of a given list of strings.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: 2
Output:
[2, 4]

Input: 10
Output:
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

Input: 3
Output:
[3, 5, 7]

Input: 17
Output:
[17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]

# License: [Link]

# Define a function named 'test' that takes an integer 'n' as input


def test(n):
# Use a list comprehension to generate a list of values: n + 2 * i for i in the
range from 0 to n-1
return [n + 2 * i for i in range(n)]

# Assign a specific integer 'n' to the variable


n = 2

# Print the number of piles


print("Number of piles:", n)

# Print the header for the output


print("Number of stones in each pile:")

# Print the result of the test function applied to the integer 'n'
print(test(n))

# Assign a different integer 'n' to the variable


n = 10

# Print the number of piles


print("\nNumber of piles:", n)

# Print the header for the output


print("Number of stones in each pile:")

# Print the result of the test function applied to the modified integer 'n'
print(test(n))
# Assign another integer 'n' to the variable
n = 3

# Print the number of piles


print("\nNumber of piles:", n)

# Print the header for the output


print("Number of stones in each pile:")

# Print the result of the test function applied to the modified integer 'n'
print(test(n))

# Assign yet another integer 'n' to the variable


n = 17

# Print the number of piles


print("\nNumber of piles:", n)

# Print the header for the output


print("Number of stones in each pile:")

# Print the result of the test function applied to the modified integer 'n'
print(test(n))

Number of piles: 2
Number of stones in each pile:
[2, 4]

Number of piles: 10
Number of stones in each pile:
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

Number of piles: 3
Number of stones in each pile:
[3, 5, 7]

Number of piles: 17
Number of stones in each pile:
[17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39

===================================================================================
=================

Puzzle 5
Question: Last update on May 30 2025 11:48:32 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:32 (UTC/GMT +8 hours)

Substring in String List Check

Write a Python program to check the nth-1string is a proper substring of the


nthstring in a given list of strings.

Visual Presentation:
Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the number of stones in each [Link]:Find a list of one hundred


integers between 0 and 999 which all differ by ten from one another.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['a', 'abb', 'sfs', 'oo', 'de', 'sfde']
Output:
True

Input:
['a', 'abb', 'sfs', 'oo', 'ee', 'sfde']
Output:
False

Input:
['a', 'abb', 'sad', 'ooaaesdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwrew']
Output:
False

Input:
['a', 'abb', 'sad', 'ooaaesdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwsfsdfrew']
Output:
True

# License: [Link]

# Define a function named 'test' that takes a list of strings 'str1' as input
def test(str1):
# Check if the second-to-last character of the last string in 'str1' is a
proper substring of the last string
# and if the second-to-last character is different from the last character
return str1[len(str1) - 2] in str1[len(str1) - 1] and str1[len(str1) - 2] !=
str1[len(str1) - 1]

# Create a list of strings 'str11' with specific elements


str11 = ["a", "abb", "sfs", "oo", "de", "sfde"]

# Print the original list


print("Original list:")
print(str11)

# Print the result of the test function applied to the 'str11' list
print("Check the nth-1 string is a proper substring of nth string of the said list
of strings:")
print(test(str11))

# Create a different list of strings 'str11' with specific elements


str11 = ["a", "abb", "sfs", "oo", "ee", "sfde"]

# Print the original list


print("\nOriginal list:")
print(str11)

# Print the result of the test function applied to the modified 'str11' list
print("Check the nth-1 string is a proper substring of nth string of the said list
of strings:")
print(test(str11))

# Create another list of strings 'str11' with specific elements


str11 = ["a", "abb", "sad", "ooaa", "esdfe", "sfsdfde", "sfsd", "sfsdf", "qwrew"]

# Print the original list


print("\nOriginal list:")
print(str11)

# Print the result of the test function applied to the modified 'str11' list
print("Check the nth-1 string is a proper substring of nth string of the said list
of strings:")
print(test(str11))

# Create one more list of strings 'str11' with specific elements


str11 = ["a", "abb", "sad", "ooaa", "esdfe", "sfsdfde", "sfsd", "sfsdf",
"qwsfsdfrew"]

# Print the original list


print("\nOriginal list:")
print(str11)

# Print the result of the test function applied to the modified 'str11' list
print("Check the nth-1 string is a proper substring of nth string of the said list
of strings:")
print(test(str11))

Original list:
['a', 'abb', 'sfs', 'oo', 'de', 'sfde']
Check the nth-1 string is a proper substring of nth string of the said list of
strings:
True

Original list:
['a', 'abb', 'sfs', 'oo', 'ee', 'sfde']
Check the nth-1 string is a proper substring of nth string of the said list of
strings:
False

Original list:
['a', 'abb', 'sad', 'ooaaesdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwrew']
Check the nth-1 string is a proper substring of nth string of the said list of
strings:
False

Original list:
['a', 'abb', 'sad', 'ooaaesdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwsfsdfrew']
Check the nth-1 string is a proper substring of nth string of the said list of
strings:
True

===================================================================================
=================

Puzzle 6
Question: Last update on May 30 2025 11:48:32 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:32 (UTC/GMT +8 hours)

List Integers Differ by Ten

Write a Python program to test a list of one hundred integers between 0 and 999,
which all differ by ten from one another. Return True otherwise False.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Check the nth-1string is a proper substring of nthstring of a given list


of [Link]:List of integers where the sum of the first i integers is i.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170,
180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330,
340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490,
500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650,
660, 670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790, 800, 810,
820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 950, 960, 970,
980, 990]
Output:
True
Input:
[0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320,
340, 360, 380, 400, 420, 440, 460, 480, 500, 520, 540, 560, 580, 600, 620, 640,
660, 680, 700, 720, 740, 760, 780, 800, 820, 840, 860, 880, 900, 920, 940, 960,
980]
Output:
False

# License: [Link]

# Define a function named 'test' that takes a list 'li' as input


def test(li):
# Check if all elements in 'li' are within the range [0, 999] and have a
minimum absolute difference of 10
# Also, ensure that all elements are distinct and there are exactly 100 unique
elements in 'li'
return all(i in range(1000) and abs(i - j) >= 10 for i in li for j in li if i !
= j) and len(set(li)) == 100

# Create a list 'nums' containing one hundred integers from 0 to 999 with a
difference of 10 between each pair
nums = list(range(0, 1000, 10))

# Print the original list


print("Original list:")
print(nums)

# Print the result of the test function applied to the 'nums' list
print("Check whether the said list contains one hundred integers between 0 and 999
which all differ by ten from one another:")
print(test(nums))

# Create a different list 'nums' containing one hundred integers from 0 to 999 with
a difference of 20 between each pair
nums = list(range(0, 1000, 20))

# Print the original list


print("\nOriginal list:")
print(nums)

# Print the result of the test function applied to the modified 'nums' list
print("Check whether the said list contains one hundred integers between 0 and 999
which all differ by ten from one another:")
print(test(nums))

Original list:
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170,
180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330,
340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490,
500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650,
660, 670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790, 800, 810,
820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 950, 960, 970,
980, 990]
Check whether the said list contains one hundred integers between 0 and 999 which
all differ by ten from one another:
True
Original list:
[0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320,
340, 360, 380, 400, 420, 440, 460, 480, 500, 520, 540, 560, 580, 600, 620, 640,
660, 680, 700, 720, 740, 760, 780, 800, 820, 840, 860, 880, 900, 920, 940, 960,
980]
Check whether the said list contains one hundred integers between 0 and 999 which
all differ by ten from one another:
False

===================================================================================
=================

Puzzle 7
Question: Last update on May 30 2025 11:48:33 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:33 (UTC/GMT +8 hours)

Sum of First i Equals i

Write a Python program to check a given list of integers where the sum of the first
i integers is i.

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find a list of one hundred integers between 0 and 999 which all differ by
ten from one [Link]:Split a string of words separated by commas and spaces
into 2 lists: words and separators.
Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[0, 1, 2, 3, 4, 5]
Output:
False

Input:
[1, 1, 1, 1, 1, 1]
Output:
True

Input:
[2, 2, 2, 2, 2]
Output:
False

# Define a function named 'test' that takes a list 'li' and an integer 'i' as input
def test(li, i):
# Check if the sum of the first 'i' integers in 'li' equals 'i'
return sum(li[:i]) == i

# Create a list 'nums' with specific elements


nums = [0, 1, 2, 3, 4, 5]

# Assign an integer 'i' to the variable


i = 1

# Print the original list


print("Original list:")
print(nums)

# Print a message indicating the current value of 'i'


print("Check the said list, where the sum of the first i integers is i: i =", i)

# Print the result of the test function applied to the 'nums' list with the current
value of 'i'
print(test(nums, 1))

# Update the value of 'i'


i = 3

# Print a message indicating the updated value of 'i'


print("\nOriginal list:")
print(nums)
# Print the result of the test function applied to the 'nums' list with the updated
value of 'i'
print("Check the said list, where the sum of the first i integers is i: i =", i)
print(test(nums, 3))

# Update the value of 'i' and 'nums'


i = 6
nums = [1, 1, 1, 1, 1, 1]

# Print a message indicating the updated value of 'i'


print("\nOriginal list:")
print(nums)

# Print the result of the test function applied to the updated 'nums' list with the
updated value of 'i'
print("Check the said list, where the sum of the first i integers is i: i =", i)
print(test(nums, 6))

# Update the value of 'i' and 'nums'


i = 2
nums = [2, 2, 2, 2, 2]

# Print a message indicating the updated value of 'i'


print("\nOriginal list:")
print(nums)

# Print the result of the test function applied to the updated 'nums' list with the
updated value of 'i'
print("Check the said list, where the sum of the first i integers is i: i =", i)
print(test(nums, 2))

Original list:
[0, 1, 2, 3, 4, 5]
Check the said list, where the sum of the first i integers is i: i = 1
False

Original list:
[0, 1, 2, 3, 4, 5]
Check the said list, where the sum of the first i integers is i: i = 3
True

Original list:
[1, 1, 1, 1, 1, 1]
Check the said list, where the sum of the first i integers is i: i = 6
True

Original list:
[2, 2, 2, 2, 2]
Check the said list, where the sum of the first i integers is i: i = 2
False

===================================================================================
=================

Puzzle 8
Question: Last update on May 30 2025 11:48:33 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:33 (UTC/GMT +8 hours)

Split String into Words and Separators

Write a Python program to split a string of words separated by commas and spaces
into two lists, words and separators.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:List of integers where the sum of the first i integers is [Link]:List


integers containing exactly three distinct values, such that no integer repeats
twice consecutively.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
W3resource Python, Exercises.
Output:
[['W3resource', 'Python', 'Exercises.'], [' ', ', ']]

Input:
The dance, held in the school gym, ended at midnight.
Output:
[['The', 'dance', 'held', 'in', 'the', 'school', 'gym', 'ended', 'at',
'midnight.'], [' ', ', ', ' ', ' ', ' ', ' ', ', ', ' ', ' ']]

Input:
The colors in my studyroom are blue, green, and yellow.
Output:
[['The', 'colors', 'in', 'my', 'studyroom', 'are', 'blue', 'green', 'and',
'yellow.'], [' ', ' ', ' ', ' ', ' ', ' ', ', ', ', ', ' ']]
# License: [Link]

# Define a function named 'test' that takes a string 'string' as input


def test(string):
# Import the 're' module for regular expressions
import re

# Use regular expression to split the string into words and separators and
store in the 'merged' list
merged = [Link](r"([ ,]+)", string)

# Return a list containing two sublists: words (even indices) and separators
(odd indices) from the 'merged' list
return [merged[::2], merged[1::2]]

# Assign a specific string 's' to the variable


s = "W3resource Python, Exercises."

# Print the original string


print("Original string:", s)

# Print a message indicating the operation to be performed on the string


print("Split the said string into 2 lists: words and separators:")

# Print the result of the test function applied to the string 's'
print(test(s))

# Assign a different string 's' to the variable


s = "The dance, held in the school gym, ended at midnight."

# Print the original string


print("\nOriginal string:", s)

# Print a message indicating the operation to be performed on the string


print("Split the said string into 2 lists: words and separators:")

# Print the result of the test function applied to the modified string 's'
print(test(s))

# Assign another string 's' to the variable


s = "The colors in my studyroom are blue, green, and yellow."

# Print the original string


print("\nOriginal string:", s)

# Print a message indicating the operation to be performed on the string


print("Split the said string into 2 lists: words and separators:")

# Print the result of the test function applied to the modified string 's'
print(test(s))

Original string: W3resource Python, Exercises.


Split the said string into 2 lists: words and separators:
[['W3resource', 'Python', 'Exercises.'], [' ', ', ']]
Original string: The dance, held in the school gym, ended at midnight.
Split the said string into 2 lists: words and separators:
[['The', 'dance', 'held', 'in', 'the', 'school', 'gym', 'ended', 'at',
'midnight.'], [' ', ', ', ' ', ' ', ' ', ' ', ', ', ' ', ' ']]

Original string: The colors in my studyroom are blue, green, and yellow.
Split the said string into 2 lists: words and separators:
[['The', 'colors', 'in', 'my', 'studyroom', 'are', 'blue', 'green', 'and',
'yellow.'], [' ', ' ', ' ', ' ', ' ', ' ', ', ', ', ', ' ']]

===================================================================================
=================

Puzzle 9
Question: Last update on May 30 2025 11:48:34 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:34 (UTC/GMT +8 hours)

Four Distinct Values Non-Consecutive

Write a Python program to find a list of integers containing exactly four distinct
values, such that no integer repeats twice consecutively among the first twenty
entries.

Note: The list needs to have length greater than ten.)

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Split a string of words separated by commas and spaces into 2 lists: words
and [Link]:Separate Parentheses Groups Perfectly.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Output:
True

Input:
[1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3]
Output:
False

Input:
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Output:
False

# License: [Link]

# Define a function named 'test' that takes a list of integers 'nums' as input
def test(nums):
# Check if no integer in 'nums' repeats consecutively and if there are exactly
four distinct values in 'nums'
return all([nums[i] != nums[i + 1] for i in range(len(nums) - 1)]) and
len(set(nums)) == 4

# Create a list of integers 'nums' with specific elements


nums = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]

# Print the original list


print("Original list:")
print(nums)

# Print a message indicating the condition being checked on the list


print("Check said list of integers containing exactly four distinct values, such
that no integer repeats twice consecutively:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Create a different list of integers 'nums' with specific elements


nums = [1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3]

# Print the original list


print("\nOriginal list:")
print(nums)

# Print a message indicating the condition being checked on the list


print("Check said list of integers containing exactly four distinct values, such
that no integer repeats twice consecutively:")

# Print the result of the test function applied to the modified 'nums' list
print(test(nums))
# Create another list of integers 'nums' with specific elements
nums = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

# Print the original list


print("\nOriginal list:")
print(nums)

# Print a message indicating the condition being checked on the list


print("Check said list of integers containing exactly four distinct values, such
that no integer repeats twice consecutively:")

# Print the result of the test function applied to the modified 'nums' list
print(test(nums))

Original list:
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Check said list of integers containing exactly four distinct values, such that no
integer repeats twice consecutively:
True

Original list:
[1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3]
Check said list of integers containing exactly four distinct values, such that no
integer repeats twice consecutively:
False

Original list:
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Check said list of integers containing exactly four distinct values, such that no
integer repeats twice consecutively:
False

===================================================================================
=================

Puzzle 10
Question: Last update on May 30 2025 11:48:34 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:34 (UTC/GMT +8 hours)

Split Matched Parentheses Groups

Given a string consisting of whitespace and groups of matched parentheses, write a


Python program to split it into groups of perfectly matched parentheses without any
whitespace.

Visual Presentation:

Sample Solution:

Python Code:
Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:List integers containing exactly three distinct values, such that no


integer repeats twice [Link]:Find the indexes of numbers, below a given
threshold.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
( ()) ((()()())) (()) ()
Output:
['(())', '((()()()))', '(())', '()']

Input:
() (( ( )() ( )) ) ( ())
Output:
['()', '((()()()))', '(())']

# License: [Link]

# Define a function named 'test' that takes a string 'combined' as input


def test(combined):
# Initialize an empty list 'ls' to store separate parentheses groups
ls = []

# Initialize an empty string 's2' to build individual parentheses groups


s2 = ""

# Iterate through each character in the modified 'combined' string (spaces


removed)
for s in [Link](' ', ''):
# Concatenate the character to 's2'
s2 += s

# Check if the count of opening parentheses '(' equals the count of closing
parentheses ')'
if [Link]("(") == [Link](")"):
# Append the built parentheses group to the 'ls' list
[Link](s2)

# Reset 's2' to an empty string for the next parentheses group


s2 = ""

# Return the list of separate parentheses groups


return ls

# Assign a specific string 'combined' to the variable


combined = '( ()) ((()()())) (()) ()'

# Print the original parentheses string


print("Parentheses string:")
print(combined)

# Print a message indicating the operation to be performed on the string


print("Separate parentheses groups of the said string:")

# Print the result of the test function applied to the 'combined' string
print(test(combined))

# Assign a different string 'combined' to the variable


combined = '() (( ( )() ( )) ) ( ())'

# Print the original parentheses string


print("\nParentheses string:")
print(combined)

# Print a message indicating the operation to be performed on the string


print("Separate parentheses groups of the said string:")

# Print the result of the test function applied to the modified 'combined' string
print(test(combined))

Parentheses string:
( ()) ((()()())) (()) ()
Separate parentheses groups of the said string:
['(())', '((()()()))', '(())', '()']

Parentheses string:
() (( ( )() ( )) ) ( ())
Separate parentheses groups of the said string:
['()', '((()()()))', '(())']

===================================================================================
=================

Puzzle 11
Question: Last update on May 30 2025 11:48:35 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:35 (UTC/GMT +8 hours)

Indices Below Threshold

Write a Python program to find the indexes of numbers in a given list below a given
threshold.
Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Separate Parentheses Groups [Link]:Test whether the given strings


are palindromes.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[(100,(0, 12, 45, 3, 4923, 322, 105, 29, 15, 39, 55))]
Output:
[0, 1, 2, 3, 7, 8, 9, 10]

Input:
[(10,(0, 12, 4, 3, 49, 9, 1, 5, 3))]
Output:
[0, 2, 3, 5, 6, 7, 8]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' and a
threshold 'thresh' as input
def test(nums, thresh):
# Use a list comprehension to find the indexes (i) of numbers in 'nums' that
are below the given threshold 'thresh'
return [i for i, n in enumerate(nums) if n < thresh]

# Create a list of numbers 'nums' with specific elements


nums = [0, 12, 45, 3, 4923, 322, 105, 29, 15, 39, 55]

# Assign a threshold value 'thresh' to the variable


thresh = 100

# Print the original list of numbers


print("Original list:")
print(nums)

# Print the threshold value


print("Threshold: ", thresh)

# Print a message indicating the operation to be performed on the list


print("Check the indexes of numbers of the said list below the given threshold:")

# Print the result of the test function applied to the 'nums' list with the given
threshold value
print(test(nums, thresh))

# Create a different list of numbers 'nums' with specific elements


nums = [0, 12, 4, 3, 49, 9, 1, 5, 3]

# Assign a different threshold value 'thresh' to the variable


thresh = 10

# Print the original list of numbers


print("\nOriginal list:")
print(nums)

# Print the updated threshold value


print("Threshold: ", thresh)

# Print a message indicating the operation to be performed on the list


print("Check the indexes of numbers of the said list below the given threshold:")

# Print the result of the test function applied to the modified 'nums' list with
the updated threshold value
print(test(nums, thresh))

Original list:
[0, 12, 45, 3, 4923, 322, 105, 29, 15, 39, 55]
Threshold: 100
Check the indexes of numbers of the said list below the given threshold:
[0, 1, 2, 3, 7, 8, 9, 10]

Original list:
[0, 12, 4, 3, 49, 9, 1, 5, 3]
Threshold: 10
Check the indexes of numbers of the said list below the given threshold:
[0, 2, 3, 5, 6, 7, 8]

===================================================================================
=================

Puzzle 12
Question: Last update on May 30 2025 11:48:35 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:35 (UTC/GMT +8 hours)

Check Palindromes in List


Write a Python program to check whether the given strings are palindromes or not.
Return True otherwise False.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the indexes of numbers, below a given [Link]:Find the strings


in a list, starting with a given prefix.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['palindrome', 'madamimadam', '', 'foo', 'eyes']

Output:
[False, True, True, False, False]

# License: [Link]

# Define a function named 'test' that takes a list of strings 'strs' as input
def test(strs):
# Use a list comprehension to check if each string in 'strs' is a palindrome
(reads the same forwards and backwards)
return [s == s[::-1] for s in strs]

# Create a list of strings 'strs' with specific elements


strs = ['palindrome', 'madamimadam', '', 'foo', 'eyes']

# Print the original list of strings


print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed on the list


print("\nTest whether the given strings are palindromes or not:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

Original strings:
['palindrome', 'madamimadam', '', 'foo', 'eyes']

Test whether the given strings are palindromes or not:


[False, True, True, False, False]

# Define a function named 'test' that takes a list of strings 'strs' as input
def test(strs):
# Initialize an empty list 'results' to store the results of palindrome tests
results = []

# Iterate through each string in 'strs'


for _ in strs:
# Padding each string with spaces on both sides
# By default, the pad_to parameter is 50
s = ' ' * 50 + _ + ' ' * 50

# Initialize variables 'i' and 'j' for indexing the string 's'
i = 0
j = len(s) - 1

# Use a while loop to check if the string 's' is a palindrome


while i < j:
if s[i] != s[j]:
# If characters at positions 'i' and 'j' are not equal, append
False to 'results' and break out of the loop
[Link](False)
break
else:
# Increment 'i' and decrement 'j' to compare the next pair of
characters
i += 1
j -= 1
else:
# If the while loop completes without a break, append True to 'results'
[Link](True)
# Return the list of results
return results

# Create a list of strings 'strs' with specific elements


strs = ['palindrome', 'madamimadam', '', 'foo', 'eyes']

# Print the original list of strings


print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed on the list


print("\nTest whether the given strings are palindromes or not:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

Original strings:
['palindrome', 'madamimadam', '', 'foo', 'eyes']

Test whether the given strings are palindromes or not:


[False, True, True, False, False]

===================================================================================
=================

Puzzle 13
Question: Last update on May 30 2025 11:48:36 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:36 (UTC/GMT +8 hours)

Find Strings with Prefix

Write a Python program to find strings in a given list starting with a given
prefix.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Test whether the given strings are [Link]:Find the lengths of a


list of non-empty strings.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[( ca,('cat', 'car', 'fear', 'center'))]
Output:
['cat', 'car']

Input:
[(do,('cat', 'dog', 'shatter', 'donut', 'at', 'todo'))]
Output:
['dog', 'donut']

# License: [Link]

# Define a function named 'test' that takes a list of strings 'strs' and a prefix
'prefix' as input
def test(strs, prefix):
# Use a list comprehension to filter strings in 'strs' that start with the
given 'prefix'
return [s for s in strs if [Link](prefix)]

# Create a list of strings 'strs' with specific elements


strs = ['cat', 'car', 'fear', 'center']

# Assign a specific prefix 'prefix' to the variable


prefix = "ca"

# Print the original list of strings


print("Original strings:")
print(strs)

# Print the starting prefix


print("Starting prefix:", prefix)

# Print a message indicating the operation to be performed on the list


print("Strings in the said list starting with a given prefix:")

# Print the result of the test function applied to the 'strs' list with the given
prefix
print(test(strs, prefix))

# Create a different list of strings 'strs' with specific elements


strs = ['cat', 'dog', 'shatter', 'donut', 'at', 'todo']

# Assign a different prefix 'prefix' to the variable


prefix = "do"
# Print the original list of strings
print("\nOriginal strings:")
print(strs)

# Print the updated starting prefix


print("Starting prefix:", prefix)

# Print a message indicating the operation to be performed on the list


print("Strings in the said list starting with a given prefix:")

# Print the result of the test function applied to the modified 'strs' list with
the updated prefix
print(test(strs, prefix))

Original strings:
['cat', 'car', 'fear', 'center']
Starting prefix: ca
Strings in the said list starting with a given prefix:
['cat', 'car']

Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo']
Starting prefix: do
Strings in the said list starting with a given prefix:
['dog', 'donut']

===================================================================================
=================

Puzzle 14
Question: Last update on May 30 2025 11:48:36 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:36 (UTC/GMT +8 hours)

Length of Strings in List

Write a Python program to find the length of a given list of non-empty strings.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:
Previous:Find the strings in a list, starting with a given [Link]:Find the
longest string of a list of strings.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['cat', 'car', 'fear', 'center']
Output:
[3, 3, 4, 6]

Input:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Output:
[3, 3, 7, 5, 2, 4, 0]

# License: [Link]

# Define a function named 'test' that takes a list of strings 'strs' as input
def test(strs):
# Use the map function to apply the len function to each string in 'strs', and
convert the result to a list
return [*map(len, strs)]

# Create a list of strings 'strs' with specific elements


strs = ['cat', 'car', 'fear', 'center']

# Print the original list of strings


print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed on the list


print("Lengths of the said list of non-empty strings:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

# Create a different list of strings 'strs' with specific elements


strs = ['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']

# Print the original list of strings


print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed on the list


print("Lengths of the said list of non-empty strings:")
# Print the result of the test function applied to the modified 'strs' list
print(test(strs))

Original strings:
['cat', 'car', 'fear', 'center']
Lengths of the said list of non-empty strings:
[3, 3, 4, 6]

Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Lengths of the said list of non-empty strings:
[3, 3, 7, 5, 2, 4, 0]

===================================================================================
=================

Puzzle 15
Question: Last update on May 30 2025 11:48:37 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:37 (UTC/GMT +8 hours)

Longest String in List

Write a Python program to find the longest string in a given list of strings.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the lengths of a list of non-empty [Link]:Find the strings in a


list containing a given substring.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['cat', 'car', 'fear', 'center']
Output:
center

Input:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Output:
shatter

# Define a function named 'test' that takes a list of strings 'words' as input
def test(words):
# Use the max function to find the string with the maximum length in 'words'
based on the key=len (length of each string)
return max(words, key=len)

# Create a list of strings 'strs' with specific elements


strs = ['cat', 'car', 'fear', 'center']

# Print the original list of strings


print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed on the list


print("Longest string of the said list of strings:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

# Create a different list of strings 'strs' with specific elements


strs = ['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']

# Print the original list of strings


print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed on the list


print("Longest string of the said list of strings:")

# Print the result of the test function applied to the modified 'strs' list
print(test(strs))

Original strings:
['cat', 'car', 'fear', 'center']
Longest string of the said list of strings:
center

Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Longest string of the said list of strings:
shatter

# License: [Link]

# Define a function named 'test' that takes a list of strings 'words' as input
def test(words):
# Define the lowercase and uppercase alphabet characters
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabet = alphabet + [Link]()

# Initialize an empty dictionary 'alphabet_dict' to store True for each


alphabet character
alphabet_dict = {}

# Populate the 'alphabet_dict' with True for each alphabet character


for k in alphabet:
alphabet_dict[k] = True

# Create a set 'alphabet_set' containing all alphabet characters


alphabet_set = set(alphabet)

# Initialize 'max_word' with the first word in the list


max_word = words[0]

# Iterate through each element 'el' in the list 'words'


for el in words:
# Check if 'el' contains only alphabet characters and is a subset of
'alphabet_set'
# Also, check if it has the same set of characters as its intersection with
'alphabet_dict.keys()'
if not ((set(el) <= alphabet_set) and (set(el) ==
set(el).intersection(alphabet_dict.keys()))):
continue

# Check if the length of 'el' is greater than or equal to the length of


'max_word'
if len(el) >= len(max_word):
# Update 'max_word' if the length condition is met
max_word = el

# Return the longest valid word


return max_word

# Create a list of strings 'strs' with specific elements


strs = ['cat', 'car', 'fear', 'center']

# Print the original list of strings


print("Original strings:")
print(strs)
# Print a message indicating the operation to be performed on the list
print("Longest string of the said list of strings:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

# Create a different list of strings 'strs' with specific elements


strs = ['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']

# Print the original list of strings


print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed on the list


print("Longest string of the said list of strings:")

# Print the result of the test function applied to the modified 'strs' list
print(test(strs))

Original strings:
['cat', 'car', 'fear', 'center']
Longest string of the said list of strings:
center

Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Longest string of the said list of strings:
shatter

===================================================================================
=================

Puzzle 16
Question: Last update on May 30 2025 11:48:37 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:37 (UTC/GMT +8 hours)

Find Strings with Substring

Write a Python program to find strings in a given list containing a given


substring.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:
For more Practice: Solve these Related Problems:

Go to:

Previous:Find the longest string of a list of [Link]:Find a string consisting


of the non-negative integers up to n inclusive.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[(ca,('cat', 'car', 'fear', 'center'))]
Output:
['cat', 'car']

Input:
[(o,('cat', 'dog', 'shatter', 'donut', 'at', 'todo', ''))]
Output:
['dog', 'donut', 'todo']

Input:
[(oe,('cat', 'dog', 'shatter', 'donut', 'at', 'todo', ''))]
Output:
[]

# Define a function named 'test' that takes a list of strings 'strs' and a
substring 'substr' as input
def test(strs, substr):
# Use a list comprehension to filter strings in 'strs' that contain the given
'substr'
return [s for s in strs if substr in s]

# Create a list of strings 'strs' with specific elements


strs = ['cat', 'car', 'fear', 'center']

# Print the original list of strings


print("Original strings:")
print(strs)

# Assign a specific substring 'substrs' to the variable


substrs = 'ca'

# Print the substring


print("Substring: " + substrs)

# Print a message indicating the operation to be performed on the list


print("Strings in the said list containing a given substring:")

# Print the result of the test function applied to the 'strs' list with the given
substring
print(test(strs, substrs))

# Create a different list of strings 'strs' with specific elements


strs = ['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']

# Print the original list of strings


print("\nOriginal strings:")
print(strs)

# Assign a different substring 'substrs' to the variable


substrs = 'o'

# Print the substring


print("Substring: " + substrs)

# Print a message indicating the operation to be performed on the list


print("Strings in the said list containing a given substring:")

# Print the result of the test function applied to the modified 'strs' list with
the updated substring
print(test(strs, substrs))

# Create another list of strings 'strs' with specific elements


strs = ['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']

# Print the original list of strings


print("\nOriginal strings:")
print(strs)

# Assign a different substring 'substrs' to the variable


substrs = 'oe'

# Print the substring


print("Substring: " + substrs)

# Print a message indicating the operation to be performed on the list


print("Strings in the said list containing a given substring:")

# Print the result of the test function applied to the modified 'strs' list with
the updated substring
print(test(strs, substrs))

Original strings:
['cat', 'car', 'fear', 'center']
Substring: ca
Strings in the said list containing a given substring:
['cat', 'car']

Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Substring: o
Strings in the said list containing a given substring:
['dog', 'donut', 'todo']
Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Substring: oe
Strings in the said list containing a given substring:
[]

===================================================================================
=================

Puzzle 17
Question: Last update on May 30 2025 11:48:38 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:38 (UTC/GMT +8 hours)

Create String of Non-Negatives to n

Write a Python program to create a string consisting of non-negative integers up to


n inclusive.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the strings in a list containing a given [Link]:Find the


indices of all occurrences of target in the uneven matrix.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?


Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
4
Output:
0 1 2 3 4

Input:
15
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

# Define a function named 'test' that takes a non-negative integer 'n' as input
def test(n):
# Use the map function to convert each integer in the range from 0 to 'n'
(inclusive) to a string
# Then, use ' '.join to concatenate the strings with a space separator
return ' '.join(map(str, range(n + 1)))

# Assign a specific non-negative integer 'n' to the variable


n = 4

# Print the non-negative integer


print("Non-negative integer:")
print(n)

# Print a message indicating the operation to be performed


print("Non-negative integers up to n inclusive:")

# Print the result of the test function applied to the 'n' value
print(test(n))

# Assign a different non-negative integer 'n' to the variable


n = 15

# Print the non-negative integer


print("\nNon-negative integer:")
print(n)

# Print a message indicating the operation to be performed


print("Non-negative integers up to n inclusive:")

# Print the result of the test function applied to the updated 'n' value
print(test(n))

Non-negative integer:
4
Non-negative integers up to n inclusive:
0 1 2 3 4

Non-negative integer:
15
Non-negative integers up to n inclusive:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

# Define a function named 'test' that takes a non-negative integer 'n' as input
def test(n):
# Use a generator expression to convert each integer in the range from 0 to 'n'
(inclusive) to a string
# Then, use ' '.join to concatenate the strings with a space separator
return ' '.join(str(i) for i in range(n + 1))

# Assign a specific non-negative integer 'n' to the variable


n = 4

# Print the non-negative integer


print("Non-negative integer:")
print(n)

# Print a message indicating the operation to be performed


print("Non-negative integers up to n inclusive:")

# Print the result of the test function applied to the 'n' value
print(test(n))

# Assign a different non-negative integer 'n' to the variable


n = 15

# Print the non-negative integer


print("\nNon-negative integer:")
print(n)

# Print a message indicating the operation to be performed


print("Non-negative integers up to n inclusive:")

# Print the result of the test function applied to the updated 'n' value
print(test(n))

Non-negative integer:
4
Non-negative integers up to n inclusive:
0 1 2 3 4

Non-negative integer:
15
Non-negative integers up to n inclusive:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

===================================================================================
=================

Puzzle 18
Question: Last update on May 30 2025 11:48:38 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:38 (UTC/GMT +8 hours)

Target Indices in Uneven Matrix


An irregular/uneven matrix, or ragged matrix, is a matrix that has a different
number of elements in each row. Ragged matrices are not used in linear algebra,
since standard matrix transformations cannot be performed on them, but they are
useful as arrays in computing.

Write a Python program to find the indices of all occurrences of target in the
uneven matrix.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find a string consisting of the non-negative integers up to n


[Link]:Split a string into strings if there is a space in the string,
otherwise split on commas, otherwise the list of lowercase letters with odd order.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[([1, 3, 2, 32, 19], [19, 2, 48, 19], [], [9, 35, 4], [3, 19]), 19]
Output:
[[0, 4], [1, 0], [1, 3], [4, 1]]

Input:
[([1, 2, 3, 2], [], [7, 9, 2, 1, 4]),2]
Output:
[[0, 1], [0, 3], [2, 2]]

# Define a function named 'test' that takes an uneven matrix 'M' and a target value
'T' as input
def test(M, T):
# Use a nested list comprehension to generate a list of indices [i, j] for all
occurrences of the target value 'T' in matrix 'M'
return [[i, j] for i, row in enumerate(M) for j, n in enumerate(row) if n == T]

# Create an uneven matrix 'M' with specific elements


M = [[1, 3, 2, 32, 19], [19, 2, 48, 19], [], [9, 35, 4], [3, 19]]

# Assign a specific target value 'T' to the variable


T = 19

# Print the matrix 'M'


print("Matrix:")
print(M)

# Print the target value 'T'


print("Target value:")
print(T)

# Print a message indicating the operation to be performed


print("Indices of all occurrences of the target value in the said uneven matrix:")

# Print the result of the test function applied to the 'M' matrix and the 'T'
target value
print(test(M, T))

# Create a different uneven matrix 'M' with specific elements


M = [[1, 2, 3, 2], [], [7, 9, 2, 1, 4]]

# Assign a different target value 'T' to the variable


T = 2

# Print the matrix 'M'


print("\nMatrix:")
print(M)

# Print the target value 'T'


print("Target value:")
print(T)

# Print a message indicating the operation to be performed


print("Indices of all occurrences of the target value in the said uneven matrix:")

# Print the result of the test function applied to the updated 'M' matrix and the
updated 'T' target value
print(test(M, T))
Matrix:
[[1, 3, 2, 32, 19], [19, 2, 48, 19], [], [9, 35, 4], [3, 19]]
Target value:
19
Indices of all occurrences of the target value in the said uneven matrix:
[[0, 4], [1, 0], [1, 3], [4, 1]]

Matrix:
[[1, 2, 3, 2], [], [7, 9, 2, 1, 4]]
Target value:
2
Indices of all occurrences of the target value in the said uneven matrix:
[[0, 1], [0, 3], [2, 2]]

# License: [Link]

# Define a function named 'test' that takes an uneven matrix 'M' and a target value
'T' as input
def test(M, T):
# Initialize an empty list 'indices' to store the indices of the found elements
indices = []

# Search for the target value 'T' in the first row of the matrix 'M'
for i, num in enumerate(M[0]):
if num == T:
# Append the index [0, i] to 'indices' if the target value is found
[Link]([0, i])

# Search for the target value 'T' in the remaining rows of the matrix 'M'
for row, row_num in zip(M[1:], range(1, len(M))):
for i, num in enumerate(row):
if num == T:
# Append the index [row_num, i] to 'indices' if the target value is
found in subsequent rows
[Link]([row_num, i])

# Return the list of indices of all occurrences of the target value in the
uneven matrix 'M'
return indices

# Create an uneven matrix 'M' with specific elements


M = [[1, 3, 2, 32, 19], [19, 2, 48, 19], [], [9, 35, 4], [3, 19]]

# Assign a specific target value 'T' to the variable


T = 19

# Print the matrix 'M'


print("Matrix:")
print(M)

# Print the target value 'T'


print("Target value:")
print(T)

# Print a message indicating the operation to be performed


print("Indices of all occurrences of the target value in the said uneven matrix:")
# Print the result of the test function applied to the 'M' matrix and the 'T'
target value
print(test(M, T))

# Create a different uneven matrix 'M' with specific elements


M = [[1, 2, 3, 2], [], [7, 9, 2, 1, 4]]

# Assign a different target value 'T' to the variable


T = 2

# Print the matrix 'M'


print("\nMatrix:")
print(M)

# Print the target value 'T'


print("Target value:")
print(T)

# Print a message indicating the operation to be performed


print("Indices of all occurrences of the target value in the said uneven matrix:")

# Print the result of the test function applied to the updated 'M' matrix and the
updated 'T' target value
print(test(M, T))

Matrix:
[[1, 3, 2, 32, 19], [19, 2, 48, 19], [], [9, 35, 4], [3, 19]]
Target value:
19
Indices of all occurrences of the target value in the said uneven matrix:
[[0, 4], [1, 0], [1, 3], [4, 1]]

Matrix:
[[1, 2, 3, 2], [], [7, 9, 2, 1, 4]]
Target value:
2
Indices of all occurrences of the target value in the said uneven matrix:
[[0, 1], [0, 3], [2, 2]]

===================================================================================
=================

Puzzle 19
Question: Last update on May 30 2025 11:48:39 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:39 (UTC/GMT +8 hours)

Split String on Space or Comma

Write a Python program to split a given string (s) into strings if there is a space
in s, otherwise split on commas if there is a comma, otherwise return the list of
lowercase letters in odd order (order of a = 0, b = 1, etc.).

Visual Presentation:
Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the indices of all occurrences of target in the uneven


[Link]:Determine the direction ('increasing' or 'decreasing') of monotonic
sequence numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
a b c d
Split the said string into strings if there is a space in the string,
otherwise split on commas if there is a comma,
Output:
['a', 'b', 'c', 'd']

Input:
a,b,c,d
Split the said string into strings if there is a space in the string,
otherwise split on commas if there is a comma,
Output:
['a', 'b', 'c', 'd']

Input:
abcd
Split the said string into strings if there is a space in the string,
otherwise split on commas if there is a comma,
Output:
['b', 'd']
# License: [Link]

# Define a function named 'test' that takes a string 's' as input


def test(s):
# Check if there is a space in the string 's'
if " " in s:
# Split the string into a list of strings using space as the delimiter
return [Link](" ")
# Check if there is a comma in the string 's'
elif "," in s:
# Split the string into a list of strings using comma as the delimiter
return [Link](",")
else:
# Return a list of lowercase letters with odd ASCII values
return [c for c in s if [Link]() and ord(c) % 2 == 0]

# Assign a specific string 'strs' to the variable


strs = "a b c d"

# Print the original string 'strs'


print("Original string:")
print(strs)

# Print a message indicating the operation to be performed


print("Split the said string into strings if there is a space in the string, \
notherwise split on commas if there is a comma, \notherwise return the list of
lowercase letters with odd order:")

# Print the result of the test function applied to the 'strs' string
print(test(strs))

# Assign a different string 'strs' to the variable


strs = "a,b,c,d"

# Print the original string 'strs'


print("\nOriginal string:")
print(strs)

# Print a message indicating the operation to be performed


print("Split the said string into strings if there is a space in the string, \
notherwise split on commas if there is a comma, \notherwise return the list of
lowercase letters with odd order:")

# Print the result of the test function applied to the updated 'strs' string
print(test(strs))

# Assign another different string 'strs' to the variable


strs = "abcd"

# Print the original string 'strs'


print("\nOriginal string:")
print(strs)

# Print a message indicating the operation to be performed


print("Split the said string into strings if there is a space in the string, \
notherwise split on commas if there is a comma, \notherwise return the list of
lowercase letters with odd order:")

# Print the result of the test function applied to the updated 'strs' string
print(test(strs))

Original string:
a b c d
Split the said string into strings if there is a space in the string,
otherwise split on commas if there is a comma,
otherwise return the list of lowercase letters with odd order:
['a', 'b', 'c', 'd']

Original string:
a,b,c,d
Split the said string into strings if there is a space in the string,
otherwise split on commas if there is a comma,
otherwise return the list of lowercase letters with odd order:
['a', 'b', 'c', 'd']

Original string:
abcd
Split the said string into strings if there is a space in the string,
otherwise split on commas if there is a comma,
otherwise return the list of lowercase letters with odd order:
['b', 'd']

===================================================================================
=================

Puzzle 20
Question: Last update on May 30 2025 11:48:39 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:39 (UTC/GMT +8 hours)

Determine Monotonic Direction

Monotonic sequences are sequences, which constantly increase or constantly


[Link] a Python program to determine the direction ('increasing' or
'decreasing') of monotonic sequence numbers.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:


Go to:

Previous:Split a string into strings if there is a space in the string, otherwise


split on commas, otherwise the list of lowercase letters with odd
[Link]:Determine, for each string in a list, whether the last character is an
isolated letter.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 2, 3, 4, 5, 6]
Output:
Increasing.

Input:
[6, 5, 4, 3, 2, 1]
Output:
Decreasing.

Input:
[19, 19, 5, 5, 5, 5, 5]
Output:
Not a monotonic sequence!

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Check if all elements in the list 'nums' are in increasing order
if all(nums[i] < nums[i + 1] for i in range(len(nums) - 1)):
return "Increasing."
# Check if all elements in the list 'nums' are in decreasing order
elif all(nums[i + 1] < nums[i] for i in range(len(nums) - 1)):
return "Decreasing."
else:
return "Not a monotonic sequence!"

# Assign a specific list of numbers 'nums' to the variable


nums = [1, 2, 3, 4, 5, 6]

# Print the original list of numbers 'nums'


print("Original list:")
print(nums)

# Print a message indicating the operation to be performed


print("Check the direction ('increasing' or 'decreasing') of the said list:")
# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign a different list of numbers 'nums' to the variable


nums = [6, 5, 4, 3, 2, 1]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)

# Print a message indicating the operation to be performed


print("Check the direction ('increasing' or 'decreasing') of the said list:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

# Assign another different list of numbers 'nums' to the variable


nums = [19, 19, 5, 5, 5, 5, 5]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)

# Print a message indicating the operation to be performed


print("Check the direction ('increasing' or 'decreasing') of the said list:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

Original list:
[1, 2, 3, 4, 5, 6]
Check the direction ('increasing' or 'decreasing') of the said list:
Increasing.

Original list:
[6, 5, 4, 3, 2, 1]
Check the direction ('increasing' or 'decreasing') of the said list:
Decreasing.

Original list:
[19, 19, 5, 5, 5, 5, 5]
Check the direction ('increasing' or 'decreasing') of the said list:
Not a monotonic sequence!

===================================================================================
=================

Puzzle 21
Question: Last update on May 30 2025 11:48:40 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:40 (UTC/GMT +8 hours)

Isolated Last Character Check

Write a Python program to check, for each string in a given list, whether the last
character is an isolated letter or not. Return True otherwise False.
Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Determine the direction ('increasing' or 'decreasing') of monotonic


sequence [Link]:Compute the sum of the ASCII values of the upper-case
characters in a given string.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['cat', 'car', 'fear', 'center']
Output:
[False, False, False, False]

Input:
['ca t', 'car', 'fea r', 'cente r']
Output:
[True, False, True, True]

# License: [Link]

# Define a function named 'test' that takes a list of strings 'strs' as input
def test(strs):
# List comprehension to check whether the last character of each string is an
isolated letter
return [len([Link](" ")[-1]) == 1 for s in strs]

# Assign a specific list of strings 'strs' to the variable


strs = ['cat', 'car', 'fear', 'center']

# Print the original list of strings 'strs'


print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed


print("Check, for each string in the said list, whether the last character is an
isolated letter:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

# Assign a different list of strings 'strs' to the variable


strs = ['ca t', 'car', 'fea r', 'cente r']

# Print the original list of strings 'strs'


print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed


print("Check, for each string in the said list, whether the last character is an
isolated letter:")

# Print the result of the test function applied to the updated 'strs' list
print(test(strs))

Original strings:
['cat', 'car', 'fear', 'center']
Check, for each string in the said list, whether the last character is an isolated
letter:
[False, False, False, False]

Original strings:
['ca t', 'car', 'fea r', 'cente r']
Check, for each string in the said list, whether the last character is an isolated
letter:
[True, False, True, True]

===================================================================================
=================

Puzzle 22
Question: Last update on May 30 2025 11:48:40 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:40 (UTC/GMT +8 hours)

Sum of ASCII for Uppercase Letters

Write a Python program to compute the sum of the ASCII values of the upper-case
characters in a given string.

Visual Presentation:

Sample Solution-1:
Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

Sample Solution-3:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Determine, for each string in a list, whether the last character is an


isolated [Link]:Find the indices for which the numbers in the list drops.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
PytHon ExerciSEs
Output:
373

Input:
JavaScript
Output:
157
# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Use the 'filter' function to extract uppercase characters and 'map' to get
their ASCII values
# Finally, calculate the sum of ASCII values of uppercase characters
return sum(map(ord, filter([Link], strs)))

# Assign a specific string 'strs' to the variable


strs = "PytHon ExerciSEs"

# Print the original string 'strs'


print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed


print("Sum of the ASCII values of the upper-case characters in the said string:")

# Print the result of the test function applied to the 'strs' string
print(test(strs))

# Assign a different string 'strs' to the variable


strs = "JavaScript"

# Print the original string 'strs'


print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed


print("Sum of the ASCII values of the upper-case characters in the said string:")

# Print the result of the test function applied to the updated 'strs' string
print(test(strs))

Original strings:
PytHon ExerciSEs
Sum of the ASCII values of the upper-case characters in the said string:
373

Original strings:
JavaScript
Sum of the ASCII values of the upper-case characters in the said string:
157

# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Initialize a variable 'tot' to store the total ASCII value of uppercase
characters
tot = 0
# Iterate through each character 'c' in the string 'strs'
for c in strs:
# Check if the character is uppercase using 'isupper()' method
if [Link]():
# If uppercase, add its ASCII value to 'tot'
tot += ord(c)

# Return the total ASCII value of uppercase characters


return tot

# Assign a specific string 'strs' to the variable


strs = "PytHon ExerciSEs"

# Print the original string 'strs'


print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed


print("Sum of the ASCII values of the upper-case characters in the said string:")

# Print the result of the test function applied to the 'strs' string
print(test(strs))

# Assign a different string 'strs' to the variable


strs = "JavaScript"

# Print the original string 'strs'


print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed


print("Sum of the ASCII values of the upper-case characters in the said string:")

# Print the result of the test function applied to the updated 'strs' string
print(test(strs))

# Assign another string 'strs' to the variable


strs = "ARt"

# Print the original string 'strs'


print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed


print("Sum of the ASCII values of the upper-case characters in the said string:")

# Print the result of the test function applied to the updated 'strs' string
print(test(strs))

Original strings:
PytHon ExerciSEs
Sum of the ASCII values of the upper-case characters in the said string:
373

Original strings:
JavaScript
Sum of the ASCII values of the upper-case characters in the said string:
157

Original strings:
ARt
Sum of the ASCII values of the upper-case characters in the said string:
147

# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Initialize a variable 'tot' to store the total ASCII value of uppercase
characters
tot = 0

# Iterate through each character 'c' in the string 'strs'


for c in strs:
# Check if the character is uppercase using 'isupper()' method
if [Link]():
# If uppercase, add its ASCII value to 'tot'
tot += ord(c)

# Return the total ASCII value of uppercase characters


return tot

# Assign a specific string 'strs' to the variable


strs = "PytHon ExerciSEs"

# Print the original string 'strs'


print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed


print("Sum of the ASCII values of the upper-case characters in the said string:")

# Print the result of the test function applied to the 'strs' string
print(test(strs))

# Assign a different string 'strs' to the variable


strs = "JavaScript"

# Print the original string 'strs'


print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed


print("Sum of the ASCII values of the upper-case characters in the said string:")

# Print the result of the test function applied to the updated 'strs' string
print(test(strs))

# Assign another string 'strs' to the variable


strs = "ARt"

# Print the original string 'strs'


print("\nOriginal strings:")
print(strs)
# Print a message indicating the operation to be performed
print("Sum of the ASCII values of the upper-case characters in the said string:")

# Print the result of the test function applied to the updated 'strs' string
print(test(strs))

Original strings:
PytHon ExerciSEs
Sum of the ASCII values of the upper-case characters in the said string:
373

Original strings:
JavaScript
Sum of the ASCII values of the upper-case characters in the said string:
157

Original strings:
ARt
Sum of the ASCII values of the upper-case characters in the said string:
147

===================================================================================
=================

Puzzle 23
Question: Last update on May 30 2025 11:48:41 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:41 (UTC/GMT +8 hours)

Indices of Drops in List

Write a Python program to find the indices at which the numbers in the list drop.

NOTE: You can detect multiple drops just by checking if nums[i] < nums[i-1].

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:


Go to:

Previous:Compute the sum of the ASCII values of the upper-case characters in a


given [Link]:Create a list whose ith element is the maximum of the first i
elements of the input list.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]
Output:
[1, 4, 6, 8, 10, 11, 15, 16, 18]

Input:
[6, 5, 4, 3, 2, 1]
Output:
[1, 2, 3, 4, 5]

Input:
[1, 19, 5, 15, 5, 25, 5]
Output:
[2, 4, 6]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Initialize an empty list 'drop_indices' to store indices where numbers drop
drop_indices = []

# Iterate through the list starting from the second element


for i in range(1, len(nums)):
# Check if the current number is less than the previous number
if nums[i] < nums[i - 1]:
# If true, append the index to 'drop_indices'
drop_indices.append(i)

# Return the list of indices where the numbers drop


return drop_indices

# Assign a specific list of numbers 'nums' to the variable


nums = [0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]

# Print the original list of numbers 'nums'


print("Original list:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices for which the numbers of the said list drops:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign a different list of numbers 'nums' to the variable


nums = [6, 5, 4, 3, 2, 1]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices for which the numbers of the said list drops:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

# Assign another list of numbers 'nums' to the variable


nums = [1, 19, 5, 15, 5, 25, 5]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices for which the numbers of the said list drops:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

Original list:
[0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]
Indices for which the numbers of the said list drops.:
[1, 4, 6, 8, 10, 11, 15, 16, 18]

Original list:
[6, 5, 4, 3, 2, 1]
Indices for which the numbers of the said list drops.:
[1, 2, 3, 4, 5]

Original list:
[1, 19, 5, 15, 5, 25, 5]
Indices for which the numbers of the said list drops.:
[2, 4, 6]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# List comprehension to create a list of indices where the numbers drop
# Iterate through the list starting from the second element
# Check if the current number is less than the previous number
# If true, include the index in the resulting list
return [i for i in range(1, len(nums)) if nums[i] < nums[i - 1]]

# Assign a specific list of numbers 'nums' to the variable


nums = [0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]

# Print the original list of numbers 'nums'


print("Original list:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices for which the numbers of the said list drops:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign a different list of numbers 'nums' to the variable


nums = [6, 5, 4, 3, 2, 1]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices for which the numbers of the said list drops:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

# Assign another list of numbers 'nums' to the variable


nums = [1, 19, 5, 15, 5, 25, 5]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices for which the numbers of the said list drops:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

Original list:
[0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]
Indices for which the numbers of the said list drops.:
[1, 4, 6, 8, 10, 11, 15, 16, 18]

Original list:
[6, 5, 4, 3, 2, 1]
Indices for which the numbers of the said list drops.:
[1, 2, 3, 4, 5]

Original list:
[1, 19, 5, 15, 5, 25, 5]
Indices for which the numbers of the said list drops.:
[2, 4, 6]
===================================================================================
=================

Puzzle 24
Question: Last update on May 30 2025 11:48:41 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:41 (UTC/GMT +8 hours)

Max of First i Elements

Write a Python program to create a list whose ithelement is the maximum of the
first i elements from an input list.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the indices for which the numbers in the list [Link]:Find the XOR
of two given strings interpreted as binary numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]
Output:
[0, 0, 3, 8, 8, 9, 9, 14, 14, 14, 14, 14, 14, 17, 41, 41, 41, 41, 41, 41]

Input:
[6, 5, 4, 3, 2, 1]
Output:
[6, 6, 6, 6, 6, 6]

Input:
[1, 19, 5, 15, 5, 25, 5]
Output:
[1, 19, 19, 19, 19, 25, 25]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# List comprehension to create a list of maximum values for each prefix of the
input list
# Iterate through the indices from 1 to the length of 'nums' + 1
# For each index 'i', find the maximum value in the prefix nums[:i]
return [max(nums[:i]) for i in range(1, len(nums) + 1)]

# Assign a specific list of numbers 'nums' to the variable


nums = [0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]

# Print the original list of numbers 'nums'


print("Original list:")
print(nums)

# Print a message indicating the operation to be performed


print("List whose ith element is the maximum of the first i elements of the said
list:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign a different list of numbers 'nums' to the variable


nums = [6, 5, 4, 3, 2, 1]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)

# Print a message indicating the operation to be performed


print("List whose ith element is the maximum of the first i elements of the said
list:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

# Assign another list of numbers 'nums' to the variable


nums = [1, 19, 5, 15, 5, 25, 5]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)

# Print a message indicating the operation to be performed


print("List whose ith element is the maximum of the first i elements of the said
list:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

Original list:
[0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]
List whose ith element is the maximum of the first i elements of the said list:
[0, 0, 3, 8, 8, 9, 9, 14, 14, 14, 14, 14, 14, 17, 41, 41, 41, 41, 41, 41]

Original list:
[6, 5, 4, 3, 2, 1]
List whose ith element is the maximum of the first i elements of the said list:
[6, 6, 6, 6, 6, 6]

Original list:
[1, 19, 5, 15, 5, 25, 5]
List whose ith element is the maximum of the first i elements of the said list:
[1, 19, 19, 19, 19, 25, 25]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# List comprehension to create a list of maximum values for each prefix of the
input list
# Iterate through the indices from 0 to the length of 'nums'
# For each index 'i', find the maximum value in the prefix nums[:i+1]
return [max(nums[:i+1]) for i in range(len(nums))]

# Assign a specific list of numbers 'nums' to the variable


nums = [0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]

# Print the original list of numbers 'nums'


print("Original list:")
print(nums)

# Print a message indicating the operation to be performed


print("List whose ith element is the maximum of the first i elements of the said
list:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign a different list of numbers 'nums' to the variable


nums = [6, 5, 4, 3, 2, 1]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)
# Print a message indicating the operation to be performed
print("List whose ith element is the maximum of the first i elements of the said
list:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

# Assign another list of numbers 'nums' to the variable


nums = [1, 19, 5, 15, 5, 25, 5]

# Print the original list of numbers 'nums'


print("\nOriginal list:")
print(nums)

# Print a message indicating the operation to be performed


print("List whose ith element is the maximum of the first i elements of the said
list:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

Original list:
[0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]
List whose ith element is the maximum of the first i elements of the said list:
[0, 0, 3, 8, 8, 9, 9, 14, 14, 14, 14, 14, 14, 17, 41, 41, 41, 41, 41, 41]

Original list:
[6, 5, 4, 3, 2, 1]
List whose ith element is the maximum of the first i elements of the said list:
[6, 6, 6, 6, 6, 6]

Original list:
[1, 19, 5, 15, 5, 25, 5]
List whose ith element is the maximum of the first i elements of the said list:
[1, 19, 19, 19, 19, 25, 25]

===================================================================================
=================

Puzzle 25
Question: Last update on May 30 2025 11:48:42 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:42 (UTC/GMT +8 hours)

XOR of Binary Strings

Write a Python program to find the XOR of two given strings interpreted as binary
numbers.

Note: XOR represents the inequality function, i.e., the output is true if the
inputs are not alike otherwise the output is false. A way to remember XOR is "must
have one or the other but not both". XOR can also be viewed as addition modulo 2.
As a result, XOR gates are used to implement binary addition in computers.

Visual Presentation:
Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Create a list whose ith element is the maximum of the first i elements of
the input [Link]:Find the largest number where commas or periods are decimal
points.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['0001', '1011']
Output:
0b1010

Input:
['100011101100001', '100101100101110']
Output:
0b110001001111

# License: [Link]

# Define a function named 'test' that takes a list of binary strings 'nums' as
input
def test(nums):
# Use binary XOR (^) on the integers converted from the binary strings in the
input list
# Convert the result back to a binary string
return bin(int(nums[0], 2) ^ int(nums[1], 2))

# Assign a specific list of binary strings 'nums' to the variable


nums = ["0001", "1011"]

# Print the original list of binary strings 'nums'


print("Original strings:")
print(nums)

# Print a message indicating the operation to be performed


print("XOR of two said strings interpreted as binary numbers:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign a different list of binary strings 'nums' to the variable


nums = ["100011101100001", "100101100101110"]

# Print the original list of binary strings 'nums'


print("\nOriginal strings:")
print(nums)

# Print a message indicating the operation to be performed


print("XOR of two said strings interpreted as binary numbers:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

Original strings:
['0001', '1011']
XOR of two said strings interpreted as binary numbers:
0b1010

Original strings:
['100011101100001', '100101100101110']
XOR of two said strings interpreted as binary numbers:
0b110001001111

# License: [Link]

# Define a function named 'test' that takes a list of binary strings 'nums' as
input
def test(nums):
# Unpack the list 'nums' into two binary strings 'a' and 'b'
a, b = nums

# Use binary XOR (^) on the integers converted from the binary strings in the
input list
xor = int(a, 2) ^ int(b, 2)

# Convert the result back to a binary string, ensuring it has the same length
as the input strings
return bin(xor)[2:].zfill(len(a))

# Assign a specific list of binary strings 'nums' to the variable


nums = ["0001", "1011"]

# Print the original list of binary strings 'nums'


print("Original strings:")
print(nums)

# Print a message indicating the operation to be performed


print("XOR of two said strings interpreted as binary numbers:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign a different list of binary strings 'nums' to the variable


nums = ["100011101100001", "100101100101110"]

# Print the original list of binary strings 'nums'


print("\nOriginal strings:")
print(nums)

# Print a message indicating the operation to be performed


print("XOR of two said strings interpreted as binary numbers:")

# Print the result of the test function applied to the updated 'nums' list
print(test(nums))

Original strings:
['0001', '1011']
XOR of two said strings interpreted as binary numbers:
1010

Original strings:
['100011101100001', '100101100101110']
XOR of two said strings interpreted as binary numbers:
000110001001111

===================================================================================
=================

Puzzle 26
Question: Last update on May 30 2025 11:48:42 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:42 (UTC/GMT +8 hours)

Largest Number with Commas/Periods

Write a Python program to find the largest number where commas or periods are
decimal points.

Visual Presentation:

Sample Solution-1:
Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the XOR of two given strings interpreted as binary [Link]:Find


x that minimizes mean squared deviation from a given list of numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['100', '102,1', '101.1']
Output:
102.1

# License: [Link]

# Define a function named 'test' that takes a list of strings 'str_nums' as input
def test(str_nums):
# Use a generator expression to iterate through each string in 'str_nums'
# Replace commas with periods and convert each string to a float, then find the
maximum value
return max(float([Link](",", ".")) for s in str_nums)

# Assign a specific list of strings 'str_nums' to the variable


str_nums = ["100", "102,1", "101.1"]

# Print the original list of strings 'str_nums'


print("Original list:")
print(str_nums)

# Print a message indicating the operation to be performed


print("Largest number where commas or periods are decimal points:")

# Print the result of the test function applied to the 'str_nums' list
print(test(str_nums))

Original list:
['100', '102,1', '101.1']
Largest number where commas or periods are decimal points:
102.1

# License: [Link]

# Define a function named 'test' that takes a list of strings 'str_nums' as input
def test(str_nums):
# Initialize an empty list to store converted float values
numbers = []

# Iterate through each string in 'str_nums'


for s in str_nums:
# Replace commas with periods, convert the string to a float, and append to
the 'numbers' list
[Link](float([Link](",", ".")))

# Sort the 'numbers' list in ascending order


[Link]()

# Return the largest number, which is the last element in the sorted list
return numbers[-1]

# Assign a specific list of strings 'str_nums' to the variable


str_nums = ["100", "102,1", "103.1"]

# Print the original list of strings 'str_nums'


print("Original list:")
print(str_nums)

# Print a message indicating the operation to be performed


print("Largest number where commas or periods are decimal points:")

# Print the result of the test function applied to the 'str_nums' list
print(test(str_nums))

Original list:
['100', '102,1', '103.1']
Largest number where commas or periods are decimal points:
103.1

===================================================================================
=================
Puzzle 27
Question: Last update on May 30 2025 11:48:43 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:43 (UTC/GMT +8 hours)

Minimize Mean Squared Deviation

Squared deviations from the mean (SDM) are involved in various calculations. In
probability theory and statistics, the definition of variance is either the
expected value of the SDM (when considering a theoretical distribution) or its
average value (for actual experimental data). Computations for analysis of variance
involve the partitioning of a sum of [Link] a Python program to find x that
minimizes the mean squared deviation from a given list of [Link] problem
requires minimizing the sum of squared deviations, which turns out to be the mean
mu. Moreover, if mu is the mean of the numbers then a simple calculation.

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the largest number where commas or periods are decimal


[Link]:Select a string from a given list of strings with the most unique
characters.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[4, -5, 17, -9, 14, 108, -9]
Output:
17.142857142857142
Input:
[12, -2, 14, 3, -15, 10, -45, 3, 30]
Output:
1.1111111111111112

# License: [Link]
# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Calculate the mean by summing the numbers and dividing by the length of the
list
return sum(nums) / len(nums)

# Assign a specific list of numbers 'nums' to the variable


nums = [4, -5, 17, -9, 14, 108, -9]

# Print the original list of numbers 'nums'


print("Original list:")
print(nums)

# Print a message indicating the operation to be performed


print("Minimizes mean squared deviation from the said list of numbers:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign another specific list of numbers 'nums' to the variable


nums = [12, -2, 14, 3, -15, 10, -45, 3, 30]

# Print the original list of numbers 'nums'


print("Original list:")
print(nums)

# Print a message indicating the operation to be performed


print("Minimizes mean squared deviation from the said list of numbers:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

Original list:
[4, -5, 17, -9, 14, 108, -9]
Minimizes mean squared deviation from the said list of numbers:
17.142857142857142
Original list:
[12, -2, 14, 3, -15, 10, -45, 3, 30]
Minimizes mean squared deviation from the said list of numbers:
1.1111111111111112

===================================================================================
=================

Puzzle 28
Question: Last update on May 30 2025 11:48:43 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:43 (UTC/GMT +8 hours)

String with Most Unique Characters

Write a Python program to select a string from a given list of strings with the
most unique characters.

Visual Presentation:
Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find x that minimizes mean squared deviation from a given list of


[Link]:Find the indices of two numbers that sum to 0 in a given list.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo', 'unique']
Output:
abcdefhijklmnop

Input:
['Green', 'Red', 'Orange', 'Yellow', '', 'White']
Output:
Orange

# License: [Link]

# Define a function named 'test' that takes a list of strings 'strs' as input
def test(strs):
# Use the max function to find the string with the most unique characters
# The key argument specifies a lambda function that calculates the length of
the set of characters in each string
return max(strs, key=lambda x: len(set(x)))

# Assign a specific list of strings 'strs' to the variable


strs = ['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo',
'unique']

# Print the original list of strings 'strs'


print("Original list:")
print(strs)

# Print a message indicating the operation to be performed


print("Select a string from the said list of strings with the most unique
characters:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

# Assign another specific list of strings 'strs' to the variable


strs = ['Green', 'Red', 'Orange', 'Yellow', '', 'White']

# Print the original list of strings 'strs'


print("\nOriginal list:")
print(strs)

# Print a message indicating the operation to be performed


print("Select a string from the said list of strings with the most unique
characters:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

Original list:
['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo', 'unique']
Select a string from the said list of strings with the most unique characters:
abcdefhijklmnop

Original list:
['Green', 'Red', 'Orange', 'Yellow', '', 'White']
Select a string from the said list of strings with the most unique characters:
Orange

# License: [Link]

# Define a function named 'test' that takes a list of strings 'strs' as input
def test(strs):
# Initialize variables to track the largest set, its length, and the
corresponding string
largest_set = {}
n = 0
largest = None

# Iterate over each phrase in the list of strings 'strs'


for phrase in strs:
# Calculate the difference in lengths between the current largest set and
the set of the current phrase
diff = len(largest_set) - len(set(phrase))

# Compare the differences and update variables accordingly


if diff < 0:
largest_set = set(phrase)
largest = phrase
n = len(largest_set)
elif diff == 0:
if n < len(set(phrase)):
largest_set = set(phrase)
largest = phrase
n = len(largest_set)
else:
pass

# Return the string with the most unique characters


return largest

# Assign a specific list of strings 'strs' to the variable


strs = ['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo',
'unique']

# Print the original list of strings 'strs'


print("Original list:")
print(strs)

# Print a message indicating the operation to be performed


print("Select a string from the said list of strings with the most unique
characters:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

# Assign another specific list of strings 'strs' to the variable


strs = ['Green', 'Red', 'Orange', 'Yellow', '', 'White']

# Print the original list of strings 'strs'


print("\nOriginal list:")
print(strs)

# Print a message indicating the operation to be performed


print("Select a string from the said list of strings with the most unique
characters:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

Original list:
['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo', 'unique']
Select a string from the said list of strings with the most unique characters:
abcdefhijklmnop

Original list:
['Green', 'Red', 'Orange', 'Yellow', '', 'White']
Select a string from the said list of strings with the most unique characters:
Orange

===================================================================================
=================

Puzzle 29
Question: Last update on May 30 2025 11:48:44 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:44 (UTC/GMT +8 hours)

Indices of Numbers Summing to Zero

Write a Python program to find the indices of two numbers that sum to 0 in a given
list of numbers.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Select a string from a given list of strings with the most unique
[Link]:Find the list that has fewer total characters (including
repetitions).

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.


Input:
[1, -4, 6, 7, 4]
Output:
[4, 1]

Input:
[1232, -20352, 12547, 12440, 741, 341, 525, 20352, 91, 20]
Output:
[1, 7]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Create a set 's' from the given list 'nums'
s = set(nums)

# Iterate over each element 'i' in the set 's'


for i in s:
# Check if the negation of 'i' is also in the set 's'
if -i in s:
# If found, return the indices of 'i' and its negation in the original
list 'nums'
return [[Link](i), [Link](-i)]

# Assign a specific list of numbers 'nums' to the variable


nums = [1, -4, 6, 7, 4]

# Print the original list of numbers 'nums'


print("Original List:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices of two numbers that sum to 0 in the said list:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign another specific list of numbers 'nums' to the variable


nums = [1232, -20352, 12547, 12440, 741, 341, 525, 20352, 91, 20]

# Print the original list of numbers 'nums'


print("\nOriginal List:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices of two numbers that sum to 0 in the said list:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

Original List:
[1, -4, 6, 7, 4]
Indices of two numbers that sum to 0 in the said list:
[4, 1]

Original List:
[1232, -20352, 12547, 12440, 741, 341, 525, 20352, 91, 20]
Indices of two numbers that sum to 0 in the said list:
[1, 7]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Initialize an empty list 'result' to store the indices
result = []

# Iterate over each index 'ind' in the range of the length of the list 'nums'
for ind in range(len(nums)):
# Iterate over each index 'i' in the range of the length of the list 'nums'
for i in range(len(nums)):
# Check if the current index 'ind' is not equal to the other index 'i'
# and if the sum of the numbers at the current index and the other
index is zero
if ind != i and nums[ind] + nums[i] == 0:
# Append the current index 'ind' and the other index 'i' to the
'result' list
[Link](ind)
[Link](i)
# Found the indices; no need to go through the whole list
return result

# Assign a specific list of numbers 'nums' to the variable


nums = [1, -4, 6, 7, 4]

# Print the original list of numbers 'nums'


print("Original List:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices of two numbers that sum to 0 in the said list:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign another specific list of numbers 'nums' to the variable


nums = [1232, -20352, 12547, 12440, 741, 341, 525, 20352, 91, 20]

# Print the original list of numbers 'nums'


print("\nOriginal List:")
print(nums)

# Print a message indicating the operation to be performed


print("Indices of two numbers that sum to 0 in the said list:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))
Original List:
[1, -4, 6, 7, 4]
Indices of two numbers that sum to 0 in the said list:
[1, 4]

Original List:
[1232, -20352, 12547, 12440, 741, 341, 525, 20352, 91, 20]
Indices of two numbers that sum to 0 in the said list:
[1, 7]

===================================================================================
=================

Puzzle 30
Question: Last update on May 30 2025 11:48:44 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:44 (UTC/GMT +8 hours)

List with Fewer Total Characters

Write a Python program to find a list of strings that have fewer total characters
(including repetitions).

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the indices of two numbers that sum to 0 in a given [Link]:Find


the coordinates of a triangle with the given side lengths.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[['this', 'list', 'is', 'narrow'], ['I', 'am', 'shorter but wider']]
Output:
['this', 'list', 'is', 'narrow']

Input:
[['Red', 'Black', 'Pink'], ['Green', 'Red', 'White']]

Output:
['Red', 'Black', 'Pink']

# License: [Link]

# Define a function named 'test' that takes a list of lists of strings 'strs' as
input
def test(strs):
# Use the min function with a lambda function as the key to find the list with
the fewest total characters
return min(strs, key=lambda x: sum(len(i) for i in x))

# Assign a specific list of lists of strings 'strs' to the variable


strs = [['this', 'list', 'is', 'narrow'], ['I', 'am', 'shorter but wider']]

# Print the original list of lists of strings 'strs'


print("Original List:")
print(strs)

# Print a message indicating the operation to be performed


print("\nFind the given list of strings that has fewer total characters:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

# Assign another specific list of lists of strings 'strs' to the variable


strs = [['Red', 'Black', 'Pink'], ['Green', 'Red', 'White']]

# Print the original list of lists of strings 'strs'


print("\nOriginal List:")
print(strs)

# Print a message indicating the operation to be performed


print("\nFind the given list of strings that has fewer total characters:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

Original List:
[['this', 'list', 'is', 'narrow'], ['I', 'am', 'shorter but wider']]

Find the given list of strings that has fewer total characters:
['this', 'list', 'is', 'narrow']

Original List:
[['Red', 'Black', 'Pink'], ['Green', 'Red', 'White']]

Find the given list of strings that has fewer total characters:
['Red', 'Black', 'Pink']

===================================================================================
=================

Puzzle 31
Question: Last update on May 30 2025 11:48:45 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:45 (UTC/GMT +8 hours)

Triangle Coordinates Finder

Write a Python program to find the coordinates of a triangle with given side
lengths.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the list that has fewer total characters (including


repetitions).Next:Rescale and shift numbers so that they cover the range [0, 1].

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[3, 4, 5]
Output:
[[0.0, 0.0], [3, 0.0], [3.0, 4.0]]

Input:
[5, 6, 7]
Output:
[[0.0, 0.0], [5, 0.0], [3.8, 5.878775382679628]]

# License: [Link]

# Define a function named 'test' that takes a list 'sides' representing the side
lengths of a triangle
def test(sides):
# Sort the side lengths in ascending order and assign them to variables a, b,
and c
a, b, c = sorted(sides)

# Calculate the semi-perimeter of the triangle


s = sum(sides) / 2

# Use Heron's formula to calculate the area of the triangle


area = (s * (s - a) * (s - b) * (s - c)) ** 0.5

# Calculate the height of the triangle


y = 2 * area / a

# Calculate the x-coordinate of the third vertex using the Pythagorean theorem
x = (c ** 2 - y ** 2) ** 0.5

# Return the coordinates of the vertices of the triangle as a list of lists


return [[0.0, 0.0], [a, 0.0], [x, y]]

# Assign a specific list of side lengths 'sides' to the variable


sides = [3, 4, 5]

# Print the side lengths of the triangle


print("Sides of the triangle:", sides)

# Print a message indicating the operation to be performed


print("Coordinates of a triangle with the said side lengths:")

# Print the result of the test function applied to the 'sides' list
print(test(sides))

# Assign another specific list of side lengths 'sides' to the variable


sides = [5, 6, 7]

# Print the side lengths of the triangle


print("\nSides of the triangle:", sides)

# Print a message indicating the operation to be performed


print("Coordinates of a triangle with the said side lengths:")

# Print the result of the test function applied to the 'sides' list
print(test(sides))

Sides of the triangle: [3, 4, 5]


Coordinates of a triangle with the said side lengths:
[[0.0, 0.0], [3, 0.0], [3.0, 4.0]]

Sides of the triangle: [5, 6, 7]


Coordinates of a triangle with the said side lengths:
[[0.0, 0.0], [5, 0.0], [3.8, 5.878775382679628]]

===================================================================================
=================

Puzzle 32
Question: Last update on May 30 2025 11:48:45 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:45 (UTC/GMT +8 hours)

Rescale List to [0,1]

Write a Python program to rescale and shift numbers in a given list, so that they
cover the range [0, 1].

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the coordinates of a triangle with the given side [Link]:Find


the positions of all uppercase vowels (not counting Y) in even indices.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[18.5, 17.0, 18.0, 19.0, 18.0]
Output:
[0.75, 0.0, 0.5, 1.0, 0.5]

Input:
[13.0, 17.0, 17.0, 15.5, 2.94]
Output:
[0.7155049786628734, 1.0, 1.0, 0.8933143669985776, 0.0]
# Define a function named 'test' that takes a list 'nums' as input
def test(nums):
# Find the minimum and maximum values in the list and assign them to variables
'a' and 'b'
a = min(nums)
b = max(nums)

# Check if the range between the minimum and maximum values is zero
if b - a == 0:
# If the range is zero, return a list with 0.0 as the first element and 1.0
for the remaining elements
return [0.0] + [1.0] * (len(nums) - 1)

# Iterate over the indices of the list


for i in range(len(nums)):
# Rescale and shift each element in the list to cover the range [0, 1]
nums[i] = (nums[i] - a) / (b - a)

# Return the modified list


return nums

# Assign a specific list of numbers 'nums' to the variable


nums = [18.5, 17.0, 18.0, 19.0, 18.0]

# Print a message indicating the operation to be performed


print("Original list:")

# Print the original list of numbers


print(nums)

# Print a message indicating the operation to be performed


print("Rescale and shift the numbers of the said list so that they cover the range
[0, 1]:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Assign another specific list of numbers 'nums' to the variable


nums = [13.0, 17.0, 17.0, 15.5, 2.94]

# Print a message indicating the operation to be performed


print("\nOriginal list:")

# Print the original list of numbers


print(nums)

# Print a message indicating the operation to be performed


print("Rescale and shift the numbers of the said list so that they cover the range
[0, 1]:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

Original list:
[18.5, 17.0, 18.0, 19.0, 18.0]
Rescale and shift the numbers of the said list so that they cover the range [0, 1]:
[0.75, 0.0, 0.5, 1.0, 0.5]

Original list:
[13.0, 17.0, 17.0, 15.5, 2.94]
Rescale and shift the numbers of the said list so that they cover the range [0, 1]:
[0.7155049786628734, 1.0, 1.0, 0.8933143669985776, 0.0]

===================================================================================
=================

Puzzle 33
Question: Last update on May 30 2025 11:48:46 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:46 (UTC/GMT +8 hours)

Uppercase Vowel Positions

Write a Python program to find the positions of all uppercase vowels (not counting
Y) in even indices of a given string.

From Wikipedia:A vowel is a syllabic speech sound pronounced without any stricture
in the vocal tract. Vowels are one of the two principal classes of speech sounds,
the other being the consonant. Vowels vary in quality, in loudness and also in
quantity. There are six vowels in the English language: a, e, i, o, u and sometimes
y.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Rescale and shift numbers so that they cover the range [0, 1].Next:Find
the sum of the numbers among the first k with more than 2 digits.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.


Follow us onFacebookandTwitterfor latest update.

Input:
w3rEsOUrcE
Output:
[6]

Input:
AEIOUYW

Output:
[0, 2, 4]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Use a list comprehension to generate a list of indices for uppercase vowels
(excluding 'Y') at even indices
return [i for i, c in enumerate(strs) if i % 2 == 0 and c in "AEIOU"]

# Assign a specific string 'strs' to the variable


strs = "w3rEsOUrcE "

# Print a message indicating the operation to be performed


print("Original List:", strs)

# Print the original string


print("Positions of all uppercase vowels (not counting Y) in even indices:")
# Print the result of the test function applied to the 'strs' string
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "AEIOUYW "

# Print a message indicating the operation to be performed


print("\nOriginal List:", strs)

# Print the original string


print("Positions of all uppercase vowels (not counting Y) in even indices:")
# Print the result of the test function applied to the 'strs' string
print(test(strs))

Original List: w3rEsOUrcE


Positions of all uppercase vowels (not counting Y) in even indices:
[6]

Original List: AEIOUYW


Positions of all uppercase vowels (not counting Y) in even indices:
[0, 2, 4]

===================================================================================
=================

Puzzle 34
Question: Last update on May 30 2025 11:48:46 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:46 (UTC/GMT +8 hours)

Sum of Numbers with 2+ Digits

Write a Python program to find the sum of the numbers in a given list among the
first k with more than 2 digits.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the positions of all uppercase vowels (not counting Y) in even


[Link]:Product of the odd digits in n, or 0 if there aren't any.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[4, 5, 17, 9, 14, 108, -9, 12, 76]
Value of K: 4
Output:
0
Input:
[4, 5, 17, 9, 14, 108, -9, 12, 76]
Value of K: 6
Output:
108

Input:
[114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 5
Output:
331

Input:
[114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 1
Output:
114

# Define a function named 'test' that takes a list of numbers 'nums' and an integer
'k' as input
def test(nums, k):
# Initialize a variable 's' to store the sum of numbers meeting the specified
conditions
s = 0
# Iterate through the first 'k' elements in 'nums'
for i in range(len(nums))[:k]:
# Check if the absolute value of the current number has more than 2 digits
if len(str(abs(nums[i]))) > 2:
# Add the current number to the sum 's'
s = s + nums[i]
# Return the final sum 's'
return s

# Assign a specific list of numbers 'nums' to the variable


nums = [4, 5, 17, 9, 14, 108, -9, 12, 76]

# Print a message indicating the operation to be performed


print("Original list:", nums)

# Assign a specific value 'K' to the variable


K = 4

# Print a message indicating the value of 'K'


print("Value of K:", K)

# Print a message indicating the operation to be performed


print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Assign another specific value 'K' to the variable


K = 6

# Print a message indicating the value of 'K'


print("\nOriginal list:", nums)
print("Value of K:", K)
# Print a message indicating the operation to be performed
print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Assign another specific list of numbers 'nums' to the variable


nums = [114, 215, -117, 119, 14, 108, -9, 12, 76]

# Print a message indicating the operation to be performed


print("\nOriginal list:", nums)

# Assign another specific value 'K' to the variable


K = 5

# Print a message indicating the value of 'K'


print("Value of K:", K)

# Print a message indicating the operation to be performed


print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Print an additional message indicating the original list


print("\nOriginal list:", nums)

# Assign another specific value 'K' to the variable


K = 1

# Print a message indicating the value of 'K'


print("Value of K:", K)

# Print a message indicating the operation to be performed


print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

Original list: [4, 5, 17, 9, 14, 108, -9, 12, 76]


Value of K: 4
sum of the numbers among the first k with more than 2 digits
0

Original list: [4, 5, 17, 9, 14, 108, -9, 12, 76]


Value of K: 6
sum of the numbers among the first k with more than 2 digits
108

Original list: [114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 5
sum of the numbers among the first k with more than 2 digits
331

Original list: [114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 1
sum of the numbers among the first k with more than 2 digits
114
#License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' and an integer
'k' as input
def test(nums, k):
# Use a generator expression to sum numbers meeting the specified conditions in
the first 'k' elements of 'nums'
return sum(n for n in nums[:k] if len(str(abs(n))) > 2)

# Assign a specific list of numbers 'nums' to the variable


nums = [4, 5, 17, 9, 14, 108, -9, 12, 76]

# Print a message indicating the operation to be performed


print("Original list:", nums)

# Assign a specific value 'K' to the variable


K = 4

# Print a message indicating the value of 'K'


print("Value of K:", K)

# Print a message indicating the operation to be performed


print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Assign another specific value 'K' to the variable


K = 6

# Print a message indicating the value of 'K'


print("\nOriginal list:", nums)
print("Value of K:", K)

# Print a message indicating the operation to be performed


print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Assign another specific list of numbers 'nums' to the variable


nums = [114, 215, -117, 119, 14, 108, -9, 12, 76]

# Print a message indicating the operation to be performed


print("\nOriginal list:", nums)

# Assign another specific value 'K' to the variable


K = 5

# Print a message indicating the value of 'K'


print("Value of K:", K)

# Print a message indicating the operation to be performed


print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

# Print an additional message indicating the original list


print("\nOriginal list:", nums)
# Assign another specific value 'K' to the variable
K = 1

# Print a message indicating the value of 'K'


print("Value of K:", K)

# Print a message indicating the operation to be performed


print("Sum of the numbers among the first k with more than 2 digits:")
# Print the result of the test function applied to 'nums' and 'K'
print(test(nums, K))

Original list: [4, 5, 17, 9, 14, 108, -9, 12, 76]


Value of K: 4
sum of the numbers among the first k with more than 2 digits
0

Original list: [4, 5, 17, 9, 14, 108, -9, 12, 76]


Value of K: 6
sum of the numbers among the first k with more than 2 digits
108

Original list: [114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 5
sum of the numbers among the first k with more than 2 digits
331

Original list: [114, 215, -117, 119, 14, 108, -9, 12, 76]
Value of K: 1
sum of the numbers among the first k with more than 2 digits
114

===================================================================================
=================

Puzzle 35
Question: Last update on May 30 2025 11:48:47 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:47 (UTC/GMT +8 hours)

Product of Odd Digits

Write a Python program to compute the product of the odd digits in a given number,
or 0 if there aren't any.

Visual Presentation:

Sample Solution:

Python Code:
Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the sum of the numbers among the first k with more than 2
[Link]:Find the largest k numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
123456789
Output:
945

Input:
2468
Output:
0

Input:
13579
Output:
945

#License: [Link]

# Define a function named 'test' that takes an integer 'n' as input


def test(n):
# Check if any digit in the number is odd
if any(int(c) % 2 for c in str(n)):
# Initialize a variable 'prod' to store the product of odd digits
prod = 1
# Iterate over each digit in the number
for c in str(n):
# If the digit is odd, multiply it with the current product
if int(c) % 2 == 1:
prod *= int(c)
# Return the final product of odd digits
return prod
# Return 0 if there are no odd digits in the number
return 0

# Assign a specific integer 'n' to the variable


n = 123456789

# Print a message indicating the operation to be performed


print("Original Number:", n)

# Print a message indicating the operation to be performed


print("Product of the odd digits in the said number, or 0 if there aren't any:")
# Print the result of the test function applied to 'n'
print(test(n))

# Assign another specific integer 'n' to the variable


n = 2468

# Print a message indicating the operation to be performed


print("\nOriginal Number:", n)

# Print a message indicating the operation to be performed


print("Product of the odd digits in the said number, or 0 if there aren't any:")
# Print the result of the test function applied to 'n'
print(test(n))

# Assign another specific integer 'n' to the variable


n = 13579

# Print a message indicating the operation to be performed


print("\nOriginal Number:", n)

# Print a message indicating the operation to be performed


print("Product of the odd digits in the said number, or 0 if there aren't any:")
# Print the result of the test function applied to 'n'
print(test(n))

Original Number: 123456789


Product of the odd digits in the said number, or 0 if there aren't any
945

Original Number: 2468


Product of the odd digits in the said number, or 0 if there aren't any
0

Original Number: 13579


Product of the odd digits in the said number, or 0 if there aren't any
945

===================================================================================
=================

Puzzle 36
Question: Last update on May 30 2025 11:48:47 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:47 (UTC/GMT +8 hours)

Find Largest K Numbers


Write a Python program to find the largest k numbers from a given list of numbers.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Product of the odd digits in n, or 0 if there aren't [Link]:Find the


largest integer divisor of a number n that is less than n.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 2, 3, 4, 5, 5, 3, 6, 2]
Output:
[6]
Input:
[1, 2, 3, 4, 5, 5, 3, 6, 2]
Output:
[6, 5]
Input:
[1, 2, 3, 4, 5, 5, 3, 6, 2]
Output:
[6, 5, 5]
Input:
[1, 2, 3, 4, 5, 5, 3, 6, 2]
Output:
[6, 5, 5, 4]
Input:
[1, 2, 3, 4, 5, 5, 3, 6, 2]
Output:
[6, 5, 5, 4, 3]

#License: [Link]
def test(nums, k):
if k == 0:
return []
elif k == len(nums):
return nums
else:
x = nums[0]
for n in nums:
if x < n:
x = n
result = [x]
largest = nums[:]
[Link](x)
while len(result) != k:
smallest = largest[0]
for n in largest:
if smallest < n:
smallest = n
[Link](smallest)
[Link](smallest)
return result
nums = [1, 2, 3, 4, 5, 5, 3, 6, 2]
print("Original list of numbers:",nums)
k = 1
print("Largest", k, "numbers from the said list:")
print(test(nums, k))
k = 2
print("Largest", k, "numbers from the said list:")
print(test(nums, k))
k = 3
print("Largest", k, "numbers from the said list:")
print(test(nums, k))
k = 4
print("Largest", k, "numbers from the said list:")
print(test(nums, k))
k = 5
print("Largest", k, "numbers from the said list:")
print(test(nums, k))

Original list of numbers: [1, 2, 3, 4, 5, 5, 3, 6, 2]


Largest 1 numbers from the said list:
[6]
Largest 2 numbers from the said list:
[6, 5]
Largest 3 numbers from the said list:
[6, 5, 5]
Largest 4 numbers from the said list:
[6, 5, 5, 4]
Largest 5 numbers from the said list:
[6, 5, 5, 4, 3]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' and an integer
'k' as input
def test(nums, k):
# Check if k is zero, in which case an empty list is returned
if k == 0:
return []
# Check if k is equal to the length of the list, in which case the original
list is returned
elif k == len(nums):
return nums
else:
# Initialize variable 'x' to the first element of the list
x = nums[0]
# Find the maximum element in the list and assign it to 'x'
for n in nums:
if x < n:
x = n
# Initialize a list 'result' with the maximum element as its first element
result = [x]
# Create a copy of the original list and assign it to 'largest'
largest = nums[:]
# Remove the maximum element from 'largest'
[Link](x)
# Continue adding elements to 'result' until its length becomes equal to
'k'
while len(result) != k:
# Find the smallest element in 'largest' and assign it to 'smallest'
smallest = largest[0]
for n in largest:
if smallest < n:
smallest = n
# Add the smallest element to 'result' and remove it from 'largest'
[Link](smallest)
[Link](smallest)
return result

# Assign a specific list of numbers 'nums' to the variable


nums = [1, 2, 3, 4, 5, 5, 3, 6, 2]

# Print a message indicating the original list of numbers


print("Original list of numbers:", nums)

# Assign a specific value 'k' to the variable


k = 1

# Print a message indicating the operation to be performed


print("Largest", k, "numbers from the said list:")
# Print the result of the test function applied to 'nums' and 'k'
print(test(nums, k))

# Assign another specific value 'k' to the variable


k = 2

# Print a message indicating the operation to be performed


print("Largest", k, "numbers from the said list:")
# Print the result of the test function applied to 'nums' and 'k'
print(test(nums, k))

# Assign another specific value 'k' to the variable


k = 3

# Print a message indicating the operation to be performed


print("Largest", k, "numbers from the said list:")
# Print the result of the test function applied to 'nums' and 'k'
print(test(nums, k))

# Assign another specific value 'k' to the variable


k = 4

# Print a message indicating the operation to be performed


print("Largest", k, "numbers from the said list:")
# Print the result of the test function applied to 'nums' and 'k'
print(test(nums, k))

# Assign another specific value 'k' to the variable


k = 5

# Print a message indicating the operation to be performed


print("Largest", k, "numbers from the said list:")
# Print the result of the test function applied to 'nums' and 'k'
print(test(nums, k))

Original list of numbers: [1, 2, 3, 4, 5, 5, 3, 6, 2]


Largest 1 numbers from the said list:
[6]
Largest 2 numbers from the said list:
[6, 5]
Largest 3 numbers from the said list:
[6, 5, 5]
Largest 4 numbers from the said list:
[6, 5, 5, 4]
Largest 5 numbers from the said list:
[6, 5, 5, 4, 3]

===================================================================================
=================

Puzzle 37
Question: Last update on May 30 2025 11:48:48 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:48 (UTC/GMT +8 hours)

A divisor is a number that divides another number either completely or with a


remainder.
Largest Proper Divisor

Write a Python program to find the largest integer divisor of a number n that is
less than n.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the largest k [Link]:Sort the numbers by the sum of their


digits.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
18
Output:
9
Input:
100
Output:
50
Input:
102
Output:
51
Input:
500
Output:
250
Input:
1000
Output:
500
Input:
6500
Output:
3250

# License: [Link]

# Define a function named 'test' that takes an integer 'n' as input


def test(n):
# Return the largest integer divisor of 'n' that is less than 'n'
return next(d for d in range(n - 1, 0, -1) if n % d == 0)

# Assign a specific value 'n' to the variable


n = 18

# Print a message indicating the original number


print("Original number:", n)

# Print a message indicating the operation to be performed


print("Largest integer divisor of a number n that is less than n:")
# Print the result of the test function applied to 'n'
print(test(n))

# Assign another specific value 'n' to the variable


n = 100

# Print a message indicating the original number


print("\nOriginal number:", n)

# Print a message indicating the operation to be performed


print("Largest integer divisor of a number n that is less than n:")
# Print the result of the test function applied to 'n'
print(test(n))

# Assign another specific value 'n' to the variable


n = 102

# Print a message indicating the original number


print("\nOriginal number:", n)

# Print a message indicating the operation to be performed


print("Largest integer divisor of a number n that is less than n:")
# Print the result of the test function applied to 'n'
print(test(n))

# Assign another specific value 'n' to the variable


n = 500

# Print a message indicating the original number


print("\nOriginal number:", n)

# Print a message indicating the operation to be performed


print("Largest integer divisor of a number n that is less than n:")
# Print the result of the test function applied to 'n'
print(test(n))

# Assign another specific value 'n' to the variable


n = 1000

# Print a message indicating the original number


print("\nOriginal number:", n)

# Print a message indicating the operation to be performed


print("Largest integer divisor of a number n that is less than n:")
# Print the result of the test function applied to 'n'
print(test(n))

# Assign another specific value 'n' to the variable


n = 6500

# Print a message indicating the original number


print("\nOriginal number:", n)

# Print a message indicating the operation to be performed


print("Largest integer divisor of a number n that is less than n:")
# Print the result of the test function applied to 'n'
print(test(n))

Original number: 18
Largest integer divisor of a number n that is less than n:
9
Original number: 100
Largest integer divisor of a number n that is less than n:
50
Original number: 102
Largest integer divisor of a number n that is less than n:
51
Original number: 500
Largest integer divisor of a number n that is less than n:
250
Original number: 1000
Largest integer divisor of a number n that is less than n:
500
Original number: 6500
Largest integer divisor of a number n that is less than n:
3250

===================================================================================
=================

Puzzle 38
Question: Last update on May 30 2025 11:48:48 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:48 (UTC/GMT +8 hours)

Sort by Digit Sum

Write a Python program to sort the numbers in a given list by the sum of their
digits.

Visual Presentation:
Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the largest integer divisor of a number n that is less than


[Link]:Determine which triples sum to zero.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Output:
[10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19]

Input:
[23, 2, 9, 34, 8, 9, 10, 74]
Output:
[10, 2, 23, 34, 8, 9, 9, 74]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Sort the numbers in 'nums' based on the sum of their digits
return sorted(nums, key=lambda n: sum(int(c) for c in str(n) if c != "-"))
# Assign a specific list of numbers 'nums' to the variable
nums = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

# Print a message indicating the original list of numbers


print("Original list of numbers:", nums)

# Print a message indicating the operation to be performed


print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of numbers 'nums' to the variable


nums = [23, 2, 9, 34, 8, 9, 10, 74]

# Print a message indicating the original list of numbers


print("\nOriginal list of numbers:", nums)

# Print a message indicating the operation to be performed


print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of numbers: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Sort the numbers of the said list by the sum of their digits:
[10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19]

Original list of numbers: [23, 2, 9, 34, 8, 9, 10, 74]


Sort the numbers of the said list by the sum of their digits:
[10, 2, 23, 34, 8, 9, 9, 74]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Create a copy of the original list 'nums' to preserve the original order
unordered = [Link]()
# Initialize an empty list to store the ordered numbers
ordered = []

# Continue the process until there are no more unordered numbers


while unordered:
# Select the first number in the unordered list as the smallest
smallest = unordered[0]
# Calculate the sum of digits for the current smallest number
s = sum(int(c) for c in str(smallest) if c != "-")

# Iterate through the remaining unordered numbers


for t in unordered:
# Calculate the sum of digits for the current number 't'
t_s = sum(int(c) for c in str(t) if c != "-")

# Compare the sum of digits and update the smallest if needed


if t_s < s:
smallest = t
s = t_s
# Append the smallest number to the ordered list
[Link](smallest)
# Remove the smallest number from the unordered list
[Link](smallest)

# Return the final ordered list


return ordered

# Assign a specific list of numbers 'nums' to the variable


nums = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

# Print a message indicating the original list of numbers


print("Original list of numbers:", nums)

# Print a message indicating the operation to be performed


print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of numbers 'nums' to the variable


nums = [23, 2, 9, 34, 8, 9, 10, 74]

# Print a message indicating the original list of numbers


print("\nOriginal list of numbers:", nums)

# Print a message indicating the operation to be performed


print("Sort the numbers of the said list by the sum of their digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of numbers: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Sort the numbers of the said list by the sum of their digits:
[10, 11, 20, 12, 13, 14, 15, 16, 17, 18, 19]

Original list of numbers: [23, 2, 9, 34, 8, 9, 10, 74]


Sort the numbers of the said list by the sum of their digits:
[10, 2, 23, 34, 8, 9, 9, 74]

===================================================================================
=================

Puzzle 39
Question: Last update on May 30 2025 11:48:49 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:49 (UTC/GMT +8 hours)

Zero-Sum Triples Finder

Write a Python program to determine which triples sum to zero from a given list of
lists.

Visual Presentation:

Sample Solution-1:
Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Sort the numbers by the sum of their [Link]:Find string s that, when
case is flipped gives target where vowels are replaced by chars two later.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[[1343532, -2920635, 332], [-27, 18, 9], [4, 0, -4], [2, 2, 2], [-20, 16, 4]]
Output:
[False, True, True, False, True]

Input:
[[1, 2, -3], [-4, 0, 4], [0, 1, -5], [1, 1, 1], [-2, 4, -1]]
Output:
[True, True, False, False, False]

# License: [Link]

# Define a function named 'test' that takes a list of lists of numbers 'nums' as
input
def test(nums):
# Use a list comprehension to check if the sum of each triple in 'nums' is
equal to zero
return [sum(t) == 0 for t in nums]
# Assign a specific list of lists 'nums' to the variable
nums = [[1343532, -2920635, 332], [-27, 18, 9], [4, 0, -4], [2, 2, 2], [-20, 16,
4]]

# Print a message indicating the original list of lists


print("Original list of lists:", nums)

# Print a message indicating the operation to be performed


print("Determine which triples sum to zero:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of lists 'nums' to the variable


nums = [[1, 2, -3], [-4, 0, 4], [0, 1, -5], [1, 1, 1], [-2, 4, -1]]

# Print a message indicating the original list of lists


print("\nOriginal list of lists:", nums)

# Print a message indicating the operation to be performed


print("Determine which triples sum to zero:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of lists: [[1343532, -2920635, 332], [-27, 18, 9], [4, 0, -4], [2, 2,
2], [-20, 16, 4]]
Determine which triples sum to zero:
[False, True, True, False, True]

Original list of lists: [[1, 2, -3], [-4, 0, 4], [0, 1, -5], [1, 1, 1], [-2, 4, -
1]]
Determine which triples sum to zero:
[True, True, False, False, False]

# License: [Link]

# Define a function named 'test' that takes a list of lists of numbers 'nums' as
input
def test(nums):
# Initialize an empty list to store Boolean values indicating if a triple sums
to zero
zero_sums = []

# Iterate through each triple in the list of lists 'nums'


for trip in nums:
# Iterate through each pair of indices i, j in the triple
for i in range(len(trip)):
for j in range(i + 1, len(trip)):
# Iterate through each index k greater than j
for k in range(j + 1, len(trip)):
# Check if the sum of elements at indices i, j, and k is equal
to zero
if trip[i] + trip[j] + trip[k] == 0:
# If a zero sum is found, append True to the 'zero_sums'
list and break the loop
zero_sums.append(True)
break
else:
continue
break
else:
continue
break
else:
# If no zero sum is found, append False to the 'zero_sums' list
zero_sums.append(False)

# Return the list of Boolean values indicating whether each triple sums to zero
return zero_sums

# Assign a specific list of lists 'nums' to the variable


nums = [[1343532, -2920635, 332], [-27, 18, 9], [4, 0, -4], [2, 2, 2], [-20, 16,
4]]

# Print a message indicating the original list of lists


print("Original list of lists:", nums)

# Print a message indicating the operation to be performed


print("Determine which triples sum to zero:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of lists 'nums' to the variable


nums = [[1, 2, -3], [-4, 0, 4], [0, 1, -5], [1, 1, 1], [-2, 4, -1]]

# Print a message indicating the original list of lists


print("\nOriginal list of lists:", nums)

# Print a message indicating the operation to be performed


print("Determine which triples sum to zero:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of lists: [[1343532, -2920635, 332], [-27, 18, 9], [4, 0, -4], [2, 2,
2], [-20, 16, 4]]
Determine which triples sum to zero:
[False, True, True, False, True]

Original list of lists: [[1, 2, -3], [-4, 0, 4], [0, 1, -5], [1, 1, 1], [-2, 4, -
1]]
Determine which triples sum to zero:
[True, True, False, False, False]

===================================================================================
=================

Puzzle 40
Question: Last update on May 30 2025 11:48:49 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:49 (UTC/GMT +8 hours)

Flip Case and Shift Vowels


Write a Python program to find strings that, when case is flipped, give a target
where vowels are replaced by characters two later.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Determine which triples sum to [Link]:Sort numbers based on strings.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: Python
Output:
pYTHQN

Input: aeiou
Output:
CGKQW

Input: Hello, world!


Output:
hGLLQ, WQRLD!

Input: AEIOU
Output:
cgkqw

# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Use the translate method to create a mapping for each vowel to the character
two positions later
translation_mapping = {ord(c): ord(c) + 2 for c in "aeiouAEIOU"}

# Apply the translation mapping and swap the case of the characters in the
string
result = [Link](translation_mapping).swapcase()

# Return the modified string


return result

# Assign a specific string 'strs' to the variable


strs = "Python"
# Print a message indicating the original string
print("Original string:", strs)
# Print a message indicating the operation to be performed
print("Find string s that, when case is flipped gives target where vowels are
replaced by chars two later:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "aeiou"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Find string s that, when case is flipped gives target where vowels are
replaced by chars two later:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "Hello, world!"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Find string s that, when case is flipped gives target where vowels are
replaced by chars two later:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "AEIOU"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Find string s that, when case is flipped gives target where vowels are
replaced by chars two later:")
# Print the result of the test function applied to 'strs'
print(test(strs))

Original string: Python


Find string s that, when case is flipped gives target where vowels are replaced by
chars two later:
pYTHQN

Original string: aeiou


Find string s that, when case is flipped gives target where vowels are replaced by
chars two later:
CGKQW

Original string: Hello, world!


Find string s that, when case is flipped gives target where vowels are replaced by
chars two later:
hGLLQ, WQRLD!

Original string: AEIOU


Find string s that, when case is flipped gives target where vowels are replaced by
chars two later:
cgkqw

# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Use the swapcase method to flip the case of characters and create a
translation mapping
# Replace vowels with characters two positions later in the alphabet
translation_mapping = {
ord('a'): ord('c'),
ord('e'): ord('g'),
ord('i'): ord('j'),
ord('o'): ord('q'),
ord('u'): ord('x'),
ord('A'): ord('C'),
ord('E'): ord('G'),
ord('I'): ord('J'),
ord('O'): ord('Q'),
ord('U'): ord('X'),
ord(' '): ord(' ')
}

# Apply the swapcase and translation mappings to the string


result = [Link]().translate(translation_mapping)

# Return the modified string


return result

# Assign a specific string 'strs' to the variable


strs = "Python"
# Print a message indicating the original string
print("Original string:", strs)
# Print a message indicating the operation to be performed
print("Find string s that, when case is flipped gives target where vowels are
replaced by chars two later:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "aeiou"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Find string s that, when case is flipped gives target where vowels are
replaced by chars two later:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "Hello, world!"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Find string s that, when case is flipped gives target where vowels are
replaced by chars two later:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "AEIOU"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Find string s that, when case is flipped gives target where vowels are
replaced by chars two later:")
# Print the result of the test function applied to 'strs'
print(test(strs))

Original string: Python


Find string s that, when case is flipped gives target where vowels are replaced by
chars two later:
pYTHQN

Original string: aeiou


Find string s that, when case is flipped gives target where vowels are replaced by
chars two later:
CGJQX

Original string: Hello, world!


Find string s that, when case is flipped gives target where vowels are replaced by
chars two later:
hGLLQ, WQRLD!

Original string: AEIOU


Find string s that, when case is flipped gives target where vowels are replaced by
chars two later:
cgjqx

===================================================================================
=================

Puzzle 41
Question: Last update on May 30 2025 11:48:50 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:50 (UTC/GMT +8 hours)

Sort Numbers from Words

Write a Python program to sort numbers based on strings.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find string s that, when case is flipped gives target where vowels are
replaced by chars two [Link]:Find the set of distinct characters in a string,
ignoring case.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: six one four one two three


Output:
one two three four six
Input: six one four three two nine eight
Output:
one two three four six eight nine

Input: nine eight seven six five four three two one
Output:
one two three four five six seven eight nine

# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Split the string of words into a list and filter only those present in the
predefined list
selected_numbers = [x for x in 'one two three four five six seven eight
nine'.split() if x in strs]

# Join the selected numbers into a string with spaces in between


result = ' '.join(selected_numbers)

# Return the final string


return result

# Assign a specific string 'strs' to the variable


strs = "six one four one two three"
# Print a message indicating the original string
print("Original string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "six one four three two nine eight"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "nine eight seven six five four three two one"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

Original string: six one four one two three


Sort numbers based on said strings:
one two three four six
Original string: six one four three two nine eight
Sort numbers based on said strings:
one two three four six eight nine

Original string: nine eight seven six five four three two one
Sort numbers based on said strings:
one two three four five six seven eight nine

# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Define a list of words representing numbers from zero to nine, repeated
multiple times
nums = 'zero zero zero zero zero zero zero zero zero zero one one one one one
one one one one one one one two two two two three three three three three four four
four four four four five five five five five five five six six six six six six six
seven seven seven seven seven seven seven seven eight eight eight eight eight eight
eight eight nine nine nine nine nine nine nine nine nine'.split()

# Split the input string into a list of words and map the indices of the
numbers to the corresponding words
sorted_indices = sorted([[Link](x) for x in [Link]()])

# Create a new list by selecting numbers from the predefined list based on the
sorted indices
sorted_numbers = [nums[i] for i in sorted_indices]

# Join the sorted numbers into a string with spaces in between and remove
trailing whitespaces
result = " ".join(sorted_numbers).rstrip()

# Return the final string


return result

# Assign a specific string 'strs' to the variable


strs = "six one four one two three"
# Print a message indicating the original string
print("Original string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "six one four three two nine eight"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "nine eight seven six five four three two zero one"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Sort numbers based on said strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

Original string: six one four one two three


Sort numbers based on said strings:
one one two three four six

Original string: six one four three two nine eight


Sort numbers based on said strings:
one two three four six eight nine

Original string: nine eight seven six five four three two zero one
Sort numbers based on said strings:
zero one two three four five six seven eight nine

===================================================================================
=================

Puzzle 42
Question: Last update on May 30 2025 11:48:50 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:50 (UTC/GMT +8 hours)

Distinct Characters in String

Write a Python program to find the set of distinct characters in a given string,
ignoring case.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:
Previous:Sort numbers based on [Link]:Find all words in a given string with n
consonants.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: HELLO
Output:
['h', 'o', 'l', 'e']

Input: HelLo
Output:
['h', 'o', 'l', 'e']

Input: Ignoring case


Output:
['s', 'n', 'c', 'o', 'e', 'i', 'r', 'g', 'a', ' ']

# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Convert the input string to lowercase, create a set of distinct characters,
and return it as a list
return [*set([Link]())]

# Assign a specific string 'strs' to the variable


strs = "HELLO"
# Print a message indicating the original string
print("Original string:", strs)
# Print a message indicating the operation to be performed
print("Set of distinct characters in the said string, ignoring case:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "HelLo"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Set of distinct characters in the said string, ignoring case:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "Ignoring case"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Set of distinct characters in the said string, ignoring case:")
# Print the result of the test function applied to 'strs'
print(test(strs))

Original string: HELLO


Set of distinct characters in the said string, ignoring case:
['o', 'e', 'l', 'h']

Original string: HelLo


Set of distinct characters in the said string, ignoring case:
['o', 'e', 'l', 'h']

Original string: Ignoring case


Set of distinct characters in the said string, ignoring case:
['o', ' ', 'i', 'r', 'e', 'g', 'a', 'n', 'c', 's']

# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Convert the input string to lowercase, create a set of distinct characters,
and convert it back to a list
return list(set([Link]()))

# Assign a specific string 'strs' to the variable


strs = "HELLO"
# Print a message indicating the original string
print("Original string:", strs)
# Print a message indicating the operation to be performed
print("Set of distinct characters in the said string, ignoring case:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "HelLo"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Set of distinct characters in the said string, ignoring case:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Assign another specific string 'strs' to the variable


strs = "Ignoring case"
# Print a message indicating the original string
print("\nOriginal string:", strs)
# Print a message indicating the operation to be performed
print("Set of distinct characters in the said string, ignoring case:")
# Print the result of the test function applied to 'strs'
print(test(strs))
Original string: HELLO
Set of distinct characters in the said string, ignoring case:
['h', 'o', 'l', 'e']

Original string: HelLo


Set of distinct characters in the said string, ignoring case:
['h', 'o', 'l', 'e']

Original string: Ignoring case


Set of distinct characters in the said string, ignoring case:
['s', 'n', 'c', 'o', 'e', 'i', 'r', 'g', 'a', ' ']

===================================================================================
=================

Puzzle 43
Question: Last update on May 30 2025 11:48:50 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:50 (UTC/GMT +8 hours)

Words with N Consonants

From [Link]:A consonant is a speech sound that is not a vowel. It also


refers to letters of the alphabet that represent those sounds: Z, B, T, G, and H
are all consonants. Consonants are all the non-vowel sounds, or their corresponding
letters: A, E, I, O, U and sometimes Y are not consonants.

Write a Python program to find all words in a given string with n consonants.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the set of distinct characters in a string, ignoring


[Link]:Determine which characters of a hexadecimal number correspond to prime
numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: this is our time


Output:
Number of consonants: 3
Words in the said string with 3 consonants:
['this']

Number of consonants: 2
Words in the said string with 2 consonants:
['time']

Number of consonants: 1
Words in the said string with 1 consonants:
['is', 'our']

# License: [Link]

# Define a function named 'test' that takes a string 'strs' and an integer 'n' as
inputs
def test(strs, n):
# List comprehension to filter words from the input string based on the number
of consonants
return [w for w in [Link]() if sum([c not in "aeiou" for c in [Link]()])
== n]

# Assign a specific string 'strs' to the variable


strs = "this is our time"
# Print a message indicating the original string
print("Original string:", strs)
# Assign a specific value 'n' to the variable
n = 3
# Print a message indicating the number of consonants to be checked
print("Number of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

# Assign another specific value 'n' to the variable


n = 2
# Print a message indicating the number of consonants to be checked
print("\nNumber of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

# Assign another specific value 'n' to the variable


n = 1
# Print a message indicating the number of consonants to be checked
print("\nNumber of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

Original string: this is our time


Number of consonants: 3
Words in the said string with 3 consonants:
['this']

Number of consonants: 2
Words in the said string with 2 consonants:
['time']

Number of consonants: 1
Words in the said string with 1 consonants:
['is', 'our']

# Define a function named 'test' that takes a string 'strs' and an integer 'n' as
inputs
def test(strs, n):
# List comprehension to filter words from the input string based on the number
of consonants
return [w for w in [Link]() if sum([Link]() not in "aeiou" for c in w) ==
n]

# Assign a specific string 'strs' to the variable


strs = "this is our time"
# Print a message indicating the original string
print("Original string:", strs)
# Assign a specific value 'n' to the variable
n = 3
# Print a message indicating the number of consonants to be checked
print("Number of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

# Assign another specific value 'n' to the variable


n = 2
# Print a message indicating the number of consonants to be checked
print("\nNumber of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

# Assign another specific value 'n' to the variable


n = 1
# Print a message indicating the number of consonants to be checked
print("\nNumber of consonants:", n)
# Print a message indicating the operation to be performed
print("Words in the said string with", n, "consonants:")
# Print the result of the test function applied to 'strs' and 'n'
print(test(strs, n))

Original string: this is our time


Number of consonants: 3
Words in the said string with 3 consonants:
['this']

Number of consonants: 2
Words in the said string with 2 consonants:
['time']

Number of consonants: 1
Words in the said string with 1 consonants:
['is', 'our']

===================================================================================
=================

Puzzle 44
Question: Last update on May 30 2025 11:48:51 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:51 (UTC/GMT +8 hours)

Hexadecimal Prime Characters

From Wikipedia:The hexadecimal numeral system, often shortened to "hex", is a


numeral system made up of 16 symbols (base 16). The standard numeral system is
called decimal (base 10) and uses ten symbols: 0,1,2,3,4,5,6,7,8,9. Hexadecimal
uses the decimal numbers and six extra symbols. There are no numerical symbols that
represent values greater than nine, so letters taken from the English alphabet are
used, specifically A, B, C, D, E and F. Hexadecimal A = decimal 10, and hexadecimal
F = decimal 15.A prime number (or a prime) is a natural number greater than 1 that
is not a product of two smaller natural numbers. A natural number greater than 1
that is not prime is called a composite number. For example, 5 is prime because the
only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. However, 4
is composite because it is a product (2 × 2) in which both numbers are smaller than
4. Primes are central in number theory because of the fundamental theorem of
arithmetic: every natural number greater than 1 is either a prime itself or can be
factorized as a product of primes that is unique up to their [Link] a Python
program to find which characters of a hexadecimal number correspond to prime
numbers.

Visual Presentation:

Sample Solution:
Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find all words in a given string with n [Link]:Find all even


palindromes up to n.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: 123ABCD
Output:
[False, True, True, False, True, False, True]

Input: 123456
Output:
[False, True, True, False, True, False]

Input: FACE
Output:
[False, False, False, False]

# Define a function named 'test' that takes a hexadecimal number 'hn' as input
def test(hn):
# List comprehension to check if each character in 'hn' corresponds to a prime
number or 'B' or 'D'
return [c in "2357BD" for c in hn]

# Assign a specific hexadecimal number 'hn' to the variable


hn = "123ABCD"
# Print a message indicating the original hexadecimal number
print("Original hexadecimal number:", hn)
# Print a message indicating the operation to be performed
print("Characters of the said hexadecimal number correspond to prime numbers:")
# Print the result of the test function applied to 'hn'
print(test(hn))

# Assign another specific hexadecimal number 'hn' to the variable


hn = "123456"
# Print a message indicating the original hexadecimal number
print("\nOriginal hexadecimal number:", hn)
# Print a message indicating the operation to be performed
print("Characters of the said hexadecimal number correspond to prime numbers:")
# Print the result of the test function applied to 'hn'
print(test(hn))

# Assign another specific hexadecimal number 'hn' to the variable


hn = "FACE"
# Print a message indicating the original hexadecimal number
print("\nOriginal hexadecimal number:", hn)
# Print a message indicating the operation to be performed
print("Characters of the said hexadecimal number correspond to prime numbers:")
# Print the result of the test function applied to 'hn'
print(test(hn))

Original hexadecimal number: 123ABCD


Characters of the said hexadecimal number correspond to prime numbers:
[False, True, True, False, True, False, True]

Original hexadecimal number: 123456


Characters of the said hexadecimal number correspond to prime numbers:
[False, True, True, False, True, False]

Original hexadecimal number: FACE


Characters of the said hexadecimal number correspond to prime numbers:
[False, False, False, False]

===================================================================================
=================

Puzzle 45
Question: Last update on May 30 2025 11:48:51 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:51 (UTC/GMT +8 hours)

Find Even Palindromes

From Wikipedia,A palindromic number (also known as a numeral palindrome or a


numeric palindrome) is a number (such as 16461) that remains the same when its
digits are reversed. In other words, it has reflectional symmetry across a vertical
axis. The term palindromic is derived from palindrome, which refers to a word (such
as rotor or racecar) whose spelling is unchanged when its letters are reversed. The
first 30 palindromic numbers (in decimal) are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11,
22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191,
202, ...

Write a Python program to find all even palindromes up to n.

Sample Solution:

Python Code:

Sample Output:

Flowchart:
For more Practice: Solve these Related Problems:

Go to:

Previous:Determine which characters of a hexadecimal number correspond to prime


[Link]:Find the minimum even value and its index from a given array of
numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Output:
Even palindromes up to 50 -
[0, 2, 4, 6, 8, 22, 44]

Even palindromes up to 100 -


[0, 2, 4, 6, 8, 22, 44, 66, 88]

Even palindromes up to 500 -


[0, 2, 4, 6, 8, 22, 44, 66, 88, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292,
404, 414, 424, 434, 444, 454, 464, 474, 484, 494]

Even palindromes up to 2000 -


[0, 2, 4, 6, 8, 22, 44, 66, 88, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292,
404, 414, 424, 434, 444, 454, 464, 474, 484, 494, 606, 616, 626, 636, 646, 656,
666, 676, 686, 696, 808, 818, 828, 838, 848, 858, 868, 878, 888, 898]

# Define a function named 'test' that takes a positive integer 'n' as input
def test(n):
# List comprehension to find even palindromic numbers up to 'n'
return [i for i in range(0, n, 2) if str(i) == str(i)[::-1]]

# Assign a specific value to the variable 'n'


n = 50
# Print a message indicating the range of even palindromes to be found
print("\nEven palindromes up to", n, "-")
# Print the result of the test function applied to 'n'
print(test(n))

# Assign another specific value to the variable 'n'


n = 100
# Print a message indicating the range of even palindromes to be found
print("\nEven palindromes up to", n, "-")
# Print the result of the test function applied to 'n'
print(test(n))
# Assign another specific value to the variable 'n'
n = 500
# Print a message indicating the range of even palindromes to be found
print("\nEven palindromes up to", n, "-")
# Print the result of the test function applied to 'n'
print(test(n))

# Assign another specific value to the variable 'n'


n = 2000
# Print a message indicating the range of even palindromes to be found
print("\nEven palindromes up to", n, "-")
# Print the result of the test function applied to 'n'
print(test(n))

Even palindromes up to 50 -
[0, 2, 4, 6, 8, 22, 44]

Even palindromes up to 100 -


[0, 2, 4, 6, 8, 22, 44, 66, 88]

Even palindromes up to 500 -


[0, 2, 4, 6, 8, 22, 44, 66, 88, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292,
404, 414, 424, 434, 444, 454, 464, 474, 484, 494]

Even palindromes up to 2000 -


[0, 2, 4, 6, 8, 22, 44, 66, 88, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292,
404, 414, 424, 434, 444, 454, 464, 474, 484, 494, 606, 616, 626, 636, 646, 656,
666, 676, 686, 696, 808, 818, 828, 838, 848, 858, 868, 878, 888, 898]

===================================================================================
=================

Puzzle 46
Question: Last update on May 30 2025 11:48:51 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:51 (UTC/GMT +8 hours)

Minimum Even Value and Index

Given an array of numbers representing a branch on a binary tree, write a Python


program to find the minimum even value and its index. In the case of a tie, return
the smallest index. If there are no even numbers, the answer is [].

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:
For more Practice: Solve these Related Problems:

Go to:

Previous:Find all even palindromes up to [Link]:Filter for the numbers in a list


whose sum of digits is >0, where the first digit can be negative..

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 9, 4, 6, 10, 11, 14, 8]
Output:
Minimum even value and its index of the said array of numbers:
[4, 2]
Input:
[1, 7, 4, 4, 9, 2]
Output:
Minimum even value and its index of the said array of numbers:
[2, 5]
Input:
[1, 7, 7, 5, 9]
Output:
Minimum even value and its index of the said array of numbers:
[]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Check if any element in 'nums' is even
if any(n % 2 == 0 for n in nums):
# If there is an even number, find the minimum even value and its index
return min([v, i] for i, v in enumerate(nums) if v % 2 == 0)
else:
# If there are no even numbers, return an empty list
return []

# Assign a specific list of numbers to the variable 'nums'


nums = [1, 9, 4, 6, 10, 11, 14, 8]
# Print a message indicating the original list of numbers
print("Original list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the minimum even value and its index
print("Minimum even value and its index of the said array of numbers:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Assign another specific list of numbers to the variable 'nums'
nums = [1, 7, 4, 4, 9, 2]
# Print a message indicating the original list of numbers
print("\nOriginal list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the minimum even value and its index
print("Minimum even value and its index of the said array of numbers:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of numbers to the variable 'nums'


nums = [1, 7, 7, 5, 9]
# Print a message indicating the original list of numbers
print("\nOriginal list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the minimum even value and its index
print("Minimum even value and its index of the said array of numbers:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list:
[1, 9, 4, 6, 10, 11, 14, 8]
Minimum even value and its index of the said array of numbers:
[4, 2]
Original list:
[1, 7, 4, 4, 9, 2]
Minimum even value and its index of the said array of numbers:
[2, 5]
Original list:
[1, 7, 7, 5, 9]
Minimum even value and its index of the said array of numbers:
[]

===================================================================================
=================

Puzzle 47
Question: Last update on May 30 2025 11:48:52 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:52 (UTC/GMT +8 hours)

Filter by Sum of Digits

Write a Python program to filter for numbers in a given list whose sum of digits is
> 0, where the first digit can be negative.

Sample Solution:

Python Code:

Sample Output:

Flowchart:
For more Practice: Solve these Related Problems:

Go to:

Previous:Find the minimum even value and its index from a given array of
[Link]:Find the indices of two entries that show that the list is not in
increasing order.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[11, -6, -103, -200]
Output:
[11, -103]

Input:
[1, 7, -4, 4, -9, 2]
Output:
[1, 7, 4, 2]

Input:
[10, -11, -71, -13, 14, -32]
Output:
[10, -13, 14]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Use a list comprehension to filter numbers based on the sum of their digits
return [n for n in nums if int(str(n)[:2]) + sum(map(int, str(n)[2:])) > 0]

# Assign a specific list of numbers to the variable 'nums'


nums = [11, -6, -103, -200]
# Print a message indicating the original list of numbers
print("Original list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the numbers whose sum of digits is >0, allowing
negative first digits
print("Find the numbers in the said list whose sum of digits is >0, where the first
digit can be negative:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of numbers to the variable 'nums'


nums = [1, 7, -4, 4, -9, 2]
# Print a message indicating the original list of numbers
print("\nOriginal list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the numbers whose sum of digits is >0, allowing
negative first digits
print("Find the numbers in the said list whose sum of digits is >0, where the first
digit can be negative:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of numbers to the variable 'nums'


nums = [10, -11, -71, -13, 14, -32]
# Print a message indicating the original list of numbers
print("\nOriginal list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the numbers whose sum of digits is >0, allowing
negative first digits
print("Find the numbers in the said list whose sum of digits is >0, where the first
digit can be negative:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list:
[11, -6, -103, -200]
Find the numbers in the said list whose sum of digits is >0, where the first digit
can be negative:
[11, -103]

Original list:
[1, 7, -4, 4, -9, 2]
Find the numbers in the said list whose sum of digits is >0, where the first digit
can be negative:
[1, 7, 4, 2]

Original list:
[10, -11, -71, -13, 14, -32]
Find the numbers in the said list whose sum of digits is >0, where the first digit
can be negative:
[10, -13, 14]

===================================================================================
=================

Puzzle 48
Question: Last update on May 30 2025 11:48:52 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:52 (UTC/GMT +8 hours)

Find Non-Increasing Indices

Write a Python program to find the indices of two entries that show that the list
is not in increasing order. If there are no violations (they are increasing),
return an empty list.
Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Filter for the numbers in a list whose sum of digits is >0, where the
first digit can be [Link]:Find the h-index, the largest positive number h
such that h occurs in the sequence at least h times.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 2, 3, 0, 4, 5, 6]
Output:
[2, 3]

Input:
[1, 2, 3, 4, 5, 6]
Output:
[]

Input:
[1, 2, 3, 4, 6, 5, 7]
Output:
[4, 5]

Input:
[-3, -2, -3, 0, 2, 3, 4]
Output:
[1, 2]
# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Iterate over the indices of the list, except the last element
for i in range(len(nums) - 1):
# Check if the current element is greater than or equal to the next element
if nums[i] >= nums[i + 1]:
# Return the indices of the two entries that violate the increasing
order
return [i, i + 1]
# If no violation is found, return an empty list
return []

# Assign a specific list of numbers to the variable 'nums'


nums = [1,2,3,0,4,5,6]
# Print a message indicating the original list of numbers
print("Original list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the indices of two entries that violate the increasing
order
print("Indices of two entries that show that the list is not in increasing order:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of numbers to the variable 'nums'


nums = [1,2,3,4,5,6]
# Print a message indicating the original list of numbers
print("\nOriginal list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the indices of two entries that violate the increasing
order
print("Indices of two entries that show that the list is not in increasing order:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of numbers to the variable 'nums'


nums = [1,2,3,4,6,5,7]
# Print a message indicating the original list of numbers
print("\nOriginal list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the indices of two entries that violate the increasing
order
print("Indices of two entries that show that the list is not in increasing order:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of numbers to the variable 'nums'


nums = [-3,-2,-3,0,2,3,4]
# Print a message indicating the original list of numbers
print("\nOriginal list:")
# Print the original list of numbers
print(nums)
# Print a message indicating the indices of two entries that violate the increasing
order
print("Indices of two entries that show that the list is not in increasing order:")
# Print the result of the test function applied to 'nums'
print(test(nums))
Original list:
[1, 2, 3, 0, 4, 5, 6]
Indices of two entries that show that the list is not in increasing order:
[2, 3]

Original list:
[1, 2, 3, 4, 5, 6]
Indices of two entries that show that the list is not in increasing order:
[]

Original list:
[1, 2, 3, 4, 6, 5, 7]
Indices of two entries that show that the list is not in increasing order:
[4, 5]

Original list:
[-3, -2, -3, 0, 2, 3, 4]
Indices of two entries that show that the list is not in increasing order:
[1, 2]

===================================================================================
=================

Puzzle 49
Question: Last update on May 30 2025 11:48:53 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:53 (UTC/GMT +8 hours)

H-Index Finder

Write a Python program to find the h-index, the largest positive number h such that
h occurs in the sequence at least h times. If there is no such positive number
return h = -1.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the indices of two entries that show that the list is not in
increasing [Link]:Find the even-length words and sort them by length.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 2, 2, 3, 3, 4, 4, 4, 4]
Output:
4

Input:
[1, 2, 2, 3, 4, 5, 6]
Output:
2

Input:
[3, 1, 4, 17, 5, 17, 2, 1, 41, 32, 2, 5, 5, 5, 5]
Output:
5

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Calculate the h-index, which is the largest positive number h
# such that h occurs in the sequence at least h times
return max([-1] + [i for i in nums if i > 0 and [Link](i) >= i])

# Assign a specific list of numbers to the variable 'nums'


nums = [1, 2, 2, 3, 3, 4, 4, 4, 4]
# Print a message indicating the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the h-index calculation
print("h-index, the largest positive number h such that h occurs in the said
sequence at least h times:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Assign another specific list of numbers to the variable 'nums'


nums = [1,2,2,3,4,5,6]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the h-index calculation
print("h-index, the largest positive number h such that h occurs in the said
sequence at least h times:")
# Print the result of the test function applied to 'nums'
print(test(nums))
# Assign another specific list of numbers to the variable 'nums'
nums = [3, 1, 4, 17, 5, 17, 2, 1, 41, 32, 2, 5, 5, 5, 5]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the h-index calculation
print("h-index, the largest positive number h such that h occurs in the said
sequence at least h times:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of numbers:


[1, 2, 2, 3, 3, 4, 4, 4, 4]
h-index, the largest positive number h such that h occurs in the said sequence at
least h times:
4

Original list of numbers:


[1, 2, 2, 3, 4, 5, 6]
h-index, the largest positive number h such that h occurs in the said sequence at
least h times:
2

Original list of numbers:


[3, 1, 4, 17, 5, 17, 2, 1, 41, 32, 2, 5, 5, 5, 5]
h-index, the largest positive number h such that h occurs in the said sequence at
least h times:
5

===================================================================================
=================

Puzzle 50
Question: Last update on May 30 2025 11:48:53 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:53 (UTC/GMT +8 hours)

Sort Even-Length Words

Write a Python program to find even-length words from a given list of words and
sort them by length.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:
For more Practice: Solve these Related Problems:

Go to:

Previous:Find the h-index, the largest positive number h such that h occurs in the
sequence at least h [Link]:Find the first n Fibonacci numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['Red', 'Black', 'White', 'Green', 'Pink', 'Orange']
Output:
['Pink', 'Orange']

Input:
['The', 'worm', 'ate', 'a', 'bird', 'imagine', 'that', '!', 'Absurd', '!!']
Output:
['!!', 'bird', 'that', 'worm', 'Absurd']

# Define a function named 'test' that takes a list of words 'words' as input
def test(words):
# Use a list comprehension to filter words with even lengths
# Sort the filtered words first by length and then lexicographically
return sorted([w for w in words if len(w) % 2 == 0], key=lambda w: (len(w), w))

# Assign a specific list of words to the variable 'words'


words = ["Red", "Black", "White", "Green", "Pink", "Orange"]
# Print a message indicating the original list of words
print("Original list of words:")
# Print the original list of words
print(words)
# Print a message indicating the even-length words sorting
print("Find the even-length words and sort them by length in the said list of
words:")
# Print the result of the test function applied to 'words'
print(test(words))

# Assign another specific list of words to the variable 'words'


words = ['The', 'worm', 'ate', 'a', 'bird', 'imagine', 'that', '!', 'Absurd', '!!']
# Print a message indicating the original list of words
print("\nOriginal list of words:")
# Print the original list of words
print(words)
# Print a message indicating the even-length words sorting
print("Find the even-length words and sort them by length in the said list of
words:")
# Print the result of the test function applied to 'words'
print(test(words))

Original list of words:


['Red', 'Black', 'White', 'Green', 'Pink', 'Orange']
Find the even-length words and sort them by length in the said list of words:
['Pink', 'Orange']

Original list of words:


['The', 'worm', 'ate', 'a', 'bird', 'imagine', 'that', '!', 'Absurd', '!!']
Find the even-length words and sort them by length in the said list of words:
['!!', 'bird', 'that', 'worm', 'Absurd']

===================================================================================
=================

Puzzle 51
Question: Last update on May 30 2025 11:48:54 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:54 (UTC/GMT +8 hours)

Product of Units Digits

Write a Python program to find the product of the units digits in the numbers in a
given list.

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the even-length words and sort them by [Link]:Reverse the case
of all strings. For those strings, which contain no letters, reverse the strings.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: 10
Output:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Input: 15
Output:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
Input: 50
Output:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181,
6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040,
1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169,
63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170,
1836311903, 2971215073, 4807526976, 7778742049, 12586269025]

# Define a function named 'test' that generates Fibonacci numbers


def test(n):
# Initialize a list 'a' with the first two Fibonacci numbers
a = [1, 1]
# Continue adding Fibonacci numbers to the list until it reaches the desired
count 'n'
while len(a) < n:
# Append the sum of the last two numbers in the list to generate the next
Fibonacci number
a += [sum(a[-2:])]
# Return the first 'n' Fibonacci numbers
return a[:n]

# Set the value of 'n' to 10


n = 10
# Print a message indicating the task and the value of 'n'
print("\nFind the first",n,"Fibonacci numbers:")
# Print the result of the test function applied to 'n'
print(test(n))

# Set the value of 'n' to 15


n = 15
# Print a message indicating the task and the value of 'n'
print("\nFind the first",n,"Fibonacci numbers:")
# Print the result of the test function applied to 'n'
print(test(n))

# Set the value of 'n' to 50


n = 50
# Print a message indicating the task and the value of 'n'
print("\nFind the first",n,"Fibonacci numbers:")
# Print the result of the test function applied to 'n'
print(test(n))
Find the first 10 Fibonacci numbers:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Find the first 15 Fibonacci numbers:


[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]

Find the first 50 Fibonacci numbers:


[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181,
6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040,
1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169,
63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170,
1836311903, 2971215073, 4807526976, 7778742049, 12586269025]

# Define a function named 'test' that generates Fibonacci numbers


def test(n):
# Initialize a list 'result' with the first two Fibonacci numbers
result = [1, 1]
# Continue adding Fibonacci numbers to the list until it reaches the desired
count 'n'
while len(result) < n:
# Append the sum of the last two numbers in the list to generate the next
Fibonacci number
[Link](result[-1] + result[-2])
# Return the first 'n' Fibonacci numbers
return result

# Set the value of 'n' to 10


n = 10
# Print a message indicating the task and the value of 'n'
print("Find the first", n, "Fibonacci numbers:")
# Print the result of the test function applied to 'n'
print(test(n))

# Set the value of 'n' to 15


n = 15
# Print a message indicating the task and the value of 'n'
print("\nFind the first", n, "Fibonacci numbers:")
# Print the result of the test function applied to 'n'
print(test(n))

# Set the value of 'n' to 50


n = 50
# Print a message indicating the task and the value of 'n'
print("\nFind the first", n, "Fibonacci numbers:")
# Print the result of the test function applied to 'n'
print(test(n))

Find the first 10 Fibonacci numbers:


[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Find the first 15 Fibonacci numbers:


[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
Find the first 50 Fibonacci numbers:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181,
6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040,
1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169,
63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170,
1836311903, 2971215073, 4807526976, 7778742049, 12586269025]

===================================================================================
=================

Puzzle 52
Question: Last update on May 30 2025 11:48:54 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:54 (UTC/GMT +8 hours)

Reverse Case or Reverse Strings

Write a Python program to reverse the case of all strings. For those strings, which
contain no letters, reverse the strings.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the first n Fibonacci [Link]:Find the product of the units


digits in the numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo', 'unique']
Output:
['CAT', 'CATATATATCTSA', 'ABCDEFHIJKLMNOP', '521581932952421', '', 'FOO', 'UNIQUE']
Input:
['Green', 'Red', 'Orange', 'Yellow', '', 'White']
Output:
['gREEN', 'rED', 'oRANGE', 'yELLOW', '', 'wHITE']

Input:
['Hello', '!@#', '!@#$', '123#@!']
Output:
['hELLO', '!@#', '!@#$', '123#@!']

# Define a function named 'test' that takes a list of strings as input and returns
a modified list
def test(strs: list[str]) -> list[str]:
# Use a list comprehension to create a new list with modified strings
# If a string contains any alphabetic characters, reverse its case; otherwise,
reverse the string
return [[Link]() if any([Link]() for c in s) else s[::-1] for s in strs]

# Create a list of strings named 'strs'


strs = ['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo',
'unique']
# Print a message indicating the task and the original list
print("Original list:")
# Print the original list of strings
print(strs)
# Print a message indicating the task and the result of the test function applied
to 'strs'
print("Reverse the case of all strings. For those strings which contain no letters,
reverse the strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Create another list of strings named 'strs'


strs = ['Green', 'Red', 'Orange', 'Yellow', '', 'White']
# Print a message indicating the task and the original list
print("\nOriginal list:")
# Print the original list of strings
print(strs)
# Print a message indicating the task and the result of the test function applied
to 'strs'
print("Reverse the case of all strings. For those strings which contain no letters,
reverse the strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Create yet another list of strings named 'strs'


strs = ["Hello", "!@#", "!@#$", "123#@!"]
# Print a message indicating the task and the original list
print("\nOriginal list:")
# Print the original list of strings
print(strs)
# Print a message indicating the task and the result of the test function applied
to 'strs'
print("Reverse the case of all strings. For those strings which contain no letters,
reverse the strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))
Original list:
['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo', 'unique']
Reverse the case of all strings. For those strings which contain no letters,
reverse the strings:
['CAT', 'CATATATATCTSA', 'ABCDEFHIJKLMNOP', '521581932952421', '', 'FOO', 'UNIQUE']

Original list:
['Green', 'Red', 'Orange', 'Yellow', '', 'White']
Reverse the case of all strings. For those strings which contain no letters,
reverse the strings:
['gREEN', 'rED', 'oRANGE', 'yELLOW', '', 'wHITE']

Original list:
['Hello', '!@#', '!@#$', '123#@!']
Reverse the case of all strings. For those strings which contain no letters,
reverse the strings:
['hELLO', '#@!', '$#@!', '!@#321']

===================================================================================
=================

Puzzle 53
Question: Last update on May 30 2025 11:48:55 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:55 (UTC/GMT +8 hours)

Product of Units in List

Write a Python program to find the product of the units digits in the numbers in a
given list.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:


Go to:

Previous:Reverse the case of all strings. For those strings, which contain no
letters, reverse the [Link]:Remove duplicates from a list of integers,
preserving order.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[12, 23]
Output:
6

Input:
[12, 23, 43]
Output:
18

Input:
[113, 234]
Output:
12

Input:
[1002, 2005]
Output:
10

# Define a function named 'test' that takes a list of numbers as input and
calculates the product of their units digits
def test(nums):
# Use list comprehension to extract the units digits of each number and join
them as a string
# Evaluate the resulting expression to get the product of the units digits
return eval('*'.join([str(x % 10) for x in nums]))

# Create a list of numbers named 'nums'


nums = [12, 23]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create another list of numbers named 'nums'


nums = [12, 23, 43]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create yet another list of numbers named 'nums'


nums = [113, 234]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create one more list of numbers named 'nums'


nums = [1002, 2005]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of numbers:


[12, 23]
Product of the units digits in the numbers of the said:
6

Original list of numbers:


[12, 23, 43]
Product of the units digits in the numbers of the said:
18

Original list of numbers:


[113, 234]
Product of the units digits in the numbers of the said:
12

Original list of numbers:


[1002, 2005]
Product of the units digits in the numbers of the said:
10

# Define a function named 'test' that takes a list of numbers as input and
calculates the product of the absolute values of their units digits
def test(nums):
# Initialize a variable 'prod' to 1 to store the product of units digits
prod = 1
# Iterate through each number in the list 'nums'
for n in nums:
# Update 'prod' by multiplying it with the absolute value of the units
digit of the current number
prod *= abs(n % 10)
# Return the final product of the units digits
return prod

# Create a list of numbers named 'nums'


nums = [12, 23]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create another list of numbers named 'nums'


nums = [12, 23, 43]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create yet another list of numbers named 'nums'


nums = [113, 234]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create one more list of numbers named 'nums'


nums = [1002, 2005]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Product of the units digits in the numbers of the said:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of numbers:


[12, 23]
Product of the units digits in the numbers of the said:
6

Original list of numbers:


[12, 23, 43]
Product of the units digits in the numbers of the said:
18

Original list of numbers:


[113, 234]
Product of the units digits in the numbers of the said:
12

Original list of numbers:


[1002, 2005]
Product of the units digits in the numbers of the said:
10

===================================================================================
=================

Puzzle 54
Question: Last update on May 30 2025 11:48:55 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:55 (UTC/GMT +8 hours)

Remove Duplicates, Preserve Order

Write a Python program to remove duplicates from a list of integers, preserving


order.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:
Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the product of the units digits in the [Link]:Find the numbers
that are greater than 10 and have odd first and last digits.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 3, 4, 10, 4, 1, 43]
Output:
[1, 3, 4, 10, 43]

Input:
[10, 11, 13, 23, 11, 25, 23, 76, 99]
Output:
[10, 11, 13, 23, 25, 76, 99]

# Define a function named 'test' that takes a list of numbers as input and removes
duplicates while preserving the original order
def test(nums):
# Use a dictionary to convert the list to a set, effectively removing
duplicates, and then convert it back to a list
return list([Link](nums))

# Create a list of numbers named 'nums'


nums = [1, 3, 4, 10, 4, 1, 43]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Remove duplicates from the said list of integers, preserving order:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create another list of numbers named 'nums'


nums = [10, 11, 13, 23, 11, 25, 23, 76, 99]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Remove duplicates from the said list of integers, preserving order:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of numbers:


[1, 3, 4, 10, 4, 1, 43]
Remove duplicates from the said list of integers, preserving order:
[1, 3, 4, 10, 43]

Original list of numbers:


[10, 11, 13, 23, 11, 25, 23, 76, 99]
Remove duplicates from the said list of integers, preserving order:
[10, 11, 13, 23, 25, 76, 99]

# Define a function named 'test' that takes a list of numbers as input and removes
duplicates while preserving the original order
def test(nums):
# Initialize an empty list named 'result' to store unique elements
result = []
# Iterate through each element 'x' in the input list 'nums'
for x in nums:
# Check if 'x' is not already present in the 'result' list
if x not in result:
# If not present, append 'x' to the 'result' list
[Link](x)
# Return the final list with duplicates removed
return result

# Create a list of numbers named 'nums'


nums = [1, 3, 4, 10, 4, 1, 43]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Remove duplicates from the said list of integers, preserving order:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create another list of numbers named 'nums'


nums = [10, 11, 13, 23, 11, 25, 23, 76, 99]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Remove duplicates from the said list of integers, preserving order:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of numbers:


[1, 3, 4, 10, 4, 1, 43]
Remove duplicates from the said list of integers, preserving order:
[1, 3, 4, 10, 43]

Original list of numbers:


[10, 11, 13, 23, 11, 25, 23, 76, 99]
Remove duplicates from the said list of integers, preserving order:
[10, 11, 13, 23, 25, 76, 99]

===================================================================================
=================

Puzzle 55
Question: Last update on May 30 2025 11:48:56 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:56 (UTC/GMT +8 hours)

Odd First and Last Digits

Write a Python program to find numbers that are greater than 10 and have odd first
and last digits.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Remove duplicates from a list of integers, preserving [Link]:Find an


integer exponent x such that a^x = n.
Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 3, 79, 10, 4, 1, 39, 62]
Output:
[79, 39]

Input:
[11, 31, 77, 93, 48, 1, 57]
Output:
[11, 31, 77, 93, 57]

# Define a function named 'test' that takes a list of numbers as input


# The function filters numbers greater than 10 with odd first and last digits
def test(nums):
# Use a list comprehension to filter numbers based on specified conditions
return [x for x in nums if x > 10 and x % 10 % 2 and int(str(x)[0]) % 2]

# Create a list of numbers named 'nums'


nums = [1, 3, 79, 10, 4, 1, 39]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Numbers of the said array that are greater than 10 and have odd first and
last digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create another list of numbers named 'nums'


nums = [11, 31, 77, 93, 48, 1, 57]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Numbers of the said array that are greater than 10 and have odd first and
last digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))
Original list of numbers:
[1, 3, 79, 10, 4, 1, 39]
Numbers of the said array that are greater than 10 and have odd first and last
digits:
[79, 39]

Original list of numbers:


[11, 31, 77, 93, 48, 1, 57]
Numbers of the said array that are greater than 10 and have odd first and last
digits:
[11, 31, 77, 93, 57]

# Define a function named 'test' that takes a list of numbers as input


# The function filters numbers greater than 10 with the product of their first and
last digits being odd
def test(nums):
# Use a list comprehension to filter numbers based on specified conditions
return [n for n in nums if n > 10 and (int(str(n)[0]) * int(str(n)[-1])) % 2]

# Create a list of numbers named 'nums'


nums = [1, 3, 79, 10, 4, 1, 39]
# Print a message indicating the task and the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Numbers of the said array that are greater than 10 and have odd first and
last digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))

# Create another list of numbers named 'nums'


nums = [11, 31, 77, 93, 48, 1, 57]
# Print a message indicating the task and the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function applied
to 'nums'
print("Numbers of the said array that are greater than 10 and have odd first and
last digits:")
# Print the result of the test function applied to 'nums'
print(test(nums))

Original list of numbers:


[1, 3, 79, 10, 4, 1, 39]
Numbers of the said array that are greater than 10 and have odd first and last
digits:
[79, 39]

Original list of numbers:


[11, 31, 77, 93, 48, 1, 57]
Numbers of the said array that are greater than 10 and have odd first and last
digits:
[11, 31, 77, 93, 57]

===================================================================================
=================

Puzzle 56
Question: Last update on May 30 2025 11:48:56 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:56 (UTC/GMT +8 hours)

Find Integer Exponent

Write a Python program to find an integer exponent x such that a^x = n.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the numbers that are greater than 10 and have odd first and last
[Link]:Sum of the magnitudes of the elements in the array with product signs.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
a = 2 : n = 1024
Output:
10
Input:
a = 3 : n = 81

Output:
4
Input:
a = 3 : n =
1290070078170102666248196035845070394933441741644993085810116441344597492642263849

Output:
170

# Define a function named 'test' that takes two parameters, n and a


# The function finds an integer exponent x such that a^x = n
def test(n, a):
# Initialize variables m and x
m = 1
x = 0

# Use a while loop to find the exponent x


while m != n:
# Increment x and update m by multiplying it with a
x += 1
m *= a

# Return the found exponent x


return x

# Set values for variables a and n


a = 2
n = 1024
# Print a message indicating the values of a and n
print("a = ", a, ": n = ", n)
# Print a message indicating the task and the result of the test function
print("Find an integer exponent x such that a^x = n:")
# Print the result of the test function applied to the given values of a and n
print(test(n, a))

# Set new values for variables a and n


a = 3
n = 81
# Print a message indicating the values of a and n
print("\na = ", a, ": n = ", n)
# Print a message indicating the task and the result of the test function
print("Find an integer exponent x such that a^x = n:")
# Print the result of the test function applied to the new values of a and n
print(test(n, a))

# Set new values for variables a and n (large integer)


a = 3
n =
1290070078170102666248196035845070394933441741644993085810116441344597492642263849
# Print a message indicating the values of a and n
print("\na = ", a, ": n = ", n)
# Print a message indicating the task and the result of the test function
print("Find an integer exponent x such that a^x = n:")
# Print the result of the test function applied to the new values of a and n
print(test(n, a))

a = 2 : n = 1024
Find an integer exponent x such that a^x = n:
10
a = 3 : n = 81

Find an integer exponent x such that a^x = n:


4
a = 3 : n =
1290070078170102666248196035845070394933441741644993085810116441344597492642263849

Find an integer exponent x such that a^x = n:


170

===================================================================================
=================

Puzzle 57
Question: Last update on May 30 2025 11:48:57 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:57 (UTC/GMT +8 hours)

Signed Sum of Magnitudes

Write a Python program to find the sum of the magnitudes of the elements in the
array. This sum should have a sign that is equal to the product of the signs of the
entries.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find an integer exponent x such that a^x = [Link]:Biggest even number


between two numbers inclusive.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 3, -2]
Output:
-6

Input:
[1, -3, 3]
Output:
-7

Input:
[10, 32, 3]
Output:
45

Input:
[-25, -12, -23]
Output:
-60

# Define a function named 'test' that takes a list of numbers as a parameter


def test(nums):
# Calculate the total sum of the magnitudes of the elements in the array
tot = sum(abs(i) for i in nums)

# Check if all elements in the array are non-zero


if all(nums):
# Return the total sum with a positive or negative sign based on the
product of the signs of the entries
return tot if sum(i < 0 for i in nums) % 2 == 0 else -tot
# If there is a zero element in the array, return 0
return 0

# Set a list of numbers as the input for the test function


nums = [1, 3, -2]
# Print a message indicating the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function
print("Sum of the magnitudes of the elements in the array with a sign that is equal
to the product of the signs of the entries:")
# Print the result of the test function applied to the given list of numbers
print(test(nums))

# Set a new list of numbers as the input for the test function
nums = [1, -3, 3]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function
print("Sum of the magnitudes of the elements in the array with a sign that is equal
to the product of the signs of the entries:")
# Print the result of the test function applied to the new list of numbers
print(test(nums))

# Set another new list of numbers as the input for the test function
nums = [10, 32, 3]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function
print("Sum of the magnitudes of the elements in the array with a sign that is equal
to the product of the signs of the entries:")
# Print the result of the test function applied to the new list of numbers
print(test(nums))

# Set yet another new list of numbers as the input for the test function
nums = [-25, -12, -23]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the task and the result of the test function
print("Sum of the magnitudes of the elements in the array with a sign that is equal
to the product of the signs of the entries:")
# Print the result of the test function applied to the new list of numbers
print(test(nums))

Original list of numbers:


[1, 3, -2]
Sum of the magnitudes of the elements in the array with a sign that is equal to the
product of the signs of the entries:
-6

Original list of numbers:


[1, -3, 3]
Sum of the magnitudes of the elements in the array with a sign that is equal to the
product of the signs of the entries:
-7

Original list of numbers:


[10, 32, 3]
Sum of the magnitudes of the elements in the array with a sign that is equal to the
product of the signs of the entries:
45

Original list of numbers:


[-25, -12, -23]
Sum of the magnitudes of the elements in the array with a sign that is equal to the
product of the signs of the entries:
-60

===================================================================================
=================

Puzzle 58
Question: Last update on May 30 2025 11:48:57 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:57 (UTC/GMT +8 hours)

Largest Even Number in Range


Write a Python program to find the biggest even number between two numbers
inclusive.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Sum of the magnitudes of the elements in the array with product


[Link]:A valid filename.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
m = 12
n = 51
Output:
50
Input:
m = 1
n = 79
Output:
78
Input:
m = 47
n = 53
Output:
52
Input:
m = 100
n = 200
Output:
200

# Define a function named 'test' that takes two parameters, m and n


def test(m, n):
# Check if m is greater than n or if both m and n are odd
if m > n or (m == n and m % 2 == 1):
# Return -1 if the condition is met
return -1
# Return n if n is even, otherwise return n - 1
return n if n % 2 == 0 else n - 1

# Set values for m and n


m = 12
n = 51
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the given values of m and n
print(test(m, n))

# Set new values for m and n


m = 1
n = 79
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the new values of m and n
print(test(m, n))

# Set another set of values for m and n


m = 47
n = 53
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the new values of m and n
print(test(m, n))

# Set additional values for m and n


m = 100
n = 200
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the additional values of m and n
print(test(m, n))

Biggest even number between 12 and 51


50
Biggest even number between 1 and 79
78

Biggest even number between 47 and 53


52

Biggest even number between 100 and 200


200

# Define a function named 'test' that takes two parameters, p and q


def test(p, q):
# Initialize a variable 'n' with the value of 'q'
n = q
# Continue looping while the least significant bit of 'n' is 1 (n is odd)
while (n & 1) == 1:
# Decrement 'n' by 1
n -= 1
# Check if 'n' has become less than 'p'
if n < p:
# Return -1 if the condition is met
return -1
# Return the final value of 'n'
return n

# Set values for m and n


m = 12
n = 51
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the given values of m and n
print(test(m, n))

# Set new values for m and n


m = 1
n = 79
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the new values of m and n
print(test(m, n))

# Set another set of values for m and n


m = 47
n = 53
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the new values of m and n
print(test(m, n))

# Set additional values for m and n


m = 100
n = 200
# Print a message indicating the range of numbers
print("\nBiggest even number between", m, "and", n)
# Print the result of the test function applied to the additional values of m and n
print(test(m, n))
Biggest even number between 12 and 51
50

Biggest even number between 1 and 79


78

Biggest even number between 47 and 53


52

Biggest even number between 100 and 200


200

===================================================================================
=================

Puzzle 59
Question: Last update on May 30 2025 11:48:58 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:58 (UTC/GMT +8 hours)

Validate Filenames

A valid filename should end in .txt, .exe, .jpg, .png, or .dll, and should have at
most three digits, no additional periods. Write a Python program to create a list
of True/False that determine whether candidate filename is valid or not.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Biggest even number between two numbers [Link]:Find numbers that


are adjacent to a prime number in the list, sorted without duplicates.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['[Link]', '[Link]', '[Link]', '[Link]', '[Link]', '[Link]']

Output:
['Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes']

Input:
['.txt', '[Link]', '[Link]', 'rose.c', '[Link]']

Output:
['No', 'Yes', 'No', 'No', 'No']

# Define a function named 'test' that takes a list of file names as input
def test(file_names):
# Use a list comprehension to iterate over each file name in the input list
return ["Yes" if
# Check conditions for a valid file name:
# - The file extension is one of ['txt', 'png', 'dll', 'exe', 'jpg']
# - The first character of the file name is alphabetic
# - The number of digits in the file name is less than 4
[Link](".")[1:] in [['txt'], ['png'], ['dll'], ['exe'], ['jpg']] and
f[0].isalpha() and sum([Link]() for c in f) < 4
# Return "Yes" if all conditions are met, otherwise "No"
else "No"
# Iterate over each file name 'f' in the input list
for f in file_names]

# Set a list of file names


file_names = ['[Link]', '[Link]', '[Link]', '[Link]', '[Link]',
'[Link]']
# Print a message indicating the original list of files
print("Original list of files:")
# Print the original list of file names
print(file_names)
# Print a message indicating valid filenames and use the 'test' function to
determine validity
print("Valid filenames:")
# Print the result of the 'test' function applied to the file names
print(test(file_names))

# Set another list of file names


file_names = ['.txt', '[Link]', '[Link]', 'rose.c', '[Link]']
# Print a message indicating the original list of files
print("\nOriginal list of files:")
# Print the original list of file names
print(file_names)
# Print a message indicating valid filenames and use the 'test' function to
determine validity
print("Valid filenames:")
# Print the result of the 'test' function applied to the file names
print(test(file_names))

Original list of files:


['[Link]', '[Link]', '[Link]', '[Link]', '[Link]', '[Link]']
Valid filenames:
['Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes']

Original list of files:


['.txt', '[Link]', '[Link]', 'rose.c', '[Link]']
Valid filenames:
['No', 'Yes', 'No', 'No', 'No']

# Define a function named 'test' that takes a list of file names as input
def test(file_names):
# Initialize an empty list to store the validity status of each file name
valids = []

# Iterate over each file name in the input list using a for loop
for sat in file_names:
# Count the number of digits in the current file name
n_digits = sum([Link]() for c in sat)

# Check conditions for a valid file name:


# - The file extension is one of ['txt', 'dll', 'exe']
# - The first character of the file name is alphabetic
# - The number of digits in the file name is less than or equal to 3
if [Link](".")[1:] not in [['txt'], ['dll'], ['exe']] or not
sat[0].isalpha() or n_digits > 3:
# Append "No" to the 'valids' list if the conditions are not met
[Link]("No")
else:
# Append "Yes" to the 'valids' list if all conditions are met
[Link]("Yes")

# Return the list of validity statuses


return valids

# Set a list of file names


file_names = ['[Link]', '[Link]', '[Link]', '[Link]', '[Link]',
'[Link]']
# Print a message indicating the original list of files
print("Original list of files:")
# Print the original list of file names
print(file_names)
# Print a message indicating valid filenames and use the 'test' function to
determine validity
print("Valid filenames:")
# Print the result of the 'test' function applied to the file names
print(test(file_names))
# Set another list of file names
file_names = ['.txt', '[Link]', '[Link]', 'rose.c', '[Link]']
# Print a message indicating the original list of files
print("\nOriginal list of files:")
# Print the original list of file names
print(file_names)
# Print a message indicating valid filenames and use the 'test' function to
determine validity
print("Valid filenames:")
# Print the result of the 'test' function applied to the file names
print(test(file_names))

Original list of files:


['[Link]', '[Link]', '[Link]', '[Link]', '[Link]', '[Link]']
Valid filenames:
['Yes', 'Yes', 'No', 'No', 'No', 'Yes']

Original list of files:


['.txt', '[Link]', '[Link]', 'rose.c', '[Link]']
Valid filenames:
['No', 'Yes', 'No', 'No', 'No']

===================================================================================
=================

Puzzle 60
Question: Last update on May 30 2025 11:48:58 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:58 (UTC/GMT +8 hours)

Numbers Adjacent to Primes

Prime number: A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7,


11).Write a Python program to find a list of all numbers that are adjacent to a
prime number in the list, sorted without duplicates.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:
Previous:A valid [Link]:Find the number which when appended to the list
makes the total 0.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[2, 17, 16, 0, 6, 4, 5]
Output:
[2, 4, 6, 16, 17]

Input:
[1, 2, 19, 16, 6, 4, 10]
Output:
[1, 2, 16, 19]

Input:
[1, 2, 3, 5, 1, 16, 7, 11, 4]
Output:
[1, 2, 3, 4, 5, 7, 11, 16]

# Define a function named 'test' that takes a list of numbers as input


def test(nums):
# Use a set comprehension to create a set of numbers that are adjacent to a
prime number in the list
return sorted({
n for i, n in enumerate(nums)
if (i > 0 and prime(nums[i - 1])) or (i < len(nums) - 1 and prime(nums[i +
1]))
})

# Define a function named 'prime' that checks if a given number is prime


def prime(m):
# Check if the number is greater than 0 and is divisible by any number in the
range (2, m - 1)
return all(m % i for i in range(2, m - 1))

# Set a list of numbers


nums = [2, 17, 16, 0, 6, 4, 5]
# Print a message indicating the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating numbers adjacent to a prime number, sorted without
duplicates
print("Numbers that are adjacent to a prime number in the said list, sorted without
duplicates:")
# Print the result of the 'test' function applied to the numbers
print(test(nums))

# Set another list of numbers


nums = [1, 2, 19, 16, 6, 4, 10]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating numbers adjacent to a prime number, sorted without
duplicates
print("Numbers that are adjacent to a prime number in the said list, sorted without
duplicates:")
# Print the result of the 'test' function applied to the numbers
print(test(nums))

# Set yet another list of numbers


nums = [1, 2, 3, 5, 1, 16, 7, 11, 4]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating numbers adjacent to a prime number, sorted without
duplicates
print("Numbers that are adjacent to a prime number in the said list, sorted without
duplicates:")
# Print the result of the 'test' function applied to the numbers
print(test(nums))

Original list of numbers:


[2, 17, 16, 0, 6, 4, 5]
Numbers that are adjacent to a prime number in the said list, sorted without
duplicates:
[2, 4, 16, 17]

Original list of numbers:


[1, 2, 19, 16, 6, 4, 10]
Numbers that are adjacent to a prime number in the said list, sorted without
duplicates:
[1, 2, 16, 19]

Original list of numbers:


[1, 2, 3, 5, 1, 16, 7, 11, 4]
Numbers that are adjacent to a prime number in the said list, sorted without
duplicates:
[1, 2, 3, 4, 5, 7, 11, 16]

===================================================================================
=================

Puzzle 61
Question: Last update on May 30 2025 11:48:59 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:59 (UTC/GMT +8 hours)

Find Missing Number for Zero Total


Write a Python program to find the number which when appended to the list makes the
total 0.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find numbers that are adjacent to a prime number in the list, sorted
without [Link]:Find the dictionary key whose case is different than all
other keys.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 2, 3, 4, 5]
Output:
-15

Input:
[-1, -2, -3, -4, 5]
Output:
5

Input:
[10, 42, 17, 9, 1315182, 184, 102, 29, 15, 39, 755]
Output:
-1316384

# Define a function named 'test' that takes a list of numbers as input


def test(nums):
# Create a set 'dset' from the input list to remove duplicates
dset = set(nums)
# Initialize 'result' with the sum of the original list 'nums'
result = sum(nums)
# Calculate the absolute difference between the minimum value in 'nums' and the
sum of the symmetric difference between 'dset' and 'nums'
dmin = abs(min(nums) - sum(dset ^ set(nums)))

# Iterate over the symmetric difference between 'dset' and 'nums'


for d in dset ^ set(nums):
# Create a copy of 'nums' and append the current value 'd'
dcopy = list(nums)
[Link](d)
# Calculate the sum of the modified list
ds = sum(dcopy)

# Check if the absolute value of the negation of the sum is less than
'dmin'
if 0 - ds < dmin:
result = ds
dmin = abs(ds)
# Check if the absolute value of the negation of the sum is equal to 'dmin'
elif 0 - ds == dmin:
# Update 'result' with the minimum value between the current 'result'
and the calculated sum 'ds'
result = min(result, ds)

# Multiply the final result by -1 and return it


return result * (-1)

# Set a list of numbers


nums = [1, 2, 3, 4, 5]
# Print a message indicating the original list of numbers
print("Original list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the number that, when appended to the list, makes the
total 0
print("Number which when appended to the list makes the total 0:")
# Print the result of the 'test' function applied to the numbers
print(test(nums))

# Set another list of numbers


nums = [-1, -2, -3, -4, 5]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the number that, when appended to the list, makes the
total 0
print("Number which when appended to the list makes the total 0:")
# Print the result of the 'test' function applied to the numbers
print(test(nums))

# Set yet another list of numbers


nums = [10, 42, 17, 9, 1315182, 184, 102, 29, 15, 39, 755]
# Print a message indicating the original list of numbers
print("\nOriginal list of numbers:")
# Print the original list of numbers
print(nums)
# Print a message indicating the number that, when appended to the list, makes the
total 0
print("Number which when appended to the list makes the total 0:")
# Print the result of the 'test' function applied to the numbers
print(test(nums))

Original list of numbers:


[1, 2, 3, 4, 5]
Number which when appended to the list makes the total 0:
-15

Original list of numbers:


[-1, -2, -3, -4, 5]
Number which when appended to the list makes the total 0:
5

Original list of numbers:


[10, 42, 17, 9, 1315182, 184, 102, 29, 15, 39, 755]
Number which when appended to the list makes the total 0:
-1316384

===================================================================================
=================

Puzzle 62
Question: Last update on May 30 2025 11:48:59 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:48:59 (UTC/GMT +8 hours)

Dictionary Key with Odd Case

Write a Python program to find the dictionary key whose case is different from all
other keys.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the number which when appended to the list makes the total
[Link]:Find the sum of the even elements that are at odd indices in a given list.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
{'red': '', 'GREEN': '', 'blue': 'orange'}
Output:
GREEN

Input:
{'RED': '', 'GREEN': '', 'orange': '#125GD'}
Output:
orange

# License: [Link]

def test(dict_data):
# Iterate over each key in the dictionary
for different in dict_data:
# Check if the case of the current key is different from all other keys
if all([Link]() != [Link]() for k in dict_data if k !=
different):
# Return the key with a different case
return different

# Example 1
dict_data1 = {"red": "", "GREEN": "", "blue": "orange"}
print("Original dictionary key-values:")
print(dict_data1)
print("Find the dictionary key whose case is different than all other keys:")
print(test(dict_data1))

# Example 2
dict_data2 = {"RED": "", "GREEN": "", "orange": "#125GD"}
print("\nOriginal dictionary key-values:")
print(dict_data2)
print("Find the dictionary key whose case is different than all other keys:")
print(test(dict_data2))

Original dictionary key-values:


{'red': '', 'GREEN': '', 'blue': 'orange'}
Find the dictionary key whose case is different than all other keys:
GREEN

Original dictionary key-values:


{'RED': '', 'GREEN': '', 'orange': '#125GD'}
Find the dictionary key whose case is different than all other keys:
orange
===================================================================================
=================

Puzzle 63
Question: Last update on May 30 2025 11:49:00 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:00 (UTC/GMT +8 hours)

Sum of Even Elements at Odd Indices

Write a Python program to find the sum of the even elements that are at odd indices
in a given list.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the dictionary key whose case is different than all other
[Link]:Find the string consisting of all the words whose lengths are prime
numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.


Follow us onFacebookandTwitterfor latest update.

Input:
[1, 2, 3, 4, 5, 6, 7]
Output:
12

Input:
[1, 2, 8, 3, 9, 4]
Output:
6

# License: [Link]

def test(nums):
# Sum the even elements at odd indices using list slicing and a conditional
expression
return sum(i for i in nums[1::2] if i % 2 == 0)

# Example 1
nums1 = [1, 2, 3, 4, 5, 6, 7]
print("Original list of numbers:")
print(nums1)
print("Sum of the even elements of the said list that are at odd indices:")
print(test(nums1))

# Example 2
nums2 = [1, 2, 8, 3, 9, 4]
print("\nOriginal list of numbers:")
print(nums2)
print("Sum of the even elements of the said list that are at odd indices:")
print(test(nums2))

Original list of numbers:


[1, 2, 3, 4, 5, 6, 7]
Sum of the even elements of the said list that are at odd indices:
12

Original list of numbers:


[1, 2, 8, 3, 9, 4]
Sum of the even elements of the said list that are at odd indices:
6

def test(nums):
# Sum the even elements at odd indices using a list comprehension
return sum([nums[i] for i in range(len(nums)) if i % 2 == 1 and nums[i] % 2 ==
0])

# Example 1
nums1 = [1, 2, 3, 4, 5, 6, 7]
print("Original list of numbers:")
print(nums1)
print("Sum of the even elements of the said list that are at odd indices:")
print(test(nums1))

# Example 2
nums2 = [1, 2, 8, 3, 9, 4]
print("\nOriginal list of numbers:")
print(nums2)
print("Sum of the even elements of the said list that are at odd indices:")
print(test(nums2))

Original list of numbers:


[1, 2, 3, 4, 5, 6, 7]
Sum of the even elements of the said list that are at odd indices:
12

Original list of numbers:


[1, 2, 8, 3, 9, 4]
Sum of the even elements of the said list that are at odd indices:
6

===================================================================================
=================

Puzzle 64
Question: Last update on May 30 2025 11:49:00 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:00 (UTC/GMT +8 hours)

Words with Prime Lengths

Write a Python program to find the string consisting of all the words whose lengths
are prime numbers.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the sum of the even elements that are at odd indices in a given
[Link]:Circular shift number.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
The quick brown fox jumps over the lazy dog.
Output:
The quick brown fox jumps the

Input:
Omicron Effect: Foreign Flights Won't Resume On Dec 15, Decision Later.
Output:
Omicron Effect: Foreign Flights Won't On Dec 15,

# License: [Link]

def test(strs):
# Join words whose lengths are prime numbers using list comprehension
return " ".join(strs for strs in [Link]() if is_prime(len(strs)))

def is_prime(n):
# Check if a number is prime
return n > 1 and all(n % j for j in range(2, int(n ** 0.5) + 1))

# Example 1
strs1 = "The quick brown fox jumps over the lazy dog."
print("Original list of words:")
print(strs1)
print("Words whose lengths are prime numbers in the said string:")
print(test(strs1))

# Example 2
strs2 = "Omicron Effect: Foreign Flights Won't Resume On Dec 15, Decision Later."
print("\nOriginal list of words:")
print(strs2)
print("Words whose lengths are prime numbers in the said string:")
print(test(strs2))

Original list of numbers:


The quick brown fox jumps over the lazy dog.
Words whose lengths are prime numbers in the said string:
The quick brown fox jumps the

Original list of numbers:


Omicron Effect: Foreign Flights Won't Resume On Dec 15, Decision Later.
Words whose lengths are prime numbers in the said string:
Omicron Effect: Foreign Flights Won't On Dec 15,

===================================================================================
=================
Puzzle 65
Question: Last update on May 30 2025 11:49:01 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:01 (UTC/GMT +8 hours)

Circular Shift of Digits

Write a Python program to shift the decimal digits n places to the left, wrapping
the extra digits around. If the shift > the number of digits in n, reverse the
string.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the string consisting of all the words whose lengths are prime
[Link]:Find the indices of the closest pair from given a list of numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.


Input:
n = 12345 and shift = 1
Output:
Result = 23451
Input:
n = 12345 and shift = 2
Output:
Result = 34512
Input:
n = 12345 and shift = 3
Output:
Result = 45123
Input:
n = 12345 and shift = 5
Output:
Result = 12345
Input:
n = 12345 and shift = 6
Output:
Result = 54321

# License: [Link]

def test(n, shift):


# Convert the number to a string
s = str(n)

# Check if shift is greater than the number of digits in n


if shift > len(s):
# If so, reverse the string
return s[::-1]

# Shift the decimal digits to the left by 'shift' places


return s[shift:] + s[:shift]

# Display the purpose of the code


print("Shift the decimal digits n places to the left. If shift > the number of
digits of n, reverse the string.")

# Example 1
n1 = 12345
shift1 = 1
print("\nn =", n1, " and shift =", shift1)
print("Result =", test(n1, shift1))

# Example 2
n2 = 12345
shift2 = 2
print("\nn =", n2, " and shift =", shift2)
print("Result =", test(n2, shift2))

# Example 3
n3 = 12345
shift3 = 3
print("\nn =", n3, " and shift =", shift3)
print("Result =", test(n3, shift3))
# Example 4
n4 = 12345
shift4 = 5
print("\nn =", n4, " and shift =", shift4)
print("Result =", test(n4, shift4))

# Example 5
n5 = 12345
shift5 = 6
print("\nn =", n5, " and shift =",shift5)
print("Result = ",test(n5, shift))

Shift the decimal digits n places to the left. If shift > the number of digits of
n, reverse the string.:

n = 12345 and shift = 1


Result = 23451

n = 12345 and shift = 2


Result = 34512

n = 12345 and shift = 3


Result = 45123

n = 12345 and shift = 5


Result = 12345

n = 12345 and shift = 6


Result = 54321

# License: [Link]

def test(n, shift):


# Convert the number to a list of individual digits
shifted_digits = [int(x) for x in str(n)]

# Shift the digits to the left by 'shift' places using list manipulation
for i in range(shift):
shifted_digits.append(shifted_digits.pop(0))

# Check if shift is greater than the number of digits in n


if shift > len(shifted_digits):
# If so, reverse the string representation of n
return str(n)[::-1]
else:
# Convert the shifted digits back to a string and join them
return ''.join(str(x) for x in shifted_digits)

# Display the purpose of the code


print("Shift the decimal digits n places to the left. If shift > the number of
digits of n, reverse the string.")

# Example 1
n1 = 12345
shift1 = 1
print("\nn =", n1, " and shift =", shift1)
print("Result =", test(n1, shift1))

# Example 2
n2 = 12345
shift2 = 2
print("\nn =", n2, " and shift =", shift2)
print("Result =", test(n2, shift2))

# Example 3
n3 = 12345
shift3 = 3
print("\nn =", n3, " and shift =", shift3)
print("Result =", test(n3, shift3))

# Example 4
n4 = 12345
shift4 = 5
print("\nn =", n4, " and shift =", shift4)
print("Result =", test(n4, shift4))

# Example 5
n5 = 12345
shift5 = 6
print("\nn =", n5, " and shift =", shift5)
print("Result =", test(n5, shift5))

Shift the decimal digits n places to the left. If shift > the number of digits of
n, reverse the string.:

n = 12345 and shift = 1


Result = 23451

n = 12345 and shift = 2


Result = 34512

n = 12345 and shift = 3


Result = 45123

n = 12345 and shift = 5


Result = 12345

n = 12345 and shift = 6


Result = 54321

===================================================================================
=================

Puzzle 66
Question: Last update on May 30 2025 11:49:01 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:01 (UTC/GMT +8 hours)

Indices of Closest Pair

Write a Python program to find the indices of the closest pair from a list of
numbers.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Circular shift [Link]:Find a string which, when each character is


shifted (ASCII incremented) by shift.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: [1, 7, 9, 2, 10]


Output:
[0, 3]

Input: [1.1, 4.25, 0.79, 1.0, 4.23]


Output:
[4, 1]

Input: [0.21, 11.3, 2.01, 8.0, 10.0, 3.0, 15.2]


Output:
[2, 5]

# License: [Link]

def test(nums):
# Initialize variables to store indices of the closest pair and the closest
distance
closest_inds = None
closest_dist = None

# Iterate through each element in the list


for ind, num in enumerate(nums):
# Compare each element with every other element in the list
for other_ind, num2 in enumerate(nums):
# Check if the elements are distinct and calculate the absolute
difference
if num != num2 and ((closest_dist is None) or abs(num - num2) <
closest_dist):
# Update the closest distance and indices
closest_dist = abs(num - num2)

# Determine the order of indices based on the magnitude of the


numbers
if num <= num2:
closest_inds = [ind, other_ind]
else:
closest_inds = [other_ind, ind]

# Return the indices of the closest pair


return closest_inds

# Example 1
nums1 = [1, 7, 9, 2, 10]
print("List of numbers:", nums1)
print("Indices of the closest pair from the said list of numbers:")
print(test(nums1))

# Example 2
nums2 = [1.1, 4.25, 0.79, 1.0, 4.23]
print("\nList of numbers:", nums2)
print("Indices of the closest pair from the said list of numbers:")
print(test(nums2))

# Example 3
nums3 = [0.21, 11.3, 2.01, 8.0, 10.0, 3.0, 15.2]
print("\nList of numbers:", nums3)
print("Indices of the closest pair from the said list of numbers:")
print(test(nums3))

List of numbers: [1, 7, 9, 2, 10]


Indices of the closest pair from the said list of numbers:
[0, 3]

List of numbers: [1.1, 4.25, 0.79, 1.0, 4.23]


Indices of the closest pair from the said list of numbers:
[4, 1]

List of numbers: [0.21, 11.3, 2.01, 8.0, 10.0, 3.0, 15.2]


Indices of the closest pair from the said list of numbers:
[2, 5]

===================================================================================
=================

Puzzle 67
Question: Last update on May 30 2025 11:49:02 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:02 (UTC/GMT +8 hours)
ASCII Shift for String Transformation

Write a Python program to find a string which, when each character is shifted
(ASCII incremented) by shift, gives the result.

ASCII table -

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the indices of the closest pair from given a list of


[Link]:Find all 5's in integers less than n that are divisible by 9 or 15.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
Ascii character table
Shift = 1
Output:
@rbhh#bg`q`bsdq#s`akd

Input:
Ascii character table
Shift = -1
Output:
Btdjj!dibsbdufs!ubcmf
# License: [Link]

def test(strs, shift):


# Joining characters after shifting them by the specified amount
return "".join(chr(ord(c) - shift) for c in strs)

# Example 1
strs1 = "Ascii character table"
print("Original string:")
print(strs1)
shift1 = 1
print('Shift =', shift1)
print("A new string which, when each character is shifted (ASCII incremented) by
shift in the said string:")
print(test(strs1, shift1))

# Example 2
strs2 = "Ascii character table"
print("\nOriginal string:")
print(strs2)
shift2 = -1
print('Shift =', shift2)
print("A new string which, when each character is shifted (ASCII incremented) by
shift in the said string:")
print(test(strs2, shift2))

Original string:
Ascii character table
Shift = 1
A new string which, when each character is shifted (ASCII incremented) by shift in
the said string:
@rbhh#bg`q`bsdq#s`akd

Original string:
Ascii character table
Shift = -1
A new string which, when each character is shifted (ASCII incremented) by shift in
the said string:
Btdjj!dibsbdufs!ubcmf

# License: [Link]

def test(strs, shift):


# Using list comprehension to generate a list of characters after shifting
shifted_chars = [chr(ord(strs[i]) - shift) for i in range(len(strs))]
# Joining the characters to form the final shifted string
return "".join(shifted_chars)

# Example 1
strs1 = "Ascii character table"
print("Original string:")
print(strs1)
shift1 = 1
print('Shift =', shift1)
print("A new string which, when each character is shifted (ASCII decremented) by
shift in the said string:")
print(test(strs1, shift1))

# Example 2
strs2 = "Ascii character table"
print("\nOriginal string:")
print(strs2)
shift2 = -1
print('Shift =', shift2)
print("A new string which, when each character is shifted (ASCII decremented) by
shift in the said string:")
print(test(strs2, shift2))

Original string:
Ascii character table
Shift = 1
A new string which, when each character is shifted (ASCII incremented) by shift in
the said string:
@rbhh#bg`q`bsdq#s`akd

Original string:
Ascii character table
Shift = -1
A new string which, when each character is shifted (ASCII incremented) by shift in
the said string:
Btdjj!dibsbdufs!ubcmf

===================================================================================
=================

Puzzle 68
Question: Last update on May 30 2025 11:49:02 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:02 (UTC/GMT +8 hours)

Fives Divisible by 9 or 15

Write a Python program to find all 5's in integers less than n that are divisible
by 9 or 15.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:
Sample Solution-2:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find a string which, when each character is shifted (ASCII incremented) by


[Link]:Create a new string by taking s, and word by word rearranging its
characters in ASCII order.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
Value of n = 50
Output:
[[15, 1], [45, 1]]
Input:
Value of n = 65
Output:
[[15, 1], [45, 1], [54, 0]]
Input:
Value of n = 75
Output:
[[15, 1], [45, 1], [54, 0]]
Input:
Value of n = 85
Output:
[[15, 1], [45, 1], [54, 0], [75, 1]]
Input:
Value of n = 150
Output:
[[15, 1], [45, 1], [54, 0], [75, 1], [105, 2], [135, 2]]

# Define a function named 'test' that takes an integer 'n' as input


def test(n):
# Using list comprehension to generate a list of pairs [i, j] for each i and j
satisfying the conditions
return [[i, j] for i in range(n) for j in range(len(str(i))) if str(i)[j] ==
'5' and (i % 15 == 0 or i % 9 == 0)]

# Example 1
n1 = 50
print("Value of n =", n1)
print("5's in integers less than", n1, "that are divisible by 9 or 15:")
print(test(n1))

# Example 2
n2 = 65
print("\nValue of n =", n2)
print("5's in integers less than", n2, "that are divisible by 9 or 15:")
print(test(n2))

# Example 3
n3 = 75
print("\nValue of n =", n3)
print("5's in integers less than", n3, "that are divisible by 9 or 15:")
print(test(n3))

# Example 4
n4 = 85
print("\nValue of n =", n4)
print("5's in integers less than", n4, "that are divisible by 9 or 15:")
print(test(n4))

# Example 5
n5 = 150
print("\nValue of n =", n5)
print("5's in integers less than", n5, "that are divisible by 9 or 15:")
print(test(n5))

Value of n = 50
5's in integers less than 50 that are divisible by 9 or 15:
[[15, 1], [45, 1]]

Value of n = 65
5's in integers less than 65 that are divisible by 9 or 15:
[[15, 1], [45, 1], [54, 0]]

Value of n = 75
5's in integers less than 75 that are divisible by 9 or 15:
[[15, 1], [45, 1], [54, 0]]

Value of n = 85
5's in integers less than 85 that are divisible by 9 or 15:
[[15, 1], [45, 1], [54, 0], [75, 1]]

Value of n = 150
5's in integers less than 150 that are divisible by 9 or 15:
[[15, 1], [45, 1], [54, 0], [75, 1], [105, 2], [135, 2]]

# Define a function named 'test' that takes an integer 'n' as input


def test(n):
# Using list comprehension to generate a list of pairs [i, j] for each i and j
satisfying the conditions
return [[i, j] for i in range(n) if (i % 9 == 0 or i % 15 == 0) for j, c in
enumerate(str(i)) if c == '5']

# Example 1
n1 = 50
print("Value of n =", n1)
print("5's in integers less than", n1, "that are divisible by 9 or 15:")
print(test(n1))

# Example 2
n2 = 65
print("\nValue of n =", n2)
print("5's in integers less than", n2, "that are divisible by 9 or 15:")
print(test(n2))

# Example 3
n3 = 75
print("\nValue of n =", n3)
print("5's in integers less than", n3, "that are divisible by 9 or 15:")
print(test(n3))

# Example 4
n4 = 85
print("\nValue of n =", n4)
print("5's in integers less than", n4, "that are divisible by 9 or 15:")
print(test(n4))

# Example 5
n5 = 150
print("\nValue of n =", n5)
print("5's in integers less than", n5, "that are divisible by 9 or 15:")
print(test(n5))

Value of n = 50
5's in integers less than 50 that are divisible by 9 or 15:
[[15, 1], [45, 1]]

Value of n = 65
5's in integers less than 65 that are divisible by 9 or 15:
[[15, 1], [45, 1], [54, 0]]

Value of n = 75
5's in integers less than 75 that are divisible by 9 or 15:
[[15, 1], [45, 1], [54, 0]]

Value of n = 85
5's in integers less than 85 that are divisible by 9 or 15:
[[15, 1], [45, 1], [54, 0], [75, 1]]

Value of n = 150
5's in integers less than 150 that are divisible by 9 or 15:
[[15, 1], [45, 1], [54, 0], [75, 1], [105, 2], [135, 2]]

===================================================================================
=================
Puzzle 69
Question: Last update on May 30 2025 11:49:03 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:03 (UTC/GMT +8 hours)

Rearrange Words by ASCII

Write a Python program to create a new string by taking a string, and word by word
rearranging its characters in ASCII order.

Visual Presentation:

Sample Solution-1:

Python Code:

Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find all 5's in integers less than n that are divisible by 9 or


[Link]:Find the first negative balance.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: Ascii character table


Output:
Aciis aaccehrrt abelt

Input: maltos won


Output:
almost now

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Using list comprehension to iterate over words in the input string, sorting
the characters of each word in ASCII order
# Then, joining the sorted words into a new string with spaces between them
return " ".join("".join(sorted(w)) for w in [Link](' '))

# Example 1
strs1 = "Ascii character table"
print("Original string:", strs1)
print("New string by said string, and word by word rearranging its characters in
ASCII order:")
print(test(strs1))

# Example 2
strs2 = "maltos won"
print("\nOriginal string:", strs2)
print("New string by said string, and word by word rearranging its characters in
ASCII order:")
print(test(strs2))

Original string: Ascii character table


New string by said string, and word by word rearranging its characters in ASCII
order:
Aciis aaccehrrt abelt

Original string: maltos won


New string by said string, and word by word rearranging its characters in ASCII
order:
almost now

# License: [Link]

# Define a function named 'test' that takes a string 'strs' as input


def test(strs):
# Split the input string into a list of words
words = [Link](' ')
rwords = [] # Initialize an empty list to store the results

# Iterate over each word in the list of words


for word in words:
occurrences = {} # Initialize an empty dictionary to store character
occurrences
# Count occurrences of each character in the word
for c in word:
occurrences[c] = [Link](c, 0) + 1

subsequence = [] # Initialize an empty list to store characters in the


subsequence
# Iterate over the dictionary items and create a subsequence based on
character occurrences
for c, count in [Link]():
subsequence += [c]*count

[Link]() # Sort the subsequence in ASCII order

# Repeat the subsequence to match the length of the original word


subsequence = subsequence*(len(word)//len(subsequence)) +
subsequence[:len(word)%len(subsequence)]

# Join the characters of the subsequence to form a rearranged word and


append it to the result list
[Link](''.join(subsequence))

# Join the rearranged words into a new string with spaces between them
return ' '.join(rwords)

# Example 1
strs1 = "Ascii character table"
print("Original string:", strs1)
print("New string by said string, and word by word rearranging its characters in
ASCII order:")
print(test(strs1))

# Example 2
strs2 = "maltos won"
print("\nOriginal string:", strs2)
print("New string by said string, and word by word rearranging its characters in
ASCII order:")
print(test(strs2))

Original string: Ascii character table


New string by said string, and word by word rearranging its characters in ASCII
order:
Aciis aaccehrrt abelt

Original string: maltos won


New string by said string, and word by word rearranging its characters in ASCII
order:
almost now

===================================================================================
=================

Puzzle 70
Question: Last update on May 30 2025 11:49:03 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:03 (UTC/GMT +8 hours)

First Negative Balance

Write a Python program to find the first negative balance from a given list of
numbers that represent bank deposits and withdrawals.

Visual Presentation:
Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Create a new string by taking s, and word by word rearranging its


characters in ASCII [Link]:Inject a number in between each pair of adjacent
numbers in a list of numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[[12, -7, 3, -89, 14, 88, -78], [-1, 2, 7]]

Output:
[-81, -1]
Input:
[[1200, 100, -900], [100, 100, -2400]]

Output:
[None, -2200]

# License: [Link]

# Define a function named 'test' that takes a list of balances as input


def test(balances):
firsts = [] # Initialize an empty list to store the first negative balances

# Iterate over each list of balances in the input list


for bals in balances:
total = 0 # Initialize a variable to store the running total of balances

# Iterate over each balance in the list of balances


for b in bals:
total += b # Update the running total with the current balance
# Check if the running total becomes negative
if total < 0:
[Link](total) # Append the first negative balance to the
result list
break
else:
[Link](None) # If no negative balance is found, append None to
the result list

return firsts # Return the list of first negative balances

# Example 1
balances1 = [[12, -7, 3, -89, 14, 88, -78], [-1, 2, 7]]
print("Bank deposits and withdrawals:")
print(balances1)
print("\nFirst negative balance of deposits and withdrawals:")
print(test(balances1))

# Example 2
balances2 = [[1200, 100, -900], [100, 100, -2400]]
print("\nBank deposits and withdrawals:")
print(balances2)
print("\nFirst negative balance of deposits and withdrawals:")
print(test(balances2))

Bank deposits and withdrawals:


[[12, -7, 3, -89, 14, 88, -78], [-1, 2, 7]]

First negative balance of deposits and withdrawals:


[-81, -1]
Bank deposits and withdrawals:
[[1200, 100, -900], [100, 100, -2400]]

First negative balance of deposits and withdrawals:


[None, -2200]

===================================================================================
=================

Puzzle 71
Question: Last update on May 30 2025 11:49:04 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:04 (UTC/GMT +8 hours)

Inject Separator Between Numbers

Given a list of numbers and a number to inject, write a Python program to create a
list containing that number in between each pair of adjacent numbers.

Visual Presentation:

Sample Solution-1:

Python Code:
Sample Output:

Flowchart:

Sample Solution-2:

Python Code:

Sample Output:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the first negative [Link]:Find the indices of three numbers


that sum to 0 in a list.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]


Separator: 6
Output:
[12, 6, -7, 6, 3, 6, -89, 6, 14, 6, 88, 6, -78, 6, -1, 6, 2, 6, 7]

Input: [1, 2, 3, 4, 5, 6]
Separator: 9
Output:
[1, 9, 2, 9, 3, 9, 4, 9, 5, 9, 6]

# License: [Link]

# Define a function named 'test' that takes a list of numbers and a separator as
input
def test(nums, sep):
# Initialize a list 'ans' with double the length of 'nums' minus one, filled
with separators
ans = [sep] * (2 * len(nums) - 1)

# Replace every second element of 'ans' with the corresponding elements from
'nums'
ans[::2] = nums

return ans # Return the modified list


# Example 1
nums1 = [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]
separator1 = 6
print("List of numbers:", nums1)
print("Separator:", separator1)
print("Inject the separator in between each pair of adjacent numbers of the said
list:")
print(test(nums1, separator1))

# Example 2
nums2 = [1, 2, 3, 4, 5, 6]
separator2 = 9
print("\nList of numbers:", nums2)
print("Separator:", separator2)
print("Inject the separator in between each pair of adjacent numbers of the said
list:")
print(test(nums2, separator2))

List of numbers: [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]


Separator: 6
Inject the separator in between each pair of adjacent numbers of the said list:
[12, 6, -7, 6, 3, 6, -89, 6, 14, 6, 88, 6, -78, 6, -1, 6, 2, 6, 7]

List of numbers: [1, 2, 3, 4, 5, 6]


Separator: 9
Inject the separator in between each pair of adjacent numbers of the said list:
[1, 9, 2, 9, 3, 9, 4, 9, 5, 9, 6]

# License: [Link]

# Define a function named 'test' that takes a list of numbers and a separator as
input
def test(nums, sep):
result = [] # Initialize an empty list to store the result

# Iterate through the indices of 'nums'


for i in range(len(nums)):
# Check if the current index is the last one
if i == len(nums) - 1:
[Link](nums[i]) # If it is the last index, append the number
without the separator
else:
[Link](nums[i]) # Append the current number to the result
[Link](sep) # Append the separator after the current number

return result # Return the modified list

# Example 1
nums1 = [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]
separator1 = 6
print("List of numbers:", nums1)
print("Separator:", separator1)
print("Inject the separator in between each pair of adjacent numbers of the said
list:")
print(test(nums1, separator1))

# Example 2
nums2 = [1, 2, 3, 4, 5, 6]
separator2 = 9
print("\nList of numbers:", nums2)
print("Separator:", separator2)
print("Inject the separator in between each pair of adjacent numbers of the said
list:")
print(test(nums2, separator2))

List of numbers: [12, -7, 3, -89, 14, 88, -78, -1, 2, 7]


Separator: 6
Inject the separator in between each pair of adjacent numbers of the said list:
[12, 6, -7, 6, 3, 6, -89, 6, 14, 6, 88, 6, -78, 6, -1, 6, 2, 6, 7]

List of numbers: [1, 2, 3, 4, 5, 6]


Separator: 9
Inject the separator in between each pair of adjacent numbers of the said list:
[1, 9, 2, 9, 3, 9, 4, 9, 5, 9, 6]

===================================================================================
=================

Puzzle 72
Question: Last update on May 30 2025 11:49:04 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:04 (UTC/GMT +8 hours)

Indices of Three Numbers Summing to Zero

Write a Python program to find the indices of three numbers that sum to 0 in a
given list of numbers.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Inject a number in between each pair of adjacent numbers in a list of


[Link]:Find a string contains a vowel between two consonants, in a given
string.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: [12, -7, 3, -89, 14, 4, -78, -1, 2, 7]


Output:
[1, 2, 5]

Input: [1, 2, 3, 4, 5, 6, -7]


Output:
[2, 3, 6]

# License: [Link]

# Define a function named 'test' that takes a list of numbers as input


def test(nums):
inv = {n: i for i, n in enumerate(nums)} # Create a dictionary to store the
indices of numbers
# Note that later duplicates will override earlier entries

# Iterate through the indices and numbers in 'nums'


for i, n in enumerate(nums):
if inv[n] == i:
del inv[n] # Remove the entry if it corresponds to the current index

# Check if there is a pair of indices (j, k) such that nums[j] + nums[k] +


n = 0
if any((-m - n) in inv for m in nums[:i]):
# If a solution is found, get the indices (j, m) where nums[j] + m = -n
j, m = next((j, m) for j, m in enumerate(nums) if (-m - n) in inv)
# Get the index k for nums[k] = -m - n
k = inv[-m - n]
return sorted([i, j, k]) # Return the sorted list of indices

# Example 1
nums1 = [12, -7, 3, -89, 14, 4, -78, -1, 2, 7]
print("List of numbers:", nums1)
print("Indices of three numbers that sum to 0 in the said list:")
print(test(nums1))

# Example 2
nums2 = [1, 2, 3, 4, 5, 6, -7]
print("\nList of numbers:", nums2)
print("Indices of three numbers that sum to 0 in the said list:")
print(test(nums2))

List of numbers: [12, -7, 3, -89, 14, 4, -78, -1, 2, 7]


Indices of three numbers that sum to 0 in the said list:
[1, 2, 5]

List of numbers: [1, 2, 3, 4, 5, 6, -7]


Indices of three numbers that sum to 0 in the said list:
[2, 3, 6]

===================================================================================
=================

Puzzle 73
Question: Last update on May 30 2025 11:49:05 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:05 (UTC/GMT +8 hours)

Vowel Between Consonants Substring

Write a Python program to find a substring in a given string that contains a vowel
between two consonants.

From [Link]:A consonant is a speech sound that is not a vowel. It also


refers to letters of the alphabet that represent those sounds: Z, B, T, G, and H
are all consonants. Consonants are all the non-vowel sounds, or their corresponding
letters: A, E, I, O, U and sometimes Y are not consonants.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the indices of three numbers that sum to 0 in a [Link]:Find a


string consisting of space-separated characters with given counts.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.


Input: Hello
Output:
Hel

Input: Sandwhich
Output:
San

Input: Python
Output:
hon

# License: [Link]

# Define a function named 'test' that takes a string as input


def test(s):
cons = "bcdfghjklmnpqrstvwxz" # Define a string containing consonants
vows = "aeiou" # Define a string containing vowels

# Use a generator expression to find a vowel between two consonants in the


string
return next(s[i - 1:i + 2] for i in range(1, len(s) - 1)
if s[i].lower() in vows and s[i - 1].lower() in cons and s[i +
1].lower() in cons)

# Example 1
strs1 = "Hello"
print("Original string:", strs1)
print("Find a vowel between two consonants, contained in said string:")
print(test(strs1))

# Example 2
strs2 = "Sandwich"
print("\nOriginal string:", strs2)
print("Find a vowel between two consonants, contained in said string:")
print(test(strs2))

# Example 3
strs3 = "Python"
print("\nOriginal string:", strs3)
print("Find a vowel between two consonants, contained in said string:")
print(test(strs3))

Original string: Hello


Find a vowel between two consonants, contained in said string:
Hel

Original string: Sandwhich


Find a vowel between two consonants, contained in said string:
San

Original string: Python


Find a vowel between two consonants, contained in said string:
hon
===================================================================================
=================

Puzzle 74
Question: Last update on May 30 2025 11:49:05 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:05 (UTC/GMT +8 hours)

Space-Separated Characters with Counts

Write a Python program to find a string consisting of space-separated characters


with given counts.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find a string contains a vowel between two consonants, in a given


[Link]:Reorder numbers in increasing/decreasing order based on whether the
first plus last element is even/odd.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: {'f': 1, 'o': 2}


Output:
f o o

Input: {'a': 1, 'b': 1, 'c': 1}


Output:
a b c

# License: [Link]
# Define a function named 'test' that takes a dictionary of character counts as
input
def test(counts):
# Use a nested loop to generate a string with characters repeated according to
their counts
return " ".join(c for c, i in [Link]() for _ in range(i))

# Example 1
strs1 = {"f": 1, "o": 2}
print("Original string:", strs1)
print("String consisting of space-separated characters with given counts:")
print(test(strs1))

# Example 2
strs2 = {"a": 1, "b": 1, "c": 1}
print("\nOriginal string:", strs2)
print("String consisting of space-separated characters with given counts:")
print(test(strs2))

Original string: {'f': 1, 'o': 2}


String consisting of space-separated characters with given counts:
f o o

Original string: {'a': 1, 'b': 1, 'c': 1}


String consisting of space-separated characters with given counts:
a b c

===================================================================================
=================

Puzzle 75
Question: Last update on May 30 2025 11:49:06 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:06 (UTC/GMT +8 hours)

Reorder Numbers Based on Sum

Write a Python program to reorder numbers from a given array in


increasing/decreasing order based on whether the first plus last element is
odd/even.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:
For more Practice: Solve these Related Problems:

Go to:

Previous:Find a string consisting of space-separated characters with given


[Link]:Find the index of the largest prime in the list and the sum of its
digits.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[3, 7, 4]
Output:
[3, 4, 7]

Input:
[2, 7, 4]
Output:
[7, 4, 2]

Input:
[1, 5, 6, 7, 4, 2, 8]
Output:
[1, 2, 4, 5, 6, 7, 8]

Input:
[1, 5, 6, 7, 4, 2, 9]
Output:
[9, 7, 6, 5, 4, 2, 1]

# License: [Link]

# Define a function named 'test' that takes a list of numbers as input


def test(nums):
# Use the sorted function to reorder the numbers based on whether the sum of
the first and last element is odd/even
return sorted(nums, reverse=(False if (nums[0] + nums[-1]) % 2 else True))

# Print a message indicating the purpose of the code


print("Reorder numbers of a given array in increasing/decreasing order based on
whether the first plus last element is odd/even.")

# Example 1
nums1 = [3, 7, 4]
print("\nList of numbers:", nums1)
print("Result:")
print(test(nums1))

# Example 2
nums2 = [2, 7, 4]
print("\nList of numbers:", nums2)
print("Result:")
print(test(nums2))

# Example 3
nums3 = [1, 5, 6, 7, 4, 2, 8]
print("\nList of numbers:", nums3)
print("Result:")
print(test(nums3))

# Example 4
nums4 = [1, 5, 6, 7, 4, 2, 9]
print("\nList of numbers:", nums4)
print("Result:")
print(test(nums4))

Reorder numbers of a give array in increasing/decreasing order based on whether the


first plus last element is odd/even.:

List of numbers: [3, 7, 4]


Result:
[3, 4, 7]

List of numbers: [2, 7, 4]


Result:
[7, 4, 2]

List of numbers: [1, 5, 6, 7, 4, 2, 8]


Result:
[1, 2, 4, 5, 6, 7, 8]

List of numbers: [1, 5, 6, 7, 4, 2, 9]


Result:
[9, 7, 6, 5, 4, 2, 1]

===================================================================================
=================

Puzzle 76
Question: Last update on May 30 2025 11:49:06 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:06 (UTC/GMT +8 hours)

Largest Prime Index and Digit Sum

Write a Python program to find the index of the largest prime in the list and the
sum of its digits.

Visual Presentation:
Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Reorder numbers in increasing/decreasing order based on whether the first


plus last element is even/[Link]:Convert GPAs to letter grades.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[3, 7, 4]
Output:
[1, 7]

Input:
[3, 11, 7, 17, 19, 4]
Output:
[4, 10]

Input:
[23, 17, 201, 14, 10473, 43225, 421, 423, 11, 10, 2022, 342157]
Output:
[6, 7]

# License: [Link]

# Define a function named 'test' that takes a list of numbers as input


def test(nums):
# Find the maximum prime number in the list along with its index
n, i = max((n, i) for i, n in enumerate(nums) if is_prime(n))

# Return a list containing the index of the largest prime and the sum of its
digits
return [i, sum(int(c) for c in str(n))]

# Define a function named 'is_prime' that checks if a number is prime


def is_prime(n):
return n > 1 and all(n % j for j in range(2, int(n ** 0.5) + 1))

# Example 1
nums1 = [3, 7, 4]
print("List of numbers:", nums1)
print("Index of the largest prime in the said list and the sum of its digits:")
print(test(nums1))

# Example 2
nums2 = [3, 11, 7, 17, 19, 4]
print("\nList of numbers:", nums2)
print("Index of the largest prime in the said list and the sum of its digits:")
print(test(nums2))

# Example 3
nums3 = [23, 17, 201, 14, 10473, 43225, 421, 423, 11, 10, 2022, 342157]
print("\nList of numbers:", nums3)
print("Index of the largest prime in the said list and the sum of its digits:")
print(test(nums3))

List of numbers: [3, 7, 4]


Index of the largest prime in the said list and the sum of its digits:
[1, 7]

List of numbers: [3, 11, 7, 17, 19, 4]


Index of the largest prime in the said list and the sum of its digits:
[4, 10]

List of numbers: [23, 17, 201, 14, 10473, 43225, 421, 423, 11, 10, 2022, 342157]
Index of the largest prime in the said list and the sum of its digits:
[6, 7]

===================================================================================
=================

Puzzle 77
Question: Last update on May 30 2025 11:49:07 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:07 (UTC/GMT +8 hours)

GPA to Letter Grade Conversion

Write a Python program to convert GPAs to letter grades according to the following
table:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:


Go to:

Previous:Find the index of the largest prime in the list and the sum of its
[Link]:Find the two closest distinct numbers in a given a list of numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[4.0, 3.5, 3.8]
Output:
['A+', 'A-', 'A']
Input:
[5.0, 4.7, 3.4, 3.0, 2.7, 2.4, 2.0, 1.7, 1.4, 0.0]
Output:
['A+', 'A+', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'F']

# License: [Link]

# Define a function named 'test' that takes a list of GPAs as input


def test(nums):
# Use list comprehension to convert GPAs to letter grades
return ["A+" if grade >= 4.0
else ("A" if grade >= 3.7
else ("A-" if grade >= 3.4
else ("B+" if grade >= 3.0
else ("B" if grade >= 2.7
else ("B-" if grade >= 2.4
else ("C+" if grade >= 2.0
else ("C" if grade >= 1.7
else ("C-" if grade >= 1.4
else "F"))))))))
for grade in nums]

# Example 1
nums1 = [4.0, 3.5, 3.8]
print("List of numbers:", nums1)
print("Convert GPAs to letter grades:")
print(test(nums1))

# Example 2
nums2 = [5.0, 4.7, 3.4, 3.0, 2.7, 2.4, 2.0, 1.7, 1.4, 0.0]
print("\nList of numbers:", nums2)
print("Convert GPAs to letter grades:")
print(test(nums2))
List of numbers: [4.0, 3.5, 3.8]
Convert GPAs to letter grades:
['A+', 'A-', 'A']

List of numbers: [5.0, 4.7, 3.4, 3.0, 2.7, 2.4, 2.0, 1.7, 1.4, 0.0]
Convert GPAs to letter grades:
['A+', 'A+', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'F']

===================================================================================
=================

Puzzle 78
Question: Last update on May 30 2025 11:49:07 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:07 (UTC/GMT +8 hours)

Closest Distinct Pair in List

Write a Python program to find the two closest distinct numbers in a given list of
numbers.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Convert GPAs to letter [Link]:Find the largest negative and smallest


positive numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1.3, 5.24, 0.89, 21.0, 5.27, 1.3]
Output:
[5.24, 5.27]

Input:
[12.02, 20.3, 15.0, 19.0, 11.0, 14.99, 17.0, 17.0, 14.4, 16.8]
Output:
[14.99, 15.0]

# License: [Link]

# Define a function named 'test' that takes a list of numbers as input


def test(nums):
# Sort the unique elements in the list in ascending order
s = sorted(set(nums))

# Use list comprehension to find pairs of adjacent elements


# Then, find the pair with the smallest difference
return min([[a, b] for a, b in zip(s, s[1:])], key=lambda x: x[1] - x[0])

# Example 1
nums1 = [1.3, 5.24, 0.89, 21.0, 5.27, 1.3]
print("List of numbers:", nums1)
print("Two closest distinct numbers in the said list of numbers:")
print(test(nums1))

# Example 2
nums2 = [12.02, 20.3, 15.0, 19.0, 11.0, 14.99, 17.0, 17.0, 14.4, 16.8]
print("\nList of numbers:", nums2)
print("Two closest distinct numbers in the said list of numbers:")
print(test(nums2))

List of numbers: [1.3, 5.24, 0.89, 21.0, 5.27, 1.3]


Two closest distinct numbers in the said list of numbers:
[5.24, 5.27]

List of numbers: [12.02, 20.3, 15.0, 19.0, 11.0, 14.99, 17.0, 17.0, 14.4, 16.8]
Two closest distinct numbers in the said list of numbers:
[14.99, 15.0]

===================================================================================
=================

Puzzle 79
Question: Last update on May 30 2025 11:49:08 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:08 (UTC/GMT +8 hours)

Largest Negative and Smallest Positive Numbers

Write a Python program to find the largest negative and smallest positive numbers
(or 0 if none).

Visual Presentation:
Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the two closest distinct numbers in a given a list of


[Link]:Round each float in a list of numbers up to the next integer and
return the running total of the integer squares.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[-12, -6, 300, -40, 2, 2, 3, 57, -50, -22, 12, 40, 9, 11, 18]
Output:
[-6, 2]

Input:
[-1, -2, -3, -4]
Output:
[-1, 0]

Input:
[1, 2, 3, 4]
Output:
[0, 1]

Input:
[]
Output:
[0, 0]

# License: [Link]

# Define a function named 'test' that takes a list of numbers as input


def test(nums):
# Create a list 'pos' containing positive numbers from the input list
pos = [n for n in nums if n > 0]

# Create a list 'neg' containing negative numbers from the input list
neg = [n for n in nums if n < 0]

# Return a list containing the largest negative number (or 0 if none) and the
smallest positive number (or 0 if none)
return [max(neg) if neg else 0, min(pos) if pos else 0]

# Example 1
nums1 = [-12, -6, 300, -40, 2, 2, 3, 57, -50, -22, 12, 40, 9, 11, 18]
print("List of numbers:", nums1)
print("Largest negative and smallest positive numbers (or 0 if none) of the said
list:")
print(test(nums1))

# Example 2
nums2 = [-1, -2, -3, -4]
print("\nList of numbers:", nums2)
print("Largest negative and smallest positive numbers (or 0 if none) of the said
list:")
print(test(nums2))

# Example 3
nums3 = [1, 2, 3, 4]
print("\nList of numbers:", nums3)
print("Largest negative and smallest positive numbers (or 0 if none) of the said
list:")
print(test(nums3))

# Example 4
nums4 = []
print("\nList of numbers:", nums4)
print("Largest negative and smallest positive numbers (or 0 if none) of the said
list:")
print(test(nums4))

List of numbers: [-12, -6, 300, -40, 2, 2, 3, 57, -50, -22, 12, 40, 9, 11, 18]
Largest negative and smallest positive numbers (or 0 if none) of the said list:
[-6, 2]

List of numbers: [-1, -2, -3, -4]


Largest negative and smallest positive numbers (or 0 if none) of the said list:
[-1, 0]

List of numbers: [1, 2, 3, 4]


Largest negative and smallest positive numbers (or 0 if none) of the said list:
[0, 1]

List of numbers: []
Largest negative and smallest positive numbers (or 0 if none) of the said list:
[0, 0]

===================================================================================
=================

Puzzle 80
Question: Last update on May 30 2025 11:49:08 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:08 (UTC/GMT +8 hours)

Running Total of Integer Squares

Write a Python program to round each float in a given list of numbers up to the
next integer and return the running total of the integer squares.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the largest negative and smallest positive [Link]:Calculate the


average of the numbers a through b rounded to nearest integer, in binary.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[2.6, 3.5, 6.7, 2.3, 5.6]
Output:
[9, 25, 74, 83, 119]

Input:
[301.1, 401.4, -23.1, 13554122.0, 10201.0101, 10000000.0]
Output:
[91204, 252808, 253337, 183714223444221, 183714327525025, 283714327525025]

# License: [Link]

# Import the 'ceil' function from the 'math' module


from math import ceil
# Define a function named 'test' that takes a list of numbers as input
def test(nums):
# Initialize an empty list to store the running total of integer squares
running_squares = []

# Initialize a variable 'tot' to store the running total


tot = 0

# Iterate over each value 'v' in the input list 'nums'


for v in nums:
# Add the square of the ceiling of 'v' to the running total
tot += ceil(v) ** 2

# Append the current running total to the 'running_squares' list


running_squares.append(tot)

# Return the list of running totals


return running_squares

# Example 1
nums1 = [2.6, 3.5, 6.7, 2.3, 5.6]
print("List of numbers:", nums1)
print("Round each float of the said list up to the next integer and return the
running total of the integer squares:")
print(test(nums1))

# Example 2
nums2 = [301.1, 401.4, -23.1, 13554122.0, 10201.0101, 10000000.0]
print("\nList of numbers:", nums2)
print("Round each float of the said list up to the next integer and return the
running total of the integer squares:")
print(test(nums2))

List of numbers: [2.6, 3.5, 6.7, 2.3, 5.6]


Round each float of the said list up to the next integer and return the running
total of the integer squares:
[9, 25, 74, 83, 119]

List of numbers: [301.1, 401.4, -23.1, 13554122.0, 10201.0101, 10000000.0]


Round each float of the said list up to the next integer and return the running
total of the integer squares:
[91204, 252808, 253337, 183714223444221, 183714327525025, 283714327525025]

===================================================================================
=================

Puzzle 81
Question: Last update on May 30 2025 11:49:09 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:09 (UTC/GMT +8 hours)

Average in Binary of a Range

Write a Python program to calculate the average of the numbers a through b (b not
included) rounded to the nearest integer, in binary (or -1 if there are no such
numbers).
Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Round each float in a list of numbers up to the next integer and return
the running total of the integer [Link]:Find the sublist of numbers with only
odd digits in increasing order.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
4 , 7
Output:
0b101

Input:
11 , 19
Output:
0b1110

# License: [Link]

# Define a function named 'test' that takes two integer parameters, 'a' and 'b'
def test(a, b):
# Create a range of integers from 'a' to 'b' (exclusive)
r = range(a, b)

# Check if the range is empty


if len(r) == 0:
return "-1" # Return "-1" if the range is empty
# Calculate the average of the numbers in the range, round to the nearest
integer,
# and convert the result to binary representation
return bin(round(sum(r) / len(r)))

# Example 1
a1 = 4
b1 = 7
print("Range:", a1, ",", b1)
print("Average of the numbers", a1, "through", b1, "rounded to the nearest integer,
in binary:")
print(test(a1, b1))

# Example 2
a2 = 11
b2 = 19
print("\nRange:", a2, ",", b2)
print("Average of the numbers", a2, "through", b2, "rounded to the nearest integer,
in binary:")
print(test(a2, b2))

Range: 4 , 7
Average of the numbers 4 through 7 rounded to nearest integer, in binary:
0b101

Range: 11 , 19
Average of the numbers 11 through 19 rounded to nearest integer, in binary:
0b1110

===================================================================================
=================

Puzzle 82
Question: Last update on May 30 2025 11:49:09 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:09 (UTC/GMT +8 hours)

Increasing Odd-Digit Sublist

Write a Python program to find the sublist of numbers from a given list of numbers
with only odd digits in increasing order.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:


Go to:

Previous:Calculate the average of the numbers a through b rounded to nearest


integer, in [Link]:Find two indices making a given string unhappy.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 3, 79, 10, 4, 2, 39]
Output:
[1, 3, 39, 79]

Input:
[11, 31, 40, 68, 77, 93, 48, 1, 57]
Output:
[1, 11, 31, 57, 77, 93]

Input:
[9, -2, 3, 4, -2, 0, 2, -3, 8, -1]
Output:
[-3, -1, 3, 9]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Use a list comprehension to filter numbers with only odd digits and sort them
in increasing order
return sorted(n for n in nums if all(int(c) % 2 for c in str(abs(n))))

# Example 1
nums1 = [1, 3, 79, 10, 4, 2, 39]
print("Original list of numbers:")
print(nums1)
print("Sublist of numbers with only odd digits in increasing order:")
print(test(nums1))

# Example 2
nums2 = [11, 31, 40, 68, 77, 93, 48, 1, 57]
print("\nOriginal list of numbers:")
print(nums2)
print("Sublist of numbers with only odd digits in increasing order:")
print(test(nums2))

# Example 3
nums3 = [9, -2, 3, 4, -2, 0, 2, -3, 8, -1]
print("\nOriginal list of numbers:")
print(nums3)
print("Sublist of numbers with only odd digits in increasing order:")
print(test(nums3))

Original list of numbers:


[1, 3, 79, 10, 4, 2, 39]
Sublist of numbers of the said list with only odd digits in increasing order:
[1, 3, 39, 79]

Original list of numbers:


[11, 31, 40, 68, 77, 93, 48, 1, 57]
Sublist of numbers of the said list with only odd digits in increasing order:
[1, 11, 31, 57, 77, 93]

Original list of numbers:


[9, -2, 3, 4, -2, 0, 2, -3, 8, -1]
Sublist of numbers of the said list with only odd digits in increasing order:
[-3, -1, 3, 9]

===================================================================================
=================

Puzzle 83
Question: Last update on May 30 2025 11:49:10 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:10 (UTC/GMT +8 hours)

Indices for Unhappy String

A string is happy if every three consecutive characters are distinct. Write a


Python program to find two indices associated with a given string being unhappy.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the sublist of numbers with only odd digits in increasing


[Link]:Find the index of the matching parentheses for each character in a given
string.
Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
Python
Output:
None

Input:
Unhappy
Output:
[4, 5]

Input:
Find
Output:
None

Input:
Street
Output:
[3, 4]

# License: [Link]

# Define a function named 'test' that takes a string 's' as input


def test(s):
# Iterate through the characters of the string up to the second-to-last
character
for i in range(len(s) - 2):
# Check if consecutive characters are the same, return the indices if true
if s[i] == s[i + 1]:
return [i, i + 1]
# Check if characters with one character in between are the same, return
the indices if true
if s[i] == s[i + 2]:
return [i, i + 2]

# Example 1
strs1 = "Python"
print("Original string:", strs1)
print("Find two indices making the string unhappy:")
print(test(strs1))

# Example 2
strs2 = "Unhappy"
print("\nOriginal string:", strs2)
print("Find two indices making the string unhappy:")
print(test(strs2))

# Example 3
strs3 = "Find"
print("\nOriginal string:", strs3)
print("Find two indices making the string unhappy:")
print(test(strs3))

# Example 4
strs4 = "Street"
print("\nOriginal string:", strs4)
print("Find two indices making the string unhappy:")
print(test(strs4))

Original string: Python


Find two indices making the said string unhappy!
None

Original string: Unhappy


Find two indices making the said string unhappy!
[4, 5]

Original string: Find


Find two indices making the said string unhappy!
None

Original string: Street


Find two indices making the said string unhappy!
[3, 4]

===================================================================================
=================

Puzzle 84
Question: Last update on May 30 2025 11:49:10 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:10 (UTC/GMT +8 hours)

Matching Parentheses Indices

Write a Python program to find the index of the matching parentheses for each
character in a given string.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:
Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find two indices making a given string [Link]:Find an increasing


sequence consisting of the elements of the original list.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
()(())
Output:
[1, 0, 5, 4, 3, 2]

Input:
()()()
Output:
[1, 0, 3, 2, 5, 4]

Input:
((()))
Output:
[5, 4, 3, 2, 1, 0]

# License: [Link]

# Define a function named 'test' that takes a string of parentheses 'parens' as


input
def test(parens):
# Convert the string of parentheses into a list
a = list(parens)
# Initialize an empty stack to keep track of the indices of opening parentheses
stack = []

# Iterate through the enumerated characters in the list 'a'


for i, c in enumerate(a):
# Check if the character is an opening parenthesis "("
if c == "(":
# Push the current index onto the stack
[Link](i)
else:
# Update the indices of the matching parentheses
a[stack[-1]] = i
a[i] = [Link]()

# Return the list with indices of the matching parentheses


return a

# Example 1
parens1 = "()(())"
print("Original parentheses:", parens1)
print("Index of the matching parentheses for each character in a given string:")
print(test(parens1))

# Example 2
parens2 = "()()()"
print("\nOriginal parentheses:", parens2)
print("Index of the matching parentheses for each character in a given string:")
print(test(parens2))

# Example 3
parens3 = "((()))"
print("\nOriginal parentheses:", parens3)
print("Index of the matching parentheses for each character in a given string:")
print(test(parens3))

Original parentheses: ()(())


Index of the matching parentheses for each character in a given string:
[1, 0, 5, 4, 3, 2]

Original parentheses: ()()()


Index of the matching parentheses for each character in a given string:
[1, 0, 3, 2, 5, 4]

Original parentheses: ((()))


Index of the matching parentheses for each character in a given string:
[5, 4, 3, 2, 1, 0]

===================================================================================
=================

Puzzle 85
Question: Last update on May 30 2025 11:49:10 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:10 (UTC/GMT +8 hours)

Increasing Sequence from List

Write a Python program to find an increasing sequence consisting of the elements of


the original list.

Visual Presentation:

Sample Solution:

Python Code:
Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the index of the matching parentheses for each character in a given
[Link]:Find the vowels from each of the original texts (y counts as a vowel at
the end of the word).

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 3, 79, 10, 4, 2, 39]
Output:
[1, 2, 3, 4, 10, 39, 79]

Input:
[11, 31, 40, 68, 77, 93, 48, 1, 57]
Output:
[1, 11, 31, 40, 48, 57, 68, 77, 93]

Input:
[9, -2, 3, 4, -2, 0, 2, -3, 8, -1]
Output:
[-3, -2, -1, 0, 2, 3, 4, 8, 9]

# License: [Link]

# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# Use the 'set' data structure to remove duplicates and then sort the unique
elements
result = sorted(set(nums))
# Return the sorted and unique elements as the result
return result

# Example 1
nums1 = [1, 3, 79, 10, 4, 2, 39]
print("Original list of numbers:")
print(nums1)
print("Increasing sequence consisting of the elements of the said list:")
print(test(nums1))

# Example 2
nums2 = [11, 31, 40, 68, 77, 93, 48, 1, 57]
print("\nOriginal list of numbers:")
print(nums2)
print("Increasing sequence consisting of the elements of the said list:")
print(test(nums2))

# Example 3
nums3 = [9, -2, 3, 4, -2, 0, 2, -3, 8, -1]
print("\nOriginal list of numbers:")
print(nums3)
print("Increasing sequence consisting of the elements of the said list:")
print(test(nums3))

Original list of numbers:


[1, 3, 79, 10, 4, 2, 39]
Increasing sequence consisting of the elements of the said list:
[1, 2, 3, 4, 10, 39, 79]

Original list of numbers:


[11, 31, 40, 68, 77, 93, 48, 1, 57]
Increasing sequence consisting of the elements of the said list:
[1, 11, 31, 40, 48, 57, 68, 77, 93]

Original list of numbers:


[9, -2, 3, 4, -2, 0, 2, -3, 8, -1]
Increasing sequence consisting of the elements of the said list:
[-3, -2, -1, 0, 2, 3, 4, 8, 9]

===================================================================================
=================

Puzzle 86
Question:
Solution:

Last update on May 30 2025 11:49:10 (UTC/GMT +8 hours)

Extract Vowels with Y as Vowel

Write a Python program to find the vowels from each of the original texts (y counts
as a vowel at the end of the word) from a given list of strings.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:
For more Practice: Solve these Related Problems:

Go to:

Previous:Find an increasing sequence consisting of the elements of the original


[Link]:Find a valid substring of s that contains matching brackets, at least one
of which is nested.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
['w3resource', 'Python', 'Java', 'C++']
Output:
['eoue', 'o', 'aa', '']

Input:
['ably', 'abruptly', 'abecedary', 'apparently', 'acknowledgedly']
Output:
['ay', 'auy', 'aeeay', 'aaey', 'aoeey']

# License: [Link]

# Define a function named 'test' that takes a list of strings 'strs' as input
def test(strs):
# Use a list comprehension to iterate over each text in 'strs'
return [
"".join(c for c in text if [Link]() in "aeiou") + (text[-1] if text[-
1].lower() == "y" else "")
for text in strs
]

# Example 1
strs1 = ["w3resource", "Python", "Java", "C++"]
print("Original List of strings:", strs1)
print("Vowels from each of the original texts (y counts as a vowel at the end of
the word:")
print(test(strs1))

# Example 2
strs2 = ["ably", "abruptly", "abecedary", "apparently", "acknowledgedly"]
print("\nOriginal List of strings:", strs2)
print("Positions of all uppercase vowels (not counting Y) in even indices:")
print(test(strs2))
Original List of strings: ['w3resource', 'Python', 'Java', 'C++']
Vowels from each of the original texts (y counts as a vowel at the end of the word:
['eoue', 'o', 'aa', '']

Original List of strings: ['ably', 'abruptly', 'abecedary', 'apparently',


'acknowledgedly']
Positions of all uppercase vowels (not counting Y) in even indices:
['ay', 'auy', 'aeeay', 'aaey', 'aoeey']

===================================================================================
=================

Puzzle 87
Question: Last update on May 30 2025 11:49:11 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:11 (UTC/GMT +8 hours)

Valid Substring with Nested Brackets

Write a Python program to find a valid substring of a given string that contains
matching brackets, at least one of which is nested.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the vowels from each of the original texts (y counts as a vowel at
the end of the word).Next:Find an integer with the given number of even and odd
digits.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.


Input:
]][][[]]]

Output:
[[]]

Input:
]]]]]]]]]]]]]]]]][][][][]]]]]]]]]]][[[][[][[[[[][][][]][[[[[[[[[[[[[[[[[[

Output:
[[][][][]]

# License: [Link]

# Define a function named 'test' that takes a string 's' as input


def test(s):
import re
# Use regular expression to search for a valid substring with matching brackets
return [Link](r"\[(\[\])+\]", s).group(0)

# Example 1
brackets1 = "]][][[]]]"
print("Original List of strings:", brackets1)
print("Find a valid substring of the said string that contains matching brackets,
at least one of which is nested:")
print(test(brackets1))

# Example 2
brackets2 = "]]]]]]]]]]]]]]]]][][][][]]]]]]]]]]][[[][[][[[[[][][][]]
[[[[[[[[[[[[[[[[[["
print("\nOriginal List of strings:", brackets2)
print("\nFind a valid substring of the said string that contains matching brackets,
at least one of which is nested:")
print(test(brackets2))

Original List of strings: ]][][[]]]

Find a valid substring of the said string that contains matching brackets, at least
one of which is nested:
[[]]

Original List of strings: ]]]]]]]]]]]]]]]]][][][][]]]]]]]]]]][[[][[][[[[[][][][]]


[[[[[[[[[[[[[[[[[[

Find a valid substring of the said string that contains matching brackets, at least
one of which is nested:
[[][][][]]

===================================================================================
=================

Puzzle 88
Question: Last update on May 30 2025 11:49:11 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:11 (UTC/GMT +8 hours)

Integer with Specified Even and Odd Digits

Write a Python program to find an integer (n >= 0) with the given number of even
and odd digits.

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find a valid substring of s that contains matching brackets, at least one


of which is [Link]:Find all integers that are the product of exactly three
primes.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
Number of even digits: 2 ,Number of odd digits: 3
Output:
22333

Input:
Number of even digits: 4 ,Number of odd digits: 7
Output:
22223333333

# License: [Link]

# Define a function named 'test' that takes two parameters 'evens' and 'odds'
def test(evens, odds):
# Generate an integer (>= 0) with the given number of even and odd digits
return int(evens * "2" + odds * "3")

# Example 1
evens1 = 2
odds1 = 3
print("Number of even digits:", evens1, ", Number of odd digits:", odds1)
print("Integer (>= 0) with the given number of even and odd digits:")
print(test(evens1, odds1))

# Example 2
evens2 = 4
odds2 = 7
print("\nNumber of even digits:", evens2, ", Number of odd digits:", odds2)
print("Integer (>= 0) with the given number of even and odd digits:")
print(test(evens2, odds2))

Number of even digits: 2 ,Number of odd digits: 3


Integer(>= 0) with the given number of even and odd digits:
22333

Number of even digits: 4 ,Number of odd digits: 7


Integer(>= 0) with the given number of even and odd digits:
22223333333

===================================================================================
=================

Puzzle 89
Question: Last update on May 30 2025 11:49:12 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:12 (UTC/GMT +8 hours)

Integers as Product of Three Primes

Write a Python program to find all integers <= 1000 that are the product of exactly
three primes. Each integer should be represented as a list of its three prime
factors.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find an integer with the given number of even and odd [Link]:Each
triple of eaten, need, stock return a pair of total appetite and remaining.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
10
Output:
[[2, 2, 2]]

Input:
50
Output:
[[2, 2, 2], [2, 2, 3], [2, 2, 5], [2, 2, 7], [2, 2, 11], [2, 3, 2], [2, 3, 3], [2,
3, 5], [2, 3, 7], [2, 5, 2], [2, 5, 3], [2, 5, 5], [2, 7, 2], [2, 7, 3], [2, 11,
2], [3, 2, 2], [3, 2, 3], [3, 2, 5], [3, 2, 7], [3, 3, 2], [3, 3, 3], [3, 3, 5],
[3, 5, 2], [3, 5, 3], [3, 7, 2], [5, 2, 2], [5, 2, 3], [5, 2, 5], [5, 3, 2], [5, 3,
3], [5, 5, 2], [7, 2, 2], [7, 2, 3], [7, 3, 2], [11, 2, 2]]

# License: [Link]

# Define a function named 'test' that takes an integer parameter 'n'


def test(n):
# Generate a list 'ps' containing all prime numbers less than 'n'
ps = [p for p in range(2, n) if all(p % sat != 0 for sat in range(2, p))]

# Generate a list of lists containing all integers <= 'n' that are the product
of exactly three primes
return [[p, q, r] for p in ps for q in ps for r in ps if p * q * r <= n]

# Example 1
n1 = 10
print("Number:", n1)
print("Find all integers <= said number that are the product of exactly three
primes:")
print(test(n1))

# Example 2
n2 = 50
print("\nNumber:", n2)
print("Find all integers <= said number that are the product of exactly three
primes:")
print(test(n2))

# Example 3
n3 = 1000
print("\nNumber:", n3)
print("Find all integers <= said number that are the product of exactly three
primes:")
print(test(n3))
Number: 10
Find all integers <= said number that are the product of exactly three primes:
[[2, 2, 2]]

Number: 50
Find all integers <= said number that are the product of exactly three primes:
[[2, 2, 2], [2, 2, 3], [2, 2, 5], [2, 2, 7], [2, 2, 11], [2, 3, 2], [2, 3, 3], [2,
3, 5], [2, 3, 7], [2, 5, 2], [2, 5, 3], [2, 5, 5], [2, 7, 2], [2, 7, 3], [2, 11,
2], [3, 2, 2], [3, 2, 3], [3, 2, 5], [3, 2, 7], [3, 3, 2], [3, 3, 3], [3, 3, 5],
[3, 5, 2], [3, 5, 3], [3, 7, 2], [5, 2, 2], [5, 2, 3], [5, 2, 5], [5, 3, 2], [5, 3,
3], [5, 5, 2], [7, 2, 2], [7, 2, 3], [7, 3, 2], [11, 2, 2]]

Number: 1000
Find all integers <= said number that are the product of exactly three primes:
[[2, 2, 2], [2, 2, 3], [2, 2, 5], [2, 2, 7], [2, 2, 11], [2, 2, 13], [2, 2, 17],
[2, 2, 19], [2, 2, 23], [2, 2, 29], [2, 2, 31], [2, 2, 37], [2, 2, 41], [2, 2, 43],
[2, 2, 47], [2, 2, 53], [2, 2, 59], [2, 2, 61], [2, 2, 67], [2, 2, 71], [2, 2, 73],
[2, 2, 79], [2, 2, 83], [2, 2, 89], [2, 2, 97], [2, 2, 101], [2, 2, 103], [2, 2,
107], [2, 2, 109], [2, 2, 113], [2, 2, 127], [2, 2, 131], [2, 2, 137], [2, 2, 139],
[2, 2, 149], [2, 2, 151], [2, 2, 157], [2, 2, 163], [2, 2, 167], [2, 2, 173], [2,
2, 179], [2, 2, 181], [2, 2, 191], [2, 2, 193], [2, 2, 197], [2, 2, 199], [2, 2,
211], [2, 2, 223], [2, 2, 227], [2, 2, 229], [2, 2, 233], [2, 2, 239], [2, 2, 241],
[2, 3, 2], [2, 3, 3], [2, 3, 5], [2, 3, 7], [2, 3, 11], [2, 3, 13], [2, 3, 17], [2,
3, 19], [2, 3, 23], [2, 3, 29], [2, 3, 31], [2, 3, 37], [2, 3, 41], [2, 3, 43], [2,
3, 47], [2, 3, 53], [2, 3, 59], [2, 3, 61], [2, 3, 67], [2, 3, 71], [2, 3, 73], [2,
3, 79], [2, 3, 83], [2, 3, 89], [2, 3, 97], [2, 3, 101], [2, 3, 103], [2, 3, 107],
[2, 3, 109], [2, 3, 113], [2, 3, 127], [2, 3, 131], [2, 3, 137], [2, 3, 139], [2,
3, 149], [2, 3, 151], [2, 3, 157], [2, 3, 163], [2, 5, 2], [2, 5, 3], [2, 5, 5],
[2, 5, 7], [2, 5, 11], [2, 5, 13], [2, 5, 17], [2, 5, 19], [2, 5, 23], [2, 5, 29],
[2, 5, 31], [2, 5, 37], [2, 5, 41], [2, 5, 43], [2, 5, 47], [2, 5, 53], [2, 5, 59],
[2, 5, 61], [2, 5, 67], [2, 5, 71], [2, 5, 73], [2, 5, 79], [2, 5, 83], [2, 5, 89],
[2, 5, 97], [2, 7, 2], [2, 7, 3], [2, 7, 5], [2, 7, 7], [2, 7, 11], [2, 7, 13], [2,
7, 17], [2, 7, 19], [2, 7, 23], [2, 7, 29], [2, 7, 31], [2, 7, 37], [2, 7, 41], [2,
7, 43], [2, 7, 47], [2, 7, 53], [2, 7, 59], [2, 7, 61], [2, 7, 67], [2, 7, 71], [2,
11, 2], [2, 11, 3], [2, 11, 5], [2, 11, 7], [2, 11, 11], [2, 11, 13], [2, 11, 17],
[2, 11, 19], [2, 11, 23], [2, 11, 29], [2, 11, 31], [2, 11, 37], [2, 11, 41], [2,
11, 43], [2, 13, 2], [2, 13, 3], [2, 13, 5], [2, 13, 7], [2, 13, 11], [2, 13, 13],
[2, 13, 17], [2, 13, 19], [2, 13, 23], [2, 13, 29], [2, 13, 31], [2, 13, 37], [2,
17, 2], [2, 17, 3], [2, 17, 5], [2, 17, 7], [2, 17, 11], [2, 17, 13], [2, 17, 17],
[2, 17, 19], [2, 17, 23], [2, 17, 29], [2, 19, 2], [2, 19, 3], [2, 19, 5], [2, 19,
7], [2, 19, 11], [2, 19, 13], [2, 19, 17], [2, 19, 19], [2, 19, 23], [2, 23, 2],
[2, 23, 3], [2, 23, 5], [2, 23, 7], [2, 23, 11], [2, 23, 13], [2, 23, 17], [2, 23,
19], [2, 29, 2], [2, 29, 3], [2, 29, 5], [2, 29, 7], [2, 29, 11], [2, 29, 13], [2,
29, 17], [2, 31, 2], [2, 31, 3], [2, 31, 5], [2, 31, 7], [2, 31, 11], [2, 31, 13],
[2, 37, 2], [2, 37, 3], [2, 37, 5], [2, 37, 7], [2, 37, 11], [2, 37, 13], [2, 41,
2], [2, 41, 3], [2, 41, 5], [2, 41, 7], [2, 41, 11], [2, 43, 2], [2, 43, 3], [2,
43, 5], [2, 43, 7], [2, 43, 11], [2, 47, 2], [2, 47, 3], [2, 47, 5], [2, 47, 7],
[2, 53, 2], [2, 53, 3], [2, 53, 5], [2, 53, 7], [2, 59, 2], [2, 59, 3], [2, 59, 5],
[2, 59, 7], [2, 61, 2], [2, 61, 3], [2, 61, 5], [2, 61, 7], [2, 67, 2], [2, 67, 3],
[2, 67, 5], [2, 67, 7], [2, 71, 2], [2, 71, 3], [2, 71, 5], [2, 71, 7], [2, 73, 2],
[2, 73, 3], [2, 73, 5], [2, 79, 2], [2, 79, 3], [2, 79, 5], [2, 83, 2], [2, 83, 3],
[2, 83, 5], [2, 89, 2], [2, 89, 3], [2, 89, 5], [2, 97, 2], [2, 97, 3], [2, 97, 5],
[2, 101, 2], [2, 101, 3], [2, 103, 2], [2, 103, 3], [2, 107, 2], [2, 107, 3], [2,
109, 2], [2, 109, 3], [2, 113, 2], [2, 113, 3], [2, 127, 2], [2, 127, 3], [2, 131,
2], [2, 131, 3], [2, 137, 2], [2, 137, 3], [2, 139, 2], [2, 139, 3], [2, 149, 2],
[2, 149, 3], [2, 151, 2], [2, 151, 3], [2, 157, 2], [2, 157, 3], [2, 163, 2], [2,
163, 3], [2, 167, 2], [2, 173, 2], [2, 179, 2], [2, 181, 2], [2, 191, 2], [2, 193,
2], [2, 197, 2], [2, 199, 2], [2, 211, 2], [2, 223, 2], [2, 227, 2], [2, 229, 2],
[2, 233, 2], [2, 239, 2], [2, 241, 2], [3, 2, 2], [3, 2, 3], [3, 2, 5], [3, 2, 7],
[3, 2, 11], [3, 2, 13], [3, 2, 17], [3, 2, 19], [3, 2, 23], [3, 2, 29], [3, 2, 31],
[3, 2, 37], [3, 2, 41], [3, 2, 43], [3, 2, 47], [3, 2, 53], [3, 2, 59], [3, 2, 61],
[3, 2, 67], [3, 2, 71], [3, 2, 73], [3, 2, 79], [3, 2, 83], [3, 2, 89], [3, 2, 97],
[3, 2, 101], [3, 2, 103], [3, 2, 107], [3, 2, 109], [3, 2, 113], [3, 2, 127], [3,
2, 131], [3, 2, 137], [3, 2, 139], [3, 2, 149], [3, 2, 151], [3, 2, 157], [3, 2,
163], [3, 3, 2], [3, 3, 3], [3, 3, 5], [3, 3, 7], [3, 3, 11], [3, 3, 13], [3, 3,
17], [3, 3, 19], [3, 3, 23], [3, 3, 29], [3, 3, 31], [3, 3, 37], [3, 3, 41], [3, 3,
43], [3, 3, 47], [3, 3, 53], [3, 3, 59], [3, 3, 61], [3, 3, 67], [3, 3, 71], [3, 3,
73], [3, 3, 79], [3, 3, 83], [3, 3, 89], [3, 3, 97], [3, 3, 101], [3, 3, 103], [3,
3, 107], [3, 3, 109], [3, 5, 2], [3, 5, 3], [3, 5, 5], [3, 5, 7], [3, 5, 11], [3,
5, 13], [3, 5, 17], [3, 5, 19], [3, 5, 23], [3, 5, 29], [3, 5, 31], [3, 5, 37], [3,
5, 41], [3, 5, 43], [3, 5, 47], [3, 5, 53], [3, 5, 59], [3, 5, 61], [3, 7, 2], [3,
7, 3], [3, 7, 5], [3, 7, 7], [3, 7, 11], [3, 7, 13], [3, 7, 17], [3, 7, 19], [3, 7,
23], [3, 7, 29], [3, 7, 31], [3, 7, 37], [3, 7, 41], [3, 7, 43], [3, 7, 47], [3,
11, 2], [3, 11, 3], [3, 11, 5], [3, 11, 7], [3, 11, 11], [3, 11, 13], [3, 11, 17],
[3, 11, 19], [3, 11, 23], [3, 11, 29], [3, 13, 2], [3, 13, 3], [3, 13, 5], [3, 13,
7], [3, 13, 11], [3, 13, 13], [3, 13, 17], [3, 13, 19], [3, 13, 23], [3, 17, 2],
[3, 17, 3], [3, 17, 5], [3, 17, 7], [3, 17, 11], [3, 17, 13], [3, 17, 17], [3, 17,
19], [3, 19, 2], [3, 19, 3], [3, 19, 5], [3, 19, 7], [3, 19, 11], [3, 19, 13], [3,
19, 17], [3, 23, 2], [3, 23, 3], [3, 23, 5], [3, 23, 7], [3, 23, 11], [3, 23, 13],
[3, 29, 2], [3, 29, 3], [3, 29, 5], [3, 29, 7], [3, 29, 11], [3, 31, 2], [3, 31,
3], [3, 31, 5], [3, 31, 7], [3, 37, 2], [3, 37, 3], [3, 37, 5], [3, 37, 7], [3, 41,
2], [3, 41, 3], [3, 41, 5], [3, 41, 7], [3, 43, 2], [3, 43, 3], [3, 43, 5], [3, 43,
7], [3, 47, 2], [3, 47, 3], [3, 47, 5], [3, 47, 7], [3, 53, 2], [3, 53, 3], [3, 53,
5], [3, 59, 2], [3, 59, 3], [3, 59, 5], [3, 61, 2], [3, 61, 3], [3, 61, 5], [3, 67,
2], [3, 67, 3], [3, 71, 2], [3, 71, 3], [3, 73, 2], [3, 73, 3], [3, 79, 2], [3, 79,
3], [3, 83, 2], [3, 83, 3], [3, 89, 2], [3, 89, 3], [3, 97, 2], [3, 97, 3], [3,
101, 2], [3, 101, 3], [3, 103, 2], [3, 103, 3], [3, 107, 2], [3, 107, 3], [3, 109,
2], [3, 109, 3], [3, 113, 2], [3, 127, 2], [3, 131, 2], [3, 137, 2], [3, 139, 2],
[3, 149, 2], [3, 151, 2], [3, 157, 2], [3, 163, 2], [5, 2, 2], [5, 2, 3], [5, 2,
5], [5, 2, 7], [5, 2, 11], [5, 2, 13], [5, 2, 17], [5, 2, 19], [5, 2, 23], [5, 2,
29], [5, 2, 31], [5, 2, 37], [5, 2, 41], [5, 2, 43], [5, 2, 47], [5, 2, 53], [5, 2,
59], [5, 2, 61], [5, 2, 67], [5, 2, 71], [5, 2, 73], [5, 2, 79], [5, 2, 83], [5, 2,
89], [5, 2, 97], [5, 3, 2], [5, 3, 3], [5, 3, 5], [5, 3, 7], [5, 3, 11], [5, 3,
13], [5, 3, 17], [5, 3, 19], [5, 3, 23], [5, 3, 29], [5, 3, 31], [5, 3, 37], [5, 3,
41], [5, 3, 43], [5, 3, 47], [5, 3, 53], [5, 3, 59], [5, 3, 61], [5, 5, 2], [5, 5,
3], [5, 5, 5], [5, 5, 7], [5, 5, 11], [5, 5, 13], [5, 5, 17], [5, 5, 19], [5, 5,
23], [5, 5, 29], [5, 5, 31], [5, 5, 37], [5, 7, 2], [5, 7, 3], [5, 7, 5], [5, 7,
7], [5, 7, 11], [5, 7, 13], [5, 7, 17], [5, 7, 19], [5, 7, 23], [5, 11, 2], [5, 11,
3], [5, 11, 5], [5, 11, 7], [5, 11, 11], [5, 11, 13], [5, 11, 17], [5, 13, 2], [5,
13, 3], [5, 13, 5], [5, 13, 7], [5, 13, 11], [5, 13, 13], [5, 17, 2], [5, 17, 3],
[5, 17, 5], [5, 17, 7], [5, 17, 11], [5, 19, 2], [5, 19, 3], [5, 19, 5], [5, 19,
7], [5, 23, 2], [5, 23, 3], [5, 23, 5], [5, 23, 7], [5, 29, 2], [5, 29, 3], [5, 29,
5], [5, 31, 2], [5, 31, 3], [5, 31, 5], [5, 37, 2], [5, 37, 3], [5, 37, 5], [5, 41,
2], [5, 41, 3], [5, 43, 2], [5, 43, 3], [5, 47, 2], [5, 47, 3], [5, 53, 2], [5, 53,
3], [5, 59, 2], [5, 59, 3], [5, 61, 2], [5, 61, 3], [5, 67, 2], [5, 71, 2], [5, 73,
2], [5, 79, 2], [5, 83, 2], [5, 89, 2], [5, 97, 2], [7, 2, 2], [7, 2, 3], [7, 2,
5], [7, 2, 7], [7, 2, 11], [7, 2, 13], [7, 2, 17], [7, 2, 19], [7, 2, 23], [7, 2,
29], [7, 2, 31], [7, 2, 37], [7, 2, 41], [7, 2, 43], [7, 2, 47], [7, 2, 53], [7, 2,
59], [7, 2, 61], [7, 2, 67], [7, 2, 71], [7, 3, 2], [7, 3, 3], [7, 3, 5], [7, 3,
7], [7, 3, 11], [7, 3, 13], [7, 3, 17], [7, 3, 19], [7, 3, 23], [7, 3, 29], [7, 3,
31], [7, 3, 37], [7, 3, 41], [7, 3, 43], [7, 3, 47], [7, 5, 2], [7, 5, 3], [7, 5,
5], [7, 5, 7], [7, 5, 11], [7, 5, 13], [7, 5, 17], [7, 5, 19], [7, 5, 23], [7, 7,
2], [7, 7, 3], [7, 7, 5], [7, 7, 7], [7, 7, 11], [7, 7, 13], [7, 7, 17], [7, 7,
19], [7, 11, 2], [7, 11, 3], [7, 11, 5], [7, 11, 7], [7, 11, 11], [7, 13, 2], [7,
13, 3], [7, 13, 5], [7, 13, 7], [7, 17, 2], [7, 17, 3], [7, 17, 5], [7, 17, 7], [7,
19, 2], [7, 19, 3], [7, 19, 5], [7, 19, 7], [7, 23, 2], [7, 23, 3], [7, 23, 5], [7,
29, 2], [7, 29, 3], [7, 31, 2], [7, 31, 3], [7, 37, 2], [7, 37, 3], [7, 41, 2], [7,
41, 3], [7, 43, 2], [7, 43, 3], [7, 47, 2], [7, 47, 3], [7, 53, 2], [7, 59, 2], [7,
61, 2], [7, 67, 2], [7, 71, 2], [11, 2, 2], [11, 2, 3], [11, 2, 5], [11, 2, 7],
[11, 2, 11], [11, 2, 13], [11, 2, 17], [11, 2, 19], [11, 2, 23], [11, 2, 29], [11,
2, 31], [11, 2, 37], [11, 2, 41], [11, 2, 43], [11, 3, 2], [11, 3, 3], [11, 3, 5],
[11, 3, 7], [11, 3, 11], [11, 3, 13], [11, 3, 17], [11, 3, 19], [11, 3, 23], [11,
3, 29], [11, 5, 2], [11, 5, 3], [11, 5, 5], [11, 5, 7], [11, 5, 11], [11, 5, 13],
[11, 5, 17], [11, 7, 2], [11, 7, 3], [11, 7, 5], [11, 7, 7], [11, 7, 11], [11, 11,
2], [11, 11, 3], [11, 11, 5], [11, 11, 7], [11, 13, 2], [11, 13, 3], [11, 13, 5],
[11, 17, 2], [11, 17, 3], [11, 17, 5], [11, 19, 2], [11, 19, 3], [11, 23, 2], [11,
23, 3], [11, 29, 2], [11, 29, 3], [11, 31, 2], [11, 37, 2], [11, 41, 2], [11, 43,
2], [13, 2, 2], [13, 2, 3], [13, 2, 5], [13, 2, 7], [13, 2, 11], [13, 2, 13], [13,
2, 17], [13, 2, 19], [13, 2, 23], [13, 2, 29], [13, 2, 31], [13, 2, 37], [13, 3,
2], [13, 3, 3], [13, 3, 5], [13, 3, 7], [13, 3, 11], [13, 3, 13], [13, 3, 17], [13,
3, 19], [13, 3, 23], [13, 5, 2], [13, 5, 3], [13, 5, 5], [13, 5, 7], [13, 5, 11],
[13, 5, 13], [13, 7, 2], [13, 7, 3], [13, 7, 5], [13, 7, 7], [13, 11, 2], [13, 11,
3], [13, 11, 5], [13, 13, 2], [13, 13, 3], [13, 13, 5], [13, 17, 2], [13, 17, 3],
[13, 19, 2], [13, 19, 3], [13, 23, 2], [13, 23, 3], [13,
29, 2], [13, 31, 2], [13, 37, 2], [17, 2, 2], [17, 2, 3], [17, 2, 5], [17, 2, 7],
[17, 2, 11], [17, 2, 13], [17, 2, 17], [17, 2, 19], [17, 2, 23], [17, 2, 29], [17,
3, 2], [17, 3, 3], [17, 3, 5], [17, 3, 7], [17, 3, 11], [17, 3, 13], [17, 3, 17],
[17, 3, 19], [17, 5, 2], [17, 5, 3], [17, 5, 5], [17, 5, 7], [17, 5, 11], [17, 7,
2], [17, 7, 3], [17, 7, 5], [17, 7, 7], [17, 11, 2], [17, 11, 3], [17, 11, 5], [17,
13, 2], [17, 13, 3], [17, 17, 2], [17, 17, 3], [17, 19, 2], [17, 19, 3], [17, 23,
2], [17, 29, 2], [19, 2, 2], [19, 2, 3], [19, 2, 5], [19, 2, 7], [19, 2, 11], [19,
2, 13], [19, 2, 17], [19, 2, 19], [19, 2, 23], [19, 3, 2], [19, 3, 3], [19, 3, 5],
[19, 3, 7], [19, 3, 11], [19, 3, 13], [19, 3, 17], [19, 5, 2], [19, 5, 3], [19, 5,
5], [19, 5, 7], [19, 7, 2], [19, 7, 3], [19, 7, 5], [19, 7, 7], [19, 11, 2], [19,
11, 3], [19, 13, 2], [19, 13, 3], [19, 17, 2], [19, 17, 3], [19, 19, 2], [19, 23,
2], [23, 2, 2], [23, 2, 3], [23, 2, 5], [23, 2, 7], [23, 2, 11], [23, 2, 13], [23,
2, 17], [23, 2, 19], [23, 3, 2], [23, 3, 3], [23, 3, 5], [23, 3, 7], [23, 3, 11],
[23, 3, 13], [23, 5, 2], [23, 5, 3], [23, 5, 5], [23, 5, 7], [23, 7, 2], [23, 7,
3], [23, 7, 5], [23, 11, 2], [23, 11, 3], [23, 13, 2], [23, 13, 3], [23, 17, 2],
[23, 19, 2], [29, 2, 2], [29, 2, 3], [29, 2, 5], [29, 2, 7], [29, 2, 11], [29, 2,
13], [29, 2, 17], [29, 3, 2], [29, 3, 3], [29, 3, 5], [29, 3, 7], [29, 3, 11], [29,
5, 2], [29, 5, 3], [29, 5, 5], [29, 7, 2], [29, 7, 3], [29, 11, 2], [29, 11, 3],
[29, 13, 2], [29, 17, 2], [31, 2, 2], [31, 2, 3], [31, 2, 5], [31, 2, 7], [31, 2,
11], [31, 2, 13], [31, 3, 2], [31, 3, 3], [31, 3, 5], [31, 3, 7], [31, 5, 2], [31,
5, 3], [31, 5, 5], [31, 7, 2], [31, 7, 3], [31, 11, 2], [31, 13, 2], [37, 2, 2],
[37, 2, 3], [37, 2, 5], [37, 2, 7], [37, 2, 11], [37, 2, 13], [37, 3, 2], [37, 3,
3], [37, 3, 5], [37, 3, 7], [37, 5, 2], [37, 5, 3], [37, 5, 5], [37, 7, 2], [37, 7,
3], [37, 11, 2], [37, 13, 2], [41, 2, 2], [41, 2, 3], [41, 2, 5], [41, 2, 7], [41,
2, 11], [41, 3, 2], [41, 3, 3], [41, 3, 5], [41, 3, 7], [41, 5, 2], [41, 5, 3],
[41, 7, 2], [41, 7, 3], [41, 11, 2], [43, 2, 2], [43, 2, 3], [43, 2, 5], [43, 2,
7], [43, 2, 11], [43, 3, 2], [43, 3, 3], [43, 3, 5], [43, 3, 7], [43, 5, 2], [43,
5, 3], [43, 7, 2], [43, 7, 3], [43, 11, 2], [47, 2, 2], [47, 2, 3], [47, 2, 5],
[47, 2, 7], [47, 3, 2], [47, 3, 3], [47, 3, 5], [47, 3, 7], [47, 5, 2], [47, 5, 3],
[47, 7, 2], [47, 7, 3], [53, 2, 2], [53, 2, 3], [53, 2, 5], [53, 2, 7], [53, 3, 2],
[53, 3, 3], [53, 3, 5], [53, 5, 2], [53, 5, 3], [53, 7, 2], [59, 2, 2], [59, 2, 3],
[59, 2, 5], [59, 2, 7], [59, 3, 2], [59, 3, 3], [59, 3, 5], [59, 5, 2], [59, 5, 3],
[59, 7, 2], [61, 2, 2], [61, 2, 3], [61, 2, 5], [61, 2, 7], [61, 3, 2], [61, 3, 3],
[61, 3, 5], [61, 5, 2], [61, 5, 3], [61, 7, 2], [67, 2, 2], [67, 2, 3], [67, 2, 5],
[67, 2, 7], [67, 3, 2], [67, 3, 3], [67, 5, 2], [67, 7, 2], [71, 2, 2], [71, 2, 3],
[71, 2, 5], [71, 2, 7], [71, 3, 2], [71, 3, 3], [71, 5, 2], [71, 7, 2], [73, 2, 2],
[73, 2, 3], [73, 2, 5], [73, 3, 2], [73, 3, 3], [73, 5, 2], [79, 2, 2], [79, 2, 3],
[79, 2, 5], [79, 3, 2], [79, 3, 3], [79, 5, 2], [83, 2, 2], [83, 2, 3], [83, 2, 5],
[83, 3, 2], [83, 3, 3], [83, 5, 2], [89, 2, 2], [89, 2, 3], [89, 2, 5], [89, 3, 2],
[89, 3, 3], [89, 5, 2], [97, 2, 2], [97, 2, 3], [97, 2, 5], [97, 3, 2], [97, 3, 3],
[97, 5, 2], [101, 2, 2], [101, 2, 3], [101, 3, 2], [101, 3, 3], [103, 2, 2], [103,
2, 3], [103, 3, 2], [103, 3, 3], [107, 2, 2], [107, 2, 3], [107, 3, 2], [107, 3,
3], [109, 2, 2], [109, 2, 3], [109, 3, 2], [109, 3, 3], [113, 2, 2], [113, 2, 3],
[113, 3, 2], [127, 2, 2], [127, 2, 3], [127, 3, 2], [131, 2, 2], [131, 2, 3], [131,
3, 2], [137, 2, 2], [137, 2, 3], [137, 3, 2], [139, 2, 2], [139, 2, 3], [139, 3,
2], [149, 2, 2], [149, 2, 3], [149, 3, 2], [151, 2, 2], [151, 2, 3], [151, 3, 2],
[157, 2, 2], [157, 2, 3], [157, 3, 2], [163, 2, 2], [163, 2, 3], [163, 3, 2], [167,
2, 2], [173, 2, 2], [179, 2, 2], [181, 2, 2], [191, 2, 2], [193, 2, 2], [197, 2,
2], [199, 2, 2], [211, 2, 2], [223, 2, 2], [227, 2, 2], [229, 2, 2], [233, 2, 2],
[239, 2, 2], [241, 2, 2]]

===================================================================================
=================

Puzzle 90
Question:
Solution:

Last update on May 30 2025 11:49:12 (UTC/GMT +8 hours)

Appetite and Stock Calculation

For each triple of eaten, need, and stock, write a Python program to get a pair of
total appetite and remaining.

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find all integers that are the product of exactly three [Link]:Find
all n-digit integers that start or end with 2.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[[2, 5, 6], [3, 9, 22]]
Output:
[[7, 1], [12, 13]]
Input:
[[2, 3, 18], [4, 9, 2], [2, 5, 7], [3, 8, 12], [4, 9, 106]]
Output:
[[5, 15], [6, 0], [7, 2], [11, 4], [13, 97]]

Input:
[[1, 2, 3], [4, 5, 6]]
Output:
[[3, 1], [9, 1]]

# License: [Link]

# Define a function named 'test' that takes a list of triples as parameter 'nums'
def test(nums):
# Return a list of lists, where each inner list contains the total appetite and
remaining items
return [[a + min(b, c), max(0, c - b)] for a, b, c in nums]

# Example 1
nums1 = [[2, 5, 6], [3, 9, 22]]
print("Original list (triple) of lists:")
print(nums1)
print("Each triple of eaten, need, stock return a pair of total appetite and
remaining:")
print(test(nums1))

# Example 2
nums2 = [[2, 3, 18], [4, 9, 2], [2, 5, 7], [3, 8, 12], [4, 9, 106]]
print("\nOriginal list (triple) of lists:")
print(nums2)
print("Each triple of eaten, need, stock return a pair of total appetite and
remaining:")
print(test(nums2))

# Example 3
nums3 = [[1, 2, 3], [4, 5, 6]]
print("\nOriginal list (triple) of lists:")
print(nums3)
print("Each triple of eaten, need, stock return a pair of total appetite and
remaining:")
print(test(nums3))

Original list (triple) of lists:


[[2, 5, 6], [3, 9, 22]]
Each triple of eaten, need, stock return a pair of total appetite and remaining:
[[7, 1], [12, 13]]

Original list (triple) of lists:


[[2, 3, 18], [4, 9, 2], [2, 5, 7], [3, 8, 12], [4, 9, 106]]
Each triple of eaten, need, stock return a pair of total appetite and remaining:
[[5, 15], [6, 0], [7, 2], [11, 4], [13, 97]]

Original list (triple) of lists:


[[1, 2, 3], [4, 5, 6]]
Each triple of eaten, need, stock return a pair of total appetite and remaining:
[[3, 1], [9, 1]]

===================================================================================
=================

Puzzle 91
Question: Last update on May 30 2025 11:49:13 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:13 (UTC/GMT +8 hours)

Find n-Digit Integers Starting or Ending with 2

Write a Python program to find all n-digit integers that start or end with 2.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Each triple of eaten, need, stock return a pair of total appetite and
[Link]:Start with a list of integers, keep every other element in place and
otherwise sort the list.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: 1
Output:
[2]

Input: 2
Output:
[12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 42, 52, 62, 72, 82, 92]

Input: 3
Output:
[102, 112, 122, 132, 142, 152, 162, 172, 182, 192, 200, 201, 202, 203, 204, 205,
206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237,
238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253,
254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269,
270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285,
286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 302, 312,
322, 332, 342, 352, 362, 372, 382, 392, 402, 412, 422, 432, 442, 452, 462, 472,
482, 492, 502, 512, 522, 532, 542, 552, 562, 572, 582, 592, 602, 612, 622, 632,
642, 652, 662, 672, 682, 692, 702, 712, 722, 732, 742, 752, 762, 772, 782, 792,
802, 812, 822, 832, 842, 852, 862, 872, 882, 892, 902, 912, 922, 932, 942, 952,
962, 972, 982, 992]

# Define a function named 'test' that takes an integer 'n' as a parameter


def test(n):
# Initialize an empty list to store the results
ans = []

# Iterate through all n-digit integers


for i in range(10 ** (n - 1), 10 ** n):
# Assert that the length of the current integer is equal to 'n'
assert len(str(i)) == n

# Check if the integer starts or ends with '2'


if str(i).startswith("2") or str(i).endswith("2"):
[Link](i)

# Return the list of integers that meet the specified conditions


return ans

# Example 1
n1 = 1
print("Number:", n1)
print("All", n1, "- digit integers that start or end with 2:")
print(test(n1))

# Example 2
n2 = 2
print("\nNumber:", n2)
print("All", n2, "- digit integers that start or end with 2:")
print(test(n2))

# Example 3
n3 = 3
print("\nNumber:", n3)
print("All", n3, "- digit integers that start or end with 2:")
print(test(n3))

Number: 1
All 1 - digit integers that start or end with 2:
[2]

Number: 2
All 2 - digit integers that start or end with 2:
[12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 42, 52, 62, 72, 82, 92]

Number: 3
All 3 - digit integers that start or end with 2:
[102, 112, 122, 132, 142, 152, 162, 172, 182, 192, 200, 201, 202, 203, 204, 205,
206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237,
238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253,
254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269,
270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285,
286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 302, 312,
322, 332, 342, 352, 362, 372, 382, 392, 402, 412, 422, 432, 442, 452, 462, 472,
482, 492, 502, 512, 522, 532, 542, 552, 562, 572, 582, 592, 602, 612, 622, 632,
642, 652, 662, 672, 682, 692, 702, 712, 722, 732, 742, 752, 762, 772, 782, 792,
802, 812, 822, 832, 842, 852, 862, 872, 882, 892, 902, 912, 922, 932, 942, 952,
962, 972, 982, 992]

===================================================================================
=================

Puzzle 92
Question: Last update on May 30 2025 11:49:13 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:13 (UTC/GMT +8 hours)

Sort Keeping Every Other Element Fixed

Write a Python program to start with a list of integers, keep every other element
in place and otherwise sort the list.

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find all n-digit integers that start or end with [Link]:Find the closest
palindrome.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.


Input:
[2, 5, 6, 3, 1, 4, 34]
Output:
[1, 5, 2, 3, 6, 4, 34]

Input:
[8, 0, 7, 2, 9, 4, 1, 2, 8, 3]
Output:
[1, 0, 7, 2, 8, 4, 8, 2, 9, 3]

# Define a function named 'test' that takes a list of numbers 'nums' as a parameter
def test(nums):
# Create a copy of the original list to avoid modifying the input list
li = [Link]()

# Iterate through the elements of the list with even indices


for i in range(len(li)):
# Check if the current index is even
if i % 2 == 0:
# Iterate through the elements with even indices after the current
index
for j in range(i + 2, len(li), 2):
# Check if the element at index 'j' is less than the element at
index 'i'
if li[j] < li[i]:
# Call the 'swap' function to swap elements at indices 'i' and
'j'
swap(li, i, j)

# Return the modified list


return li

# Define a function named 'swap' that swaps elements at indices 'i' and 'j' in the
given list 'li'
def swap(li, i, j):
# Temporary variable to store the value at index 'i'
temp = li[i]
# Swap the values at indices 'i' and 'j'
li[i] = li[j]
li[j] = temp

# Example 1
nums1 = [2, 5, 6, 3, 1, 4, 34]
print("Original list of numbers:")
print(nums1)
print("Keep every other element in place and otherwise sort the list:")
print(test(nums1))

# Example 2
nums2 = [8, 0, 7, 2, 9, 4, 1, 2, 8, 3]
print("\nOriginal list of numbers:")
print(nums2)
print("Keep every other element in place and otherwise sort the list:")
print(test(nums2))
Original list (triple) of lists:
[2, 5, 6, 3, 1, 4, 34]
In the said list, keep every other element in place and otherwise sort the list.:
[1, 5, 2, 3, 6, 4, 34]

Original list (triple) of lists:


[8, 0, 7, 2, 9, 4, 1, 2, 8, 3]
In the said list, keep every other element in place and otherwise sort the list.:
[1, 0, 7, 2, 8, 4, 8, 2, 9, 3]

===================================================================================
=================

Puzzle 93
Question: Last update on May 30 2025 11:49:14 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:14 (UTC/GMT +8 hours)

Closest Palindrome to String

Write a Python program to find the closest palindrome to a given string.

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Start with a list of integers, keep every other element in place and
otherwise sort the [Link]:Separate parentheses groups.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
cat
Output:
cac

Input:
madan
Output:
madam
Input:
radivider
Output:
radividar

Input:
madan
Output:
madam
Input:
abc
Output:
aba

Input:
racecbr
Output:
racecar

# Define a function named 'test' that finds the closest palindrome of a given
string 's'
def test(s):
# Initialize a variable 'odd' to count the number of differing characters
between mirrored positions
odd = 0

# Iterate through the characters of the string along with their indices
for i, c in enumerate(s):
# Check if the character at the current position is not equal to its
mirrored position
if c != s[~i]:
# Increment the 'odd' count
odd += 1

# Check if the count of differing characters is an odd number


if odd % 2 == 1:
# Calculate the half of the 'odd' count
half = odd // 2
# Construct a palindrome by taking characters from the original string and
its mirrored positions
pal = "".join((s[i] if i < half else s[~i] for i in range(len(s))))
# Return the closest palindrome
return pal
else:
# Calculate the half of the 'odd' count
half = odd // 2
# Construct a palindrome by taking characters from the original string and
its mirrored positions
pal = "".join((s[i] if i <= half else s[~i] for i in range(len(s))))
# Return the closest palindrome
return pal

# Example 1
s1 = "cat"
print("Original string:", s1)
print("Closest palindrome of the said string:")
print(test(s1))

# Example 2
s2 = "madan"
print("\nOriginal string:", s2)
print("Closest palindrome of the said string:")
print(test(s2))

# Example 3
s3 = "radivider"
print("Original string:", s3)
print("Closest palindrome of the said string:")
print(test(s3))

# Example 4
s4 = "madan"
print("\nOriginal string:", s4)
print("Closest palindrome of the said string:")
print(test(s4))

# Example 5
s5 = "abc"
print("Original string:", s5)
print("Closest palindrome of the said string:")
print(test(s5))

# Example 6
s6 = "racecbr"
print("\nOriginal string:", s6)
print("Closest palindrome of the said string:")
print(test(s6))

Original string: cat


Closest palindrome of the said string:
cac

Original string: madan


Closest palindrome of the said string:
madam
Original string: radivider
Closest palindrome of the said string:
radividar

Original string: madan


Closest palindrome of the said string:
madam
Original string: abc
Closest palindrome of the said string:
aba

Original string: racecbr


Closest palindrome of the said string:
racecar

===================================================================================
=================

Puzzle 94
Question: Last update on May 30 2025 11:49:14 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:14 (UTC/GMT +8 hours)

Split Matched Parentheses Groups

Given a string consisting of whitespace and groups of matched parentheses, write a


Python program to split it into groups of perfectly matched parentheses without any
whitespace.

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find the closest [Link]:Find a palindrome of a given length


containing a given string.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
( ()) ((()()())) (()) ()
Output:
['(())', '((()()()))', '(())', '()']

Input:
() (( ( )() ( )) ) ( ())
Output:
['()', '((()()()))', '(())']

# Define a function named 'test' that separates parentheses groups from a combined
string
def test(combined):
# Initialize an empty list 'ls' to store separate parentheses groups
ls = []

# Initialize an empty string 's2' to build each parentheses group


s2 = ""

# Iterate through each character in the combined string (ignoring spaces)


for s in [Link](' ', ''):
# Append the character to 's2'
s2 += s

# Check if the count of "(" equals the count of ")"


if [Link]("(") == [Link](")"):
# Append the current parentheses group to the list 'ls'
[Link](s2)

# Reset 's2' for the next parentheses group


s2 = ""

# Return the list of separate parentheses groups


return ls

# Example 1
combined1 = '( ()) ((()()())) (()) ()'
print("Parentheses string:")
print(combined1)
print("Separate parentheses groups of the said string:")
print(test(combined1))

# Example 2
combined2 = '() (( ( )() ( )) ) ( ())'
print("\nParentheses string:")
print(combined2)
print("Separate parentheses groups of the said string:")
print(test(combined2))

Parentheses string:
( ()) ((()()())) (()) ()
Separate parentheses groups of the said string:
['(())', '((()()()))', '(())', '()']

Parentheses string:
() (( ( )() ( )) ) ( ())
Separate parentheses groups of the said string:
['()', '((()()()))', '(())']

===================================================================================
=================

Puzzle 95
Question: Last update on May 30 2025 11:49:15 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:15 (UTC/GMT +8 hours)

Generate Palindrome of Specific Length

Write a Python program to generate a palindrome of a given length from a string.


Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Separate parentheses [Link]:Single digits in numbers sorted backwards


and converted to English words.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: madam , 7
Output:
madaadam

Input: madam , 6
Output:
maddam

Input: madam , 5
Output:
maaaam

Input: madam , 3
Output:
maam

Input: madam , 2
Output:
mm

Input: madam , 1
Output:
aa

# License: [Link]

# Function to generate a palindrome of a given length from a string


def test(s, length):
s_index = 0
# Calculate the half length of the palindrome
length_half = (length - (length % 2)) // 2
ans = ""

# Build the first half of the palindrome


while len(ans) < length_half:
ans += s[s_index % len(s)]
s_index += 1

# Add a middle character if the length is odd


if length % 2 == 1:
ans += "a"

# Complete the palindrome by adding the reversed first half


return ans + ans[::-1]

# Test cases with different string and palindrome lengths


s = 'madam'
length = 7
print("String and length of the palindrome:", s, ",", length)
print("Palindrome of the said string and length:")
print(test(s, length))

s = 'madam'
length = 6
print("\nString and length of the palindrome:", s, ",", length)
print("Palindrome of the said string and length:")
print(test(s, length))

length = 5
print("\nString and length of the palindrome:", s, ",", length)
print("Palindrome of the said string and length:")
print(test(s, length))

length = 3
print("\nString and length of the palindrome:", s, ",", length)
print("Palindrome of the said string and length:")
print(test(s, length))

length = 2
print("\nString and length of the palindrome:", s, ",", length)
print("Palindrome of the said string and length:")
print(test(s, length))

length = 1
print("\nString and length of the palindrome:", s, ",", length)
print("Palindrome of the said string and length:")
print(test(s, length))

String and length of the palindrome: madam , 7


Palindrome of the said string and length:
madaadam

String and length of the palindrome: madam , 6


Palindrome of the said string and length:
maddam

String and length of the palindrome: madam , 5


Palindrome of the said string and length:
maaaam

String and length of the palindrome: madam , 3


Palindrome of the said string and length:
maam

String and length of the palindrome: madam , 2


Palindrome of the said string and length:
mm

String and length of the palindrome: madam , 1


Palindrome of the said string and length:
aa

===================================================================================
=================

Puzzle 96
Question: Last update on May 30 2025 11:49:15 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:15 (UTC/GMT +8 hours)

Single Digits to English Words in Reverse Order

Write a Python program to get the single digits in numbers sorted backwards and
converted into English words.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Find a palindrome of a given length containing a given [Link]:Strange


sort of list of numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 3, 4, 5, 11]
Output:
['five', 'four', 'three', 'one']

Input:
[27, 3, 8, 5, 1, 31]
Output:
['eight', 'five', 'three', 'one']

# License: [Link]

# Function to convert single-digit numbers in a list to English words,


# sort them backwards, and return the result
def test(nums):
# Dictionary mapping English words to their corresponding digits
digits = {
"zero": None,
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9
}

# Create a reverse dictionary mapping digits to their corresponding English


words
digits_backwards = {digits[k]: k for k in digits}

# Convert the original dictionary values to a list


digits = [digits[s] for s in digits]

# Extract single-digit numbers from the input list and convert them to English
words
li = [digits[n] for n in nums if n in digits]

# Return the sorted list of English words corresponding to the digits


return [digits_backwards[n] for n in sorted(li, reverse=True)]

# Test cases with different lists of numbers


nums = [1, 3, 4, 5, 11]
print("Original list of numbers:")
print(nums)
print("Return the single digits in nums sorted backwards and converted to English
words:")
print(test(nums))
nums = [27, 3, 8, 5, 1, 31]
print("\nOriginal list of numbers:")
print(nums)
print("Return the single digits in nums sorted backwards and converted to English
words:")
print(test(nums))

Original list of numbers:


[1, 3, 4, 5, 11]
Return the single digits in nums sorted backwards and converted to English words:
['five', 'four', 'three', 'one']

Original list of numbers:


[27, 3, 8, 5, 1, 31]
Return the single digits in nums sorted backwards and converted to English words:
['eight', 'five', 'three', 'one']

===================================================================================
=================

Puzzle 97
Question: Last update on May 30 2025 11:49:16 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:16 (UTC/GMT +8 hours)

Strange Sort: Alternating Min-Max

Write a Python program to find the following strange sort of list of numbers: the
first element is the smallest, the second is the largest of the remaining, the
third is the smallest of the remaining, the fourth is the smallest of the
remaining, etc.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Single digits in numbers sorted backwards and converted to English


[Link]:Compute the depth of groups of matched nested parentheses separated by
parentheses.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
[1, 3, 4, 5, 11]
Output:
[1, 11, 3, 5, 4]

Input:
[27, 3, 8, 5, 1, 31]
Output:
[1, 31, 3, 27, 5, 8]

Input:
[1, 2, 7, 3, 4, 5, 6]
Output:
[1, 7, 2, 6, 3, 5, 4]

# License: [Link]

# Function to perform a strange sorting on a list of numbers


def test(nums):
# If the list has less than 2 elements, return the list as it is
if len(nums) < 2:
return nums

# Initialize an empty result list to store the sorted numbers


result = []

# Iterate through the first half of the list


for i in range(len(nums)//2):
# Append the minimum value to the result
[Link](min(nums))
# Remove the minimum value from the list
[Link](min(nums))
# Append the maximum value to the result
[Link](max(nums))
# Remove the maximum value from the list
[Link](max(nums))

# If there is one element left in the list, append it to the result


if len(nums) > 0:
[Link](nums[0])

# If the result list is still smaller than twice the length of the original
list,
# extend it with the remaining elements from the original list
if len(result) < 2 * len(nums):
[Link](nums[len(result) // 2 + 1:len(result) // 2 + 1 + len(nums) -
len(result)])

# Return the final sorted list


return result

# Test cases with different lists of numbers


nums = [1, 3, 4, 5, 11]
print("Original list of numbers:")
print(nums)
print("Strange sort of list of said numbers:")
print(test(nums))

nums = [27, 3, 8, 5, 1, 31]


print("\nOriginal list of numbers:")
print(nums)
print("Strange sort of list of said numbers:")
print(test(nums))

nums = [1, 2, 7, 3, 4, 5, 6]
print("\nOriginal list of numbers:")
print(nums)
print("Strange sort of list of said numbers:")
print(test(nums))

Original list of numbers:


[1, 3, 4, 5, 11]
Strange sort of list of said numbers:
[1, 11, 3, 5, 4]

Original list of numbers:


[27, 3, 8, 5, 1, 31]
Strange sort of list of said numbers:
[1, 31, 3, 27, 5, 8]

Original list of numbers:


[1, 2, 7, 3, 4, 5, 6]
Strange sort of list of said numbers:
[1, 7, 2, 6, 3, 5, 4]

===================================================================================
=================

Puzzle 98
Question: Last update on May 30 2025 11:49:16 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:16 (UTC/GMT +8 hours)

Depth of Matched Parentheses Groups

Given a string consisting of groups of matched nested parentheses separated by


parentheses, write a Python program to compute the depth of each group.

Visual Presentation:

Sample Solution:
Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Strange sort of list of [Link]:Expand Spaces.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input: (()) (()) () ((()()()))

Output:
[2, 2, 1, 3]
Input: () (()) () () () ()

Output:
[1, 2, 1, 1, 1, 1]
Input: (((((((()))))))) () (()) ((()()()))

Output:
[8, 1, 2, 3]

# License: [Link]

# Function to calculate the depth of groups of matched nested parentheses


def test(parens):
# Split the input string into individual parentheses groups
# and calculate the depth of each group
return [len([Link](')')[0]) for s in [Link]()]

# Test cases with different strings of parentheses


parentheses = '(()) (()) () ((()()())) '
print("Parentheses strings:", parentheses)
print("\nDepth of groups of matched nested parentheses separated by parentheses:")
print(test(parentheses))

parentheses = '() (()) () () () ()'


print("Parentheses strings:", parentheses)
print("\nDepth of groups of matched nested parentheses separated by parentheses:")
print(test(parentheses))

parentheses = '(((((((()))))))) () (()) ((()()()))'


print("Parentheses strings:", parentheses)
print("\nDepth of groups of matched nested parentheses separated by parentheses:")
print(test(parentheses))

Parentheses strings: (()) (()) () ((()()()))

Depth of groups of matched nested parentheses separated by parentheses:


[2, 2, 1, 3]
Parentheses strings: () (()) () () () ()

Depth of groups of matched nested parentheses separated by parentheses:


[1, 2, 1, 1, 1, 1]
Parentheses strings: (((((((()))))))) () (()) ((()()()))

Depth of groups of matched nested parentheses separated by parentheses:


[8, 1, 2, 3]

===================================================================================
=================

Puzzle 99
Question: Last update on May 30 2025 11:49:17 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:17 (UTC/GMT +8 hours)

Replace Spaces with Underscore and Hyphen

Write a Python program to find a string such that, when three or more spaces are
compacted to a '-' and one or two spaces are replaced by underscores, leads to the
target.

Visual Presentation:

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Compute the depth of groups of matched nested parentheses separated by


[Link]:Find four positive even integers whose sum is n.

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
Python-Exercises
Output:
Python Exercises

Input:
Python_Exercises
Output:
Python Exercises

Input:
-Hello,_world!__This_is-so-easy!-
Output:
Hello, world! This is so easy!

# License: [Link]

# Function to replace hyphens with three spaces and underscores with a single space
def test(strs):
return [Link]("-", " " * 3).replace("_", " ")

# Test cases with different strings


strs = "Python-Exercises"
print("Original strings:", strs)
print("Depth of groups of matched nested parentheses separated by parentheses:")
print(test(strs))

strs = "Python_Exercises"
print("\nOriginal strings:", strs)
print("Depth of groups of matched nested parentheses separated by parentheses:")
print(test(strs))

strs = "-Hello,_world!__This_is-so-easy!-"
print("\nOriginal strings:", strs)
print("Depth of groups of matched nested parentheses separated by parentheses:")
print(test(strs))

Original strings: Python-Exercises


Depth of groups of matched nested parentheses separated by parentheses:
Python Exercises

Original strings: Python_Exercises


Depth of groups of matched nested parentheses separated by parentheses:
Python Exercises

Original strings: -Hello,_world!__This_is-so-easy!-


Depth of groups of matched nested parentheses separated by parentheses:
Hello, world! This is so easy!

===================================================================================
=================

Puzzle 100
Question: Last update on May 30 2025 11:49:17 (UTC/GMT +8 hours)
Solution:
Last update on May 30 2025 11:49:17 (UTC/GMT +8 hours)

Four Even Integers Summing to n

Write a Python program to find four positive even integers whose sum is a given
integer.

Sample Solution:

Python Code:

Sample Output:

Flowchart:

For more Practice: Solve these Related Problems:

Go to:

Previous:Expand Spaces.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments)
through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource'squiz.

Follow us onFacebookandTwitterfor latest update.

Input:
n = 100
Output:
[94, 2, 2, 2]

Input:
n = 1000
Output:
[994, 2, 2, 2]

Input:
n = 10000
Output:
[9994, 2, 2, 2]

Input:
n = 1234567890
Output:
[1234567884, 2, 2, 2]

# License: [Link]

# Function to find four positive even integers whose sum is equal to a given number
def test(n):
# Iterate over possible values for the first even integer (a)
for a in range(n, 0, -1):
# Skip odd values for a
if not a % 2 == 0:
continue

# Iterate over possible values for the second even integer (b)
for b in range(n - a, 0, -1):
# Skip odd values for b
if not b % 2 == 0:
continue

# Iterate over possible values for the third even integer (c)
for c in range(n - b - a, 0, -1):
# Skip odd values for c
if not c % 2 == 0:
continue

# Iterate over possible values for the fourth even integer (d)
for d in range(n - b - c - a, 0, -1):
# Skip odd values for d
if not d % 2 == 0:
continue

# Check if the sum of a, b, c, and d equals the target number


if a + b + c + d == n:
# Return the list of found even integers
return [a, b, c, d]

# Test cases with different values of n


n = 100
print("Four positive even integers whose sum is", n)
print(test(n))

n = 1000
print("\nFour positive even integers whose sum is", n)
print(test(n))

n = 10000
print("\nFour positive even integers whose sum is", n)
print(test(n))

n = 1234567890
print("\nFour positive even integers whose sum is", n)
print(test(n))

Four positive even integers whose sum is 100


[94, 2, 2, 2]

Four positive even integers whose sum is 1000


[994, 2, 2, 2]

Four positive even integers whose sum is 10000


[9994, 2, 2, 2]

Four positive even integers whose sum is 1234567890


[1234567884, 2, 2, 2]

===================================================================================
=================

You might also like