Skip to content

Commit

Permalink
[Tutorial] String Reversal Techniques (inspirezonetech#142)
Browse files Browse the repository at this point in the history
* I added two ways of reversing strings , but I was contemplating on adding the deque method but thought it was too complex

* fixed the flake8 errors
  • Loading branch information
i-ganza007 authored Apr 11, 2024
1 parent 3cee4c7 commit cf50a38
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions string-formatting/string-reversal.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
# ------------------------------------------------------------------------------------
# Tutorial: brief description of tutorial content
# We're going to look at a few different ways that you can reverse the structure of a string .
# Let's begin.
# ------------------------------------------------------------------------------------

# Code here explaining concept with comments to guide
# Single Words , A string is an iterable and anything within these --> ""
# 1. The built-in reverse method
# This method is used on iterables , commonly on lists and strings
# Example of functions that reverse a string.
def reverseString(word):
reversedWord = "".join(reversed(word))
return reversedWord
'''
Let's break down what's above:
Remember that all strings are immutable but iterable. So if that's the case , we can't use the .reverse() function like we could on a list.
So we iterate through each letter and use the reversed() function . Now since the reversed() function return an iterable;a list .
We use the join() to concatenate the letters and then we return the reversed String.
'''
# 2.The reverse slice method
# This method also works on iterables too.
# Let's use the same example with this method too
def backString(word):
reversedWord = word[::-1]
return reversedWord
'''
Let's break this down as well:
The slicing technique is used to get a certain portion of string and using the starting and ending index values.
e.g [a:b], this slices from position to position before b ; so b-1 . What we don't commonly use is the third option of [a:b:c] <-- C.
What c does is specify the number of characters to jump or skip over . So if c == 2 , we're gonna skip over 2 characters .But if c == -1,this specifies that the string begins from the back.
We commonly use -1 when we want the last letter or character in a string e.g word = "pet",word[-1] == t.
Back to the function , if we don't specify the starting and end values , the computer just assumes it's the whole string .
So e.g [a:] this is from a to the last , [:b] from the first to b , [a::c] from a to the last every c characters , [:b:c] from the first to b every c characters .
So [::-1] means from the first to last in reverse .
'''

# ------------------------------------------------------------------------------------
# Challenge: list challenges to be completed here. minimum of one challenge per tutorial
# ------------------------------------------------------------------------------------
# The format is the same even for sentences : So write a function that reverses the sentence "I love ketchup".

0 comments on commit cf50a38

Please sign in to comment.