logo

Scope and Variable Issues

   

Added on  2019-09-19

8 Pages2141 Words359 Views
 | 
 | 
 | 
TCSS142 REVIEW QUESTIONS1REVIEW QUESTIONS1) String ManipulationCorrect the following:print(“”This is a test,\” said the teacher\’s assistant, ”of “ + “what you \”know about backslash \ characters.’)Write code to change the line, “Bilbo Baggins and Gandalf the Grey were old friends.”, so thatAll the consonants are capitalizedAll the vowels are removedAll the capital letters replaced by their ASCII valuesThere are no duplicate letters (case ignored)The string is reversed character-by-characterThe string is sorted by word in alphabetical orderChange your above code so that it is robust (any string could be inputand the output will be correct); you should probably use for loops.Write a program that takes user input as a string and then prints out that string with every word backwards (a word as defined by any sequence of characters separated by a space).2) File Reading and WritingWrite a program that reads a file filled with one month and year per line (e.g., “March 2004”, “February 2000”) and prints to the console the number of days in that month (e.g., “31 days in March 2004”). You should use nested conditionals and have your program correctly report leap years’ differing February days using the rules according to Wikipedia.
Scope and Variable Issues_1

TCSS142 REVIEW QUESTIONS2Write a program that can write to a file from another file, each specified as entry from the console. (Use any of the text files provided you from this course or make your own.) From the file being read, you should iterate over each line and character, passing only punctuation, numbers, and the letters ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, and ‘n’to the file being written to.Write a program that reads a file and counts up how many of each type of ASCII value appears throughout the file. Print this out as a table using tabs to format it.3) Tracing, Scope, and Pre/PostconditionsGiven the following code, expand the table below recording all of the variable values every time a variable changes:i = 100j = 0h = 4000while i >= j: j = ih = i * 50while j > 5:j -= 20while h > i:h /= 10i += 1.5 i -= 32.5Variable Changeijhi100j1000h10004000... ... ... ... For the following code, record the value of a every time it changes:a = 1while (a < 100):a *= 4a -= a // 2
Scope and Variable Issues_2

TCSS142 REVIEW QUESTIONS3Given the following code, trace the code line by line for each function, using the table below as a model:def method1(d, c, b, a): print(a, b, c, d) d *= len(b) c += '5' b += [d, a] a = d < sum(b) print(a, b, c, d)def method2(w, x, y, z): print(w, x, y, z) w = w != w x = y y = z z = x * 3 print(w, x, y, z) method3(z, z, y, z)def method3(h, i, j, k): print(h, i, j, k) h = 10 i += ' word' j = [h, h, i, k] k *= 2 print(h, i, j, k)def main(): a = 15 b = 'the' c = [2.0, 3.1, 4.2] d = True print(a, b, c, d) method1(a, b, c, d) method2(d, c, b, a) method3(a, b, b, a) print(a, b, c, d)main()FunctionLine NumberVariable Names and Valuelist contentsmain11, 2, 3, 42a = 155, 6, 7, 83a = 15, b = ‘the’4a = 15, b = ‘the’, c =5a = 15, b = ‘the’, c = , d = Truemethod11d = 15, c = ‘the’, b = , a = True... ... ...
Scope and Variable Issues_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents