
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Number of Lines To Write String in Python
Suppose we have a string S and we have to write the letters of that given string, from left to right into lines. Here each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, that will be written on the next line. We also have an array widths, here widths[0] is the width of 'a', widths[1] is the width of 'b'and so on.
We have to find the answers of two questions −
- How many lines have at least one character from S
- What is the width used by the last such line?
We will return the answer as an integer list of length 2.
So, if the input is like [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10] and S = "bbbcccdddaaa", then the output will be [2, 4], as all letters except 'a' have the same length of 10, and the string "bbbcccdddaa" will cover 9 * 10 + 2 * 4 = 98 spaces. For the last 'a', it is written on the second line because there is only 2 units left in the first line. So the answer is 2 lines, plus 4 units in the second line.
To solve this, we will follow these steps −
- line := 1, count := 0
- for each i in S, do
- count := count + widths[ASCII of i - 97]
- if count > 100, then
- line := line + 1
- count := widths[ASCII of i - 97]
- return [line, count]
Let us see the following implementation to get better understanding −
Example
class Solution: def numberOfLines(self, widths, S): line = 1 count = 0 for i in S: count += widths[ord(str(i))-97] if count > 100: line += 1 count = widths[ord(str(i))-97] return [line, count] ob = Solution() print(ob.numberOfLines([4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], "bbbcccdddaaa"))
Input
[4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],"bbbcccdddaaa"
Output
[2, 4]
- Related Articles
- Count of lines required to write the given String in C++
- How to write multiple lines in text file using Python?
- Write a C program to find total number of lines in an existing file
- How to Print Lines Containing Given String in File using Python?
- Write a python program to count occurrences of a word in string?
- Python program to count number of substring present in string
- Python program to count the number of spaces in string
- How to write strings with new lines in R?
- How to compare string and number in Python?
- How to write functions in Python that accept any number of arguments
- Write a program in Python to count the number of digits in a given number N
- Program to find number of ways to split a string in Python
- Python Program to Count Number of Lowercase Characters in a String
- Program to find minimum number of monotonous string groups in Python
- Program to count number of operations needed to make string as concatenation of same string twice in Python
