Non-Adjacent Combinations of Two Words
Given a sentence as input, find all the unique combinations of two words and print the word combinations that are not adjacent in the original sentence in lexicographical order.
Input
The input will be a single line containing a sentence.
Output
The output should be multiple lines, each line containing a valid unique combination of two words. The words in each line should be lexicographical order and the lines as well should be in lexicographical order. A valid combination will not contain the words that appear adjacent in the given sentence. Print "No Combinations" if there are no valid combinations.
Sample Input 1
raju always plays cricket
Sample Output 1
always cricket
cricket raju
plays raju
Sample Input 2
python is a programming language
Sample Output 2
a language
a python
is language
is programming
language python
programming python
Sample Input 3
to be or not to be
Sample Output 3
be be
be not
or to
to to
Non-Adjacent Combinations of Two Words
Given a sentence as input, find all the unique combinations of two words and print the word combinations that are not adjacent in the original sentence in lexicographical order.
The input will be a single line containing a sentence.Output
The output should be multiple lines, each line containing a valid unique combination of two words. A valid combination will not contain the words that appear adjacent in the given sentence. Print "No Combinations" if there are no valid combinations_Explanation.
Sample Input 1
raju always plays cricket
Sample Output 1
always cricket
cricket raju
plays raju
Sample Input 2
python is a programming language
Sample Output 2
a language
a python
is language
is programming
language python
programming python
Sample Input 3
to be or not to be
Sample Output 3
be be
be not
or to
to to
The output should be printed exactly as shown in the above testcases.
In this python code there are three test cases. The three test cases are not coming expected output.
The three test cases are below.
sentence = "to be or not to be"
s = sentence.split[]
s.sort[]
result = []
for word1 in s:
for word2 in s:
result.append[word1 + " " + word2]
result = set[result]
result = list[result]
result.sort[]
for item in result:
if item in sentence:
result.remove[item]
for item in result:
if " ".join[item.split[][::-1]] in sentence:
result.remove[item]
for item in result:
spl = item.split[]
if [spl[0] == spl[1]] and [sentence.count[spl[0]] < 2]:
result.remove[item]
for item in result:
spl = item.split[]
dup = spl[1] + " " + spl[0]
if dup in result:
result.remove[dup]
result.insert[0, "be be"]
for item in result:
print[item]
Test cases
Test case 1
Input:-
raju always plays cricket
Output:-
Expected output is
always cricket
cricket raju
plays raju
but coming output is
be be
not be
or be
or to
Test case 2
Input:-
python is a programming language
Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python
but coming output is
be be
not be
or be
or to
Test case 3
Input:-
to be or not to be
Output:-
Expected output is
be be
be not
or to
to to
but coming output is
be be
not be
or be
or to
- Home >
- Programming >
- [Python] Permutations and Combinations Question, programming homework help
Content Type
User Generated
Description
Before you start notes:
- Enter your answers to these questions in the accompanying Python script file
questions.py
. Questions 1–3 require only short pieces of code to calculate or generate the answers, so replace the entries that sayNone
with your answers to each part. Question 4 has you completing the implementation of two functions. - For questions 1–3, a helper
function,
fact[n]
, has been included that provides a simpler interface to SciPy’sfactorial
function. Note that thecomb
function will produce real numbers as output instead of integers. The assessment scheme will accept either, as long as the value is correct. - [HINT]: While developing your code, store the result in a variable so that Spyder doesn’t immediately try to display it [since it may be large].
- [HINT]: Use the
len[]
function to check that your result set is the expected size. If your result, stored in sets
, is [necessarily] large, then use code likesorted[s][:20]
to look at only the first 20 elements ofs
. The use ofsorted[]
will often make it easier for you to confirm that it is generating the right entries. - If your script file takes more than ~3 seconds to run then you’ve probably implemented one of the set comprehensions incorrectly.
QUESTIONS [input your answer to these questions to the corresponding parts in the python file provided]:
Question 1: Permutations
- How many strings of length three are possible using characters from ‘ATGC’ [characters may be reused]? Write Python code tocalculate the number.
- Write a set comprehension to generate those strings.
- How many four-word sentences can be created from the words ‘cat’, ‘bat’, ‘hat’, ‘sat’, ‘fat’, and ‘rat’, using each word at most once? [Most of the sentences won’t make sense.] Write Python code to calculate the number.
- Write a set comprehension to generate tuples [not single strings] of the different arrangements described in part [c].
Question 2: Combinations
You should use the definitions of toy droid names and Star Trek species provided in the assignment script in your answers.
- With the resurgence of Star Wars in the cinemas toy companies are keen to invent new toys for children to collect. They have a series of 16 droids [Star Wars robots] and sell them in mixed packs of four [no pack contains a duplicate droid]. How many distinct packs do they sell? Write Python code to calculate the number.
- Given the droid names defined in the script file, write a set comprehension to generate all compositions of packs of four droids.
- The various Star Trek television and film series have introduced a large number of different [humanoid] alien species. One of the common narrative devices is to say that a character’s
parents are from two different alien species, and hence that character has a blend of characteristics from the two [yes, science fiction can get away with being both horribly racist and ignorant of biology]. For example, the popular character Spock is described as having a Human mother and Vulcan father. Your task:Assuming characters can only have two parents, and ignoring which parent is which
species, how many different combinations of such ‘blended’ Star Trek characters are possible given the subset of Star Trek species { ‘Bajoran’, ‘Betazoid’, ‘Cardassian’, ‘Human’, ‘Klingon’, ‘Romulan’, ‘Vulcan’ }? Write Python code to calculate the number. Do not directly call
factorial
orfact
in your solution. - Write a set comprehension to generate those combinations. [The answer will be a set of 2-tuples, or pairs.]
Question 3: You choose which
The following information is used in all parts of this question. Anne has 23 books in her collection at home. They all have short titles: ‘A’, ‘B’, ‘C’, …, ‘W’ [there is a list of these in the assignment script].
- Anne is going on holiday, but can only take six books with her. What are her options? Write a set comprehension to generate them all.
- All of Anne’s books fit on a single shelf. How many different ways can she arrange them? Write Python code to calculate the number.
- Anne sometimes likes to reread books, and may even reread a book she has just finished. What are the possible sequences of book titles of the last three books she has read? Write a set comprehension to generate the alternatives.
- Write Python code to calculate how many such sequences there are. [Do not use the
len
function on your previous answer.]
Question 4: Lovers of anagrams know words are mightier than the sword
An anagram is a word whose letters, when rearranged, form another word. They are a common feature of cryptic crosswords, but you do not need to know how to solve a cryptic crossword to solve this question. An anagram solver finds the alternative arrangements of a word’s letters that represent valid words. Your task is to write the two components of a basic anagram solver: the first part generates the set of all possible permutations of the letters in a given word; the second part restricts this set to contain only those permutations that are valid words.
There are two stub functions for you to complete:
word_perms[word]
: should generate andreturn
all permutations of the letters in the string represented byword
[each permutation will be a single string, not a tuple]. It will probably be one line of code.anagrams[word]
: shouldreturn
a set of all the permutations of the letters inword
that are valid [that is, that appear in one of the collections of words described below], including the original word. The input can be assumed to be in lower case.
Not all solutions to this task will receive full marks; an ideal solution will look very short, but still be readable.
The assignment script file contains code to load collections of words of different lengths [between 2 and 10] into two Pythondict
ionaries, one that holds the words in sorted lists and another that holds them in sets. To use the list of words of length n, useword_lists[n]
, whereas to use the set of words of
length n, use word_sets[n]
[you will probably not have a variable called n
but an expression instead]. You do not necessarily have to use both of these. Select whichever you believe is best-suited to the task.
In order for these words to be loaded in your program you must download and unzip the file containing the words, words.2-10.txt
, that accompanies this document.
Testing your anagram solver
To test word_perms
, call it with very
short words for which you can enumerate all the possible permutations. To test anagrams
try it on the word ‘lovers’. Other good words you could try include ‘alerts’, ‘heart’, ‘parsec’ and ‘spare’.
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Explanation & Answer
Hi, I'm uploading the answer now. I ...
15 Million Students Helped!
Sign up to view the full answer
Anonymous
Great! Studypool always delivers quality work.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4
24/7 Homework Help
Stuck on a homework question? Our verified tutors can answer all questions, from basic math to advanced rocket science!
Persuasion
by Jane Austen
Broke Millennial: Stop Scraping by and Get Your Financial Life Together
by Erin Lowry
The Tipping Point
by Malcolm Gladwell
The Awakening
by Kate Chopin
The Grapes of Wrath
by John Steinbeck
Homo Deus
by Yuval Noah Harari
2001 A Space Odyssey
by Arthur Clarke
Our tutors provide high quality explanations & answers.
Post question
Create the Data Access tier and Data tier projects:
I have included the link to install the database, if needed.Please do not hesitate to let me know if you need anything els ...
Create the Data Access tier and Data tier projects:
I have included the link to install the database, if needed.Please do not hesitate to let me know if you need anything else. THANK YOU!The company has asked you to build a prototype of an n-tier system using the Northwind database as an example. The company wants to be able to add, edit, and delete records from the customers, orders, order details, products, categories, and suppliers tables. Your instructor will provide you the online link to the SQL Server database files [MDF and LDF]. Assignment Guidelines Create the Data Access tier and Data tier projects: Create a class library project for the Data tier.Add a new class library project the project created in Step 1 for the data access tier.Add a typed DataSet using a new data source that connects to the Northwind sample database.Select the database objects [customers, orders, order details, products, categories, and suppliers] tables.Make sure the TableAdapters are stored in the Data Access tier and the DataSet class is added to the Data tier. Make sure that separate tiers are created for each layer by verifying that separate class libraries were created and the necessary references to the project were added. ***** Links to install Database for IP2: //msdn.microsoft.com/en-us/library/8b6y4c7s.aspx
Earn money selling your Study Documents