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

Assignment 3

Python for ai and robotics

Uploaded by

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

Assignment 3

Python for ai and robotics

Uploaded by

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

1. Write a program in python to Plotting x and y point arrays on a plot.

import matplotlib.pyplot as plt

# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]

# plotting the points


plt.plot(x, y)

# naming the x axis


plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')

# giving a title to my graph


plt.title('My first graph!')

# function to show the plot


plt.show()

2. Write a program to use different type of markers and line styles on the plot.

import matplotlib.pyplot as plt


import random as random

students = ["Jane", "Joe", "Beck", "Tom",


"Sam", "Eva", "Samuel", "Jack",
"Dana", "Ester", "Carla", "Steve",
"Fallon", "Liam", "Culhane", "Candance",
"Ana", "Mari", "Steffi", "Adam"]

marks = []
for i in range(0, len(students)):
marks.append(random.randint(0, 101))

plt.xlabel("Students")
plt.ylabel("Marks")
plt.title("CLASS RECORDS")
plt.plot(students, marks, 'm--')

# Adding grid lines


plt.grid(True, which='both', linestyle='--', linewidth=0.5)

plt.show()
3. Write a Python program to calculate the electricity bill. Accept the last meter reading
and current meter reading and the rate per unit from the user. Calculate the number of
units and total bill consumption for the user

# Python program to calculate electricity bill

# Function to calculate the electricity bill


def calculate_electricity_bill(last_meter_reading, current_meter_reading,
rate_per_unit):
# Calculate the number of units consumed
units_consumed = current_meter_reading - last_meter_reading
# Calculate the total bill
total_bill = units_consumed * rate_per_unit
return units_consumed, total_bill

# Input: last meter reading, current meter reading, and rate per unit
last_meter_reading = float(input("Enter the last meter reading: "))
current_meter_reading = float(input("Enter the current meter reading: "))
rate_per_unit = float(input("Enter the rate per unit: "))

# Calculate units consumed and total bill


units_consumed, total_bill = calculate_electricity_bill(last_meter_reading,
current_meter_reading, rate_per_unit)

# Display the results


print(f"Units Consumed: {units_consumed}")
print(f"Total Bill: ${total_bill:.2f}")

4. Write a program to determine if two strings are anagrams (i.e., they have the same
characters in a different order).

def are_anagrams(str1, str2):


# Sort both strings and compare
return sorted(str1) == sorted(str2)

# Input two strings


string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")

# Check and print result


if are_anagrams(string1, string2):
print("The two strings are anagrams.")
else:
print("The two strings are not anagrams.")
5. Write a program to do linear search.

# Function to perform linear search


def linear_search(arr, target):
# Iterate through each element in the list
for i in range(len(arr)):
if arr[i] == target: # Check if current element matches the target
return i # Return the index if the target is found
return -1 # Return -1 if the target is not found in the list

arr = [10, 23, 45, 70, 11, 15]


target = int(input("Enter the element to search for: "))

result = linear_search(arr, target)


if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found in the list")

You might also like