0% found this document useful (0 votes)
24 views

Lab 5 - Python

The document provides instructions for 5 programming exercises: 1) Write a function to check if a number is a palindrome and a program using it. 2) Write a program to count character occurrences in a string using a dictionary. 3) Write a program to count word occurrences in text using a dictionary and string's split function. 4) Write a function to multiply two matrices and check it against NumPy's matmul function. 5) Write a function to evaluate a polynomial at a point using coefficients, check against NumPy, and plot the polynomial against a range of x values using Matplotlib.

Uploaded by

Nony Shin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lab 5 - Python

The document provides instructions for 5 programming exercises: 1) Write a function to check if a number is a palindrome and a program using it. 2) Write a program to count character occurrences in a string using a dictionary. 3) Write a program to count word occurrences in text using a dictionary and string's split function. 4) Write a function to multiply two matrices and check it against NumPy's matmul function. 5) Write a function to evaluate a polynomial at a point using coefficients, check against NumPy, and plot the polynomial against a range of x values using Matplotlib.

Uploaded by

Nony Shin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab 6

1. Palindrome number
Write a function to check if the given number is a palindrome number. The function returns True
or False. A palindrome number is a number that is same after reverse. For example 98189, is a
palindrome number. Write a program using that function.

2. Counting characters occurrences


Write a program counting occurrences of all characters within a string. For example, the input is
“Letter”, the output is {‘L’:1, ‘e’:2, ‘t’:2, ‘r’: 1}. Hint: use dictionary.

3. Counting word occurrences


Write a program counting occurrences of all words within a text. For example, the input is “I chose
the red color, because I like red”, the output is {“I”:2, “chose”:1, “the”:1, “red”: 2, “color”:1,
“because”:1, “like”:1}. Hint: use dictionary and function split(str) returning a list with each
element as a word in str.

4. Matrix multiplication
Write a function to multiply two matrices: A[n,m] and B[n,o]. Then, check the result by using the
Numpy library (function matmul()).

Hint: use nested list to represent matrix. For example, the list [[1,2], [3,4], [4,5]] represents a 3x2
matrix. Use the statement: X = [ [ 0 for i in range(m)] for j in range(n)] to create an empty matrix
X of n rows and m columns.

5. Evaluating and plotting polynomial

Write a function to evaluate a polynomial f(x), defined by its coefficients (c[0] + c[1].x + c[2].x2
+ ...), at a given point x. Hint: x**i computes xi. Then, check the result by using the Numpy library
(function polyval()).

Plot the polynomial with X = [-4, -3, -2, -1, 0, 1, 2, 3, 4]. Hint: use library Matplotlib.

For example, the graph of f(x) = 2x3 - x2 + 1 is illustrated in the figure bellow.

You might also like