• Nov 19, 2022 •CodeCatch
0 likes • 1 view
def print_pyramid_pattern(n): # outer loop to handle number of rows # n in this case for i in range(0, n): # inner loop to handle number of columns # values changing acc. to outer loop for j in range(0, i+1): # printing stars print("* ",end="") # ending line after each row print("\r") print_pyramid_pattern(10)
• Mar 12, 2021 •mo_ak
0 likes • 2 views
prime_lists=[] # a list to store the prime numbers def prime(n): # define prime numbers if n <= 1: return False # divide n by 2... up to n-1 for i in range(2, n): if n % i == 0: # the remainder should'nt be a 0 return False else: prime_lists.append(n) return True for n in range(30,1000): # calling function and passing starting point =30 coz we need primes >30 prime(n) check=0 # a var to limit the output to 10 only for n in prime_lists: for x in prime_lists: val= n *x if (val > 1000 ): check=check +1 if (check <10) : print("the num is:", val , "=",n , "* ", x ) break
0 likes • 0 views
# Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. # For example, if n is 10, the output should be “2, 3, 5, 7”. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19”. # Python program to print all primes smaller than or equal to # n using Sieve of Eratosthenes def SieveOfEratosthenes(n): # Create a boolean array "prime[0..n]" and initialize # all entries it as true. A value in prime[i] will # finally be false if i is Not a prime, else true. prime = [True for i in range(n + 1)] p = 2 while (p * p <= n): # If prime[p] is not changed, then it is a prime if (prime[p] == True): # Update all multiples of p for i in range(p * 2, n + 1, p): prime[i] = False p += 1 prime[0]= False prime[1]= False # Print all prime numbers for p in range(n + 1): if prime[p]: print (p) # driver program if __name__=='__main__': n = 30 print("Following are the prime numbers smaller") print("than or equal to ", n) print("than or equal to ", n) SieveOfEratosthenes(n)
0 likes • 3 views
def max_n(lst, n = 1): return sorted(lst, reverse = True)[:n] max_n([1, 2, 3]) # [3] max_n([1, 2, 3], 2) # [3, 2]
def check_prop(fn, prop): return lambda obj: fn(obj[prop]) check_age = check_prop(lambda x: x >= 18, 'age') user = {'name': 'Mark', 'age': 18} check_age(user) # True
• Sep 20, 2025 •cntt.dsc-f4b6
1 like • 2 views
print(123)