Reference no: EM133091480
Lab - Strings, Substrings and Files
Exercises
Question 1. Write a program to count and display how many times the substring notice appears in the string:
If you notice this notice you'll notice this notice is not worth noticing.
Question 2. Write a program that splits each line of the following limerick into two halves and displays them on alternating lines with dashes taking up the space of the 'left' half (before the 'right' half is printed):
The limerick packs laughs astronomical
Into space that is quite economical
But the good ones I've seen
Very seldom are clean
And the clean ones so seldom are comical.
Your output should look like this:
The limerick packs
-------------------laughs astronomical
Into space that i
-----------------s quite economical
But the good
-------------ones I've seen
Very seldo
----------m are clean
And the clean ones s
--------------------o seldom are comical.
Question 3. Write a program that opens the provided file alice_in_wonderland.txt and reads it into a list of lines (don't forget to close the file when you've done so!).Then, ask the user to enter a word to search for. Once you have a word, search through all the lines of text in the book counting how many times that word occurs in a case-insensitive manner, and then display the final count.
For example, if the user entered the word what then your output should be:
Please enter a word to count occurrences of: what
The word 'what' appears 44 times in this book.
Question 4. Write a program that takes a string from the user and converts it into MiXeDCaSe- that is, each alphabetical character in the string should alternate between upper and lower case (don't try to change digits or punctuation into upper or lower case).
For example, your output should look like this:
Please enter a string: I know you are, but what am I?
I KnOwyOuaRe, BuTWhAtaM I?
Question 5. Here's a function that calculates the prime numbers between an interval and returns a list of those values:
defget_primes_in_range(start, end):
prime_list = []
fornum in range(start, end + 1):
if num> 1:
found_divisor = False
fori in range(2, int(num/2)+1):
if (num % i == 0):
found_divisor = True
break
if (found_divisor == False):
prime_list.append(num)
return prime_list
So if you ran get_primes_in_range(2, 10) it would return you the list [2, 3, 5, 7, 9]
Write a program that asks the user for two integer values and a string for a filename. Then run the get_primes_in_range() function with the values the user provided and get the returned list.
Finally, open a new file for writing with the filename the user provided and write each prime number in the list to the file.
For example, a run of the program might go like this:
Enter the prime range starting value: 10
Enter the prime range ending value: 20
Filename to write the primes to: primes.txt
Question 6. CHALLENGE TASK: Write a program that programmatically prints out the following poem. Think about how you might go about it - design it on paper if you like. It's definitely a problem, but not a terribly hard one - and when you break it down there's really only just a handful of steps involved.
So solve it - for Science!
Once you've done this, try modifying your code to work on any string, without adding the letters per loop (i.e. ‘A', ‘B', ‘C' etc.). If you run this with the phrase "The quick brown fox jumps over the lazy dog" - which is just a test phrase that contains every letter in the alphabet - then you end up with the following:
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lzy dog
The quick rown fox jumps over the lzy dog
The quikrown fox jumps over the lzy dog
The quikrown fox jumps over the lzyog
Thquikrown fox jumps ovrthlzyog
Thquikrown ox jumps ovrthlzyog
Thquikrown ox jumps ovrthlzy o
T quikrown ox jumps ovr t lzy o
T qukrown ox jumps ovr t lzy o
T qukrown ox umps ovr t lzy o
T qurown ox umps ovr t lzy o
T qurown ox umps ovr t zy o
T qurown ox ups ovr t zy o
T qu row ox ups ovr t zy o
T qurw x ups vr t zy
T qurw x us vr t zy
T u rw x us vr t zy
T u w x us v t zy
T u w x u v t zy
u w x u v zy
w x vzy
w x zy
x zy
zy
z