From cf50a386171e9dd28681b2a9059f93deb44eae3d Mon Sep 17 00:00:00 2001 From: Ian Ganza <144003894+i-ganza007@users.noreply.github.com> Date: Thu, 11 Apr 2024 19:20:47 +0300 Subject: [PATCH] [Tutorial] String Reversal Techniques (#142) * 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 --- string-formatting/string-reversal.py | 36 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/string-formatting/string-reversal.py b/string-formatting/string-reversal.py index e088818..c355273 100644 --- a/string-formatting/string-reversal.py +++ b/string-formatting/string-reversal.py @@ -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".