0% found this document useful (0 votes)
3 views

Lab 04

study set

Uploaded by

feyzacetinel07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab 04

study set

Uploaded by

feyzacetinel07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS 115 - Introduction to Programming in Python

Lab 04
Lab Objectives: Strings, Files, Modules

Notes:
● You should only use functionality covered in CS115 in your solution.
● Your solution for this lab should not use the str.split() method, lists, tuples, dictionaries.
● Include a docstring for your functions. A template for function docstrings is below:
"""Summary or Description of the Function

Parameters:
argument1 (type): Description of arg1

Returns:
type: return value
"""

1. Create a module, Lab04_yourname_module.py that contains the following functions:

a. ruleChecker(s):takes a string s as a parameter and checks whether it conforms to a special rule


or not. The function returns -1, if the input string obeys the rule (see below). Otherwise, it returns the
index of the first word in which the rule is violated. Use containsLetter() function.
RULE: the i th word in the text must contain (i % 26) letter of the alphabet.
th

For example, the following sentences conform to the rule:


A bear can discover precious food disguised beneath ice.
Any butcher certainly needs meat freezer.

The following sentence does not follow the rule:


A beautifully crafted diamond necklace for girl was gifted by the groom. -> rule violated in word 7 .t

(Start counting from 0)

Hint1: To check the rule, store all of the English alphabetic letters in a string variable and use their
index to access them.

ALPHABET = "abcdefghijklmnopqrstuvwxyz"

Hint2: You can replace ‘\n’ character at the end of the line with a space character to process that
word also.

b. containsLetter(s,c): takes a string s, and a character c as parameters and returns true if the
string contains the given character and false if not.
2. Write a script, Lab04_yourname_application.py that
• Reads lines from a text file (specialText.txt), and checks whether each line conforms to the rule
using the ruleChecker()function. The program should output a statement to an output file,
out.txt, indicating whether each line conforms to the rule or not (see sample file output below).

Sample input file:


A bear can discover precious food disguised beneath ice.
A beautifully crafted dish was discovered.
Any butcher certainly needs meat freezer.

Sample output file:


The string in line 1 conforms to the special rule.
The string in line 2 does not conform to the special rule. (In word number 4)
The string in line 3 conforms to the special rule.

3. Upload Lab04_yourname_module.py and Lab04_yourname_application.py in a compressed file


with the name: Lab04_yourname.zip.

You might also like