Skip to main content

when predicate lambda

0 likes • Nov 19, 2022 • 0 views
Python
Loading...

More Python Posts

radians to degrees

0 likes • Nov 19, 2022 • 0 views
Python
from math import pi
def rads_to_degrees(rad):
return (rad * 180.0) / pi
rads_to_degrees(pi / 2) # 90.0

CSCE 2100 Question 3

0 likes • Nov 18, 2022 • 3 views
Python
# question3.py
from itertools import product
V='∀'
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 b
return c
def 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 b
return c
print("part A:")
tt(parta,3)
print("part B:")
tt(partb,3)

Input 2D Matrix

0 likes • Nov 19, 2022 • 0 views
Python
# Input for row and column
R = int(input())
C = int(input())
# Using list comprehension for input
matrix = [[int(input()) for x in range (C)] for y in range(R)]

Check Armstrong Number

0 likes • May 31, 2023 • 0 views
Python
# Function to check Armstrong number
def is_armstrong_number(number):
# Convert number to string to iterate over its digits
num_str = str(number)
# Calculate the sum of the cubes of each digit
digit_sum = sum(int(digit) ** len(num_str) for digit in num_str)
# Compare the sum with the original number
if digit_sum == number:
return True
else:
return False
# Prompt user for a number
number = int(input("Enter a number: "))
# Check if the number is an Armstrong number
if is_armstrong_number(number):
print(number, "is an Armstrong number.")
else:
print(number, "is not an Armstrong number.")

Palindrome checker

0 likes • Nov 19, 2022 • 3 views
Python
# function which return reverse of a string
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")

Bitonic sort

0 likes • Nov 19, 2022 • 0 views
Python
# 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/2
for 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 order
def bitonicSort(a, low, cnt,dire):
if cnt > 1:
k = cnt/2
bitonicSort(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 order
def sort(a,N, up):
bitonicSort(a,0, N, up)
# Driver code to test above
a = [3, 7, 4, 8, 6, 2, 1, 5]
n = len(a)
up = 1
sort(a, n, up)
print ("\n\nSorted array is")
for i in range(n):
print("%d" %a[i]),