Lesson Assignment
There are 13 questions regarding slicing. The first 10 are required to pass. The last 3 are extra credit.
You will place all your answers in the lesson.py tab. Select that tab and take a look at how it's done.
For your answers, each function (already defined) returns a tuple:
the first item is the answer using index notation (e.g. text[16:20])
the second item is the answer using slice notation (e.g. slice(16,20,None))
the numbers you use for the slice function should be the same (you can use None for any missing indices to the slice function) so don't overthink it!
when you solve each question, do NOT use the slice object. It's only used for the testing framework for verification.
You can test your code before submitting by running the following (not required, but helpful for debugging) code:
import slicetest
# run the tests over questions in lesson.py
print("total correct", slicetest.do_tests())
Question 1 (already finished)
Extract: "Data"
From: "Introduction to Data Science"
def question1(text):
return (text[16:20], slice(16,20,1))
Notes and hints:
'extract' means return the substring that is to be 'extracted' (i.e. removed) from the 'From' text.
if you want to debug your answers, just do the following (in main.py)
import lesson as ans
result = ans.question1("Introduction to Data Science")
print(result[0] == 'Data') # looking for True
you can also test each part of your answer:
expect = "Data"
word = "Introduction to Data Science"
s1 = slice(16,20,None)
value = word[s1]
if (value != expect):
print("Bad Slice", expect, value)
value = word[16:20]
if (value != expect):
print("Bad Index", expect, value)
In each of your answers, you should ONLY use the slice function as part of the final return and you must use the parameter text in the return statement.
def some_answer(text):
idx = some_calculation()
return (text[idx:20], slice(idx,20,1))
Bonus: ideally, you should solve each of these without any hardcoded numbers other than the step amount:
Extract: "Data"
From: "Introduction to Data Science"
def question1(text):
word = 'Data'
wl = len(word)
idx = text.find(word)
return (text[idx:idx+wl], slice(idx, idx+wl, 1))
Question 2:
Extract: "ar"
From: "Orange"
def question2(text):
return (?,?)
Question 3:
Extract: "Oa"
From: "Orange"
def question3(text):
return (?,?)
Question 4:
Extract: "ence"
From: "Introduction to Data Science"
def question4(text):
return (?,?)
Question 5:
Extract: "ecne"
From: "Introduction to Data Science"
def question5(text):
return (?,?)
Questions 6 through 10 involve manipulating text from a horror book:
My Book, by mike Chapter 1: set up The little boy in bed was scared. Chapter 2: suspense "Daddy, daddy there's a monster under my bed." Chapter 3: climax The father then looked under the bed .. and saw his son. Chapter 4: resolution "Daddy, daddy, someone's in my bed!" (the end) About the author: a first time novelist
Notes and Hints:
the contents of the book is everything from 'Chapter 1' until but not including '(the end)'.
if you find yourself counting the characters, you need to rethink your solution. Use Python to get the indices, not your fingers (see documentation on find)
test each part of your answer (see previous hints)
you should NOT need to add text to any of these. If you find yourself wanting to add periods, newlines, etc, you are on the wrong path.
'remove' means return the book but without the part requested to be removed.
'extract' means return the part of the book that is to be 'extracted' (i.e. removed)
The text is also in the variable book (found in the module slicetest) if you want to work with the book directly:
print(slicetest.book)
Question 6:
Remove: remove the front matter of the book
From: the text of the book
the front matter is the stuff before the contents
def question6(text):
return (?,?)
Question 7:
Remove: remove the back matter of the book
From: the text of the book
the back matter is the stuff after the contents
def question7(text):
return (?,?)
Question 8:
Extract: the front matter of the book
From: the text of the book
def question8(text):
return (?,?)
Question 9:
Extract: the back matter of the book (About the author)
From: the text of the book
Note: do NOT include (the end)\n
def question9(text):
return (?,?)
Question 10:
Extract: the contents of the book
From: the text of the book
def question10(text):
return (?,?)
Extra Credit:
The last 3 questions want you to extract a hidden message from a gibberish of text.
If you want to skip this part put the following line of code in lesson.py:
skip_extra = True
Question 11:
Extract: I Love Python
From: slicetest.mystery11 variable
def question11(text):
return (?,?)
Question 12:
Extract: Data Science Rocks
From: slicetest.mystery12 variable
def question12(text):
return (?,?)
Question 13:
Extract: Info 490 is fun
From: slicetest.mystery13 variable
def question13(text):
return (?,?)
ANSWER:
CODE TEXT
a. lesson.py
# function question2
def question2(text):
word = 'ar'
wl = len(word)
# finding word in reversed text, reversed by reverse
indexing
idx = text[::-1].find(word)
# using fetched index and reversed indexing to find
word
return
text[-(idx+1):-(idx+1+wl):-1],slice(-(idx+1),-(idx+1+wl),-1)
# function question3
def question3(text):
word = "Oa"
wl = len(word)
# finding word in 2 char slided string
idx = text[::2].find(word)
# doubling fetched index to match index in original
string
return
text[idx*2:(idx+wl)*2:2],slice(idx*2,(idx+wl)*2,2)
def question4(text):
# This can be solved using question1 solution
# but here I am hardcoding length to use the negative
indexing
wl = 4
return text[-wl:],slice(-wl,None,None)
def question5(text):
# This can be solved using question2 solution
# but here I am hardcoding length to use the reverse
slicing
wl = 4
return
text[-1:-(wl+1):-1],slice(-1,-(wl+1),-1)
b. main.py
## Importing
import lesson as ans
## Testing function question2
print('Test question2')
text = "Orange"
expect = "ar"
text_slice,s1 = ans.question2(text)
value=text[s1]
if text[s1] != expect:
print("Bad Slice",expect,value)
else:
print('Correct Slice')
if text_slice != expect:
print("Bad Index",expect,value)
else:
print("Correct Index")
## Texting function question3
print('Test question3')
text = "Orange"
expect = "Oa"
text_slice,s1 = ans.question3(text)
value=text[s1]
if text[s1] != expect:
print("Bad Slice",expect,value)
else:
print('Correct Slice')
if text_slice != expect:
print("Bad Index",expect,value)
else:
print("Correct Index")
## Texting function question3
print('Test question4')
text = "Introduction to Data Science"
expect = "ence"
text_slice,s1 = ans.question4(text)
value=text[s1]
if text[s1] != expect:
print("Bad Slice",expect,value)
else:
print('Correct Slice')
if text_slice != expect:
print("Bad Index",expect,value)
else:
print("Correct Index")
## Texting function question3
print('Test question5')
text = "Introduction to Data Science"
expect = "ecne"
text_slice,s1 = ans.question5(text)
value=text[s1]
if text[s1] != expect:
print("Bad Slice",expect,value)
else:
print('Correct Slice')
if text_slice != expect:
print("Bad Index",expect,value)
else:
print("Correct Index")
CODE IMAGES
a. lesson.py
b. main.py
OUTPUT IMAGE
Lesson Assignment There are 13 questions regarding slicing. The first 10 are required to pass. The...
Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...
Hey I have a task which consists of two part. Part A asks for writing a program of WORD & LINE CONCORDANCE APPLICATION in python which I have completed it. Now the second part has given 5 dictionary implementation codes namely as: (ChainingDict, OpenAddrHashDict with linear probing, OpenAddrHashDict with quadratic probing, and 2 tree-based dictionaries from lab 12 (BST-based dictionary implementation) and asks for my above program WORD & LINE CONCORDANCE APPLICATION to use these implemented code and show the time...
Deletion of List Elements The del statement removes an element or slice from a list by position. The list method list.remove(item) removes an element from a list by it value (deletes the first occurance of item) For example: a = ['one', 'two', 'three'] del a[1] for s in a: print(s) b = ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b'] del b[1:5] print(b) b.remove('a') print(b) # Output: ''' one three ['a', 'f', 'a', 'b'] ['f', 'a', 'b'] ''' Your textbook...
This project is meant to give you experience writing linked lists and graphs. As such, you are not permitted to use arrays or any data structure library. You may, however, make use of code presented in class and posted to Blackboard. Objective Your goal for this project is to take a block of text, analyze it, and produce random sentences in the style of the original text. For example, your program, given Wizard of Oz, might produce: how quite lion...
My Python file will not work below and I am not sure why, please help me debug! ********************************* Instructions for program: You’ll use these functions to put together a program that does the following: Gives the user sentences to type, until they type DONE and then the test is over. Counts the number of seconds from when the user begins to when the test is over. Counts and reports: The total number of words the user typed, and how many...
Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for...
Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors. Create Functions, include headers and other files, Loops(while, for), conditional(if, switch), datatypes, etc. Assignment You work at the computer science library, and your boss just bought a bunch of new books for the library! All of the new books need to be cataloged and sorted back on the shelf. You don’t need to keep track of which customer has the book or not, just whether...
please use C++ write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in...
QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...
In this project, you will construct an Object-Oriented framework for a library system. The library must have books, and it must have patrons. The patrons can check books out and check them back in. Patrons can have at most 3 books checked out at any given time, and can only check out at most one copy of a given book. Books are due to be checked back in by the fourth day after checking them out. For every 5 days...