Update some snippets
This commit is contained in:
@ -1,20 +1,22 @@
|
||||
---
|
||||
title: is_anagram
|
||||
tags: string
|
||||
tags: string,intermediate
|
||||
---
|
||||
Determine if 2 strings are anagrams.
|
||||
|
||||
Returns true if 2 strings are anagrams of each other, false otherwise.
|
||||
Capital letters and whitespaces are ignored.
|
||||
Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).
|
||||
|
||||
Use `str.replace()` to remove spaces from both strings.
|
||||
Compare the lengths of the two strings, return `False` if they are not equal.
|
||||
Use `sorted()` on both strings and compare the results.
|
||||
|
||||
```py
|
||||
def is_anagram(str1, str2):
|
||||
str1, str2 = str1.replace(" ", ""), str2.replace(" ", "")
|
||||
_str1, _str2 = str1.replace(" ", ""), str2.replace(" ", "")
|
||||
|
||||
if len(str1) != len(str2):
|
||||
return False
|
||||
else:
|
||||
return sorted(str1.lower()) == sorted(str2.lower())
|
||||
if len(_str1) != len(_str2):
|
||||
return False
|
||||
else:
|
||||
return sorted(_str1.lower()) == sorted(_str2.lower())
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
---
|
||||
title: is_lower_case
|
||||
tags: string
|
||||
tags: string,utility,beginner
|
||||
---
|
||||
|
||||
Checks if a string is lower case.
|
||||
|
||||
Convert the given string to lower case, using `str.lower()` method and compare it to the original.
|
||||
Convert the given string to lower case, using `str.lower()` and compare it to the original.
|
||||
|
||||
```py
|
||||
def is_lower_case(string):
|
||||
return string == string.lower()
|
||||
return string == string.lower()
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
---
|
||||
title: is_upper_case
|
||||
tags: string
|
||||
tags: string,utility,beginner
|
||||
---
|
||||
|
||||
Checks if a string is upper case.
|
||||
|
||||
Convert the given string to upper case, using `str.upper()` method and compare it to the original.
|
||||
Convert the given string to upper case, using `str.upper()` and compare it to the original.
|
||||
|
||||
```py
|
||||
def is_upper_case(string):
|
||||
return string == string.upper()
|
||||
return string == string.upper()
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,21 +1,23 @@
|
||||
---
|
||||
title: keys_only
|
||||
tags: object
|
||||
tags: object,list,beginner
|
||||
---
|
||||
Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.
|
||||
|
||||
Uses the .keys() method of "dict" objects. dict.keys() returns a view object that displays a list of all the keys. Then, list(dict.keys()) returns a list that stores all the keys of a dict.
|
||||
Returns a flat list of all the keys in a flat dictionary.
|
||||
|
||||
Use `dict.keys()` to return the keys in the given dictionary.
|
||||
Return a `list()` of the previous result.
|
||||
|
||||
```py
|
||||
def keys_only(flat_dict):
|
||||
return list(flat_dict.keys())
|
||||
return list(flat_dict.keys())
|
||||
```
|
||||
|
||||
```py
|
||||
ages = {
|
||||
"Peter": 10,
|
||||
"Isabel": 11,
|
||||
"Anna": 9,
|
||||
"Peter": 10,
|
||||
"Isabel": 11,
|
||||
"Anna": 9,
|
||||
}
|
||||
keys_only(ages) # ['Peter', 'Isabel', 'Anna']
|
||||
```
|
||||
Reference in New Issue
Block a user