Python Q&A – Abstract Data Types

Question 1: Accessing String Elements
All iterable objects such as strings, has the first element of a string starting at index 0. For the example, if the input string is ‘Education’, E has an index of 0, d has an index of 1, and so on…
Task: Print the first, second, and last letters of the user’s input.
‘Spam’ -> ‘S’, ‘p’, ‘m’
‘Python’ -> ‘P’, ‘y’, ‘n’
‘No’ -> ‘N’, ‘o’, ‘o’

Answer:

Explanation: As stated before, a variable needs to be created to store the inputted word. Then we print the individual string elements. The form is variable_name[index_number]. The first letter has an index of 0, therefore it is a[0]. The second letter has an index of 1, therefore it is a[1]. To get the final letter of any word, the index would be -1, therefore it is a[-1].

Question 2: Slicing Strings
Any iterable object can be sliced. Slicing is used to extract a sub-collection of elements.
Task:
Write a function slice_from(string, start, end) which returns the characters in string from index start up to, but not including, index end. (Note that this characteristic of including the character at the start index, but not including the one at the end index, is the default slicing behaviour.)

Write a function reverse_string(string) which returns the characters in string in reverse order. You must use a single slice statement to achieve this. 

s = ‘Hello’; slice_from(s, 0, len(s)) -> ‘Hello’
s = ‘Hello’; slice_from(s, 1, len(s) – 1) -> ‘ell’
s = ‘Hello’; slice_from(s, 2, len(s)) -> ‘llo’
reverse_string(‘a’) -> ‘a’
reverse_string(‘ab’) -> ‘ba’
reverse_string(‘Hello’) -> ‘olleH’

Answer:

Explanation: The first function is self-explanatory since the question already tells us the inputs of the functions. In the first function, we return the string[start:end], where string is the string we inputted, start is the first index, and end is the second index.

Question 3: Processing Strings Using a For Loop
For loops provide another mechanism (like while loops) for carrying out repeated computations. The main difference is that a for loop iterates through the elements of an iterable object.
A For Statement has the form:
for var in iterable:
body_code
Task: Write a function occurrences(text1, text2) that takes two string arguments. The function returns the number of times a character from the first argument occurs in the second argument. 

occurrences(‘a’, ‘aaa’) -> 3
occurrences(‘aaa’, ‘a’) -> 1
occurrences(‘Hello’, ‘Hello’) -> 5
occurrences(‘fooled’, ‘hello world’) -> 7

Answer:

Explanation: A for loop statement is used to carry out repeated computations. In this case, we want to find out how often the characters from text1 occurs in text2. Therefore we set a variable (number) which I used to keep a “count” of how many characters in text1 are also in text2. By using a for loop, and an if statement, we can add one every time a character from text1 is also in text2. The c in for c in text2 can be anything, because it is just a random variable that the loop uses.

Question 4: Filtering Characters from a String
Sometimes it is useful to filter specific characters from a string using a for loop.
Task: Write a function filter_string(str1, str2) which returns a copy of str1 with all characters from str2 removed. 

filter_string(‘python’, ”) -> ‘python’
filter_string(”, ‘python’) -> ”
filter_string(‘python’, ‘y’) -> ‘pthon’
filter_string(‘hello’, ‘l’) -> ‘heo’
filter_string(‘monty’, ‘ot’) -> ‘mny’
filter_string(‘Blue.  No yel–  Auuuuuuuugh!’, ‘.-!u’) -> ‘Ble  No yel  Agh’

Answer:

Explanation: It is essentially the same process as the previous question, but instead we replace the first string (str1) using the built-in functionality of replace (str1.replace). The replace function has two inputs, and the first input is what we want to replace, and the second input is what we want to be replace with. In this case, we want to replace any letter in str1 that is also in str2 with ” (which is a blank). We then return str1 and not print str1.

Question 5: Intersecting Strings
Sometimes we also want to use a for loop to get the characters that are in both strings.
Task: Write a function intersection(str1, str2) which returns the intersection of str1 and str2. However, each letter should be represented at most once in the output. 

intersection(‘a’, ‘b’) -> ”
intersection(‘a’, ‘a’) -> ‘a’
intersection(‘aa’, ‘aa’) -> ‘a’
intersection(‘monty’, ‘monty’) -> ‘monty’
intersection(‘hello’, ‘hello’) -> ‘helo’
intersection(‘monty’, ‘python’) -> ‘onty’
intersection(‘On hot sunny days’, ‘There is much rain’) -> ‘n hsua’

Answer:

Explanation: We first set a variable (out) to equal an empty string so that we can keep the similar characters in str1 and str2. In the for loop, an if statement and a not … in statement are used, so that the similar characters in str1 and str2 are added into the out variable and also makes it so that there are no duplicates.

Question 6: Tuple Assignment
The components of a tuple can be assigned to individual variables. So, for example, if we create a point in 3D space: pt = (1, 2, 3). We can later assign its components to individual variables: x, y, z = pt.

The str.partition method can be used to separate the components of a string.

Task: Write a function get_names() which prompts the user to enter their name, and then returns a tuple containing their first and last names. You must use str.partition to separate the user’s first and last names. You must ‘unpack’ the result of str.partition into separate variables. You may assume that the user only enters two names. 

get_names() < ‘John Smith’ -> (‘John’, ‘Smith’)
get_names() < ‘John Albert Smith’ -> (‘John’, ‘Albert Smith’)

Answer:

Explanation: We first assign a variable (name) to the inputted value that is made into a string using str. We then assign random variable (first, middle, last) to name.partition(‘ ‘), so that it will put a space between the variable. We then only want to return the first and last words of the name.

, , , , , , , , , , , , , , , , , , , , , , , , , ,

No comments yet.

Leave a Reply

Powered by WordPress. Designed by WooThemes

Free WordPress Themes