Last active
November 12, 2018 08:46
-
-
Save sp90/377c12e2b69136b6da658ed38ef36294 to your computer and use it in GitHub Desktop.
Helper functions
This file contains 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
# Numbers between | |
def numbers_between(numbers, min_range, max_range): | |
# Create empty list to contain our results | |
result = [] | |
# Run through each number | |
for number in numbers: | |
# If number is bigger or equal to min_range and bigger or equal to max_range | |
if number >= min_range and number <= max_range: | |
# Then append it to our result list | |
result.append(number) | |
# Return the numbers that fit our if sentence | |
return result | |
# List of test numbers | |
test_numbers = [-3, -2, 4, 8, 90, 100, 183, 200, 212, 300]; | |
# Find numbers between 3 & 100 | |
between_3_and_100 = numbers_between(test_numbers, 3, 100) | |
# Print the result | |
print(between_3_and_100) # [4, 8, 90, 100] | |
# Numbers containing | |
def numbers_containing_number(numbers, number_to_contain): | |
# Create empty list to contain our results | |
result = [] | |
# Run through each number | |
for number in numbers: | |
#Convert both numbers to strings (because you cant test like this on integers) | |
if str(number_to_contain) in str(number): | |
# Then append it to our result list | |
result.append(number) | |
# Return the numbers that contains the number_to_contain | |
return result | |
# List of test numbers | |
test_numbers = [-3, -2, 4, 8, 90, 100, 183, 200, 212, 300]; | |
# Find numbers containing the number 2 | |
containing_2 = numbers_containing_number(test_numbers, 2) | |
# Print the result | |
print(containing_2) # [-2, 200, 212] | |
# Sort string lengths | |
# List of test strings | |
array_of_strings_1 = ['hus', 'have', 'gade', 'kaffe', 'eksamen', 'hoppe'] | |
array_of_strings_2 = ['bb', 'eeeee', 'ccc', 'a', 'dddd'] | |
# Use built in sort method to sort by string length | |
array_of_strings_1.sort(key=len) | |
# Use built in sort method to sort by string length in reverse order | |
array_of_strings_2.sort(key=len, reverse=True) | |
# Print results from the sorting | |
print(array_of_strings_1) # ['hus', 'have', 'gade', 'kaffe', 'hoppe', 'eksamen'] | |
print(array_of_strings_2) # ['eeeee', 'dddd', 'ccc', 'bb', 'a'] | |
# Find each string containing a part string | |
# Example 1 | |
# if 'e' in 'test' | |
# which is true because test contains the string 'e' | |
# | |
# Example 2 | |
# if 'est' in 'test' | |
# which is also true because test contains the string 'est' | |
def strings_containing_string(strings, string_to_contain): | |
# Create empty list to contain our results | |
result = [] | |
# Run through each string | |
for string in strings: | |
# Find out if string_to_contain are in the string | |
if string_to_contain in string: | |
# If it is append it to our results | |
result.append(string) | |
return result | |
# List of test strings | |
test_strings = ['hus', 'have', 'gade', 'kaffe', 'eksamen', 'hoppe'] | |
# Find the strings containing a | |
strings_containing_a = strings_containing_string(test_strings, 'a') | |
# Find the strings containing am | |
strings_containing_am = strings_containing_string(test_strings, 'am') | |
# Print the result | |
print(strings_containing_a) # ['have', 'gade', 'kaffe', 'eksamen'] | |
print(strings_containing_am) # ['eksamen'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment