Loading...
More Python Posts
my_list = [1, 2, 3, 4, 5]removed_element = my_list.pop(2) # Remove and return element at index 2print(removed_element) # 3print(my_list) # [1, 2, 4, 5]last_element = my_list.pop() # Remove and return the last elementprint(last_element) # 5print(my_list) # [1, 2, 4]
def to_roman_numeral(num):lookup = [(1000, 'M'),(900, 'CM'),(500, 'D'),(400, 'CD'),(100, 'C'),(90, 'XC'),(50, 'L'),(40, 'XL'),(10, 'X'),(9, 'IX'),(5, 'V'),(4, 'IV'),(1, 'I'),]res = ''for (n, roman) in lookup:(d, num) = divmod(num, n)res += roman * dreturn resto_roman_numeral(3) # 'III'to_roman_numeral(11) # 'XI'to_roman_numeral(1998) # 'MCMXCVIII'
# question3.pyfrom itertools import productV='∀'E='∃'def tt(f,n) :xss=product((0,1),repeat=n)print('function:',f.__name__)for xs in xss : print(*xs,':',int(f(*xs)))print('')# this is the logic for part A (p\/q\/r) /\ (p\/q\/~r) /\ (p\/~q\/r) /\ (p\/~q\/~r) /\ (~p\/q\/r) /\ (~p\/q\/~r) /\ (~p\/~q\/r) /\ (~p\/~q\/~r)def parta(p,q,r) :a=(p or q or r) and (p or q or not r) and (p or not q or r)and (p or not q or not r)b=(not p or q or r ) and (not p or q or not r) and (not p or not q or r) and (not p or not q or not r)c= a and breturn cdef partb(p,q,r) :a=(p or q and r) and (p or not q or not r) and (p or not q or not r)and (p or q or not r)b=(not p or q or r ) and (not p or q or not r) and (not p or not q or r) and (not p or not q or not r)c= a and breturn cprint("part A:")tt(parta,3)print("part B:")tt(partb,3)
from functools import partialdef curry(fn, *args):return partial(fn, *args)add = lambda x, y: x + yadd10 = curry(add, 10)add10(20) # 30
prime_lists=[] # a list to store the prime numbersdef prime(n): # define prime numbersif n <= 1:return False# divide n by 2... up to n-1for i in range(2, n):if n % i == 0: # the remainder should'nt be a 0return Falseelse:prime_lists.append(n)return Truefor n in range(30,1000): # calling function and passing starting point =30 coz we need primes >30prime(n)check=0 # a var to limit the output to 10 onlyfor n in prime_lists:for x in prime_lists:val= n *xif (val > 1000 ):check=check +1if (check <10) :print("the num is:", val , "=",n , "* ", x )break
# Python program for Bitonic Sort. Note that this program# works only when size of input is a power of 2.# The parameter dir indicates the sorting direction, ASCENDING# or DESCENDING; if (a[i] > a[j]) agrees with the direction,# then a[i] and a[j] are interchanged.*/def compAndSwap(a, i, j, dire):if (dire==1 and a[i] > a[j]) or (dire==0 and a[i] > a[j]):a[i],a[j] = a[j],a[i]# It recursively sorts a bitonic sequence in ascending order,# if dir = 1, and in descending order otherwise (means dir=0).# The sequence to be sorted starts at index position low,# the parameter cnt is the number of elements to be sorted.def bitonicMerge(a, low, cnt, dire):if cnt > 1:k = cnt/2for i in range(low , low+k):compAndSwap(a, i, i+k, dire)bitonicMerge(a, low, k, dire)bitonicMerge(a, low+k, k, dire)# This funcion first produces a bitonic sequence by recursively# sorting its two halves in opposite sorting orders, and then# calls bitonicMerge to make them in the same orderdef bitonicSort(a, low, cnt,dire):if cnt > 1:k = cnt/2bitonicSort(a, low, k, 1)bitonicSort(a, low+k, k, 0)bitonicMerge(a, low, cnt, dire)# Caller of bitonicSort for sorting the entire array of length N# in ASCENDING orderdef sort(a,N, up):bitonicSort(a,0, N, up)# Driver code to test abovea = [3, 7, 4, 8, 6, 2, 1, 5]n = len(a)up = 1sort(a, n, up)print ("\n\nSorted array is")for i in range(n):print("%d" %a[i]),