In: Computer Science
Reference: Case Study 5
1. def scramble2Encrypt(plainText ):
2 evenChars = ""
3 oddChars = ""
4 charCount = 0
5 for ch in plainText :
6 if charCount % 2== 0:
7 evenChars =
evenChars + ch
8 else:
9 oddChars
= oddChars + ch
10 charCount = charCount + 1
11 cipherText = oddChars + evenChars
12 return cipherText
22. (10 points) Refer to the code in Case Study 5. Provide a
line-by-line description of what is happening on lines 5 through
10. What are those lines of code actually doing? Please don’t
merely translate the code to words. I know that in line 7, we are
adding ch to evenChars, but what does that mean? =)
Basically here you are converting a plain text to some sort of encrypted way of text i.e For Ex: you are defining your own salt function to convert the plain text so that you form a new text(cypher text) so that no one understand its representation or meaning except you(because you have defined it in your way). This is done for security purposes. Now lets get into the code.
Line by Line explaination:
Line 5: for ch in plainText :
Here plainText is a string. So the above for loop is iterating over the plainText. For every iteration, ch is nothing but pointing out to each character value in plainText.
Ex: plainText="Hello". for ch in plainText: gives me ch="H",ch="e",ch="l",ch="l",ch="o" over the loop.
Line 6: if charCount % 2== 0:
Initially charCount is 0. This basically checks whether charCount value is even or odd number. If charCount%2==0, then charCount is even. Else, charCount is odd.
Ex: charCount=0 -> even
charCount=3 ->odd
charCount=6 -> even
Line 7: evenChars = evenChars + ch
evenChars is declared as empty string initially. So that if you add some string to it using +, that string will concatenate to evenChars (Note: In python, character is also considered as string)
If the line 6 is satisfied i.e if charCount is even, we add that particular character(ch in for loop) to evenChars string. We keep on doing this when charCount is even.
Line 8: else:
This represents if charCount is not even i.e if charCount is odd.
Line 9: oddChars = oddChars + ch
oddChars is declared as empty string initially. So that if you add some string to it using +, that string will concatenate to oddChars (Note: In python, character is also considered as string)
If charCount is odd, then that character(ch in for loop) will be concatenated to oddChars string. We keep on doing this when charCount is odd.
Line 10: charCount = charCount + 1
(Note: I can see in the above code that indendation is not follwed in Line 10. So ensure that this line is in proper format to, for loop. Else, you will get error while executing the code.)
This basically count the number of characters present in the string plainText. For each iteration of for loop, this charCount will get incremented by 1. So by end of the loop you will get the length of the string plainText.
Ex: plainText="Hello". by end of the for loop, charCount will be 5
#Please dont forget to upvote if you find the solution helpful. Thank you.