Python Practice Problems Level 1.5-2
Python Practice Problems Level 1.5-2
To check if a number is a palindrome without converting it to a string, reverse the number using arithmetic operations. Start by initializing a reversed number as 0. Then, repeatedly take the last digit of the input number (using modulus 10), add it to the reversed number after multiplying the reversed number by 10, and remove the last digit of the input by integer division by 10. Compare the original number with the reversed number .
To create a reverse multiplication table for a number, start a loop from 10 to 1, multiplying the given number `n` by each iterating number `i` and print the result in each iteration. This will display the table in reverse order from 10 to 1 .
To find all factors of a number, initialize a loop that iterates through numbers from 2 up to the square root of the number. For each iteration, check if it divides the number evenly. If it does, it is a factor. Collect these in a list and exclude 1 and the number itself .
To count the digits in a number without converting it to a string, repeatedly divide the number by 10 until it becomes 0. With each division, increment a counter to keep track of the number of divisions, which equates to the digit count .
To efficiently sum the first n prime numbers, implement a function to check if a number is prime and use it in an iterative process. Start from number 2 and keep adding each confirmed prime number to the sum until n prime numbers are found and summed .
To find the sum of even and odd digits in a number, initialize two sums for even and odd digits. Then, iterate over each digit of the number (using modulus and division operations), checking if each digit is even or odd and adding it to the respective sum .
A three-digit number is an Armstrong number if the sum of the cubes of its digits equals the number itself. For example, for the number 153, calculate 1^3 + 5^3 + 3^3, which equals 153, confirming it as an Armstrong number .
To count vowels and consonants in a string, iterate through each character, checking whether it is a vowel or consonant using conditions or membership tests in a set of vowels. Increment respective counters for vowels and consonants excluding non-alphabetic characters .
To generate a star pyramid pattern, use a loop to incrementally increase the number of stars printed on each line. For `n` lines, the k-th line will contain `(2*k - 1)` stars, centered by adjusting the spaces .
To implement a simple login system with limited attempts, set predefined username and password credentials. Allow the user to input their credentials up to 3 times, and lock them out after 3 failed attempts without converting attempts into explicit iterations in case structures .