This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num = [1, 2, 3, 4] | |
double_num = map(lambda x: x + x, num) | |
print(double_num) | |
# Output | |
# [2, 4, 6, 8] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Method | Execution Speed | |
---|---|---|
Slicing | 0.111 μs | |
Appending characters | 0.359 μs | |
Using reversed() | 0.429 μs | |
Built-in reverse() function | 0.434 μs | |
Recursion | 1.0199 μs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reverse_string_loop(original_string): | |
reversed_string = "" | |
for a in original_string: | |
# appending characters in reverse | |
reversed_string = a + reversed_string | |
return reversed_string | |
original_string = "ABCDE" | |
print("Original String : ", original_string) | |
print("Reversed String : ", reverse_string_loop(original_string)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reverse_string_iterator(original_string): | |
reversed_iterator = reversed(original_string) | |
reversed_string = "".join(reversed_iterator) | |
return reversed_string | |
original_string = "ABCDE" | |
print("Original String1 : ", original_string) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reverse_string(original_string): | |
temp_list = list(original_string) | |
temp_list.reverse() | |
reversed_string = "".join(temp_list) | |
return reversed_string | |
original_string = "ABCDE" | |
print("Original String : ", original_string) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def recursion_reverse(s): | |
# Base Case | |
if(len(s)==1): | |
return s | |
else: | |
return recursion_reverse(s[1:]) + s[0] | |
original_string = "ABCDE" | |
print("Original String : ", original_string) | |
print("Reversed String : ", recursion_reverse(original_string)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reversing a string using slicing | |
my_string = "ABCDE" | |
reversed_string = my_string[::-1] | |
print(reversed_string) | |
# Output | |
# EDCBA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Multiplying each element in a list by 2 | |
original_list = [1,2,3,4] | |
new_list = [2*x for x in original_list] | |
print(new_list) | |
# [2,4,6,8] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# finding frequency of each element in a list | |
from collections import Counter | |
my_list = ['a','a','b','b','b','c','d','d','d','d','d'] | |
count = Counter(my_list) # defining a counter object | |
print(count) # Of all elements | |
# Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1}) | |
print(count['b']) # of individual element |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_string = "my name is chaitanya baweja" | |
# using the title() function of string class | |
new_string = my_string.title() | |
print(new_string) | |
# Output | |
# My Name Is Chaitanya Baweja |