2018-02-28 HW

posted Feb 28, 2018, 5:58 AM by Konstantinovich Samuel   [ updated Feb 28, 2018, 7:18 AM ]
-complete the ROT13 section on codingbat

-To simplify keeping track of your problems, I will put only the problems you are required to complete on this page:
EXCEPT: String2 (you only need to have done half so far!)

You can still see all the problems on the old page, but don't have to worry if you were supposed to do more or not. 
You can also still go ahead because the problems on both pages are linked (you don't have to do them twice)

Do Now: (On paper, do not use the computer!)
Evaluate each expression:
a) 3 / 4                b) 3 / 4.0
c) 16 % 5            d) 5 % 16
e) "1" + "2"           f) 1 + "2" 

Trace Through the loop: (On paper, do not use the computer!)
sum = 0
i = 3
while i < 20:
  sum = sum + i + 1

g) What is the value of sum when the loop ends?
h) How many times did i get incremented? 

Goal: How NOT to hide your thoughts from the NSA.


Some examples:


OVT → BIG   






Note that ROT13 is a poor encryption method, because it is trivial to decrypt. This made it useful for posting spoilers or text that some people didn't want to see, because you must actively change the words in order to read it.

Basic algorithm:

We can go through a string, and decide what to do with each character. Append the new character to a new string, and after you append all the modified characters return that answer string.

This is a more in-depth explanation than on the website:

rot13char(c)  which returns the rot13 value of a single character c, but only if it is a valid upper or lower case letter. It returns the original character otherwise. It MUST RETURN a value, and not print anything. 

rot13char('a')  'n'

rot13char('A')  'N'

rot13char('N')  'A'

rot13char('-')  '-'  #non letters stay the same! This is important.

2. then use that function to help write rot13(s) which returns a string that is the rot13 equivalent of the entire string s. It must return the value and not print anything. 

rot13('CLERK')  'PYREX'

rot13('ovt-HEY!')  'big-URL!'

You will need to loop through the original string, and build a new string starting with the empty string "", then adding on the converted characters one at a time!

3. Make TWO new functions that rotate by a different number:  rotXchar(c,x) and rotX(s,x). Now you have to decide when the letters wrap. BEFORE you write these functions, read all examples, and ANSWER the questions below. 

rotXchar('a',1) → 'b'

rotXchar('a',3) → 'd'

rotXchar('z',1)  'a'

rotXchar('z',13)  'm'    #this is like rot13

rotX('abcxyz',1) → 'bcdyza'

rotX('what',13) → 'jung'

FOR 3:

Figure this out on paper before you start writing any code: (I want to see it in your notes!)

When you rotate by 1, which letters wrap around?

When you rotate by 3, which letters wrap around?

When you rotate by -3, which letters wrap around?

When you rotate by 27 what is this equivalent to?

When you wrap around how much does the value change?