Skip to content

Commit 3349c19

Browse files
committed
Solutions for hackerrank problems python
1 parent 5e51d05 commit 3349c19

File tree

8 files changed

+79
-0
lines changed

8 files changed

+79
-0
lines changed

OTHERS/Hackerrank/exception.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
list_a = [1,2,3,4,5]
2+
3+
try:
4+
index = int(input("Enter the index: "))
5+
6+
try:
7+
print(list_a[index])
8+
except IndexError:
9+
print("The index {} is incorrect and index should lie between -5 and 4".format(index))
10+
11+
except ValueError:
12+
print("Use an integer as an input")

OTHERS/Hackerrank/frozensets.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
set1 = {'A','B','C','D'}
2+
set2 = {'A',2,'C',4}
3+
frozen_set_1 = frozenset(set1)
4+
frozen_set_2 = frozenset(set2)
5+
6+
frozenset_union = frozen_set_1.union(frozen_set_2)
7+
frozenset_common = frozen_set_1.intersection(frozen_set_2)
8+
frozenset_difference = frozen_set_1.difference(frozen_set_2)
9+
frozenset_distinct = frozen_set_1.symmetric_difference(frozen_set_2)
10+
11+
print("frozen_set_1:",frozen_set_1)
12+
print("frozen_set_2:",frozen_set_2)
13+
print("frozenset_union:",frozenset_union)
14+
print("frozenset_common:",frozenset_common)
15+
print("frozenset_difference:",frozenset_difference)
16+
print("frozenset_distinct:",frozenset_distinct)

OTHERS/Hackerrank/immutable.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tuple1 = (1,2,3,4,5,6,7,8,9,10)
2+
3+
tuple1[5] = 100
4+
5+
print(tuple1)
6+
7+
# Tuples are immutable because the tuple object doesnot support assignment of any elements to any particular index
8+
# inorder to change the value of a particular index we need to create a new tuple object and assign it to the old tuple object
9+
10+
# Immutable is a property of an object that cannot be changed once it is created. to change the value of a particular index we need to reassign the tuple to same tuple object.
11+
# Immutable objects include sets and tuples.

OTHERS/Hackerrank/list-removal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
list_a = ["car", "place", "tree", "under", "grass", "price"]
2+
3+
remove_items_containing_a_or_A = lambda list_1 : [item for item in list_1 if "a" not in item.lower()]
4+
5+
print(remove_items_containing_a_or_A(list_a))

OTHERS/Hackerrank/pattern.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
user_input = int(input("Enter a number of your choice : "))
2+
3+
if(user_input == 1):
4+
for i in range(5,0,-1):
5+
print("*"*i)
6+
7+
elif(user_input == 2):
8+
for i in range(5,0,-1):
9+
print("*"*i)
10+
for j in range(2,6):
11+
print("-"*j)
12+
13+
else:
14+
print("Invalid input")

OTHERS/Hackerrank/sequence.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
A = [1,2,3,4,5,6]
2+
3+
for element in A:
4+
print(element**2)
5+
else:
6+
print("The sequence has ended")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
list_a = ["car", "place", "tree", "under", "grass", "price"]
2+
3+
remove_items_containing_a_or_A = lambda list_1 : [item for item in list_1 if "a" not in item.lower()]
4+
5+
print(remove_items_containing_a_or_A(list_a))

OTHERS/Hackerrank/tuples.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
t_1 = (1,4,9,16,25,36)
2+
3+
t_modified = tuple(i**2 for i in t_1)
4+
5+
print("t_modified:",t_modified)
6+
7+
print("Element at index postion 4 of t_modified:",t_modified[4])
8+
9+
t_sliced = t_modified[1:4]
10+
print("t_sliced:",t_sliced)

0 commit comments

Comments
 (0)