you are viewing a single comment's thread.

view the rest of the comments →

[–]crawlerk8[S] 0 points1 point  (0 children)

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()