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

RAI Program Assignment

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

RAI Program Assignment

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

Akshat Srivastava

Class- 10 I
roll no- 7
admission no-115102
Subject- RAI Program assignment

BOYS’ HIGHSCHOOL AND COLLEGE


PRAYAGRAJ
ASSIGNMENT (ROBOTICS AND ARTIFICIAL INTELLIGENCE)
CLASS X

Q1-wapp to find second largest element in a list.

L=list(input("enter list of numbers\n"))

L.sort(reverse=True)

s=L[1]

if s is not None:

print("the second largest element is",s)

Output:

>enter any number:1,2,3,4,5,9,7

The second largest element is7

Q2-Wapp to count number of words in a given string.

s=input("enter any string\n")

l=len(s)

print(l)

Output:

>enter any string: hello world

11

Q3-Wapp to find greatest common divisor (GCD) of two numbers

n1=int(input("enter number1\n"))

n2=int(input("enter number2\n"))

while n2!=0:

n1,n2=n2,n1%n2

print("GCD=",(n1),sep="")
Output:

>enter number1

54

>enter number2

24

GCD=6

Q4-Wapp to count number of vowels in a given string

s=input("enter ay string\n")

vowels="aeiouAEIOU"

c=0

for char in s:

if char in vowels:

c+=1

print("number of vowels",c)

output:

>enter any string

Hello world

Number of vowel is 3

Q5-Wapp to find the sum of the digits of a given number n

n=int(input("enter any number\n"))

s=0

while n>0:

digit=n%10

s+=digit

n//=10

print("sum of digits",s)

Output:

>enter any number


12345

Sum of digits is 15

Q6-Wapp to check if a string is palindrome or not

s1=input("enter any string\n")

s2=s1[::-1]

if s1==s2:

print("paliindrome")

else:

print("Not Palindrome")

Output:

>enter any string

racecar

racecar is a palindrome

Q7-Wapp to reverse a string

s=input("enter any string\n")

r=s[::-1]

print(r)

Output:

>enter any string

Akshat

tahska

Q8-Wapp to check if a given number is prime or not

num=int(input("enter any number"))

if num>1:

for i in range(2,num):

if (num%i)==0:

print(num,"is not a prime number")

break
else:

print(num,"PRIME NUMBER")

else:

print(num,"NOT A PRIME NUMBER")

Output:

>enter any number

11

11 prime number

Q9-Wapp to find factorial of a given number

num=int(input("enter any number:\n"))

factorial=1

for i in range(1,num+1):

factorial*=i

print("Factorial of",num,"is",factorial)

Output:

>enter any number

Factorial of 5 is 120

Q10-Wapp to find the sum of first n natural number

sum=0

for i in range(1,6):

sum+=i

print("sum of first five natural numbers are",sum)

OUTPUT:

SUM OF FIVE NATURAL NUMBERS IS 15

Q11-Wapp to check if a given string contains only digits

s=input("enter any string\n")

s=s.isdigit()

print(s)
Output:

>enter any string

“123”

True

Q12-Wapp to calculate the average of numbers in a list

l=[1,2,3,4,5]

a=sum(l)/len(l)

print("average of list=",a,sep="")

Output:

>average of list=3.0

Q13-write a python program to find all prime number up to a given number n.

n=int(input("enter any number\n"))

for x in range(1,n+1):

count=0

for y in range(1,x+1):

if x%y==0:

count+=1

if count==2:

print(x)

Output:

>enter any number:20

11

13

17

19
Q14-Wapp to check if two lists are identical (contain the elements in the same orde)r

list1=[1,2,3,4,5]

list2=[1,2,3,4,5]

print(list1==list2)

Output:

True

Q15-Wapp to remove all non-alphanumeric characters from a string

s="Hello, World!#$&*1234"

c=''.join(char for char in s if char.isalnum())

print(c)

Output:

HelloWorld1234

Q16-Wapp to rotate a list by k elements in two lists

l=[1,2,3,4,5]

k=2

k=k%len(l)

r=l[-k:]+l[:-k]

print(r)

Output:

[4, 5, 1, 2, 3]

Q17-Wapp to find most frequent element in a list

l=[1,23,4,4,55,5,9,9,10,10]

print(max(set(l),key=l.count))

Output:

Q18-Wapp to find common elements in two list

l1=[1,2,3,4,5,6,7,8,9,10]

l2=[4,5,6,8]

c=list(set(l1)and set(l2))
print(c)

Output:

[8, 4, 5, 6]

Q19-Wapp to find the intersection of multiple list

list1 = [1, 2, 3, 4, 5]

list2 = [2, 3, 6, 7]

list3 = [0, 2, 3, 8]

intersection = set(list1)

intersection &= set(list2)

intersection &= set(list3)

result = list(intersection)

print("Intersection of the lists:", result)

Output:

[9, 10, 4, 5]

Q20-Wapp to check if two strings are anagram or not

str1 = input("Enter the first string: ")

str2 = input("Enter the second string: ")

is_anagram = sorted(str1.replace(" ", "").lower()) == sorted(str2.replace(" ", "").lower())

if is_anagram:

print(f'"{str1}" and "{str2}" are anagrams.')

else:

print(f'"{str1}" and "{str2}" are not anagrams.')

Output:

Enter the first string: listen

Enter the second string: silent

"listen" and "silent" are anagrams.

Q21-Wapp to remove duplicates from a list while maintaining the original order

lst = [1, 2, 3, 2, 4, 5, 1, 6, 5]

unique_lst = []
for item in lst:

if item not in unique_lst:

unique_lst.append(item)

print("List after removing duplicates:", unique_lst)

Output:

List after removing duplicates: [1, 2, 3, 4, 5, 6]

Q22 Wapp to find highest common factor of two numbers

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

while num2 != 0:

num1, num2 = num2, num1 % num2

print("The HCF is:", num1)

Output:

Enter the first number: 54

Enter the second number: 24

The HCF is: 6

Q23-Wapp to calculate the factorial of a number using function

n = int(input("Enter a number: "))

factorial = (lambda f: (lambda x: f(f, x)))(lambda f, x: 1 if x == 0 else x * f(f, x - 1))

print(f"Factorial of {n} is:", factorial(n))

Output:

Enter a number: 5

Factorial of 5 is: 120

Q24-Wapp to find duplicate elements in a list

lst = [1, 2, 3, 4, 2, 5, 6, 3, 7, 8, 1]

duplicates = []

for i in range(len(lst)):

for j in range(i + 1, len(lst)):

if lst[i] == lst[j] and lst[i] not in duplicates:


duplicates.append(lst[i])

print("Duplicate elements in the list:", duplicates)

Output:

Duplicate elements in the list: [1, 2, 3]

Q25-Wapp to calculate compound intrest

principal = float(input("Enter the principal amount: "))

rate = float(input("Enter the annual interest rate (in %): "))

time = float(input("Enter the time in years: "))

n = int(input("Enter the number of times interest is compounded per year: "))

amount = principal * (1 + (rate / 100) / n) ** (n * time)

compound_interest = amount - principal

print(f"The compound interest is: {compound_interest:.2f}")

print(f"The total amount after {time} years is: {amount:.2f}")

Output:

Enter the principal amount: 800

Enter the annual interest rate (in %): 40

Enter the time in years: 3

Enter the number of times interest is compounded per year: 4

The compound interest is: 1710.74

The total amount after 3.0 years is: 2510.74

Q26-Wapp to remove all vowels from a given list

lst = ['a', 'b', 'c', 'e', 'i', 'o', 'u', 'd', 'f', 'g']

vowels = ['a', 'e', 'i', 'o', 'u']

no_vowels = []

for char in lst:

if char.lower() not in vowels:

no_vowels.append(char)

print("List after removing vowels:", no_vowels)


Output:

List after removing vowels: ['b', 'c', 'd', 'f', 'g']

Q27-Wapp to calculate sum of squares of numbers from 1 to n

n = int(input("Enter a number: "))

sum_of_squares = 0

for i in range(1, n + 1):

sum_of_squares += i ** 2

print(f"The sum of squares from 1 to {n} is:", sum_of_squares)

Output:

Enter a number: 20

The sum of squares from 1 to 20 is: 2870

Q28-Wapp to generate a list of squares of numbers from 1 to n # Input list of numbers

numbers = list(map(int, input("Enter the list of numbers separated by space: ").split()))

squares = []

for num in numbers:

squares.append(num ** 2)

print("List of squares:", squares)

Output:

Q29-Write a Python program to find the sum of all even numbers in a list

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

sum_even = sum(num for num in numbers if num % 2 == 0)

print("Sum of all even numbers:", sum_even)

Output:

Sum of all even numbers: 30

Q30-Write a Python program to convert a list of characters into a single string.

chars = ['h', 'e', 'l', 'l', 'o']

single_string = ''.join(chars)

print("String:", single_string)
Output:

String: hello

Q31-Write a Python program to find the first non-repeated character in a given string

s = "swiss"

for char in s:

if s.count(char) == 1:

print("First non-repeated character:", char)

break

Output:

First non-repeated character: w

Q32-Write a Python program to find the smallest element in a list.

numbers = [3, 1, 4, 1, 5, 9, 2, 6]

smallest = min(numbers)

print("Smallest element in the list:", smallest)

Output:
Smallest element in the list: 1

Q33-Write a Python program to check if a given number is a palindrome

number = 121

num_str = str(number)

if num_str == num_str[::-1]:

print(number, "is a palindrome")

else:

print(number, "is not a palindrome")

Output;

121 is a palindrome

Q34-Write a Python program to reverse a list

numbers = [1, 2, 3, 4, 5]

reversed_list = numbers[::-1]
print("Reversed list:", reversed_list)

Output:
Reversed list: [5, 4, 3, 2, 1]

Q35-Write a Python program to find the union of two lists

l1 = [1, 2, 3]

l2 = [3, 4, 5]

union_list = list(set(l1) | set(l2))

print("Union of the two lists:", union_list)

Output:

Union of the two lists: [1, 2, 3, 4, 5]

Q36-Write a Python program to check if a given string is a program (contains every letter of the alphabet
at least once).

import string

s = "The quick brown fox jumps over the lazy dog"

alphabet = set(string.ascii_lowercase)

if alphabet <= set(s.lower()):

print(f'"{s}" is a program')

else:

print(f'"{s}" is not a program')

Output:

"The quick brown fox jumps over the lazy dog" is a program

Q37-Write a Python program to find all substrings of a given string.

s = "abc"

substrings = []

for i in range(len(s)):

for j in range(i + 1, len(s) + 1):

substrings.append(s[i:j])

print("All substrings:", substrings)

Output:

All substrings: ['a', 'ab', 'abc', 'b', 'bc', 'c']


Q38-Write a Python program to sort words in a given sentence in alphabetical order

sentence = "the quick brown fox jumps over the lazy dog"

words = sentence.split()

sorted_words = sorted(words)

sorted_sentence = ' '.join(sorted_words)

print("Sorted words:", sorted_sentence)

Output:

Sorted words: brown dog fox jumps lazy over quick the the

Q39Write a Python program to count the number of consonants and vowels in a given string.-

s = "Hello, World!"

vowels = "aeiouAEIOU"

vowel_count = sum(1 for char in s if char in vowels)

consonant_count = sum(1 for char in s if char.isalpha() and char not in vowels)

print("Number of vowels:", vowel_count)

print("Number of consonants:", consonant_count)

Output:

Number of vowels: 3

Number of consonants: 7

Q40-Write a Python program to calculate the Least Common Multiple (LCM) of two numbers.

import math

num1=12

num2=15

lcm=(num1 * num2) // math.gcd(num1, num2)

print("Least Common Multiple (LCM) of", num1, "and", num2, "is:", l cm)

Output:

Least Common Multiple (LCM) of 12 and 15 is: 60

Q41-Write a Python program to calculate the square root of a given number.

import math
number = 16

square_root = math.sqrt(number)

print("Square root of", number, "is:", square_root)

Output:

Square root of 16 is: 4.0

Q42-Write a Python program to check if a given number is a perfect number or not.

number = 28

sum_of_divisors = sum(i for i in range(1, number) if number % i == 0)

if sum_of_divisors == number:

print(number, "is a perfect number")

else:

print(number, "is not a perfect number")

Output:
28 is a perfect number

Q43-Write a Python program to find all Armstrong numbers in a given range.

lower = 100

upper = 1000

armstrong_numbers = []

for num in range(lower, upper + 1):

num_digits = len(str(num))

sum_of_powers = sum(int(digit) ** num_digits for digit in str(num))

if sum_of_powers == num:

armstrong_numbers.append(num)

print("Armstrong numbers in range [", lower, ",", upper, "]:", armstrong_numbers)

Output:

Armstrong numbers in range [ 100 , 1000 ]: [153, 370, 371, 407]

Q44-Write a Python program to find the length of a string without using the built-in len() function

s="Hello, World!"

count=0
for char in s:

count += 1

print("Length of the string is:", count)

Output:

Length of the string is: 13

Q45-Write a Python program to calculate the sum of even and odd numbers in a list separately

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

sum_even = 0

sum_odd = 0

for num in numbers:

if num % 2 == 0:

sum_even += num

else:

sum_odd += num

print("Sum of even numbers:", sum_even)

print("Sum of odd numbers:", sum_odd)

Output:

Sum of even numbers: 30

Sum of odd numbers: 25

Q46-Write a Python program to check if a given string is a palindrome using function.

s = "radar"

is_palindrome = s == s[::-1]

if is_palindrome:

print(f'"{s}" is a palindrome')

else:

print(f'"{s}" is not a palindrome')

Output:
"radar" is a palindrome

Q47-Check if the element 'banana' exists in the list l = ['apple', 'banana', 'cherry'].

l = ['apple', 'banana', 'cherry']

if 'banana' in l:

print("'banana' exists in the list")

else:

print("'banana' does not exist in the list")

Output:
'banana' exists in the list

Q48-Sort the list l = [3, 1, 4, 1, 5, 9, 2, 6] in ascending order and print the sorted list.

l = [3, 1, 4, 1, 5, 9, 2, 6]

sorted_list = sorted(l, reverse=True)

print("Sorted list in descending order:", sorted_list)

Output:

Sorted list in descending order: [9, 6, 5, 4, 3, 2, 1, 1]

Q49-Convert the list l = [1, 2, 3] into a tuple t and convert the tuple t = (4, 5, 6) into a list. Print both

l = [1, 2, 3]

t = tuple(l)

t = (4, 5, 6)

list_from_tuple = list(t)

print("Tuple from list:", t)

print("List from tuple:", list_from_tuple)

Output:
Tuple from list: (4, 5, 6)

List from tuple: [4, 5, 6]

Q50- Create two lists l1 with elements [1, 2, 3] and l2 with elements [4, 5, 6]. Concatenate them into a
single list l3 and print l3.

l1 = [1, 2, 3]

l2 = [4, 5, 6]

l3 = l1 + l2
print("Concatenated list:", l3)

Output:

Concatenated list: [1, 2, 3, 4, 5, 6]

Q51-Create a tuple t with elements (1, 2, 3, 4, 5) and print the element at index 3.

t = (1, 2, 3, 4, 5)

print("Element at index 3:", t[3])

Output:

Element at index 3: 4

Q52-Create two tuples t1 with elements (1, 2, 3) and t2 with elements (4, 5, 6). Concatenate them into a
single tuple t3 and print t3

t1 = (1, 2, 3)

t2 = (4, 5, 6)

t3 = t1 + t2

print("Concatenated tuple:", t3)

Output:

Concatenated tuple: (1, 2, 3, 4, 5, 6)

Q53-Create a tuple t with elements (10, 20, 30, 40, 50). Print the first and last element.

t = (10, 20, 30, 40, 50)

print("First element:", t[0])

print("Last element:", t[-1])

Output:

First element: 10

Last element: 50

Q54-Create a tuple t with elements (10, 20, 30, 40, 50). Print the length of the tuple

t = (10, 20, 30, 40, 50)

length = len(t)

print("Length of the tuple:", length)

Output:

Length of the tuple: 5


Q55-Create a tuple t = (5, 1, 8, 3, 9, 2) and find the maximum and minimum elements

t = (5, 1, 8, 3, 9, 2)

max_element = max(t)

min_element = min(t)

print("Maximum element:", max_element)

print("Minimum element:", min_element)

Output:

Maximum element: 9

Minimum element: 1

Q56-Create a list l = ['apple', 'banana', 'cherry']. Find the index of the element 'banana'.

l = ['apple', 'banana', 'cherry']

index_of_banana = l.index('banana')

print("Index of 'banana':", index_of_banana)

Output:

Index of 'banana': 1

Q57-Remove duplicates from the list l = [1, 2, 3, 2, 4, 5, 1] and print the list without duplicates

l = [1, 2, 3, 2, 4, 5, 1]

l_no_duplicates = list(set(l))

print("List without duplicates:", l_no_duplicates)

Output:
List without duplicates: [1, 2, 3, 4, 5]

Q58-Write a Python program to swap the case of all characters in the string "Hello World"

s = "Hello World"

swapped_case = s.swapcase()

print("Swapped case:", swapped_case)

Output:
Swapped case: hELLO wORLD

Q59-Write a Python program to remove leading and trailing whitespace from the string " hello world"

s = " hello world "

trimmed_string = s.strip()
print("String without leading and trailing whitespace:", trimmed_string)

Q60-Write a Python program to find the index of the character 'w' in the string "hello world".

s = "hello world"

index_of_w = s.index('w')

print("Index of 'w':", index_of_w)

Output:
Index of 'w': 6

Q61-Write a Python program to remove all punctuation from the string "hello, world!".

import string

s = "hello, world!"

s_no_punctuation = s.translate(str.maketrans('', '', string.punctuation))

print("String without punctuation:", s_no_punctuation)

Output:

String without punctuation: hello world

Q62-Write a Python program to convert the string "hello world" to uppercase

s="hello world"

uppercased_string = s.upper()

print("Uppercase string:", uppercased_string)

Output:

Uppercase string: HELLO WORLD

Q63-Write a Python program to find the length of the string "OpenAI"

s = "OpenAI"

length = len(s)

print("Length of the string:", length)

Output:

Length of the string: 6

Q64-Write a Python program to split the string "hello world" into a list of words.

s = "hello world"

words_list = s.split()
print("List of words:", words_list)

Output:

List of words: ['hello', 'world']

Q65-Write a Python program to join the list ['hello', 'world'] into a single string with spaces.

words_list = ['hello', 'world']

joined_string = ' '.join(words_list)

print("Joined string:", joined_string)

Output:
Joined string: hello world

You might also like