CSc 250 Lab 6

In this lab, you will be tasked with writing several functions that require you to use lists and some string methods. You should create one file named lab6.py and put all of the functions you complete, along with the test-cases, in the same file. Submit just this file to D2L when you are finished.

Problem 1 (warm-up)

Write a function named separate_into_lines. This function will take two parameters. The first you can assume is a string, and the second is a string that will have a single character in it. The function will split the string based on that single character. It will print each of the resulting list elements on their own line. You'll want to use the split() function to do this.
separate_into_lines('ONE TWO THREE FOUR', ' ')
print('---')
separate_into_lines('ONE TWO THREE FOUR', 'T')
print('---')
separate_into_lines('look-at-this-string!', '-')
Should produce this output:
ONE
TWO
THREE
FOUR
---
ONE
WO
HREE FOUR
---
look
at
this
string!

Problem 2

Write a function named get_longest_word. This functtion will take on parameter: A sentence of text (a string). This function will find the longest word in the string and return it. You can assume that the input parameter will have at least one word in it. For example, these tests:
print(get_longest_word('this is a sentence with a looooooooong word'))
print(get_longest_word('one two three four five'))
print(get_longest_word('this is a suuuper duuuper string'))
Should produce:
looooooooong
three
suuuper
Notice that if there is a tie, it returns the first of the tied words.

Problem 3

Create a function named get_word_lengths. This function will accept a single parameter, a sentence of words. This function will return a new list, which will have the same length as the number of words in the parameter string The returned list should have a number that corresponds to the length of the words at that position in the parameter. For example, these tests:
print(get_word_lengths('this is a sentence with a looooooooong word'))
print(get_word_lengths('one two three four five'))
print(get_word_lengths('a is the best three'))
Should produce this output:
[4, 2, 1, 8, 4, 1, 12, 4]
[3, 3, 5, 4, 4]
[1, 2, 3, 4, 5]

Problem 4

Create a function named print_triples. This function will take a single string in as a parameter. The string will be a sequence of words, separated by the comma character (,), and the number of words will be a multiple of three. The function will print out chunks of three words at-a-time, separated by a space instead of a comma. This test case:
print_triples('these,are,some,words,to,use')
print('---')
print_triples('one,two,three,four,five,six,seven,eight,nine')
print('---')
print_triples('seventy,times,seven')
Should produce this output:
these are some
words to use
---
one two three
four five six
seven eight nine
---
seventy times seven

You must turn in at least 2 of the problems on D2L to get credit for this.

Solutions: lab6.py