|
| 1 | +# Count total positive numbers |
| 2 | +numbers = [1, -2, 3, -4, 5, 6, -7, -8, 9, 10] |
| 3 | +number_counter = 0 |
| 4 | +for num in numbers: |
| 5 | + if num > 0: |
| 6 | + number_counter += 1 |
| 7 | +print('Total Positive numbers are:', number_counter) |
| 8 | + |
| 9 | +print('-' * 50) |
| 10 | + |
| 11 | +# Count how many even numbers are up to a given number |
| 12 | +number = int(input('Enter any number: ')) |
| 13 | +even_count = 0 |
| 14 | +for i in range(0, number + 1): |
| 15 | + if i % 2 == 0: |
| 16 | + even_count += 1 |
| 17 | +print('Total Even Numbers:', even_count) |
| 18 | + |
| 19 | +print('-' * 50) |
| 20 | + |
| 21 | +# Multiplication table, skipping 5 |
| 22 | +number = int(input("Enter the number: ")) |
| 23 | +for i in range(0, 11): |
| 24 | + if i == 5: |
| 25 | + continue # Skip 5 |
| 26 | + print(number, 'X', i, '=', i * number) |
| 27 | + |
| 28 | +print('-' * 50) |
| 29 | + |
| 30 | +# Reverse a string |
| 31 | +string = 'Haider' |
| 32 | +reversed_string = '' |
| 33 | +for char in string: |
| 34 | + reversed_string = char + reversed_string |
| 35 | +print('Reversed string:', reversed_string) |
| 36 | + |
| 37 | +print('-' * 50) |
| 38 | + |
| 39 | +# First non-repeating character |
| 40 | +string = 'successfully' |
| 41 | +for char in string: |
| 42 | + if string.count(char) == 1: |
| 43 | + print('First non-repeating character:', char) |
| 44 | + break |
| 45 | + |
| 46 | +print('-' * 50) |
| 47 | + |
| 48 | +# Factorial of a number |
| 49 | +number = int(input('Enter number for factorial: ')) |
| 50 | +factorial = 1 |
| 51 | +while number > 0: |
| 52 | + factorial *= number |
| 53 | + number -= 1 |
| 54 | +print('Factorial value is:', factorial) |
| 55 | + |
| 56 | +print('-' * 50) |
| 57 | + |
| 58 | +# Ask user to enter number between 1 and 10 |
| 59 | +while True: |
| 60 | + number = int(input("Enter a number between 1 to 10: ")) |
| 61 | + if 1 <= number <= 10: |
| 62 | + print('Thanks') |
| 63 | + break |
| 64 | + else: |
| 65 | + print('Invalid Number, try again.') |
| 66 | + |
| 67 | +print('-' * 50) |
| 68 | + |
| 69 | +# Check if number is prime |
| 70 | +number = int(input("Enter a number to check if prime: ")) |
| 71 | +if number <= 1: |
| 72 | + print(False) |
| 73 | +else: |
| 74 | + is_prime = True |
| 75 | + for i in range(2, number): |
| 76 | + if number % i == 0: |
| 77 | + is_prime = False |
| 78 | + break |
| 79 | + print('Is Prime:', is_prime) |
| 80 | + |
| 81 | +print('-' * 50) |
| 82 | + |
| 83 | +# Detect first duplicate in list |
| 84 | +items = ['apple', 'orange', 'Banana', 'Kino', 'apple'] |
| 85 | +seen_items = set() |
| 86 | +for item in items: |
| 87 | + if item in seen_items: |
| 88 | + print('Duplicate found:', item) |
| 89 | + break |
| 90 | + seen_items.add(item) |
| 91 | + |
| 92 | +print('-' * 50) |
| 93 | + |
| 94 | +# Retry system with exponential wait |
| 95 | +import time |
| 96 | +wait_time = 1 |
| 97 | +max_retries = 5 |
| 98 | +attempts = 0 |
| 99 | +while attempts < max_retries: |
| 100 | + print('Attempt', attempts + 1, '- wait time', wait_time, 'seconds') |
| 101 | + time.sleep(wait_time) |
| 102 | + wait_time *= 2 |
| 103 | + attempts += 1 |
0 commit comments