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

Assignment 1 - Colab

The document contains a series of Python code snippets for various assignments, including generating random numbers, calculating maximum, minimum, and average values, searching for elements in arrays, and sorting. It also includes functions for mathematical operations like calculating the volume of a sphere, counting digits in an integer, and finding the greatest common divisor (GCD) and least common multiple (LCM). Additionally, there are examples of working with 1D and 2D arrays, as well as counting occurrences of elements.

Uploaded by

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

Assignment 1 - Colab

The document contains a series of Python code snippets for various assignments, including generating random numbers, calculating maximum, minimum, and average values, searching for elements in arrays, and sorting. It also includes functions for mathematical operations like calculating the volume of a sphere, counting digits in an integer, and finding the greatest common divisor (GCD) and least common multiple (LCM). Additionally, there are examples of working with 1D and 2D arrays, as well as counting occurrences of elements.

Uploaded by

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

2/16/25, 8:50 PM Assignment 1 - 1D - Colab

keyboard_arrow_down 1D
1 import random
2 li=[]
3 max=0
4 min =999999999999
5 avg=0
6 e=0
7 for i in range(int(input())):
8 li.append(random.randint(1,100))
9 print(li)
10 for j in li:
11 e+=1
12 if j>max :
13 max=j
14 if j<min:
15 min=j
16 avg+=j
17 #print(e)
18 avg=avg/e
19 print(f'max={max}\nmin={min}\navg={avg}')
20
21
22

15
[53, 64, 6, 11, 58, 96, 34, 44, 10, 18, 39, 50, 55, 87, 100]
max=100
min=6
avg=48.333333333333336

1 import random
2
3 size=int(input('Enter Array Size : '))
4 li=[]
5 for i in range(size):
6 li.append(random.randint(0,20))
7 print(li)
8 search=int(input('Search Number : '))
9 index=0
10 t=0
11 indices=[]
12 for j in li :
13 index+=1
14 if j == search:
15 indices.append(index-1)
16 t+=1
17 if t>0:
18 print(f'the number {search} is found at{indices}\nit appears {t} times')
19 else:
20 print('Not found')
21
22
23
24
25

Enter Array Size : 20


[16, 8, 7, 15, 9, 9, 10, 9, 10, 19, 13, 13, 2, 9, 5, 18, 5, 3, 11, 15]
Search Number : 11
the number 11 is found at[18]
it appears 1 times

1 size = 10
2 li=[]
3 for i in range(size):
4 a=int(input('Enter List Elements: '))
5 li.append(a)
6 #print(li[::-1])
7 print(li)
8 if li==sorted(li):
9 print('Ac')
10 elif li==sorted(li, reverse=True):

https://colab.research.google.com/drive/14fGr4A7KvLYJnES1UrQ6_YfnnPypdEsF#scrollTo=IBNyvoquOGjb&printMode=true 1/5
2/16/25, 8:50 PM Assignment 1 - 1D - Colab
11 print('Dc')
12 else:
13 print('Not')
14
15
16

Enter List Elements: 1


Enter List Elements: 2
Enter List Elements: 3
Enter List Elements: 4
Enter List Elements: 5
Enter List Elements: 6
Enter List Elements: 7
Enter List Elements: 8
Enter List Elements: 9
Enter List Elements: 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Ac

1 import random
2 size=int(input('List Size : '))
3 arr=[]
4 arrEven=[]
5 arrOdd=[]
6
7 for i in range(size):
8 arr.append(random.randint(0,15))
9 for j in arr:
10 if j%2==0 :
11 arrEven.append(j)
12 else:
13 arrOdd.append(j)
14 print(f'The Main Array : {arr}\nThe Even Array : {arrEven}\nThe Odd Array : {arrOdd}')
15
16

List Size : 10
The Main Array : [12, 3, 1, 14, 5, 12, 13, 12, 9, 15]
The Even Array : [12, 14, 12, 12]
The Odd Array : [3, 1, 5, 13, 9, 15]

1 import random
2 size=int(input('List Size : '))
3 arr=[]
4 arrPrime=[]
5
6
7 for i in range(size):
8 arr.append(random.randint(2,15))
9
10 for j in arr :
11 x=True
12 for o in range(2,j):
13 if j%o==0:
14 x=False
15 break
16 if x:
17 arrPrime.append(j)
18
19 print(f'{arr}\n{arrPrime}')
20

List Size : 15
[3, 9, 2, 7, 9, 12, 2, 5, 10, 10, 8, 6, 2, 6, 7]
[3, 2, 7, 2, 5, 2, 7]

1 arr = [3, 5, 3, 2, 8, 5, 2, 8, 8, 3]
2 counted = []
3
4 for i in arr:
5 if i not in counted:
6 count = 0
7 for j in arr:
8 if i == j:
9 count += 1
10 print(f"{i}: {count}")

https://colab.research.google.com/drive/14fGr4A7KvLYJnES1UrQ6_YfnnPypdEsF#scrollTo=IBNyvoquOGjb&printMode=true 2/5
2/16/25, 8:50 PM Assignment 1 - 1D - Colab
11 counted.append(i)
12
13

3: 3
5: 2
2: 2
8: 3

keyboard_arrow_down 2D
1 a=[[1,2,3],[4,5,6],[7,8,9],[2,3,4]]
2 b=[[10,20,30],[40,50,60],[70,80,90],[9,3,5]]
3
4 c=[[b[i][j] - a[i][j] for j in range(3)] for i in range(4)]
5 print(c)

[[9, 18, 27], [36, 45, 54], [63, 72, 81], [7, 0, 1]]

1 import random
2 a=[]
3 b=[]
4 for j in range(4):
5 li=[]
6 for i in range (3):
7 c=random.randint(0,10)
8 li.append(c)
9 a.append(li)
10 for j in range(4):
11 li2=[]
12 for i in range (3):
13 c=random.randint(0,10)
14 li2.append(c)
15 b.append(li2)
16 print(a)
17 print(b)
18 c=[[b[i][j]-a[i][j]for j in range(3)]for i in range (4)]
19 print(c)
20

[[8, 9, 0], [5, 7, 5], [5, 1, 0], [4, 10, 1]]


[[3, 1, 1], [4, 3, 3], [5, 5, 9], [10, 4, 5]]
[[-5, -8, 1], [-1, -4, -2], [0, 4, 9], [6, -6, 4]]

1 import random
2 li=[]
3
4 for i in range(15):
5 a=[]
6 for j in range(10):
7 c=random.randint(0,10)
8 a.append(c)
9 li.append(a)
10 print(li)
11 for ave in li :
12 average=0
13 for e in ave:
14 average+=e
15 print(average)
16

[[9, 0, 9, 7, 8, 4, 1, 7, 1, 9], [6, 5, 5, 4, 2, 7, 8, 7, 3, 0], [3, 9, 1, 1, 6, 3, 5, 3, 9, 0], [7, 2, 5, 0, 10, 5, 5, 10, 9, 1], [7, 9
55
47
40
54
43
38
55
69
50
61
52
40
51

https://colab.research.google.com/drive/14fGr4A7KvLYJnES1UrQ6_YfnnPypdEsF#scrollTo=IBNyvoquOGjb&printMode=true 3/5
2/16/25, 8:50 PM Assignment 1 - 1D - Colab
58
61

1 li=[0,1,2,3,4,5,6,7,8,9]
2 li2=[]
3 li3=[]
4 li4=[]
5 count=0
6 for i in li:
7 if count<5:
8 li2.append(i)
9 count+=1
10 else:
11 li3.append(i)
12 li4.append(li2)
13 li4.append(li3)
14 print(li4)
15
16
17
18
19
20

[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]

keyboard_arrow_down Functions
1 def sphere_volm(r):
2 pi=3.14159265359
3 volume=(4/3)*pi*r**3
4 return round(volume,2)
5
6 radius=float(input('Enter Radius : '))
7 sphere_volm(radius)

Enter Radius : 4.5


381.7

1 def count_int(c):
2 count=0
3 C=c
4 for i in range(c):
5 if C // 10 >=1:
6 count+=1
7 C=C//10
8 else:
9 count+=1
10 break
11 return count
12 num=int(input('enter an Integer : '))
13 count_int(num)
14
15
16

enter an Integer : 8456972


7

1 def reverse(x):
2 r=str(x)
3 re=int(r[::-1])
4 return re
5 v=int(input('Enter a Number : '))
6 reverse(v)

Enter a Number : 4561


1654

1 def gcd(a, b):


2 while b:

https://colab.research.google.com/drive/14fGr4A7KvLYJnES1UrQ6_YfnnPypdEsF#scrollTo=IBNyvoquOGjb&printMode=true 4/5
2/16/25, 8:50 PM Assignment 1 - 1D - Colab
3 a, b = b, a % b
4 return a
5
6 def lcm(a, b):
7 return abs(a * b) // gcd(a, b)
8
9 num1 = 12
10 num2 = 18
11 print("LCM:", lcm(num1, num2))
12

1 import random
2 def sort_array(x):
3 x.sort()
4 return x
5
6 emp=[]
7 for i in range(10):
8 xx=random.randint(0,10)
9 emp.append(xx)
10 print(emp)
11 v=sort_array(emp)
12 print(v)

[4, 2, 8, 5, 5, 8, 2, 0, 2, 2]
[0, 2, 2, 2, 2, 4, 5, 5, 8, 8]

1 def count_words(s):
2 return len(s.split())
3
4 # Example usage:
5 text =input('Enter your Sentence : ')
6 print("Number of words:", count_words(text)) # Output: 6
7

Enter your Sentence : hey, wassup?


Number of words: 2

https://colab.research.google.com/drive/14fGr4A7KvLYJnES1UrQ6_YfnnPypdEsF#scrollTo=IBNyvoquOGjb&printMode=true 5/5

You might also like