
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find K Partitions After Truncating Sentence Using Python
Suppose we have sentence s where some English words are present, that are separated by a single space with no leading or trailing spaces. We also have another value k. We have to find only the first k words after truncating.
So, if the input is like s = "Coding challenges are really helpful for students" k = 5, then the output will be True (See the image)
To solve this, we will follow these steps −
words := split s by spaces
join first k letters from words array by separating spaces and return
Let us see the following implementation to get better understanding −
Example
def solve(s, k): words = s.split() return " ".join(words[:k]) s = "Coding challenges are really helpful for students" k = 5 print(solve(s, k))
Input
"Coding challenges are really helpful for students", 5
Output
Coding challenges are really helpful
Advertisements