Posts Homework 2
Post
Cancel

Homework 2

Answers to all of these questions will be available at solutions 24 hours before the next lecture.

1

Notice anything peculiar about “ikbal” and “kbali?” When we take the first letter of ikbal and snap it to the end, we get kbali. We can continue this and get balik, alikb, likba and finally return to ikbal. Let’s call such words rotations of each other. Write a function that takes two strings and checks whether these strings are rotations of each other. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> def rotation_test(str1, str2):
    ...
    ...
    ...
>>> rotation_test('ikbal', 'kbali')
True
>>> rotation_test('xxx', 'xxx')
True
>>> rotation_test('xxy', 'xyx')
True
>>> rotation_test('booyah', 'yahboo')
True
>>> rotation_test('hello', 'hlleo')
False
>>> rotation_test('ikbal', 'ilkba')
False

2

Write the last week’s function but use dictionaries this time.

3

Make a function that will take a function and return a table of values.

4

Make a function that will return the reversed version of any string. For example:

1
2
3
4
5
6
7
>>> def reverse_string(string):
    ...
    ...
>>> reverse_string('ikbal')
labki
>>> reverse_string('xxy')
yxx
This post is licensed under CC BY 4.0 by the author.