Skip to content

Instantly share code, notes, and snippets.

@x0xman
Last active August 28, 2022 13:06
Show Gist options
  • Save x0xman/101523e749534206d549a4a2dd0cfb2c to your computer and use it in GitHub Desktop.
Save x0xman/101523e749534206d549a4a2dd0cfb2c to your computer and use it in GitHub Desktop.
Optimztion Code
"""
:func return_string_letters
"""
def return_string_letters(string1, string2):
list_boolean = []
for key1 , key2 in zip(string1 , string2):
if len(key1) > len(key2):
list_boolean.append(len(key1))
else:
list_boolean.append(len(key2))
return list_boolean
"""
:func less_or_more_than_zero
"""
def less_or_more_than_zero(*number):
statement_number = [ number > 0 , number == 0 , number < 0 ]
output_statement = [ "Greater than zero" , "Equal to zero" , "Less than zero"]
map_func = list(map( lambda x : j for j in range( 0 , len(x) ) if x[j] , statement_number))
return map_func
"""
:func largest_smallest
"""
def largest_smallest(array):
pass
"""
:func find_element
"""
def find_element(array, element):
"""
array :: [3 , 5 , 6 , 7 , 9 ]
#Output element(1) :: [5] -> True
"""
return True if array.index(element) else False
"""
:func sort_array
"""
def sub_arrays(arr1,arr2):
"""
arr1 = [4 , 2 , 88]
arr2 = [2 , 4 , 88]
"""
return list( map(lambda a1 , a2 : a1 - a2 , arr1 , arr2) )
"""
:func sort_array
"""
def sort_array(array):
"""
return sorted(array)
:::: or you can use bad another way
"""
list_sort = []
for j in range(0 , len(array)):
if array[j] > array[j + 1] and j < len(array):
list_sort.append(array[j])
else:
list_sort.append(array[j + 1 ])
return list_sort
"""
:func removeSpecialCharacters
"""
def removeSpecialCharacters(string):
special_character = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
except_special_character = "- ـ"
solve = ""
for j in string:
if string not in special_character and except_special_character is in string :
solve += string
return solve
"""
:func num_elements
"""
def num_elements(array):
"""
array :: [10 , 11 , 2 , 4 , 5 ]
#Output :: 4
"""
return len(array)
"""
:func hashtag_it
"""
def hashtag_it(array):
"""
array :: ["hello" , "ahmed" , "Saud"]
#Output :: ["@hello" , "@ahmed" , "@Saud"]
"""
return "#" * " ".join( [j for j in array])
"""
:func find_element
"""
def find_element(array, element):
return True if element in array else False
"""
:func max_element
"""
def max_element(arr):
"""
arr :: [10 , 1 , 4 , 5]
#Output :: 10
"""
return mix(arr)
"""
:func min_element
"""
def min_element(arr):
"""
arr :: [10 , 1 , 4 , 5]
#Output :: 1
"""
return min(arr)
"""
:func cumulative_addition
"""
def cumulative_addition(arr = [2 , 4 , 9 , 23 , 435]):
"""
arr :: [2 , 4 , 9 , 23 , 435]
#Output :: [ 473 , 4]
"""
return [ sum ( arr ) , len ( arr ) ]
"""
:delete_element_in_array
"""
def delete_element_in_array (array , index ):
"""
array :: [10 , 2 ,3 ,4]
#Output pop index(1) :: [2 ,3 ,4]
"""
pop_element = array.pop(index)
return array
"""
:func filp_even_odd
"""
def filp_even_odd(array):
"""
#array :: [24, 13, 14, 18]
#Output :: [25, 12, 15, 19]
"""
return list(map(lambda a : a + 1 if a % 2 == 0 else a - 1 , array))
"""
:func sorted_func
"""
def sorted_func():
sort_list = [4 , 9 , 3 , 2 , 7]
for p in range(0 , len(sort_list)):
for j in range(0 , len(sort_list) - 1 ):
if sort_list[j] > sort_list[j + 1]:
sort_list[j] , sort_list[j + 1] = sort_list[j + 1] , sort_list[j]
return sort_list
## Good
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
## Bad use for loop
>>> lists = []
>>> for i in range(0 , 10):
lists.append(i)
>>> lists
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
----------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment