Python Programming Puzzles Solutions
Python Programming Puzzles Solutions
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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))
# 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))
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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))
# 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 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))
# 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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Previous:Check the length and the fifth element occurs twice in a [Link]:Find
the number of stones in each pile.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input:
922
Output:
True
Input:
914
Output:
False
Input:
854
Output:
True
Input:
854
Output:
True
# License: [Link]
# 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))
# 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 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)
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:
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.
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]
# Print the result of the test function applied to the integer 'n'
print(test(n))
# 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 result of the test function applied to the modified integer 'n'
print(test(n))
# 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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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))
# 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))
# 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))
# 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)
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:
Go to:
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]
# 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 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 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)
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:
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.
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
# Print the result of the test function applied to the 'nums' list with the current
value of 'i'
print(test(nums, 1))
# 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))
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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]]
# Print the result of the test function applied to the string 's'
print(test(s))
# Print the result of the test function applied to the modified string 's'
print(test(s))
# Print the result of the test function applied to the modified string 's'
print(test(s))
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)
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.
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Previous:Split a string of words separated by commas and spaces into 2 lists: words
and [Link]:Separate Parentheses Groups Perfectly.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
What is the difficulty level of this exercise?
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
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# 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 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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input:
( ()) ((()()())) (()) ()
Output:
['(())', '((()()()))', '(())', '()']
Input:
() (( ( )() ( )) ) ( ())
Output:
['()', '((()()()))', '(())']
# License: [Link]
# 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)
# Print the result of the test function applied to the 'combined' string
print(test(combined))
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# Print the result of the test function applied to the 'nums' list with the given
threshold value
print(test(nums, thresh))
# 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)
Visual Presentation:
Sample Solution-1:
Python Code:
Sample Output:
Flowchart:
Sample Solution-2:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# Print the result of the test function applied to the 'strs' list
print(test(strs))
Original strings:
['palindrome', 'madamimadam', '', 'foo', 'eyes']
# 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 = []
# Initialize variables 'i' and 'j' for indexing the string 's'
i = 0
j = len(s) - 1
# Print the result of the test function applied to the 'strs' list
print(test(strs))
Original strings:
['palindrome', 'madamimadam', '', 'foo', 'eyes']
===================================================================================
=================
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)
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:
Go to:
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)]
# Print the result of the test function applied to the 'strs' list with the given
prefix
print(test(strs, 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)
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:
Go to:
Previous:Find the strings in a list, starting with a given [Link]:Find the
longest string of a list of strings.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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)]
# Print the result of the test function applied to the '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)
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:
Go to:
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)
# Print the result of the test function applied to the 'strs' list
print(test(strs))
# 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]()
# Print the result of the test function applied to the 'strs' list
print(test(strs))
# 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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
For more Practice: Solve these Related Problems:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# Print the result of the test function applied to the 'strs' list with the given
substring
print(test(strs, substrs))
# Print the result of the test function applied to the modified 'strs' list with
the updated substring
print(test(strs, substrs))
# 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)
Visual Presentation:
Sample Solution-1:
Python Code:
Sample Output:
Flowchart:
Sample Solution-2:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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)))
# Print the result of the test function applied to the 'n' value
print(test(n))
# 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))
# Print the result of the test function applied to the 'n' value
print(test(n))
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# Print the result of the test function applied to the 'M' matrix and the 'T'
target value
print(test(M, T))
# 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
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# Print the result of the test function applied to the 'strs' string
print(test(strs))
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
# 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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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!"
# Print the result of the test function applied to the updated 'nums' list
print(test(nums))
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# Print the result of the test function applied to the 'strs' list
print(test(strs))
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input:
PytHon ExerciSEs
Output:
373
Input:
JavaScript
Output:
157
# License: [Link]
# Print the result of the test function applied to the 'strs' string
print(test(strs))
# 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]
# Print the result of the test function applied to the 'strs' string
print(test(strs))
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
# 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]
# Print the result of the test function applied to the 'strs' string
print(test(strs))
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
# 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)
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:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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 = []
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# Print the result of the test function applied to the updated 'nums' list
print(test(nums))
# 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]]
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# Print the result of the test function applied to the updated 'nums' list
print(test(nums))
# 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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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)]
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# Print the result of the test function applied to the updated 'nums' list
print(test(nums))
# 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))]
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# Print the result of the test function applied to the updated 'nums' list
print(test(nums))
# 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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# 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))
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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)
# 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 = []
# Return the largest number, which is the last element in the sorted list
return numbers[-1]
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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)
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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)))
# Print the result of the test function applied to the 'strs' list
print(test(strs))
# 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
# Print the result of the test function applied to the 'strs' list
print(test(strs))
# 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)
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:
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).
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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)
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# 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
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
# Print the result of the test function applied to the 'strs' list
print(test(strs))
# 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)
Write a Python program to find the coordinates of a triangle with given side
lengths.
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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 x-coordinate of the third vertex using the Pythagorean theorem
x = (c ** 2 - y ** 2) ** 0.5
# Print the result of the test function applied to the 'sides' list
print(test(sides))
# Print the result of the test function applied to the 'sides' list
print(test(sides))
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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)
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# 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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input:
w3rEsOUrcE
Output:
[6]
Input:
AEIOUYW
Output:
[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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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
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)
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)
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:
Go to:
Previous:Find the sum of the numbers among the first k with more than 2
[Link]:Find the largest k numbers.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input:
123456789
Output:
945
Input:
2468
Output:
0
Input:
13579
Output:
945
#License: [Link]
===================================================================================
=================
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)
Visual Presentation:
Sample Solution-1:
Python Code:
Sample Output:
Flowchart:
Sample Solution-2:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
# 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
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
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]
# 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 = []
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]
===================================================================================
=================
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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]]
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 = []
# Return the list of Boolean values indicating whether each triple sums to zero
return zero_sums
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)
Visual Presentation:
Sample Solution-1:
Python Code:
Sample Output:
Flowchart:
Sample Solution-2:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input: Python
Output:
pYTHQN
Input: aeiou
Output:
CGKQW
Input: AEIOU
Output:
cgkqw
# License: [Link]
# Apply the translation mapping and swap the case of the characters in the
string
result = [Link](translation_mapping).swapcase()
# License: [Link]
===================================================================================
=================
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)
Visual Presentation:
Sample Solution-1:
Python Code:
Sample Output:
Flowchart:
Sample Solution-2:
Python Code:
Sample Output:
Flowchart:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input: nine eight seven six five four three two one
Output:
one two three four five six seven eight nine
# License: [Link]
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]
# 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()
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)
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:
Go to:
Previous:Sort numbers based on [Link]:Find all words in a given string with n
consonants.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input: HELLO
Output:
['h', 'o', 'l', 'e']
Input: HelLo
Output:
['h', 'o', 'l', 'e']
# License: [Link]
# License: [Link]
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
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]
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
===================================================================================
=================
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)
Sample Solution:
Python Code:
Sample Output:
Flowchart:
For more Practice: Solve these Related Problems:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Output:
Even palindromes up to 50 -
[0, 2, 4, 6, 8, 22, 44]
# 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]]
Even palindromes up to 50 -
[0, 2, 4, 6, 8, 22, 44]
===================================================================================
=================
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
For more Practice: Solve these Related Problems:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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 []
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)
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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 []
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:
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.
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])
===================================================================================
=================
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)
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
===================================================================================
=================
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)
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:
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.
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]
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]))
# 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
===================================================================================
=================
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)
Visual Presentation:
Sample Solution-1:
Python Code:
Sample Output:
Flowchart:
Sample Solution-2:
Python Code:
Sample Output:
Flowchart:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
# 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
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
===================================================================================
=================
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input:
a = 2 : n = 1024
Output:
10
Input:
a = 3 : n = 81
Output:
4
Input:
a = 3 : n =
1290070078170102666248196035845070394933441741644993085810116441344597492642263849
Output:
170
a = 2 : n = 1024
Find an integer exponent x such that a^x = n:
10
a = 3 : n = 81
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input:
[1, 3, -2]
Output:
-6
Input:
[1, -3, 3]
Output:
-7
Input:
[10, 32, 3]
Output:
45
Input:
[-25, -12, -23]
Output:
-60
# 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))
===================================================================================
=================
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)
Visual Presentation:
Sample Solution-1:
Python Code:
Sample Output:
Flowchart:
Sample Solution-2:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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
===================================================================================
=================
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:
Go to:
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]
# 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)
===================================================================================
=================
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Previous:A valid [Link]:Find the number which when appended to the list
makes the total 0.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
===================================================================================
=================
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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
# 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)
===================================================================================
=================
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)
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:
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.
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))
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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
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))
===================================================================================
=================
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)
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:
Go to:
Previous:Find the sum of the even elements that are at odd indices in a given
[Link]:Circular shift number.
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))
===================================================================================
=================
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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
# License: [Link]
# 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.:
# License: [Link]
# Shift the digits to the left by 'shift' places using list manipulation
for i in range(shift):
shifted_digits.append(shifted_digits.pop(0))
# 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.:
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
# License: [Link]
def test(nums):
# Initialize variables to store indices of the closest pair and the closest
distance
closest_inds = None
closest_dist = None
# 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))
===================================================================================
=================
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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]
# 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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]]
# 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]]
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
# 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))
# License: [Link]
# 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))
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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))
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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
# 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))
# 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
# 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))
===================================================================================
=================
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)
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:
Go to:
# License: [Link]
# 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))
===================================================================================
=================
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)
Write a Python program to find a substring in a given string that contains a vowel
between two consonants.
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input: Sandwhich
Output:
San
Input: Python
Output:
hon
# License: [Link]
# 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))
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
# 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))
===================================================================================
=================
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
For more Practice: Solve these Related Problems:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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))
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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))]
# 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: [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)
Write a Python program to convert GPAs to letter grades according to the following
table:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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: [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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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: []
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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))
===================================================================================
=================
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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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)
# 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)
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:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
===================================================================================
=================
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Input:
Python
Output:
None
Input:
Unhappy
Output:
[4, 5]
Input:
Find
Output:
None
Input:
Street
Output:
[3, 4]
# License: [Link]
# 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))
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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))
===================================================================================
=================
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
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).
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
===================================================================================
=================
Puzzle 86
Question:
Solution:
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:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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', '']
===================================================================================
=================
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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Output:
[[]]
Input:
]]]]]]]]]]]]]]]]][][][][]]]]]]]]]]][[[][[][[[[[][][][]][[[[[[[[[[[[[[[[[[
Output:
[[][][][]]
# License: [Link]
# 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))
Find a valid substring of the said string that contains matching brackets, at least
one of which is nested:
[[]]
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
===================================================================================
=================
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)
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:
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.
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]
# 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:
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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))
===================================================================================
=================
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)
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:
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.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
# 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)
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:
Go to:
Previous:Find all n-digit integers that start or end with [Link]:Find the closest
palindrome.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]()
# 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]
===================================================================================
=================
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)
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Previous:Start with a list of integers, keep every other element in place and
otherwise sort the [Link]:Separate parentheses groups.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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
# 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))
===================================================================================
=================
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)
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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 = []
# 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)
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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]
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))
===================================================================================
=================
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)
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:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
What is the difficulty level of this exercise?
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]
# 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]
===================================================================================
=================
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)
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:
Go to:
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]
# 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)])
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))
===================================================================================
=================
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)
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
Output:
[2, 2, 1, 3]
Input: () (()) () () () ()
Output:
[1, 2, 1, 1, 1, 1]
Input: (((((((()))))))) () (()) ((()()()))
Output:
[8, 1, 2, 3]
# License: [Link]
===================================================================================
=================
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)
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:
Go to:
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("_", " ")
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))
===================================================================================
=================
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)
Write a Python program to find four positive even integers whose sum is a given
integer.
Sample Solution:
Python Code:
Sample Output:
Flowchart:
Go to:
Previous:Expand Spaces.
Have another way to solve this solution? Contribute your code (and comments)
through Disqus.
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
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))
===================================================================================
=================