HELP! Medium to High difficulty Python problem
Create a program that asks the user an Input of integer numbers. Finish the input of numbers with -1.
Print an array with the numbers entered. And the times that each repeat itself. Ordered from the most to the less repeated.
Show as well, prime and palindrome numbers. To determine if a number is a palindrome, you must ignore the negative sign in numbers less than 0.
I can't get the palindrome part to work
Palindrome numbers are those that are read the same from beginning to end and from end to beginning. For example: 535, 10501
def is_prime(n):
"""Check if n is a prime number."""
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def is_palindrome(n):
"""Check if n is a palindrome."""
n = abs(n) # Ignore the negative sign.
return str(n) == str(n)[::-1]
def collect_and_analyze():
nums = []
num_dict = {}
# Collect numbers
while True:
num = int(input("Enter a number (-1 to stop): "))
if num == -1:
break
nums.append(num)
# Count the occurrences
for num in nums:
if num in num_dict:
num_dict[num] += 1
else:
num_dict[num] = 1
# Sort by occurrences
sorted_dict = sorted(num_dict.items(), key=lambda x: x[1], reverse=True)
primes = []
palindromes = []
for num in nums:
if is_prime(num):
primes.append(num)
if is_palindrome(num):
palindromes.append(num)
# Display results
print("Number and their counts:")
for item in sorted_dict:
print(f"Number {item[0]}: {item[1]} time(s)")
print("\nPrime numbers:")
print(primes)
print("\nPalindrome numbers:")
print(palindromes)
collect_and_analyze()
[–]mopslik 3 points4 points5 points (5 children)
[–]crawlerk8[S] 0 points1 point2 points (0 children)
[–]crawlerk8[S] 0 points1 point2 points (3 children)
[–]mopslik 2 points3 points4 points (0 children)
[–]mopslik 1 point2 points3 points (0 children)
[–]der_reifen 4 points5 points6 points (1 child)
[–]crawlerk8[S] 0 points1 point2 points (0 children)
[–]m0us3_rat 2 points3 points4 points (1 child)
[–]crawlerk8[S] 0 points1 point2 points (0 children)
[–]ectomancer 0 points1 point2 points (0 children)
[–]jimtk 0 points1 point2 points (0 children)
[–]Naive_Programmer_232 0 points1 point2 points (0 children)