Computer Science
Write a function WordsList(S), where S is a string. The function returns a list, named Words, that stores all the words from the string which contain 'r'.
For example, if S is "Dew drops were shining in the morning", then the list Words should be: ['drops', 'were', 'morning']
Python
Python List Manipulation
2 Likes
Answer
def WordsList(S):
words = S.split()
Words = []
for word in words:
if 'r' in word:
Words.append(word)
return Words
S = "Dew drops were shining in the morning"
print(WordsList(S))Output
['drops', 'were', 'morning']
Answered By
2 Likes
Related Questions
What will the following snippet print?
L5 = [1500, 1800, 1600, 1200, 1900] begin = 1 sum = 0 for c in range(begin, 4): sum = sum + L5[c] print(c, ':', sum) sum = sum + L5[0] * 10 print("sum is", sum)Consider the following two SQL commands with reference to a table, named MOVIES, having a column named Director:
(a) Select Distinct Director from MOVIES;
(b) Select Director from MOVIES;
- In which case will these two commands produce the same result?
- In which case will these two commands produce different results?
In a database BANK, there are two tables with a sample data given below:
Table: EMPLOYEE
ENo EName Salary Zone Age Grade Dept 1 Mona 70000 East 40 A 10 2 Mukhtar 71000 West 45 B 20 3 Nalini 60000 East 26 A 10 4 Sanaj 65000 South 36 A 20 5 Surya 58000 North 30 B 30 Table: DEPARTMENT
Dept DName HOD 10 Computers 1 20 Economics 2 30 English 5 (a) To display ENo, EName, Salary and corresponding DName of all the employees whose age is between 25 and 35 (both values inclusive).
(b) To display DName and corresponding EName from the tables DEPARTMENT and EMPLOYEE (Hint: HOD of DEPARTMENT table should be matched with ENo of EMPLOYEE table for getting the desired result).
(c) To display EName, Salary, Zone and Income Tax (Note: Income tax to be calculated as 30% of salary) of all the employees with appropriate column headings.
Write a function begEnd() in Python to read lines from text file 'TESTFILE.TXT' and display the first and the last character of every line of the file (ignoring the leading and trailing white space characters).
Example: If the file content is as follows:
An apple a day keeps the doctor away. We all pray for everyone's safety A marked difference will come in our country.Then begEnd () function should display the output as:
A. Wy A.