Skip to content

Commit cc51577

Browse files
authored
Create Practice
1 parent d0b62b7 commit cc51577

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

Practice

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# max no in array
2+
arr=[10,2,12, 4,1,7,9]
3+
4+
for i in range (0, len(arr)-1):
5+
if arr[i]>arr[i+1]:
6+
arr[i], arr[i+1]=arr[i+1], arr[i]
7+
8+
print(arr[-1])
9+
10+
11+
#factorial (recursive)
12+
def fact(no):
13+
if no==1 or no==0:
14+
return 1
15+
16+
return no*fact(no-1)
17+
18+
print(fact(4))
19+
20+
#factorial (iterative)
21+
def facto(no):
22+
factorial=1
23+
for i in range (no,0, -1):
24+
factorial*=i
25+
return factorial
26+
27+
print(facto(4))
28+
29+
# string=LETTERS repeated alph print na kry
30+
def noRepeat(string):
31+
seen_chars = [] # An empty list to keep track of seen characters
32+
result = "" # An empty string to store the unique characters in order
33+
34+
for char in string:
35+
if char not in seen_chars:
36+
seen_chars.append(char) # Add the character to the list of seen characters
37+
result += char # Add the character to the result string
38+
elif char in result:
39+
result = result.replace(char, '') # Remove any repeated occurrences of the character from the result
40+
return result
41+
42+
print(noRepeat("LETTERS"))
43+
44+
45+
# anagrams
46+
s1= "liste"
47+
s2="silent"
48+
def sort_string(word):
49+
string= list(word)
50+
for i in range(len(string)-1):
51+
if string[i]>string[i+1]:
52+
string[i], string[i+1]= string[i+1], string[i]
53+
return ''.join(string)
54+
55+
if (sort_string(s1.lower())==sort_string(s2.lower())):
56+
print (f"{s1} and {s2} are anagrams")
57+
else:
58+
print(f"{s1} and {s2} are not anagrams")
59+
60+
# arr= 1,3,5,7,9 even num agr req kia hai tou closed odd no dy. 20 req kia hai tou wo 19 dy e.g. 369
61+
arr = [1, 3, 5, 7, 9]
62+
63+
try:
64+
user_input = int(input("Enter a number: "))
65+
except ValueError:
66+
print("Invalid input. Please enter a valid number.")
67+
exit()
68+
69+
if user_input % 2 == 0:
70+
closest_odd = None
71+
min_difference = float('inf')
72+
73+
for num in arr:
74+
if num % 2 == 1:
75+
difference = abs(user_input - num)
76+
if difference < min_difference:
77+
min_difference = difference
78+
closest_odd = num
79+
80+
if closest_odd is not None:
81+
print(f"The closest odd number to {user_input} in the array is {closest_odd}.")
82+
else:
83+
print("There are no odd numbers in the array.")
84+
else:
85+
print("The user input is not even.")
86+
87+
# dict ki values count kkrni thi
88+
my_dict = {'a': 2, 'b': 3, 'c': 4}
89+
sum=0
90+
for x,y in my_dict.items():
91+
sum+=y
92+
93+
print(sum)
94+
95+
# prior datetime
96+
from datetime import datetime, timedelta
97+
98+
# Assuming you have a datetime object
99+
current_datetime = datetime.now()
100+
101+
# Calculate the prior datetime, for example, 1 day ago
102+
prior_datetime = current_datetime - timedelta(days=20)
103+
104+
print("Current datetime:", current_datetime)
105+
print("Prior datetime:", prior_datetime)
106+
107+
108+
109+
original_string = "Hello, World!"
110+
char_to_remove = ","
111+
112+
# Removing the specified character using list comprehension
113+
modified_string = "".join([char for char in original_string if char != char_to_remove])
114+
115+
print("Original String:", original_string)
116+
print("Modified String:", modified_string)
117+
118+
119+
# palindrome
120+
word= "madam"
121+
rev=""
122+
for i in range(len(word)-1, -1, -1):
123+
rev+=word[i]
124+
125+
if word==rev:
126+
print(f"{word} is a palindrome")
127+
else:
128+
print(f"{word} is not a palindrome")
129+
130+
131+
# CATEGORIZING
132+
date_list = [[25, 'Jan', 2022], [24, 'Jan', 2022], [20, 'Feb', 2022]]
133+
134+
result_dict = {}
135+
136+
for date in date_list:
137+
month_year = (date[1], date[2])
138+
if month_year in result_dict:
139+
result_dict[month_year].append(date)
140+
else:
141+
result_dict[month_year] = [date]
142+
143+
print(result_dict)

0 commit comments

Comments
 (0)