Skip to content

Add some Python codes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Codes/Occurrences_of_Characters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def encode(Test_string):
count = 0
Result = ""
for i in range(len(Test_string)):
if (i+1) < len(Test_string) and (Test_string[i] == Test_string[i+1]):
count += 1
else:
Result += str((count+1))+Test_string[i]
count = 0
return Result


print(encode("ABBBBCCCCCCCCAB"))
8 changes: 8 additions & 0 deletions Codes/Pair_Combinations_of_two_tuples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def Pair_Combinations(Test_tuple_1, Test_tuple_2):
return [(a, b) for a in Test_tuple_1 for b in Test_tuple_2] +\
[(a, b) for a in Test_tuple_2 for b in Test_tuple_1]


Test_tuple_1 = (4, 5)
Test_tuple_2 = (7, 8)
print(Pair_Combinations(Test_tuple_1, Test_tuple_2))
7 changes: 7 additions & 0 deletions Codes/Remove_Tuple_of_length_k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def Remove_Tuple(Test_list, K):
return [elements for elements in Test_list if len(elements) != K]


Test_list = [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
K = int(input("Enter K: "))
print(Remove_Tuple(Test_list, K))
25 changes: 25 additions & 0 deletions Codes/Sort_list_by_second_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Approach 1:

from operator import itemgetter


def Sort_by_second_items(Test_tup):
Test_tup.sort(key=lambda x: x[1])
return Test_tup


Test_tup = [('for', 24), ('is', 10), ('Geeks', 28),
('Geeksforgeeks', 5), ('portal', 20), ('a', 15)]
print(Sort_by_second_items(Test_tup))

# Approach 2:


def Sort_by_second_items(Test_tup):
Test_tup.sort(key=itemgetter(1))
return Test_tup


Test_tup = [('for', 24), ('is', 10), ('Geeks', 28),
('Geeksforgeeks', 5), ('portal', 20), ('a', 15)]
print(Sort_by_second_items(Test_tup))