# SYMBOLIC PROGRAMMING

**DONE BY:
<br>Thejaswin.S
<br>RA1911026010029
<br>CSE-AIML K1**

**1. Solve the following using symbolic paradigm:
<br>i. Calculate sqrt (2) with 100 decimals.**



```python
from sympy import sqrt
print(sqrt(2).evalf(100))
```

    1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573


**ii. Calculate (1/2+1/3) in rational arithmetic.**


```python
import sympy as sym
a0029 = sym.Rational(1, 2)
b0029 = sym.Rational(1, 3)
a0029+b0029
```




$\displaystyle \frac{5}{6}$



**iii. Calculate the expanded form of (x+y) ^ 6.**


```python
import sympy as sym
x0029 = sym.Symbol('x')
y0029 = sym.Symbol('y')
sym.expand((x0029+y0029)**6)
```




$\displaystyle x^{6} + 6 x^{5} y + 15 x^{4} y^{2} + 20 x^{3} y^{3} + 15 x^{2} y^{4} + 6 x y^{5} + y^{6}$



**iv. Simplify the trigonometric expression sin (x) / cos (x)**


```python
import sympy as sym
x0029=sym.Symbol('x')
sym.simplify(sym.sin(x0029)/sym.cos(x0029))
```




$\displaystyle \tan{\left(x \right)}$



**v. Calculate sin x -xx^3n**


```python
import sympy as sym
x0029=sym.Symbol('x')
n0029=sym.Symbol('n')
sym.solveset(sym.sin(x0029)-x0029*x0029**3*n0029,x0029)
```




$\displaystyle \left\{x \mid x \in \mathbb{C} \wedge - n x^{4} + \sin{\left(x \right)} = 0 \right\}$




```python
import sympy
from sympy import *
x0029,n0029=symbols('x n')
expr0029=sin(x0029)-x0029*x0029**3*n0029
smpl0029=trigsimp(expr0029)
smpl0029
```




$\displaystyle - n x^{4} + \sin{\left(x \right)}$



**2. Develop a python code for to carryout the operations on the given algebraic manipulation for the given expression a2−ab+ab−b2=a2−b2  by using the symbolic programming paradigms principles.**


```python
from sympy import *
a,b=symbols('a b')
g10029=simplify(a**2-ab+ab-b**2)
g10029
```




$\displaystyle a^{2} - b^{2}$



**3. Give the Symbolic program for the expression given below:
<br>a. ∬a2 da**


```python
a0029=symbols('a')
g0029=integrate('a**2',a)
j0029=integrate(g0029,a0029) 
print("first integration "+str(g0029))
print("second integration "+str(j0029))
```

    first integration a**3/3
    second integration a**4/12


**b. 2x+y2**


```python
x=Symbol('x')
y=Symbol('y')
2*x+y**2
```




$\displaystyle 2 x + y^{2}$



**c. 1/10 + 1/5**


```python
a0029=Rational(1,10)
b0029=Rational(1,5)
a0029+b0029
```




$\displaystyle \frac{3}{10}$



**d. d/dx(sin(x))**


```python
x0029=symbols('x')
g0029=sin(x0029)
j0029=diff(g0029,x0029) 
print("Before differentiation "+str(g0029))
print("After differentiation "+str(j0029)) 
```

    Before differentiation sin(x)
    After differentiation cos(x)


# Functional Programming

**1.	Calculate the following using Lambda calculus:
<br>a.	T AND F**


```python
y0029=lambda s0029: s0029 and False
print("T AND F = ",y0029(True)) 
```

    T AND F =  False


**b. 3 * 4**


```python
x0029 = lambda a0029,b0029 : a0029*b0029
print("3*4 =",x0029(3,4))
```

    3*4 = 12


**2.	Lambda functions
<br>a.	Write a lambda function to convert measurements from meters to feet.**


```python
x0029=lambda y0029: y0029*3.28084
print("feet : {:.6f}".format(x0029(float(input("enter in metres: ")))))
```

    enter in metres: 12.5
    feet : 41.010500


**b. Write a lambda function in Python to implement the following lambda expression:
<br>         (𝜆𝑓. 𝜆𝑚. (𝑓 + 𝑚)𝑎)(𝜆𝑥. 𝑥2)(𝑏)
<br>Note: You need to write a nested lambda function for implementing f+m where f takes the
<br>square function (which takes argument x) passed as a parameter. The above expression
<br>calculates a^2+b.**


```python
square0029=lambda x: x**2
total0029=lambda f, b: lambda a: f(a)+b
a0029=int(input("Enter value for a: "))
b0029=int(input("Enter value for b: "))
print(total0029(square0029,b0029)(a0029))
```

    Enter value for a: 10
    Enter value for b: 12
    112


**3. Passing and returning a function as an argument
<br>Define a function ‘square’ for squaring a number. Define a function named ‘twice’ that takes a
<br>function f as an argument and returns f(f(x)). Using ‘twice’ and ‘square’ create a function ‘quad’ that
<br>takes n as an argument and returns n 4 . ‘quad’ should not be defined explicitly. It should only be
<br>created as a variable which is then assigned a function.**


```python
def square0029(n0029):
    return n0029**2

def twice0029(f0029):
    return square0029(f0029)

n0029 = int(input("Enter a number: "))
quad0029 = twice0029(square0029(n0029))
print(quad0029)
```

    Enter a number: 10
    10000


**4. Closure
<br>A Closure is a function object that remembers values in enclosing scopes even if they are not present
<br>in memory. We have a closure in Python when a nested function references a value in its enclosing
<br>scope.
<br>a. Study the following program by executing it:**


```python
def multiplier_of(n0029):
    def multiplier(number0029):
        return number0029*n0029
    return multiplier

multiplywith5 = multiplier_of(5)
print(multiplywith5(9))
```

    45


**b. In a lottery system, random number is chosen by retrieving the number from a
<br>random index from a list of random numbers. Write a program to choose a random
<br>number in this way. You must use nested functions – the inner function chooses a
<br>number from a random index and the outer function generates a random list of
<br>numbers. The outer function takes n as a parameter where is the maximum number
<br>that can be put in the random list. (Your code should be similar to the program in
<br>5a)**


```python
import random
def outer_func(n0029):
    list_0029 = random.sample(range(0,n0029+1), n0029)
    def inner_func():
        index = random.randrange(0, n0029)
        return list_0029[index]
    return inner_func

n0029 = int(input("Enter a random number: "))
ans0029 = outer_func(n0029)
print("Random number generated is: {}".format(ans0029()))
```

    Enter a random number: 10
    Random number generated is: 6


**6. Map
<br>A secret message needs to be sent. Use the map function to encrypt the message using Caesar cipher.**


```python
def encrypt(letter_0029, s_0029):
    if (letter_0029.isupper()):
        return chr((ord(letter_0029) + s_0029 - 65) % 26 + 65)
    elif (letter_0029.islower()):
        return chr((ord(letter_0029) + s_0029 - 97) % 26 + 97)
    else:
        return letter_0029

text_0029 = input("Enter the word: ")
s_0029 = int(input("Enter the shift: "))
shift_0029 = [s_0029] * len(text_0029)
result_0029 = list(map(encrypt, text_0029, shift_0029))
#result_0029
print("Encrypted text: ", end="")
for i in result_0029:
    print(i, end="")
```

    Enter the word: hello
    Enter the shift: 2
    Encrypted text: jgnnq

**7. Reduce
<br>Given runs scored by 2 players in a series of matches, write a Python program using reduce function
<br>to find who is the better player of the two in terms of maintaining consistency. (You need to find
<br>SD).**


```python
import functools,operator

pl1_0029 = [51, 50, 58, 55, 53, 60]
pl2_0029 = [98, 42, 88, 74, 88, 12]

mean1_0029 = (functools.reduce(operator.add, pl1_0029)) / len(pl1_0029)
mean2_0029 = (functools.reduce(operator.add, pl2_0029)) / len(pl2_0029)

m1_0029 = [mean1_0029] * len(pl1_0029)
m2_0029 = [mean2_0029] * len(pl2_0029)

def variance_0029(p_0029, m_0029):
    var_0029 = p_0029 - m_0029
    return var_0029**2
    
sqsum1_0029 = list(map(variance_0029, pl1_0029, m1_0029))
sqsum2_0029 = list(map(variance_0029, pl2_0029, m2_0029))

var1_0029 = (functools.reduce(operator.add, sqsum1_0029)) / len(sqsum1_0029)
var2_0029 = (functools.reduce(operator.add, sqsum2_0029)) / len(sqsum2_0029)

sd1_0029 = var1_0029**0.5
sd2_0029 = var2_0029**0.5

if sd1_0029==sd2_0029:
    print("Both players are consistent.")
elif sd1_0029>sd2_0029:
    print("Player 2 is more consistent.")
else:
    print("Player 1 is more consistent.")
```

    Player 1 is more consistent.


**8. Filter
<br>The marks scored by a class of students in 5 different subjects are stored in a list of lists. Using the
<br>filter function, write a program to find the students who failed in one or more subjects.**


```python
a0029=lambda x0029:x0029<35
marks0029=[[10,90,80,75,32],[23,67,78,87,90],[90,90,90,90,90],[39, 1, 100, 56, 72]]
l=len(marks0029)
for i in range(l):
    red=filter(a0029,marks0029[i])
    if len(list(red))>=1:
        print ("student '"+str(i+1)+"' failed in atleast 1 subject")
```

    student '1' failed in atleast 1 subject
    student '2' failed in atleast 1 subject
    student '4' failed in atleast 1 subject


**9. Map+reduce+filter
<br>Given two trending topics and a bunch of tweets, write a Python program to count the number of
<br>tweets that contain each topic. You need to do this by putting together map(), reduce() and filter()
<br>functions.**


```python
import functools,operator
tweets0029 = ["Is this the final ipl of msd #IPL #chennaisuperkings",
            "Most 10-wicket wins in IPL: 4 - RCB, 2 - MI, 2 - SRH, 2 - CSK #RCBvsRR #CSK #RR #CricTracker",
            "My @chennaisuperkings outfit has arrived! Now to be well enough towear it....Thanks @MSD & the #CSK management! All the best for the rest of the #IPL!",
             "Mental health of #Students deteriorating",
             "#Students suffering inside home and inside themselves",
              "#Students not able to cope up with studies during COVID"]

csk_0029 = ["#csk"] * len(tweets0029)
students_0029 = ["students"] * len(tweets0029)

def segregate_tweets(str0029, topic0029):
    if topic0029 in str0029.lower():
        return str0029
    return ""

result_csk = list(map(segregate_tweets, tweets0029, csk_0029))
result_stud = list(map(segregate_tweets, tweets0029, students_0029))
print(result_csk)
print(result_stud)
print()
def filter_list(str0029):
    return str0029!=""

rescsk_0029 = list(filter(filter_list, result_csk))
resstud_0029 = list(filter(filter_list, result_stud))

print("No. of csk tweets: {}".format(len(rescsk_0029)))
print("No. of students tweets: {}".format(len(resstud_0029)))
```

    ['', 'Most 10-wicket wins in IPL: 4 - RCB, 2 - MI, 2 - SRH, 2 - CSK #RCBvsRR #CSK #RR #CricTracker', 'My @chennaisuperkings outfit has arrived! Now to be well enough towear it....Thanks @MSD & the #CSK management! All the best for the rest of the #IPL!', '', '', '']
    ['', '', '', 'Mental health of #Students deteriorating', '#Students suffering inside home and inside themselves', '#Students not able to cope up with studies during COVID']
    
    No. of csk tweets: 2
    No. of students tweets: 3



```python

```

969fcb4a45650f52b86ac416b42ae5dea7e11a9d
ipynb
Jupyter Notebook
WEEK-7.ipynb
thejaswin123/APP_lab
051add9bf979d8c706ff5d64c12e19a08cbc6552
MIT
WEEK-7.ipynb
thejaswin123/APP_lab
051add9bf979d8c706ff5d64c12e19a08cbc6552
MIT
WEEK-7.ipynb
thejaswin123/APP_lab
051add9bf979d8c706ff5d64c12e19a08cbc6552
MIT
Qwen/Qwen-72B
1. YES
2. YES